lichao
2021-05-18 cf05ea3d9f43e4e84d621e1f9d54cbef552b6e2b
src/shm_queue.h
@@ -19,10 +19,11 @@
#ifndef SHM_QUEUE_JE0OEUP3
#define SHM_QUEUE_JE0OEUP3
#include "robust.h"
#include "shm.h"
#include <atomic>
#include <boost/circular_buffer.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <chrono>
namespace bhome_shm
{
@@ -31,95 +32,79 @@
using Circular = boost::circular_buffer<D, Allocator<D>>;
template <class D>
class SharedQueue : private Circular<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);
   }
   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>
   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 (pred(lock)) {
         onWrite(*begin);
         Super::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) {}
       queue_(len, alloc) {}
   template <class Iter, class OnWrite>
   int TryWrite(Iter begin, Iter end, const OnWrite &onWrite)
   bool Read(D &d, const int timeout_ms)
   {
      auto tryWritePred = [this](Guard &lock) { return !this->full(); };
      return WriteAllOnCond(begin, end, tryWritePred, onWrite);
      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;
      }
   }
   template <class OnWrite>
   bool TryWrite(const D &buf, const OnWrite &onWrite) { return TryWrite(&buf, (&buf) + 1, onWrite); }
private:
   Circular<D> queue_;
   // bhome_shm::Mutex mutex_;
};
   bool TryWrite(const D &buf)
template <int Power = 4>
class SharedQ63
{
public:
   template <class... T>
   explicit SharedQ63(T &&...t) {} // easy testing
   typedef int64_t Data;
   bool Read(Data &d, const int timeout_ms)
   {
      return TryWrite(buf, [](const D &buf) {});
      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); }
   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()); }
private:
   robust::AtomicQueue<Power, Data> queue_;
};
} // namespace bhome_shm