mq always create new shm, do not find.
| | |
| | | { |
| | | static std::string ObjName(const std::string &name) { return "obj" + name; } |
| | | |
| | | protected: |
| | | public: |
| | | typedef T Data; |
| | | typedef SharedMemory ShmType; |
| | | |
| | | private: |
| | | ShmType &shm_; |
| | | std::string name_; |
| | | Data *pdata_ = nullptr; |
| | | |
| | | bool IsOk() const { return pdata_; } |
| | | |
| | | protected: |
| | | ShmType &shm() const { return shm_; } |
| | | |
| | | public: |
| | | template <class... Params> |
| | | ShmObject(ShmType &segment, const std::string &name, Params &&...t) : |
| | | shm_(segment), name_(name) |
| | | { |
| | | pdata_ = shm_.Create<Data>(ObjName(name_), std::forward<decltype(t)>(t)...); |
| | | pdata_ = shm_.FindOrCreate<Data>(ObjName(name_), std::forward<decltype(t)>(t)...); |
| | | if (!IsOk()) { |
| | | throw("Error: Not enough memory, can not allocate \"" + name_ + "\""); |
| | | } |
| | | } |
| | | template <class... Params> |
| | | ShmObject(ShmType &segment, const bool create_or_else_find, const std::string &name, Params &&...t) : |
| | | shm_(segment), name_(name) |
| | | { |
| | | if (create_or_else_find) { |
| | | pdata_ = shm_.Create<Data>(ObjName(name_), std::forward<decltype(t)>(t)...); |
| | | } else { |
| | | pdata_ = shm_.Find<Data>(ObjName(name_)); |
| | | } |
| | | } |
| | | bool IsOk() const { return pdata_; } |
| | | |
| | | static bool Remove(SharedMemory &shm, const std::string &name) { return shm.destroy<Data>(ObjName(name).c_str()); } |
| | | static Data *Find(SharedMemory &shm, const std::string &name) { return shm.Find<Data>(ObjName(name)); } |
| | | Data *Find(const std::string &name) { return Find(shm_, ObjName(name)); } |
| | |
| | | Data *operator->() { return data(); } |
| | | const Data *operator->() const { return data(); } |
| | | bool Remove() { return Remove(shm_, name_); } |
| | | |
| | | private: |
| | | ShmType &shm_; |
| | | std::string name_; |
| | | Data *pdata_ = nullptr; |
| | | }; |
| | | |
| | | } // namespace bhome_shm |
| | |
| | | } |
| | | // ShmMsgQueue memory usage: (320 + 16*length) bytes, length >= 2 |
| | | ShmMsgQueue::ShmMsgQueue(const MQId id, ShmType &segment, const int len) : |
| | | Super(segment, MsgQIdToName(id), AdjustMQLength(len), segment.get_segment_manager()), |
| | | id_(id) |
| | | id_(id), |
| | | queue_(segment, MsgQIdToName(id_), AdjustMQLength(len), segment.get_segment_manager()) |
| | | { |
| | | } |
| | | |
| | | ShmMsgQueue::ShmMsgQueue(ShmType &segment, const int len) : |
| | | ShmMsgQueue(NewId(), segment, 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() {} |
| | | |
| | |
| | | msg.Release(); |
| | | } |
| | | } |
| | | return Super::Remove(shm, MsgQIdToName(id)); |
| | | return Shmq::Remove(shm, MsgQIdToName(id)); |
| | | } |
| | | |
| | | ShmMsgQueue::Queue *ShmMsgQueue::Find(SharedMemory &shm, const MQId remote_id) |
| | | { |
| | | return Super::Find(shm, MsgQIdToName(remote_id)); |
| | | return Shmq::Find(shm, MsgQIdToName(remote_id)); |
| | | } |
| | | |
| | | bool ShmMsgQueue::TrySend(SharedMemory &shm, const MQId remote_id, const MsgI &msg, OnSend const &onsend) |
| | |
| | | public: |
| | | SharedQueue(const uint32_t len, Allocator<D> const &alloc) : |
| | | Super(len, alloc) {} |
| | | using Super::capacity; |
| | | using Super::size; |
| | | |
| | | template <class Iter, class OnWrite> |
| | | int TryWrite(Iter begin, Iter end, const OnWrite &onWrite) |
| | |
| | | |
| | | using namespace bhome_msg; |
| | | |
| | | class ShmMsgQueue : private ShmObject<SharedQueue<MsgI>>, public StaticDataRef<std::atomic<uint64_t>, ShmMsgQueue> |
| | | class ShmMsgQueue : public StaticDataRef<std::atomic<uint64_t>, ShmMsgQueue> |
| | | { |
| | | typedef ShmObject<SharedQueue<MsgI>> Super; |
| | | typedef Super::Data Queue; |
| | | typedef ShmObject<SharedQueue<MsgI>> Shmq; |
| | | typedef Shmq::ShmType ShmType; |
| | | typedef Shmq::Data Queue; |
| | | typedef std::function<void()> OnSend; |
| | | |
| | | public: |
| | |
| | | ~ShmMsgQueue(); |
| | | static bool Remove(SharedMemory &shm, const MQId id); |
| | | MQId Id() const { return id_; } |
| | | using Super::shm; |
| | | ShmType &shm() const { return queue_.shm(); } |
| | | |
| | | bool Recv(MsgI &msg, const int timeout_ms) { return data()->Read(msg, timeout_ms); } |
| | | bool TryRecv(MsgI &msg) { return data()->TryRead(msg); } |
| | | bool Recv(MsgI &msg, const int timeout_ms) { return queue_.data()->Read(msg, timeout_ms); } |
| | | bool TryRecv(MsgI &msg) { return queue_.data()->TryRead(msg); } |
| | | template <class OnData> |
| | | int TryRecvAll(OnData const &onData) { return data()->TryReadAll(onData); } |
| | | int TryRecvAll(OnData const &onData) { return queue_.data()->TryReadAll(onData); } |
| | | 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> |
| | |
| | | |
| | | private: |
| | | MQId id_; |
| | | Shmq &queue() { return queue_; } |
| | | Shmq queue_; |
| | | }; |
| | | |
| | | } // namespace bhome_shm |