From 2de7ebf4516e58a8fa959977a51dccd22823c0e0 Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期四, 06 五月 2021 18:58:35 +0800 Subject: [PATCH] fix node recursive lock. --- src/shm_queue.h | 62 ++++++++++++++----------------- 1 files changed, 28 insertions(+), 34 deletions(-) diff --git a/src/shm_queue.h b/src/shm_queue.h index 11f9893..5d5c0e9 100644 --- a/src/shm_queue.h +++ b/src/shm_queue.h @@ -29,32 +29,12 @@ template <class D> using Circular = robust::CircularBuffer<D, Allocator<D>>; - template <class D> class SharedQueue { public: SharedQueue(const uint32_t len, Allocator<D> const &alloc) : queue_(len, alloc) {} - - template <class OnWrite> - bool TryWrite(const D &d, const OnWrite &onWrite) - { - Guard lock(mutex()); - if (!queue_.full()) { - onWrite(d); - queue_.push_back(d); - return true; - } else { - return false; - } - } - - bool TryWrite(const D &d) - { - Guard lock(mutex()); - return !queue_.full() ? (queue_.push_back(d), true) : false; - } bool Read(D &d, const int timeout_ms) { @@ -69,22 +49,36 @@ } while (steady_clock::now() < end_time); return false; } - bool TryRead(D &d) - { - Guard lock(mutex()); - if (!queue_.empty()) { - queue_.pop_front(d); - return true; - } else { - return false; - } - } + bool TryRead(D &d) { return queue_.pop_front(d); } + bool TryWrite(const D &d) { return queue_.push_back(d); } private: - typedef Circular<D> Queue; - Queue queue_; - Mutex mutex_; - Mutex &mutex() { return mutex_; } + Circular<D> queue_; +}; + +template <int Power = 4> +class SharedQ63 +{ +public: + typedef int64_t Data; + bool Read(Data &d, const int timeout_ms) + { + using namespace std::chrono; + auto end_time = steady_clock::now() + milliseconds(timeout_ms); + do { + if (TryRead(d)) { + return true; + } else { + robust::QuickSleep(); + } + } while (steady_clock::now() < end_time); + return false; + } + bool TryRead(Data &d, const bool try_more = true) { return queue_.pop_front(d, try_more); } + bool TryWrite(const Data d, const bool try_more = true) { return queue_.push_back(d, try_more); } + +private: + robust::AtomicQueue<Power, Data> queue_; }; } // namespace bhome_shm -- Gitblit v1.8.0