| | |
| | | #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; |
| | | |
| | | template <class D> |
| | | class SyncedQueue : private Circular<D> |
| | | namespace bhome_shm |
| | | { |
| | | typedef Circular<D> Super; |
| | | Mutex mutex_; |
| | | Cond cond_read_; |
| | | Cond cond_write_; |
| | | Mutex & mutex() { return mutex_; } |
| | | const MQId id_; |
| | | |
| | | 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: |
| | | // template <class...T> SyncedQueue(const MQId &id, T&&...t):Super(t...), id_(id) {} |
| | | SyncedQueue(const MQId &id, const uint32_t len, Allocator<D> const& alloc):Super(len, alloc), id_(id) {} |
| | | using Super::size; |
| | | using Super::capacity; |
| | | const MQId &Id() const { return id_; } |
| | | bool Write(D buf, const int timeout_ms) { |
| | | Guard lock(mutex()); |
| | | if (cond_write_.timed_wait(lock, MSFromNow(timeout_ms), [&]() { return !this->full(); })) { |
| | | this->push_back(buf); |
| | | cond_read_.notify_one(); |
| | | return true; |
| | | } else { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | bool Read(D &buf, const int timeout_ms){ |
| | | Guard lock(mutex()); |
| | | if (cond_read_.timed_wait(lock, MSFromNow(timeout_ms), [&]() { return !this->empty(); })) { |
| | | buf = this->front(); |
| | | this->pop_front(); |
| | | cond_write_.notify_one(); |
| | | return true; |
| | | } else { |
| | | return false; |
| | | } |
| | | } |
| | | }; |
| | | |
| | | class ShmMsgQueue : private ShmObject<SyncedQueue<Msg> > |
| | | // just wrap robust::AtomicQ63 |
| | | class SharedQ63 |
| | | { |
| | | typedef ShmObject<SyncedQueue<Msg> > SharedQueue; |
| | | typedef SharedQueue::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); } |
| | | public: |
| | | ShmMsgQueue(const MQId &id, ShmType &segment, const uint32_t len); |
| | | ShmMsgQueue(ShmType &segment, const uint32_t 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 data()->Id(); } |
| | | template <class... T> |
| | | explicit SharedQ63(T &&...t) {} // easy testing |
| | | |
| | | typedef robust::AtomicQ63 AQ63; |
| | | typedef AQ63::Data Data; |
| | | 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: |
| | | AQ63 queue_; |
| | | }; |
| | | |
| | | } // namespace bhome_shm |