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 | 27 +++++++++++++++++---------- 1 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/shm_msg_queue.cpp b/src/shm_msg_queue.cpp index bc5075f..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; @@ -65,16 +66,23 @@ auto pos = imm.find(id); if (pos == imm.end()) { pos = imm.emplace(id, new Mutex(id)).first; + // pos = imm.emplace(id, new Mutex()).first; } 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)); @@ -85,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