lichao
2021-03-24 6f9521a6dca494a9f9644d1ccacdee23744dc0e5
src/shm.h
@@ -10,7 +10,7 @@
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  LiChao (),
 *         Author:  Li Chao (),
 *   Organization:  
 *
 * =====================================================================================
@@ -26,6 +26,7 @@
#include <boost/noncopyable.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/uuid/uuid.hpp>
#include "msg.h"
namespace bhome_shm {
@@ -101,6 +102,7 @@
template <class D> using Circular = boost::circular_buffer<D, Allocator<D> >;
typedef boost::uuids::uuid MQId;
template <class D>
class SyncedQueue : private Circular<D>
{
@@ -119,10 +121,11 @@
    }
public:
    template <class...T> SyncedQueue(MQId id, T&&...t):Super(t...), id_(id) {}
    // template <class...T> SyncedQueue(const MQId &id, T&&...t):Super(t...), id_(id) {}
    SyncedQueue(const MQId &id, const uint32_t len, Allocator<D> const& alloc):Super(len, alloc), id_(id) {}
    using Super::size;
    using Super::capacity;
    MQId Id() const { return id_; }
    const MQId &Id() const { return id_; }
    bool Write(D buf, const int timeout_ms) {
        Guard lock(mutex());
        if (cond_write_.timed_wait(lock, MSFromNow(timeout_ms), [&]() { return !this->full(); })) {
@@ -147,13 +150,6 @@
    }
};
// safe to be stored in shared memory.
struct Msg {
    MQId src_;
    offset_ptr<void> data_;
    size_t size_;
};
class ShmMsgQueue : private ShmObject<SyncedQueue<Msg> >
{
    typedef ShmObject<SyncedQueue<Msg> > SharedQueue;
@@ -161,13 +157,12 @@
    bool Write(const Msg &buf, const int timeout_ms) { return data()->Write(buf, timeout_ms); }
    bool Read(Msg &buf, const int timeout_ms) { return data()->Read(buf, timeout_ms); }
public:
    ShmMsgQueue(MQId id, ShmType &segment, const uint32_t len);
    ShmMsgQueue(const MQId &id, ShmType &segment, const uint32_t len);
    ShmMsgQueue(ShmType &segment, const uint32_t len);
    ~ShmMsgQueue();
    bool Send(MQId remote_id, const void *data, const size_t size, const int timeout_ms);
    bool Send(const MQId &remote_id, const void *data, const size_t size, const int timeout_ms);
    bool Recv(MQId &source_id, void *&data, size_t &size, const int timeout_ms);
    using SharedQueue::Remove;
    MQId Id() const { return data()->Id(); }
    const MQId &Id() const { return data()->Id(); }
};
} // namespace bhome_shm