diff options
author | Tor Egge <Tor.Egge@oath.com> | 2017-10-27 12:19:40 +0000 |
---|---|---|
committer | Tor Egge <Tor.Egge@oath.com> | 2017-10-27 12:19:40 +0000 |
commit | b8a1d4557d0856b4da30779ba3b75676655d691e (patch) | |
tree | 227b781f411b0643047f01a32f24fb51e98dd585 /fnet | |
parent | 0dd33f6fac0cdc3e0dc23d6a857f34663f093b84 (diff) |
Use std::mutex and std::condition_variable instead of FastOS_Cond
in fnet packet queue.
Diffstat (limited to 'fnet')
-rw-r--r-- | fnet/src/vespa/fnet/packetqueue.cpp | 29 | ||||
-rw-r--r-- | fnet/src/vespa/fnet/packetqueue.h | 43 |
2 files changed, 20 insertions, 52 deletions
diff --git a/fnet/src/vespa/fnet/packetqueue.cpp b/fnet/src/vespa/fnet/packetqueue.cpp index b2ed08d4b04..a4ab601368a 100644 --- a/fnet/src/vespa/fnet/packetqueue.cpp +++ b/fnet/src/vespa/fnet/packetqueue.cpp @@ -4,6 +4,7 @@ #include "packet.h" #include <vespa/fastos/time.h> #include <cassert> +#include <chrono> void FNET_PacketQueue_NoLock::ExpandBuf(uint32_t needentries) @@ -166,6 +167,7 @@ FNET_PacketQueue_NoLock::Print(uint32_t indent) FNET_PacketQueue::FNET_PacketQueue(uint32_t len, HP_RetCode hpRetCode) : FNET_PacketQueue_NoLock(len, hpRetCode), + _lock(), _cond(), _waitCnt(0) { @@ -190,16 +192,16 @@ void FNET_PacketQueue::QueuePacket(FNET_Packet *packet, FNET_Context context) { assert(packet != nullptr); - Lock(); + std::unique_lock<std::mutex> guard(_lock); EnsureFree(); _buf[_in_pos]._packet = packet; // insert packet ref. _buf[_in_pos]._context = context; if (++_in_pos == _bufsize) _in_pos = 0; // wrap around. _bufused++; - if (_waitCnt >= _bufused) // signal waiting thread(s) - Signal(); - Unlock(); + if (_waitCnt >= _bufused) { // signal waiting thread(s) + _cond.notify_one(); + } } @@ -207,17 +209,17 @@ FNET_Packet* FNET_PacketQueue::DequeuePacket(FNET_Context *context) { FNET_Packet *packet = nullptr; - Lock(); + std::unique_lock<std::mutex> guard(_lock); _waitCnt++; - while (_bufused == 0) - Wait(); + while (_bufused == 0) { + _cond.wait(guard); + } _waitCnt--; packet = _buf[_out_pos]._packet; *context = _buf[_out_pos]._context; if (++_out_pos == _bufsize) _out_pos = 0; // wrap around _bufused--; - Unlock(); return packet; } @@ -231,14 +233,15 @@ FNET_PacketQueue::DequeuePacket(uint32_t maxwait, FNET_Context *context) if (maxwait > 0) startTime.SetNow(); - Lock(); + std::unique_lock<std::mutex> guard(_lock); if (maxwait > 0) { bool timeout = false; _waitCnt++; while ((_bufused == 0) && !timeout - && (waitTime = (int)(maxwait - startTime.MilliSecsToNow())) > 0) - timeout = !TimedWait(waitTime); + && (waitTime = (int)(maxwait - startTime.MilliSecsToNow())) > 0) { + timeout = _cond.wait_for(guard, std::chrono::milliseconds(waitTime)) == std::cv_status::timeout; + } _waitCnt--; } if (_bufused > 0) { @@ -248,7 +251,6 @@ FNET_PacketQueue::DequeuePacket(uint32_t maxwait, FNET_Context *context) _out_pos = 0; // wrap around _bufused--; } - Unlock(); return packet; } @@ -256,7 +258,7 @@ FNET_PacketQueue::DequeuePacket(uint32_t maxwait, FNET_Context *context) void FNET_PacketQueue::Print(uint32_t indent) { - Lock(); + std::unique_lock<std::mutex> guard(_lock); uint32_t i = _out_pos; uint32_t cnt = _bufused; @@ -273,5 +275,4 @@ FNET_PacketQueue::Print(uint32_t indent) _buf[i]._context.Print(indent + 2); } printf("%*s}\n", indent, ""); - Unlock(); } diff --git a/fnet/src/vespa/fnet/packetqueue.h b/fnet/src/vespa/fnet/packetqueue.h index 099410ed385..1daca901120 100644 --- a/fnet/src/vespa/fnet/packetqueue.h +++ b/fnet/src/vespa/fnet/packetqueue.h @@ -3,7 +3,8 @@ #pragma once #include "ipackethandler.h" -#include <vespa/fastos/cond.h> +#include <mutex> +#include <condition_variable> /** * This class implements a queue of packets. Being in a queue does not @@ -174,8 +175,9 @@ private: protected: - FastOS_Cond _cond; - uint32_t _waitCnt; + std::mutex _lock; + std::condition_variable _cond; + uint32_t _waitCnt; public: @@ -190,41 +192,6 @@ public: FNET_PacketQueue(uint32_t len = 64, HP_RetCode hpRetCode = FNET_KEEP_CHANNEL); ~FNET_PacketQueue(); - - /** - * Lock this queue to gain exclusive access. - **/ - void Lock() { _cond.Lock(); } - - - /** - * Unlock this queue to yield exclusive access. - **/ - void Unlock() { _cond.Unlock(); } - - - /** - * Wait for a signal on this queue. - **/ - void Wait() { _cond.Wait(); } - - - /** - * Wait for a signal on this queue, but time out after ms - * milliseconds. - * - * @return true(got signal)/false(timeout). - * @param ms number of milliseconds to wait before timing out. - **/ - bool TimedWait(int ms) { return _cond.TimedWait(ms); } - - - /** - * Signal one thread waiting on this queue. - **/ - void Signal() { _cond.Signal(); } - - /** * Handle incoming packet by putting it on the queue. This method * uses the hpRetCode value given to the constructor to decide what |