From 1b167ec5ad101ac44451381e26cc73ab5d67d2a1 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期一, 26 四月 2021 16:37:52 +0800
Subject: [PATCH] fix socket busy loop; del locked readall; refactor.

---
 src/shm_queue.h |  156 ++++++++++++++++++++++++++++++----------------------
 1 files changed, 90 insertions(+), 66 deletions(-)

diff --git a/src/shm_queue.h b/src/shm_queue.h
index 140654c..5dbda96 100644
--- a/src/shm_queue.h
+++ b/src/shm_queue.h
@@ -20,82 +20,106 @@
 #define SHM_QUEUE_JE0OEUP3
 
 #include "shm.h"
-#include "msg.h"
+#include <atomic>
 #include <boost/circular_buffer.hpp>
 #include <boost/date_time/posix_time/posix_time.hpp>
 
-namespace bhome_shm {
-	
-template <class D> using Circular = boost::circular_buffer<D, Allocator<D> >;
-
-typedef boost::uuids::uuid MQId;
+namespace bhome_shm
+{
 
 template <class D>
-class SyncedQueue : private Circular<D>
-{
-    typedef Circular<D> Super;
-    Mutex mutex_;
-    Cond cond_read_;
-    Cond cond_write_;
-    Mutex & mutex() { return mutex_; }
-    const MQId id_;
+using Circular = boost::circular_buffer<D, Allocator<D>>;
 
-    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);
-    }
+template <class D>
+class SharedQueue : private Circular<D>
+{
+	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:
-    // 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_; }
-    template <class OnWrite>
-    bool Write(const D &buf, const int timeout_ms, const OnWrite &onWrite) {
-        Guard lock(mutex());
-        if (cond_write_.timed_wait(lock, MSFromNow(timeout_ms), [&]() { return !this->full(); })) {
-            onWrite();
-            this->push_back(buf);
-            cond_read_.notify_one();
-            return true;
-        } else {
-            return false;
-        }
-    }
-    bool Write(const D &buf, const int timeout_ms) { return Write(buf, timeout_ms, [](){}); }
+	SharedQueue(const uint32_t len, Allocator<D> const &alloc) :
+	    Super(len, alloc) {}
 
-    bool Read(D &buf, const int timeout_ms){
-        Guard lock(mutex());
-        if (cond_read_.timed_wait(lock, MSFromNow(timeout_ms), [&]() { return !this->empty(); })) {
-            using std::swap;
-            swap(buf, this->front());
-            this->pop_front();
-            cond_write_.notify_one();
-            return true;
-        } else {
-            return false;
-        }
-    }
-};
+	template <class Iter, class OnWrite>
+	int TryWrite(Iter begin, Iter end, const OnWrite &onWrite)
+	{
+		auto tryWritePred = [this](Guard &lock) { return !this->full(); };
+		return WriteAllOnCond(begin, end, tryWritePred, onWrite);
+	}
 
-class ShmMsgQueue : private ShmObject<SyncedQueue<Msg> >
-{
-    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 int len);
-    ShmMsgQueue(ShmType &segment, const int 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(); }
-    bool Send(const MQId &remote_id, const Msg &msg, const int timeout_ms);
-    bool Recv(Msg &msg, const int timeout_ms) { return Read(msg, timeout_ms); }
+	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

--
Gitblit v1.8.0