From db322f33ba13592f2492317e3f1a070454c97059 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期四, 13 五月 2021 19:34:46 +0800
Subject: [PATCH] center alloc all msgs.

---
 src/shm_msg_queue.cpp |   26 ++++++++++++++++----------
 1 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/src/shm_msg_queue.cpp b/src/shm_msg_queue.cpp
index b78c1a0..d96c511 100644
--- a/src/shm_msg_queue.cpp
+++ b/src/shm_msg_queue.cpp
@@ -56,6 +56,7 @@
 
 ShmMsgQueue::~ShmMsgQueue() {}
 
+#ifndef BH_USE_ATOMIC_Q
 ShmMsgQueue::Mutex &ShmMsgQueue::GetMutex(const MQId id)
 {
 	static std::unordered_map<MQId, std::shared_ptr<Mutex>> imm;
@@ -69,13 +70,19 @@
 	}
 	return *pos->second;
 }
+#endif
+
 bool ShmMsgQueue::Remove(SharedMemory &shm, const MQId id)
 {
 	Queue *q = Find(shm, id);
 	if (q) {
-		MsgI msg;
-		while (q->TryRead(msg.OffsetRef())) {
-			msg.Release();
+		RawData val = 0;
+		while (q->TryRead(val)) {
+			if (IsCmd(val)) {
+				LOG_DEBUG() << "clsing queue " << id << ", has a cmd" << DecodeCmd(val);
+			} else {
+				MsgI(val).Release();
+			}
 		}
 	}
 	return Shmq::Remove(shm, MsgQIdToName(id));
@@ -86,19 +93,18 @@
 	return Shmq::Find(shm, MsgQIdToName(remote_id));
 }
 
-bool ShmMsgQueue::TrySend(SharedMemory &shm, const MQId remote_id, MsgI msg)
+bool ShmMsgQueue::TrySend(SharedMemory &shm, const MQId remote_id, int64_t val)
 {
-	bool r = false;
 	try {
 		ShmMsgQueue dest(remote_id, false, shm, 1);
-		msg.AddRef();
-		DEFER1(if (!r) { msg.Release(); });
-
+#ifndef BH_USE_ATOMIC_Q
 		Guard lock(GetMutex(remote_id));
-		r = dest.queue().TryWrite(msg.Offset());
+#endif
+		return dest.queue().TryWrite(val);
 	} catch (...) {
+		// SetLastError(eNotFound, "remote not found");
+		return false;
 	}
-	return r;
 }
 
 // Test shows that in the 2 cases:

--
Gitblit v1.8.0