| | |
| | | #include "bh_util.h" |
| | | #include "proto.h" |
| | | #include "shm.h" |
| | | #include <atomic> |
| | | #include <boost/interprocess/offset_ptr.hpp> |
| | | #include <boost/uuid/uuid_generators.hpp> |
| | | #include <functional> |
| | |
| | | // store ref count, msgs shareing the same data should also hold a pointer of the same RefCount object. |
| | | class RefCount : private boost::noncopyable |
| | | { |
| | | public: |
| | | int Inc() |
| | | { |
| | | Guard lk(mutex_); |
| | | return ++num_; |
| | | } |
| | | int Dec() |
| | | { |
| | | Guard lk(mutex_); |
| | | return --num_; |
| | | } |
| | | int Get() |
| | | { |
| | | Guard lk(mutex_); |
| | | return num_; |
| | | } |
| | | std::atomic<int> num_; |
| | | |
| | | private: |
| | | Mutex mutex_; |
| | | int num_ = 1; |
| | | public: |
| | | RefCount() : |
| | | num_(1) { static_assert(std::is_pod<decltype(num_)>::value); } |
| | | int Inc() { return ++num_; } |
| | | int Dec() { return --num_; } |
| | | int Get() { return num_.load(); } |
| | | }; |
| | | |
| | | // message content layout: header_size + header + data_size + data |