diff options
author | Tor Egge <Tor.Egge@oath.com> | 2017-10-27 10:48:54 +0000 |
---|---|---|
committer | Tor Egge <Tor.Egge@oath.com> | 2017-10-27 10:51:53 +0000 |
commit | 70fd239b4e41121a45299192ac28c39242bc30df (patch) | |
tree | efd07a7556fd3a35bd8a6c28b7e0fbbc4e24d463 /fnet | |
parent | 114757a3dea6bec9521a7aad8d9495c5f3381b9b (diff) |
Use std::mutex and std::condition_variable instead of FastOS_Cond.
Diffstat (limited to 'fnet')
-rw-r--r-- | fnet/src/vespa/fnet/transport_thread.cpp | 87 | ||||
-rw-r--r-- | fnet/src/vespa/fnet/transport_thread.h | 29 |
2 files changed, 52 insertions, 64 deletions
diff --git a/fnet/src/vespa/fnet/transport_thread.cpp b/fnet/src/vespa/fnet/transport_thread.cpp index 6610d217294..bd3ad3c53ee 100644 --- a/fnet/src/vespa/fnet/transport_thread.cpp +++ b/fnet/src/vespa/fnet/transport_thread.cpp @@ -123,15 +123,16 @@ FNET_TransportThread::PostEvent(FNET_ControlPacket *cpacket, FNET_Context context) { bool wasEmpty; - Lock(); - if (_shutdown) { - Unlock(); - DiscardEvent(cpacket, context); - return false; + { + std::unique_lock<std::mutex> guard(_lock); + if (_shutdown) { + guard.unlock(); + DiscardEvent(cpacket, context); + return false; + } + wasEmpty = _queue.IsEmpty_NoLock(); + _queue.QueuePacket_NoLock(cpacket, context); } - wasEmpty = _queue.IsEmpty_NoLock(); - _queue.QueuePacket_NoLock(cpacket, context); - Unlock(); if (wasEmpty) { _selector.wakeup(); } @@ -172,9 +173,10 @@ FNET_TransportThread::UpdateStats() comp->FlushDirectWriteStats(); comp->Unlock(); } - Lock(); - _stats.Update(&_counters, ms / 1000.0); - Unlock(); + { + std::unique_lock<std::mutex> guard(_lock); + _stats.Update(&_counters, ms / 1000.0); + } _counters.Clear(); if (_config._logStats) @@ -221,6 +223,7 @@ FNET_TransportThread::FNET_TransportThread(FNET_Transport &owner_in) _selector(), _queue(), _myQueue(), + _lock(), _cond(), _started(false), _shutdown(false), @@ -235,9 +238,10 @@ FNET_TransportThread::FNET_TransportThread(FNET_Transport &owner_in) FNET_TransportThread::~FNET_TransportThread() { - Lock(); - _deleted = true; - Unlock(); + { + std::unique_lock<std::mutex> guard(_lock); + _deleted = true; + } if (_started && !_finished) { LOG(error, "Transport: delete called on active object!"); } @@ -375,12 +379,13 @@ void FNET_TransportThread::ShutDown(bool waitFinished) { bool wasEmpty = false; - Lock(); - if (!_shutdown) { - _shutdown = true; - wasEmpty = _queue.IsEmpty_NoLock(); + { + std::unique_lock<std::mutex> guard(_lock); + if (!_shutdown) { + _shutdown = true; + wasEmpty = _queue.IsEmpty_NoLock(); + } } - Unlock(); if (wasEmpty) { _selector.wakeup(); } @@ -396,11 +401,10 @@ FNET_TransportThread::WaitFinished() if (_finished) return; - Lock(); + std::unique_lock<std::mutex> guard(_lock); _waitFinished = true; while (!_finished) - Wait(); - Unlock(); + _cond.wait(guard); } @@ -409,13 +413,14 @@ FNET_TransportThread::InitEventLoop() { bool wasStarted; bool wasDeleted; - Lock(); - wasStarted = _started; - wasDeleted = _deleted; - if (!_started && !_deleted) { - _started = true; + { + std::unique_lock<std::mutex> guard(_lock); + wasStarted = _started; + wasDeleted = _deleted; + if (!_started && !_deleted) { + _started = true; + } } - Unlock(); if (wasStarted) { LOG(error, "Transport: InitEventLoop: object already active!"); return false; @@ -435,9 +440,10 @@ FNET_TransportThread::InitEventLoop() void FNET_TransportThread::handle_wakeup() { - Lock(); - CountEvent(_queue.FlushPackets_NoLock(&_myQueue)); - Unlock(); + { + std::unique_lock<std::mutex> guard(_lock); + CountEvent(_queue.FlushPackets_NoLock(&_myQueue)); + } FNET_Context context; FNET_Packet *packet = nullptr; @@ -584,9 +590,10 @@ FNET_TransportThread::EventLoopIteration() _statsTask.Kill(); // flush event queue - Lock(); - _queue.FlushPackets_NoLock(&_myQueue); - Unlock(); + { + std::unique_lock<std::mutex> guard(_lock); + _queue.FlushPackets_NoLock(&_myQueue); + } // discard remaining events FNET_Context context; @@ -616,11 +623,13 @@ FNET_TransportThread::EventLoopIteration() _queue.IsEmpty_NoLock() && _myQueue.IsEmpty_NoLock()); - Lock(); - _finished = true; - if (_waitFinished) - Broadcast(); - Unlock(); + { + std::unique_lock<std::mutex> guard(_lock); + _finished = true; + if (_waitFinished) { + _cond.notify_all(); + } + } LOG(spam, "Transport: event loop finished."); diff --git a/fnet/src/vespa/fnet/transport_thread.h b/fnet/src/vespa/fnet/transport_thread.h index 6ec29968980..8af0642eebe 100644 --- a/fnet/src/vespa/fnet/transport_thread.h +++ b/fnet/src/vespa/fnet/transport_thread.h @@ -11,6 +11,8 @@ #include <vespa/fastos/time.h> #include <vespa/vespalib/net/socket_handle.h> #include <vespa/vespalib/net/selector.h> +#include <mutex> +#include <condition_variable> class FNET_Transport; class FNET_ControlPacket; @@ -63,7 +65,8 @@ private: Selector _selector; // I/O event generator FNET_PacketQueue_NoLock _queue; // outer event queue FNET_PacketQueue_NoLock _myQueue; // inner event queue - FastOS_Cond _cond; // used for synchronization + std::mutex _lock; // used for synchronization + std::condition_variable _cond; // used for synchronization bool _started; // event loop started ? bool _shutdown; // should stop event loop ? bool _finished; // event loop stopped ? @@ -76,30 +79,6 @@ private: /** - * Lock this object. - **/ - void Lock() { _cond.Lock(); } - - - /** - * Unlock this object. - **/ - void Unlock() { _cond.Unlock(); } - - - /** - * Wait on this object. - **/ - void Wait() { _cond.Wait(); } - - - /** - * Wake all waiting on this object. - **/ - void Broadcast() { _cond.Broadcast(); } - - - /** * Add an IOComponent to the list of components. This operation is * performed immidiately and without locking. This method should * only be called in the transport thread. |