| | |
| | | #ifndef SHM_QUEUE_JE0OEUP3 |
| | | #define SHM_QUEUE_JE0OEUP3 |
| | | |
| | | #include "msg.h" |
| | | #include "shm.h" |
| | | #include <atomic> |
| | | #include <boost/circular_buffer.hpp> |
| | |
| | | return [this](Guard &lock) { return !this->empty(); }; |
| | | } |
| | | |
| | | template <class Pred, class OnData> |
| | | int ReadAllOnCond(Pred const &pred, OnData const &onData) |
| | | { |
| | | Guard lock(this->mutex()); |
| | | int n = 0; |
| | | while (pred(lock)) { |
| | | ++n; |
| | | onData(this->front()); |
| | | this->pop_front(); |
| | | this->cond_write_.notify_one(); |
| | | } |
| | | return n; |
| | | } |
| | | |
| | | template <class Pred> |
| | | bool ReadOnCond(D &buf, Pred const &pred) |
| | | { |
| | | int flag = 0; |
| | | auto only_once = [&](Guard &lock) { return flag++ == 0 && pred(lock); }; |
| | | auto onData = [&buf](D &d) { |
| | | using std::swap; |
| | | swap(buf, d); |
| | | auto Read = [&]() { |
| | | Guard lock(this->mutex()); |
| | | if (pred(lock)) { |
| | | using std::swap; |
| | | swap(buf, Super::front()); |
| | | Super::pop_front(); |
| | | return true; |
| | | } else { |
| | | return false; |
| | | } |
| | | }; |
| | | return ReadAllOnCond(only_once, onData); |
| | | return Read() ? (this->cond_write_.notify_one(), true) : false; |
| | | } |
| | | |
| | | template <class Iter, class Pred, class OnWrite> |
| | |
| | | Guard lock(mutex()); |
| | | while (pred(lock)) { |
| | | onWrite(*begin); |
| | | this->push_back(*begin); |
| | | Super::push_back(*begin); |
| | | ++n; |
| | | cond_read_.notify_one(); |
| | | if (++begin == end) { |
| | |
| | | |
| | | bool Read(D &buf, const int timeout_ms) { return ReadOnCond(buf, TimedReadPred(timeout_ms)); } |
| | | bool TryRead(D &buf) { return ReadOnCond(buf, TryReadPred()); } |
| | | }; |
| | | |
| | | using namespace bhome_msg; |
| | | |
| | | class ShmMsgQueue : public StaticDataRef<std::atomic<uint64_t>, ShmMsgQueue> |
| | | { |
| | | typedef ShmObject<SharedQueue<MsgI>> Shmq; |
| | | typedef Shmq::ShmType ShmType; |
| | | typedef Shmq::Data Queue; |
| | | typedef std::function<void()> OnSend; |
| | | |
| | | public: |
| | | typedef uint64_t MQId; |
| | | |
| | | static MQId NewId(); |
| | | |
| | | ShmMsgQueue(const MQId id, ShmType &segment, const int len); |
| | | ShmMsgQueue(ShmType &segment, const int len); |
| | | ~ShmMsgQueue(); |
| | | static bool Remove(SharedMemory &shm, const MQId id); |
| | | MQId Id() const { return id_; } |
| | | ShmType &shm() const { return queue_.shm(); } |
| | | |
| | | bool Recv(MsgI &msg, const int timeout_ms) { return queue_.data()->Read(msg, timeout_ms); } |
| | | bool TryRecv(MsgI &msg) { return queue_.data()->TryRead(msg); } |
| | | template <class OnData> |
| | | int TryRecvAll(OnData const &onData) { return queue_.data()->TryReadAll(onData); } |
| | | static Queue *Find(SharedMemory &shm, const MQId remote_id); |
| | | static bool TrySend(SharedMemory &shm, const MQId remote_id, const MsgI &msg, OnSend const &onsend = OnSend()); |
| | | template <class Iter> |
| | | static int TrySendAll(SharedMemory &shm, const MQId remote_id, const Iter begin, const Iter end, OnSend const &onsend = OnSend()) |
| | | { |
| | | Queue *remote = Find(shm, remote_id); |
| | | if (remote) { |
| | | if (onsend) { |
| | | return remote->TryWrite(begin, end, [&onsend](const MsgI &msg) { onsend(); msg.AddRef(); }); |
| | | } else { |
| | | return remote->TryWrite(begin, end, [](const MsgI &msg) { msg.AddRef(); }); |
| | | } |
| | | } else { |
| | | // SetLestError(eNotFound); |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | template <class... Rest> |
| | | bool TrySend(const MQId remote_id, Rest const &...rest) { return TrySend(shm(), remote_id, rest...); } |
| | | template <class... Rest> |
| | | int TrySendAll(const MQId remote_id, Rest const &...rest) { return TrySendAll(shm(), remote_id, rest...); } |
| | | |
| | | private: |
| | | MQId id_; |
| | | Shmq &queue() { return queue_; } |
| | | Shmq queue_; |
| | | }; |
| | | |
| | | } // namespace bhome_shm |