lichao
2021-03-25 a22dd242713636fad33ee5965fe0900a425ce50d
src/shm.h
@@ -19,18 +19,17 @@
#ifndef SHM_6CHO6D6C
#define SHM_6CHO6D6C
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/circular_buffer.hpp>
#include <boost/noncopyable.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/uuid/uuid.hpp>
#include "msg.h"
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
namespace bhome_shm {
using namespace boost::interprocess;
typedef managed_shared_memory mshm_t;
typedef interprocess_mutex Mutex;
typedef scoped_lock<Mutex> Guard;
@@ -55,7 +54,7 @@
    {}
    std::string name() const { return name_; }
    bool Remove() { return Remove(name()); }
    template <class T, class ...Params> T * New(Params const&...params) { return construct<T, std::nothrow>(anonymous_instance)(params...); }
    template <class T, class ...Params> T * New(Params const&...params) { return construct<T>(anonymous_instance, std::nothrow)(params...); }
    template <class T> void Delete(T *p) { if (p) { destroy_ptr<T>(p); }; }
    template <class T> void Delete(offset_ptr<T> p) { Delete(p.get()); }
@@ -65,7 +64,7 @@
// works like a smart pointer of an object in shared memory.
template <class T>
class ShmObject : private boost::noncopyable {
    static std::string DataName(const std::string &name) { return "dat" + name; }
    static std::string ObjName(const std::string &name) { return "obj" + name; }
protected:
    typedef T Data;
    typedef SharedMemory ShmType;
@@ -82,88 +81,22 @@
    ShmObject(ShmType &segment, const std::string &name, Params&&...t):
    shm_(segment), name_(name)
    {
        pdata_ = shm_.find_or_construct<Data>(DataName(name_).c_str())(t...);
        pdata_ = shm_.find_or_construct<Data>(ObjName(name_).c_str(), std::nothrow)(t...);
        if (!IsOk()) {
            throw("shm error: " + name_);
        }
    }
    Data *find(const std::string &name) { return shm_.find<Data>(DataName(name).c_str()).first; }
    Data *find(const std::string &name) { return shm_.find<Data>(ObjName(name).c_str()).first; }
    virtual ~ShmObject() {}
    std::string name() const { return name_; }
    Data* data() { return pdata_; }
    const Data* data() const { return pdata_; }
    Data* operator->() { return data(); }
    const Data* operator->() const { return data(); }
    virtual bool Remove() { return shm_.destroy<Data>(DataName(name_).c_str()); }
    virtual bool Remove() { return shm_.destroy<Data>(ObjName(name_).c_str()); }
};
template <class D> using Allocator = allocator<D, mshm_t::segment_manager>;
template <class D> using Circular = boost::circular_buffer<D, Allocator<D> >;
typedef boost::uuids::uuid MQId;
template <class D>
class SyncedQueue : private Circular<D>
{
    typedef Circular<D> Super;
    Mutex mutex_;
    Cond cond_read_;
    Cond cond_write_;
    Mutex & mutex() { return mutex_; }
    const MQId id_;
    static boost::posix_time::ptime MSFromNow(const int ms)
    {
        using namespace boost::posix_time;
        ptime cur = boost::posix_time::microsec_clock::universal_time();
        return cur + millisec(ms);
    }
public:
    // 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;
    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(); })) {
            this->push_back(buf);
            cond_read_.notify_one();
            return true;
        } else {
            return false;
        }
    }
    bool Read(D &buf, const int timeout_ms){
        Guard lock(mutex());
        if (cond_read_.timed_wait(lock, MSFromNow(timeout_ms), [&]() { return !this->empty(); })) {
            buf = this->front();
            this->pop_front();
            cond_write_.notify_one();
            return true;
        } else {
            return false;
        }
    }
};
class ShmMsgQueue : private ShmObject<SyncedQueue<Msg> >
{
    typedef ShmObject<SyncedQueue<Msg> > SharedQueue;
    typedef SharedQueue::Data Queue;
    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(const MQId &id, ShmType &segment, const uint32_t len);
    ShmMsgQueue(ShmType &segment, const uint32_t len);
    ~ShmMsgQueue();
    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);
    const MQId &Id() const { return data()->Id(); }
};
template <class D> using Allocator = allocator<D, SharedMemory::segment_manager>;
} // namespace bhome_shm