Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions iocore/net/AtomicEvent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/** @file

@section license License

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include <atomic>

#include "I_EventSystem.h"

class AtomicEvent
{
public:
bool
schedule(Continuation *c, EThread *t, int event, void *data, ink_hrtime delay = 0, int periodic = 0)
{
auto new_e = ::eventAllocator.alloc();
new_e->init(c, delay, periodic);
new_e->callback_event = event;
new_e->cookie = data;

Event *tmp = nullptr;
if (this->_e.compare_exchange_weak(tmp, new_e, std::memory_order_acq_rel)) {
t->schedule(new_e);
return true;
} else {
// we should not reschedule events when event is -1 or nullptr. Because the connection
// might be closed or already have events in plane.
new_e->free();
return false;
}
}

// thread unsafe. only target thread can cancel this event.
void
cancel()
{
Event *tmp = nullptr;
do {
if (tmp != nullptr) {
tmp->cancel();
}
tmp = this->_e.load(std::memory_order_acquire);
if (tmp == reinterpret_cast<Event *>(-1)) {
return;
}
} while (!this->_e.compare_exchange_weak(tmp, static_cast<Event *>(nullptr), std::memory_order_acq_rel));

if (tmp != nullptr) {
tmp->cancel();
}
}

void
close()
{
Event *tmp = nullptr;
do {
if (tmp != nullptr) {
tmp->cancel();
}

tmp = this->_e.load(std::memory_order_acquire);
ink_release_assert(tmp != reinterpret_cast<Event *>(-1));
} while (!this->_e.compare_exchange_weak(tmp, reinterpret_cast<Event *>(-1), std::memory_order_acq_rel));

if (tmp != nullptr) {
tmp->cancel();
}
}

private:
std::atomic<Event *> _e{};
};
39 changes: 37 additions & 2 deletions iocore/net/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ AM_CPPFLAGS += \

TESTS = $(check_PROGRAMS)

check_PROGRAMS = test_certlookup test_UDPNet
check_PROGRAMS = test_certlookup test_UDPNet test_UDPAcceptEcho
noinst_LIBRARIES = libinknet.a

test_certlookup_LDFLAGS = \
Expand Down Expand Up @@ -163,7 +163,41 @@ libinknet_a_SOURCES = \
UnixNetVConnection.cc \
UnixUDPConnection.cc \
UnixUDPNet.cc \
SSLDynlock.cc
SSLDynlock.cc \
UDPProcessor.cc \
UDPConnection.cc

test_UDPAcceptEcho_CPPFLAGS = \
$(AM_CPPFLAGS) \
$(iocore_include_dirs) \
-I$(abs_top_srcdir)/proxy \
-I$(abs_top_srcdir)/proxy/hdrs \
-I$(abs_top_srcdir)/proxy/http \
-I$(abs_top_srcdir)/proxy/logging \
-I$(abs_top_srcdir)/mgmt \
-I$(abs_top_srcdir)/mgmt/utils \
@OPENSSL_INCLUDES@

test_UDPAcceptEcho_LDFLAGS = \
@AM_LDFLAGS@ \
@OPENSSL_LDFLAGS@ \
@YAMLCPP_LDFLAGS@

test_UDPAcceptEcho_LDADD = \
libinknet.a \
$(top_builddir)/iocore/eventsystem/libinkevent.a \
$(top_builddir)/mgmt/libmgmt_p.la \
$(top_builddir)/lib/records/librecords_p.a \
$(top_builddir)/src/tscore/libtscore.la $(top_builddir)/src/tscpp/util/libtscpputil.la \
$(top_builddir)/proxy/ParentSelectionStrategy.o \
@HWLOC_LIBS@ @OPENSSL_LIBS@ @LIBPCRE@ @YAMLCPP_LIBS@

test_UDPAcceptEcho_SOURCES = \
libinknet_stub.cc \
UnixUDPConnection.cc \
UnixUDPNet.cc \
test_UDPAcceptEcho.cc


if ENABLE_QUIC
libinknet_a_SOURCES += \
Expand Down Expand Up @@ -192,3 +226,4 @@ include $(top_srcdir)/build/tidy.mk

clang-tidy-local: $(DIST_SOURCES)
$(CXX_Clang_Tidy)

Loading