aboutsummaryrefslogtreecommitdiffstats
path: root/fnet
diff options
context:
space:
mode:
authorTor Brede Vekterli <vekterli@yahooinc.com>2022-02-17 13:57:07 +0000
committerTor Brede Vekterli <vekterli@yahooinc.com>2022-02-18 15:38:33 +0000
commitc4438e94b71ac9006b4ba52efa621b2a978e45d2 (patch)
treec920d98af6678ca5e8ecb035ff9a3691d16d4978 /fnet
parent8f92d9d851bc8fc3c19f943c30f91b7a43ddb167 (diff)
Make `FNET_Connection::_state` atomic to allow safe polling from outside lock
Diffstat (limited to 'fnet')
-rw-r--r--fnet/src/vespa/fnet/connection.cpp20
-rw-r--r--fnet/src/vespa/fnet/connection.h6
2 files changed, 13 insertions, 13 deletions
diff --git a/fnet/src/vespa/fnet/connection.cpp b/fnet/src/vespa/fnet/connection.cpp
index 83224178e15..e3259db848b 100644
--- a/fnet/src/vespa/fnet/connection.cpp
+++ b/fnet/src/vespa/fnet/connection.cpp
@@ -125,8 +125,8 @@ FNET_Connection::SetState(State state)
std::vector<FNET_Channel::UP> toDelete;
std::unique_lock<std::mutex> guard(_ioc_lock);
- oldstate = _state;
- _state = state;
+ oldstate = GetState();
+ _state.store(state, std::memory_order_relaxed);
if (LOG_WOULD_LOG(debug) && state != oldstate) {
LOG(debug, "Connection(%s): State transition: %s -> %s", GetSpec(),
GetStateString(oldstate), GetStateString(state));
@@ -570,7 +570,7 @@ FNET_Connection::Init()
}
// handle close by admin channel init
- if (_state == FNET_CLOSED) {
+ if (GetState() == FNET_CLOSED) {
return false;
}
@@ -600,7 +600,7 @@ FNET_Connection::handle_handshake_act()
{
assert(_flags._handshake_work_pending);
_flags._handshake_work_pending = false;
- return ((_state == FNET_CONNECTING) && handshake());
+ return ((GetState() == FNET_CONNECTING) && handshake());
}
void
@@ -619,7 +619,7 @@ FNET_Connection::OpenChannel(FNET_IPacketHandler *handler,
FNET_Channel * ret = nullptr;
std::unique_lock<std::mutex> guard(_ioc_lock);
- if (__builtin_expect(_state < FNET_CLOSING, true)) {
+ if (__builtin_expect(GetState() < FNET_CLOSING, true)) {
newChannel->SetID(GetNextID());
if (chid != nullptr) {
*chid = newChannel->GetID();
@@ -698,7 +698,7 @@ FNET_Connection::PostPacket(FNET_Packet *packet, uint32_t chid)
assert(packet != nullptr);
std::unique_lock<std::mutex> guard(_ioc_lock);
- if (_state >= FNET_CLOSING) {
+ if (GetState() >= FNET_CLOSING) {
if (_flags._discarding) {
_queue.QueuePacket_NoLock(packet, FNET_Context(chid));
} else {
@@ -710,7 +710,7 @@ FNET_Connection::PostPacket(FNET_Packet *packet, uint32_t chid)
writeWork = _writeWork;
_writeWork++;
_queue.QueuePacket_NoLock(packet, FNET_Context(chid));
- if ((writeWork == 0) && (_state == FNET_CONNECTED)) {
+ if ((writeWork == 0) && (GetState() == FNET_CONNECTED)) {
AddRef_NoLock();
guard.unlock();
Owner()->EnableWrite(this, /* needRef = */ false);
@@ -756,7 +756,7 @@ FNET_Connection::HandleReadEvent()
{
bool broken = false; // is connection broken ?
- switch(_state) {
+ switch(GetState()) {
case FNET_CONNECTING:
broken = !handshake();
break;
@@ -776,7 +776,7 @@ bool
FNET_Connection::writePendingAfterConnect()
{
std::lock_guard<std::mutex> guard(_ioc_lock);
- _state = FNET_CONNECTED; // SetState(FNET_CONNECTED)
+ _state.store(FNET_CONNECTED, std::memory_order_relaxed); // SetState(FNET_CONNECTED)
LOG(debug, "Connection(%s): State transition: %s -> %s", GetSpec(),
GetStateString(FNET_CONNECTING), GetStateString(FNET_CONNECTED));
return (_writeWork > 0);
@@ -787,7 +787,7 @@ FNET_Connection::HandleWriteEvent()
{
bool broken = false; // is connection broken ?
- switch(_state) {
+ switch(GetState()) {
case FNET_CONNECTING:
broken = !handshake();
break;
diff --git a/fnet/src/vespa/fnet/connection.h b/fnet/src/vespa/fnet/connection.h
index e86b670b7e5..af4483e23e5 100644
--- a/fnet/src/vespa/fnet/connection.h
+++ b/fnet/src/vespa/fnet/connection.h
@@ -100,7 +100,7 @@ private:
vespalib::CryptoSocket::UP _socket; // socket for this conn
ResolveHandlerSP _resolve_handler; // async resolve callback
FNET_Context _context; // connection context
- State _state; // connection state
+ std::atomic<State> _state; // connection state. May be polled outside lock
Flags _flags; // Packed flags.
uint32_t _packetLength; // packet length
uint32_t _packetCode; // packet code
@@ -339,9 +339,9 @@ public:
/**
- * @return current connection state.
+ * @return current connection state. May be stale if read outside lock.
**/
- State GetState() { return _state; }
+ State GetState() const noexcept { return _state.load(std::memory_order_relaxed); }
/**