| | |
| | | return cur + millisec(ms); |
| | | } |
| | | |
| | | auto TimedReadPred(const int timeout_ms) |
| | | { |
| | | auto endtime = MSFromNow(timeout_ms); |
| | | return [this, endtime](Guard &lock) { |
| | | return (cond_read_.timed_wait(lock, endtime, [&]() { return !this->empty(); })); |
| | | }; |
| | | } |
| | | auto TryReadPred() |
| | | { |
| | | 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); |
| | | }; |
| | | return ReadAllOnCond(only_once, onData); |
| | | } |
| | | |
| | | template <class Iter, class Pred, class OnWrite> |
| | | int WriteAllOnCond(Iter begin, Iter end, Pred const &pred, OnWrite const &onWrite) |
| | | { |
| | | if (begin == end) { return 0; } |
| | | |
| | | int n = 0; |
| | | Guard lock(mutex()); |
| | | while (pred(lock)) { |
| | | onWrite(*begin); |
| | | this->push_back(*begin); |
| | | ++n; |
| | | cond_read_.notify_one(); |
| | | if (++begin == end) { |
| | | break; |
| | | } |
| | | } |
| | | return n; |
| | | } |
| | | |
| | | public: |
| | | SharedQueue(const uint32_t len, Allocator<D> const &alloc) : |
| | | Super(len, alloc) {} |
| | |
| | | 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; |
| | | auto endtime = MSFromNow(timeout_ms); |
| | | auto timedWritePred = [this, endtime](Guard &lock) { |
| | | return (cond_write_.timed_wait(lock, endtime, [&]() { return !this->full(); })); |
| | | }; |
| | | return WriteAllOnCond(begin, end, timedWritePred, onWrite); |
| | | } |
| | | |
| | | 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, 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) {}); |
| | | } |
| | | |
| | | template <class OnData> |
| | | bool Read(const int timeout_ms, OnData onData) |
| | | template <class Iter, class OnWrite> |
| | | int TryWrite(Iter begin, Iter end, const OnWrite &onWrite) |
| | | { |
| | | 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; |
| | | auto tryWritePred = [this](Guard &lock) { return !this->full(); }; |
| | | return WriteAllOnCond(begin, end, tryWritePred, onWrite); |
| | | } |
| | | |
| | | bool Read(D &buf, const int timeout_ms) |
| | | template <class OnWrite> |
| | | bool TryWrite(const D &buf, const OnWrite &onWrite) { return TryWrite(&buf, (&buf) + 1, onWrite); } |
| | | |
| | | bool TryWrite(const D &buf) |
| | | { |
| | | auto read1 = [&](D &d) { |
| | | using std::swap; |
| | | swap(buf, d); |
| | | return false; |
| | | }; |
| | | return Read(timeout_ms, read1) == 1; |
| | | return TryWrite(buf, [](const D &buf) {}); |
| | | } |
| | | |
| | | template <class OnData> |
| | | int ReadAll(const int timeout_ms, OnData const &onData) { return ReadAllOnCond(TimedReadPred(timeout_ms), onData); } |
| | | template <class OnData> |
| | | int TryReadAll(OnData const &onData) { return ReadAllOnCond(TryReadPred(), onData); } |
| | | |
| | | 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; |
| | |
| | | typedef ShmObject<SharedQueue<MsgI>> Super; |
| | | typedef Super::Data Queue; |
| | | typedef std::function<void()> OnSend; |
| | | bool Write(const MsgI &buf, const int timeout_ms) { return data()->Write(buf, timeout_ms); } |
| | | bool Read(MsgI &buf, const int timeout_ms) { return data()->Read(buf, timeout_ms); } |
| | | MQId id_; |
| | | |
| | | protected: |
| | |
| | | ~ShmMsgQueue(); |
| | | static bool Remove(SharedMemory &shm, const MQId &id); |
| | | const MQId &Id() const { return id_; } |
| | | using Super::shm; |
| | | |
| | | // bool Recv(MsgI &msg, BHMsgHead &head, const int timeout_ms); |
| | | bool Recv(MsgI &msg, const int timeout_ms) { return Read(msg, timeout_ms); } |
| | | static bool Send(SharedMemory &shm, const MQId &remote_id, const MsgI &msg, const int timeout_ms, OnSend const &onsend); |
| | | static bool Send(SharedMemory &shm, const MQId &remote_id, const MsgI &msg, const int timeout_ms); |
| | | bool Recv(MsgI &msg, const int timeout_ms) { return data()->Read(msg, timeout_ms); } |
| | | bool TryRecv(MsgI &msg) { return data()->TryRead(msg); } |
| | | template <class OnData> |
| | | int TryRecvAll(OnData const &onData) { return data()->TryReadAll(onData); } |
| | | |
| | | static bool Send(SharedMemory &shm, const MQId &remote_id, const MsgI &msg, const int timeout_ms, OnSend const &onsend = OnSend()); |
| | | static bool TrySend(SharedMemory &shm, const MQId &remote_id, const MsgI &msg, OnSend const &onsend = OnSend()); |
| | | |
| | | template <class... Rest> |
| | | bool Send(const MQId &remote_id, Rest const &...rest) { return Send(shm(), remote_id, rest...); } |
| | | template <class... Rest> |
| | | bool TrySend(const MQId &remote_id, Rest const &...rest) { return TrySend(shm(), remote_id, rest...); } |
| | | |
| | | size_t Pending() const { return data()->size(); } |
| | | }; |
| | | |