From 58d904a328c0d849769b483e901a0be9426b8209 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期二, 20 七月 2021 20:20:44 +0800
Subject: [PATCH] 调整Request C.BHFree的位置

---
 src/shm_queue.h |   65 +++++---------------------------
 1 files changed, 11 insertions(+), 54 deletions(-)

diff --git a/src/shm_queue.h b/src/shm_queue.h
index 11f9893..952dae9 100644
--- a/src/shm_queue.h
+++ b/src/shm_queue.h
@@ -19,72 +19,29 @@
 #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>>;
-
-
-template <class D>
-class SharedQueue
+// just wrap robust::AtomicQ63
+class SharedQ63
 {
 public:
-	SharedQueue(const uint32_t len, Allocator<D> const &alloc) :
-	    queue_(len, alloc) {}
+	template <class... T>
+	explicit SharedQ63(T &&...t) {} // easy testing
 
-	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)
-	{
-		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)
-	{
-		Guard lock(mutex());
-		if (!queue_.empty()) {
-			queue_.pop_front(d);
-			return true;
-		} else {
-			return false;
-		}
-	}
+	typedef robust::AtomicQ63 AQ63;
+	typedef AQ63::Data Data;
+	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:
-	typedef Circular<D> Queue;
-	Queue queue_;
-	Mutex mutex_;
-	Mutex &mutex() { return mutex_; }
+	AQ63 queue_;
 };
 
 } // namespace bhome_shm

--
Gitblit v1.8.0