From 34cd75f77d0ca94dbdba4e6cc9451fe4d33e78b3 Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期三, 19 五月 2021 19:14:13 +0800 Subject: [PATCH] add api BHQueryProcs. --- src/shm_queue.h | 163 ++++++++++++++++++++++++----------------------------- 1 files changed, 74 insertions(+), 89 deletions(-) diff --git a/src/shm_queue.h b/src/shm_queue.h index 5b30380..7871fe0 100644 --- a/src/shm_queue.h +++ b/src/shm_queue.h @@ -19,107 +19,92 @@ #ifndef SHM_QUEUE_JE0OEUP3 #define SHM_QUEUE_JE0OEUP3 +#include "robust.h" #include "shm.h" -#include "msg.h" +#include <atomic> #include <boost/circular_buffer.hpp> -#include <boost/date_time/posix_time/posix_time.hpp> +#include <chrono> -namespace bhome_shm { - -template <class D> using Circular = boost::circular_buffer<D, Allocator<D> >; - -typedef boost::uuids::uuid MQId; +namespace bhome_shm +{ template <class D> -class SharedQueue : private Circular<D> +using Circular = boost::circular_buffer<D, Allocator<D>>; + +template <class D> +class SharedQueue { - typedef Circular<D> Super; - Mutex mutex_; - Cond cond_read_; - Cond cond_write_; - Mutex & mutex() { return mutex_; } - - static boost::posix_time::ptime MSFromNow(const int ms) - { - using namespace boost::posix_time; - ptime cur = boost::posix_time::microsec_clock::universal_time(); - return cur + millisec(ms); - } - public: - SharedQueue(const uint32_t len, Allocator<D> const& alloc):Super(len, alloc) {} - using Super::size; - using Super::capacity; - template <class Iter, class OnWrite> - int Write(Iter begin, Iter end, const int timeout_ms, const OnWrite &onWrite) { - int n = 0; - if (begin != end) { - auto endtime = MSFromNow(timeout_ms); - Guard lock(mutex()); - while (cond_write_.timed_wait(lock, endtime, [&]() { return !this->full(); })) { - onWrite(*begin); - this->push_back(*begin); - ++n; - cond_read_.notify_one(); - if (++begin == end) { - break; - } - } - } - return n; - } - template <class OnWrite> - bool Write(const D &buf, const int timeout_ms, const OnWrite &onWrite) { - return Write(&buf, (&buf)+1, timeout_ms, onWrite); - } - bool Write(const D &buf, const int timeout_ms) { return Write(buf, timeout_ms, [](const D &buf){}); } + SharedQueue(const uint32_t len, Allocator<D> const &alloc) : + queue_(len, alloc) {} - template <class OnData> - bool Read(const int timeout_ms, OnData onData){ - int n = 0; - auto endtime = MSFromNow(timeout_ms); - Guard lock(mutex()); - while (cond_read_.timed_wait(lock, endtime, [&]() { return !this->empty(); })) { - const bool more = onData(this->front()); - this->pop_front(); - cond_write_.notify_one(); - ++n; - if (!more) { - break; - } - } - return n; - } - bool Read(D &buf, const int timeout_ms){ - auto read1 = [&](D &d) { - using std::swap; - swap(buf, d); - return false; - }; - return Read(timeout_ms, read1) == 1; - } + bool Read(D &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(D &d) + { + // bhome_shm::Guard lock(mutex_); + if (!queue_.empty()) { + d = queue_.front(); + queue_.pop_front(); + return true; + } else { + return false; + } + } + bool TryWrite(const D &d) + { + // bhome_shm::Guard lock(mutex_); + if (!queue_.full()) { + queue_.push_back(d); + return true; + } else { + return false; + } + } + +private: + Circular<D> queue_; + // bhome_shm::Mutex mutex_; }; -using namespace bhome_msg; - -class ShmMsgQueue : private ShmObject<SharedQueue<Msg> > +template <int Power = 4> +class SharedQ63 { - typedef ShmObject<SharedQueue<Msg> > Super; - typedef Super::Data Queue; - bool Write(const Msg &buf, const int timeout_ms) { return data()->Write(buf, timeout_ms); } - bool Read(Msg &buf, const int timeout_ms) { return data()->Read(buf, timeout_ms); } - MQId id_; -protected: - ShmMsgQueue(const std::string &raw_name, ShmType &segment, const int len); // internal use. public: - ShmMsgQueue(const MQId &id, ShmType &segment, const int len); - ShmMsgQueue(ShmType &segment, const int len); - ~ShmMsgQueue(); - bool Send(const MQId &remote_id, const void *data, const size_t size, const int timeout_ms); - bool Recv(MQId &source_id, void *&data, size_t &size, const int timeout_ms); - const MQId &Id() const { return id_; } - bool Send(const MQId &remote_id, const Msg &msg, const int timeout_ms); - bool Recv(Msg &msg, const int timeout_ms) { return Read(msg, timeout_ms); } + template <class... T> + explicit SharedQ63(T &&...t) {} // easy testing + + 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 { + for (int i = 0; i < 100; ++i) { + if (TryRead(d)) { + return true; + } + } + robust::QuickSleep(); + } while (steady_clock::now() < end_time); + return false; + } + bool TryRead(Data &d, const bool try_more = true) { return queue_.pop(d, try_more); } + bool TryWrite(const Data d, const bool try_more = true) { return queue_.push(d, try_more); } + +private: + robust::AtomicQueue<Power, Data> queue_; }; } // namespace bhome_shm -- Gitblit v1.8.0