From 68c7bef33e74f23aa0136ccd6f7faa654d671ebc Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期五, 21 五月 2021 09:23:01 +0800
Subject: [PATCH] center publish notify; fix topic partial match.

---
 src/shm_queue.h |   75 +++++++++++++++++++++++--------------
 1 files changed, 47 insertions(+), 28 deletions(-)

diff --git a/src/shm_queue.h b/src/shm_queue.h
index 11f9893..3a2cea6 100644
--- a/src/shm_queue.h
+++ b/src/shm_queue.h
@@ -19,16 +19,17 @@
 #ifndef SHM_QUEUE_JE0OEUP3
 #define SHM_QUEUE_JE0OEUP3
 
+#include "robust.h"
 #include "shm.h"
 #include <atomic>
+#include <boost/circular_buffer.hpp>
 #include <chrono>
 
 namespace bhome_shm
 {
 
 template <class D>
-using Circular = robust::CircularBuffer<D, Allocator<D>>;
-
+using Circular = boost::circular_buffer<D, Allocator<D>>;
 
 template <class D>
 class SharedQueue
@@ -36,25 +37,6 @@
 public:
 	SharedQueue(const uint32_t len, Allocator<D> const &alloc) :
 	    queue_(len, alloc) {}
-
-	template <class OnWrite>
-	bool TryWrite(const D &d, const OnWrite &onWrite)
-	{
-		Guard lock(mutex());
-		if (!queue_.full()) {
-			onWrite(d);
-			queue_.push_back(d);
-			return true;
-		} else {
-			return false;
-		}
-	}
-
-	bool TryWrite(const D &d)
-	{
-		Guard lock(mutex());
-		return !queue_.full() ? (queue_.push_back(d), true) : false;
-	}
 
 	bool Read(D &d, const int timeout_ms)
 	{
@@ -64,16 +46,27 @@
 			if (TryRead(d)) {
 				return true;
 			} else {
-				robust::QuickSleep();
+				std::this_thread::sleep_for(1ms);
 			}
 		} while (steady_clock::now() < end_time);
 		return false;
 	}
 	bool TryRead(D &d)
 	{
-		Guard lock(mutex());
+		// bhome_shm::Guard lock(mutex_);
 		if (!queue_.empty()) {
-			queue_.pop_front(d);
+			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;
@@ -81,10 +74,36 @@
 	}
 
 private:
-	typedef Circular<D> Queue;
-	Queue queue_;
-	Mutex mutex_;
-	Mutex &mutex() { return mutex_; }
+	Circular<D> queue_;
+};
+
+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)
+	{
+		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;
+				}
+			}
+			std::this_thread::sleep_for(1ms);
+		} 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); }
+
+private:
+	robust::AtomicQueue<Power, Data> queue_;
 };
 
 } // namespace bhome_shm

--
Gitblit v1.8.0