From 34cd75f77d0ca94dbdba4e6cc9451fe4d33e78b3 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期三, 19 五月 2021 19:14:13 +0800
Subject: [PATCH] add api BHQueryProcs.

---
 src/shm_msg_queue.h |   74 ++++++++++++++++++++++---------------
 1 files changed, 44 insertions(+), 30 deletions(-)

diff --git a/src/shm_msg_queue.h b/src/shm_msg_queue.h
index d7b33af..de60fde 100644
--- a/src/shm_msg_queue.h
+++ b/src/shm_msg_queue.h
@@ -18,59 +18,73 @@
 #ifndef SHM_MSG_QUEUE_D847TQXH
 #define SHM_MSG_QUEUE_D847TQXH
 
+#include "defs.h"
 #include "msg.h"
 #include "shm_queue.h"
 
 using namespace bhome_shm;
 using namespace bhome_msg;
 
+#define BH_USE_ATOMIC_Q
+
 class ShmMsgQueue : public StaticDataRef<std::atomic<uint64_t>, ShmMsgQueue>
 {
-	typedef ShmObject<SharedQueue<MsgI>> Shmq;
-	typedef Shmq::ShmType ShmType;
-	typedef Shmq::Data Queue;
-	typedef std::function<void()> OnSend;
-
 public:
+	typedef int64_t RawData;
+
+#ifdef BH_USE_ATOMIC_Q
+	typedef ShmObject<SharedQ63<0>> Shmq;
+#else
+	typedef ShmObject<SharedQueue<RawData>> Shmq;
+	// typedef robust::FMutex Mutex;
+	// typedef robust::SemMutex Mutex;
+	typedef robust::NullMutex Mutex;
+	typedef robust::Guard<Mutex> Guard;
+#endif
+
+	typedef Shmq::Data Queue;
+	typedef Shmq::ShmType ShmType;
 	typedef uint64_t MQId;
 
 	static MQId NewId();
 
-	ShmMsgQueue(const MQId id, ShmType &segment, const int len);
-	ShmMsgQueue(ShmType &segment, const int len);
+	ShmMsgQueue(ShmType &segment, const MQId id, const int len);
+	ShmMsgQueue(ShmType &segment, const bool create_or_else_find, const MQId id, const int len);
+	ShmMsgQueue(const int64_t abs_addr, ShmType &segment, const MQId id);
 	~ShmMsgQueue();
-	static bool Remove(SharedMemory &shm, const MQId id);
+	static bool Remove(ShmType &shm, const MQId id);
 	MQId Id() const { return id_; }
 	ShmType &shm() const { return queue_.shm(); }
+	int64_t AbsAddr() const { return queue_.offset(); }
 
-	bool Recv(MsgI &msg, const int timeout_ms) { return queue_.data()->Read(msg, timeout_ms); }
-	bool TryRecv(MsgI &msg) { return queue_.data()->TryRead(msg); }
-	static Queue *Find(SharedMemory &shm, const MQId remote_id);
-	static bool TrySend(SharedMemory &shm, const MQId remote_id, const MsgI &msg, OnSend const &onsend = OnSend());
-	template <class Iter>
-	static int TrySendAll(SharedMemory &shm, const MQId remote_id, const Iter begin, const Iter end, OnSend const &onsend = OnSend())
+	bool Recv(RawData &val, const int timeout_ms)
 	{
-		Queue *remote = Find(shm, remote_id);
-		if (remote) {
-			if (onsend) {
-				return remote->TryWrite(begin, end, [&onsend](const MsgI &msg) { onsend(); msg.AddRef(); });
-			} else {
-				return remote->TryWrite(begin, end, [](const MsgI &msg) { msg.AddRef(); });
-			}
-		} else {
-			// SetLestError(eNotFound);
-			return 0;
-		}
+#ifndef BH_USE_ATOMIC_Q
+		Guard lock(GetMutex(Id()));
+#endif
+		return queue().Read(val, timeout_ms);
 	}
 
-	template <class... Rest>
-	bool TrySend(const MQId remote_id, Rest const &...rest) { return TrySend(shm(), remote_id, rest...); }
-	template <class... Rest>
-	int TrySendAll(const MQId remote_id, Rest const &...rest) { return TrySendAll(shm(), remote_id, rest...); }
+	bool TryRecv(RawData &val)
+	{
+#ifndef BH_USE_ATOMIC_Q
+		Guard lock(GetMutex(Id()));
+#endif
+		return queue().TryRead(val);
+	}
+
+	bool Recv(MsgI &msg, const int timeout_ms) { return Recv(msg.OffsetRef(), timeout_ms); }
+	bool TryRecv(MsgI &msg) { return TryRecv(msg.OffsetRef()); }
+	static Queue *Find(ShmType &shm, const MQId remote);
+	static bool TrySend(ShmType &shm, const MQInfo &remote, const RawData val);
+	bool TrySend(const MQInfo &remote, const RawData val) { return TrySend(shm(), remote, val); }
 
 private:
+#ifndef BH_USE_ATOMIC_Q
+	static Mutex &GetMutex(const MQId id);
+#endif
 	MQId id_;
-	Shmq &queue() { return queue_; }
+	Queue &queue() { return *queue_.data(); }
 	Shmq queue_;
 };
 

--
Gitblit v1.8.0