From 77a6c3512a44dfe6540dde71946e6484fe4f173f Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期一, 10 五月 2021 16:05:28 +0800 Subject: [PATCH] test lock code. --- src/shm_msg_queue.h | 44 ++++++++++++++++++++------------------------ 1 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/shm_msg_queue.h b/src/shm_msg_queue.h index d7b33af..1970803 100644 --- a/src/shm_msg_queue.h +++ b/src/shm_msg_queue.h @@ -26,10 +26,15 @@ class ShmMsgQueue : public StaticDataRef<std::atomic<uint64_t>, ShmMsgQueue> { - typedef ShmObject<SharedQueue<MsgI>> Shmq; + // typedef ShmObject<SharedQ63<4>> Shmq; + typedef ShmObject<SharedQueue<int64_t>> Shmq; typedef Shmq::ShmType ShmType; typedef Shmq::Data Queue; typedef std::function<void()> OnSend; + typedef robust::FMutex Mutex; + // typedef robust::SemMutex Mutex; + // typedef robust::NullMutex Mutex; + typedef robust::Guard<Mutex> Guard; public: typedef uint64_t MQId; @@ -37,40 +42,31 @@ static MQId NewId(); ShmMsgQueue(const MQId id, ShmType &segment, const int len); + ShmMsgQueue(const MQId id, const bool create_or_else_find, ShmType &segment, const int len); ShmMsgQueue(ShmType &segment, const int len); ~ShmMsgQueue(); static bool Remove(SharedMemory &shm, const MQId id); MQId Id() const { return id_; } ShmType &shm() const { return queue_.shm(); } - 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(MsgI &msg, 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; - } + Guard lock(GetMutex(Id())); + return queue().Read(msg.OffsetRef(), 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(MsgI &msg) + { + Guard lock(GetMutex(Id())); + return queue().TryRead(msg.OffsetRef()); + } + static Queue *Find(SharedMemory &shm, const MQId remote_id); + static bool TrySend(SharedMemory &shm, const MQId remote_id, MsgI msg); + bool TrySend(const MQId remote_id, const MsgI &msg) { return TrySend(shm(), remote_id, msg); } private: + static Mutex &GetMutex(const MQId id); MQId id_; - Shmq &queue() { return queue_; } + Queue &queue() { return *queue_.data(); } Shmq queue_; }; -- Gitblit v1.8.0