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_msg_queue.cpp | 65 ++++++++------------------------ 1 files changed, 17 insertions(+), 48 deletions(-) diff --git a/src/shm_msg_queue.cpp b/src/shm_msg_queue.cpp index ae019bf..e6aceb9 100644 --- a/src/shm_msg_queue.cpp +++ b/src/shm_msg_queue.cpp @@ -29,41 +29,15 @@ return std::string(buf, n + 4); } -const int AdjustMQLength(const int len) -{ - const int kMaxLength = 10000; - const int kDefaultLen = 12; - if (len <= 0) { - return kDefaultLen; - } else if (len < kMaxLength) { - return len; - } else { - return kMaxLength; - } -} - } // namespace -ShmMsgQueue::MQId ShmMsgQueue::NewId() -{ - static auto &id = GetData(); - return ++id; -} -// ShmMsgQueue memory usage: (320 + 16*length) bytes, length >= 2 -ShmMsgQueue::ShmMsgQueue(const MQId id, ShmType &segment, const int len) : +ShmMsgQueue::ShmMsgQueue(ShmType &segment, const MQId id, Mode mode) : id_(id), - queue_(segment, MsgQIdToName(id_), AdjustMQLength(len), segment.get_segment_manager()) + queue_(segment, MsgQIdToName(id_), mode) { } - -ShmMsgQueue::ShmMsgQueue(ShmType &segment, const int len) : - id_(NewId()), - queue_(segment, true, MsgQIdToName(id_), AdjustMQLength(len), segment.get_segment_manager()) -{ - if (!queue_.IsOk()) { - throw("error create msgq " + std::to_string(id_)); - } -} +ShmMsgQueue::ShmMsgQueue(const int64_t abs_addr, ShmType &segment, const MQId id) : + id_(id), queue_(abs_addr, segment, MsgQIdToName(id_)) {} ShmMsgQueue::~ShmMsgQueue() {} @@ -71,9 +45,13 @@ { Queue *q = Find(shm, id); if (q) { - MsgI msg; - while (q->TryRead(msg)) { - 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, shm).Release(); + } } } return Shmq::Remove(shm, MsgQIdToName(id)); @@ -84,22 +62,13 @@ return Shmq::Find(shm, MsgQIdToName(remote_id)); } -bool ShmMsgQueue::TrySend(SharedMemory &shm, const MQId remote_id, const MsgI &msg, OnSend const &onsend) +bool ShmMsgQueue::TrySend(SharedMemory &shm, const MQInfo &remote, const RawData val) { - Queue *remote = Find(shm, remote_id); - if (remote) { - if (onsend) { - return remote->TryWrite(msg, [&onsend](const MsgI &msg) { onsend(); msg.AddRef(); }); - } else { - return remote->TryWrite(msg, [](const MsgI &msg) { msg.AddRef(); }); - } - } else { - // SetLestError(eNotFound); + try { + ShmMsgQueue dest(remote.offset_, shm, remote.id_); + return dest.queue().TryWrite(val); + } catch (...) { + // SetLastError(eNotFound, "remote not found"); return false; } } - -// Test shows that in the 2 cases: -// 1) build msg first, then find remote queue; -// 2) find remote queue first, then build msg; -// 1 is about 50% faster than 2, maybe cache related. -- Gitblit v1.8.0