lichao
2021-04-26 1b167ec5ad101ac44451381e26cc73ab5d67d2a1
src/shm_queue.h
@@ -19,8 +19,8 @@
#ifndef SHM_QUEUE_JE0OEUP3
#define SHM_QUEUE_JE0OEUP3
#include "msg.h"
#include "shm.h"
#include <atomic>
#include <boost/circular_buffer.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
@@ -29,8 +29,6 @@
template <class D>
using Circular = boost::circular_buffer<D, Allocator<D>>;
typedef boost::uuids::uuid MQId;
template <class D>
class SharedQueue : private Circular<D>
@@ -48,114 +46,80 @@
      return cur + millisec(ms);
   }
public:
   SharedQueue(const uint32_t len, Allocator<D> const &alloc) :
       Super(len, alloc) {}
   using Super::capacity;
   using Super::size;
   template <class Iter, class OnWrite>
   int Write(Iter begin, Iter end, const int timeout_ms, const OnWrite &onWrite)
   auto TimedReadPred(const int timeout_ms)
   {
      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) {});
   }
   template <class OnData>
   bool Read(const int timeout_ms, OnData onData)
   {
      int n = 0;
      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>
   bool ReadOnCond(D &buf, Pred const &pred)
   {
      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 Read() ? (this->cond_write_.notify_one(), true) : false;
   }
   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 (cond_read_.timed_wait(lock, endtime, [&]() { return !this->empty(); })) {
         const bool more = onData(this->front());
         this->pop_front();
         cond_write_.notify_one();
      while (pred(lock)) {
         onWrite(*begin);
         Super::push_back(*begin);
         ++n;
         if (!more) {
         cond_read_.notify_one();
         if (++begin == end) {
            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;
   }
};
using namespace bhome_msg;
class ShmMsgQueue : private ShmObject<SharedQueue<MsgI>>
{
   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(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();
   const MQId &Id() const { return id_; }
   SharedQueue(const uint32_t len, Allocator<D> const &alloc) :
       Super(len, alloc) {}
   // 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);
   template <class... Extra>
   bool Send(const MQId &remote_id, const MsgI &msg, const int timeout_ms, Extra const &...extra)
   template <class Iter, class OnWrite>
   int TryWrite(Iter begin, Iter end, const OnWrite &onWrite)
   {
      return Send(shm(), remote_id, msg, timeout_ms, extra...);
   }
   template <class Body, class... Extra>
   bool Send(const MQId &remote_id, const BHMsgHead &head, const Body &body, const int timeout_ms, Extra const &...extra)
   {
      MsgI msg;
      if (msg.Make(shm(), head, body)) {
         if (Send(shm(), remote_id, msg, timeout_ms, extra...)) {
            return true;
         } else {
            msg.Release(shm());
         }
      }
      return false;
      auto tryWritePred = [this](Guard &lock) { return !this->full(); };
      return WriteAllOnCond(begin, end, tryWritePred, onWrite);
   }
   size_t Pending() const { return data()->size(); }
   template <class OnWrite>
   bool TryWrite(const D &buf, const OnWrite &onWrite) { return TryWrite(&buf, (&buf) + 1, onWrite); }
   bool TryWrite(const D &buf)
   {
      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()); }
};
} // namespace bhome_shm