From bcd780993c176b93f7393607f8003adf66e6676a Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期一, 26 四月 2021 10:27:14 +0800 Subject: [PATCH] fix node default ignore msg. --- src/shm.h | 278 +++++++++++++++++++++++++++++------------------------- 1 files changed, 149 insertions(+), 129 deletions(-) diff --git a/src/shm.h b/src/shm.h index 94ead7f..7773ceb 100644 --- a/src/shm.h +++ b/src/shm.h @@ -10,7 +10,7 @@ * Revision: none * Compiler: gcc * - * Author: LiChao (), + * Author: Li Chao (), * Organization: * * ===================================================================================== @@ -19,154 +19,174 @@ #ifndef SHM_6CHO6D6C #define SHM_6CHO6D6C +#include <atomic> #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/sync/interprocess_condition.hpp> -#include <boost/circular_buffer.hpp> +#include <boost/interprocess/sync/interprocess_mutex.hpp> +#include <boost/interprocess/sync/scoped_lock.hpp> #include <boost/noncopyable.hpp> -#include <boost/date_time/posix_time/posix_time.hpp> +#include <chrono> +#include <thread> -namespace bhome_shm { +namespace bhome_shm +{ using namespace boost::interprocess; + typedef managed_shared_memory mshm_t; -typedef interprocess_mutex Mutex; + +class CasMutex +{ + std::atomic<bool> flag_; + bool cas(bool expected, bool new_val) { return flag_.compare_exchange_strong(expected, new_val); } + +public: + CasMutex() : + flag_(false) {} + bool try_lock() { return cas(false, true); } + void lock() + { + while (!try_lock()) { std::this_thread::yield(); } + } + void unlock() { cas(true, false); } +}; + +class MutexWithTimeLimit +{ + typedef boost::interprocess::interprocess_mutex MutexT; + // typedef CasMutex MutexT; + typedef std::chrono::steady_clock Clock; + typedef Clock::duration Duration; + static Duration Now() { return Clock::now().time_since_epoch(); } + + const Duration limit_; + std::atomic<Duration> last_lock_time_; + MutexT mutex_; + +public: + typedef MutexT::internal_mutex_type internal_mutex_type; + const internal_mutex_type &internal_mutex() const { return mutex_.internal_mutex(); } + internal_mutex_type &internal_mutex() { return mutex_.internal_mutex(); } + + explicit MutexWithTimeLimit(Duration limit) : + limit_(limit) {} + MutexWithTimeLimit() : + MutexWithTimeLimit(std::chrono::seconds(1)) {} + ~MutexWithTimeLimit() { static_assert(std::is_pod<Duration>::value); } + bool try_lock(); + void lock(); + void unlock(); +}; + +// typedef boost::interprocess::interprocess_mutex Mutex; +typedef MutexWithTimeLimit Mutex; typedef scoped_lock<Mutex> Guard; typedef interprocess_condition Cond; class SharedMemory : public mshm_t { - std::string name_; + std::string name_; - static permissions AllowAll() { - permissions perm; - perm.set_unrestricted(); - return perm; - } + static permissions AllowAll() + { + permissions perm; + perm.set_unrestricted(); + return perm; + } + void Swap(SharedMemory &a); + public: - static bool Remove(const std::string &name) { - return shared_memory_object::remove(name.c_str()); - } - SharedMemory(const std::string &name, const uint64_t size): - mshm_t(open_or_create, name.c_str(), size, 0, AllowAll()), - name_(name) - {} - 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> void Delete(T *p) { if (p) { destroy_ptr<T>(p); }; } - template <class T> void Delete(offset_ptr<T> p) { Delete(p.get()); } + static bool Remove(const std::string &name) { return shared_memory_object::remove(name.c_str()); } + SharedMemory(const std::string &name, const uint64_t size); + ~SharedMemory(); + std::string name() const { return name_; } + bool Remove() { return Remove(name()); } + template <class T, class... Params> + T *FindOrCreate(const std::string &name, Params &&...params) + { + return find_or_construct<T>(name.c_str(), std::nothrow)(std::forward<decltype(params)>(params)...); + } + template <class T, class... Params> + T *Create(const std::string &name, Params &&...params) + { + return construct<T>(name.c_str(), std::nothrow)(std::forward<decltype(params)>(params)...); + } + void *Alloc(const size_t size) { return allocate(size, std::nothrow); } + void Dealloc(void *p) + { + if (p) { deallocate(p); } + } + template <class T> + void Dealloc(offset_ptr<T> ptr) { return Dealloc(ptr.get()); } + + template <class T, class... Params> + T *New(Params &&...params) { return construct<T>(anonymous_instance, std::nothrow)(std::forward<decltype(params)>(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()); } + template <class T> + T *Find(const std::string &name) { return find<T>(name.c_str()).first; } }; + +template <class D> +using Allocator = allocator<D, SharedMemory::segment_manager>; +template <class D> +using Deleter = deleter<D, SharedMemory::segment_manager>; +template <class D> +using SharedPtr = shared_ptr<D, Allocator<void>, Deleter<D>>; // ShmObject manages an object in shared memory, but ShmObject itself is not in shared memory. -// 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; } -protected: - typedef T Data; - typedef SharedMemory ShmType; +class ShmObject : private boost::noncopyable +{ + static std::string ObjName(const std::string &name) { return "obj" + name; } + +public: + typedef T Data; + typedef SharedMemory ShmType; + ShmType &shm() const { return shm_; } + + template <class... Params> + ShmObject(ShmType &segment, const std::string &name, Params &&...t) : + shm_(segment), name_(name) + { + 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)); } + 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(); } + bool Remove() { return Remove(shm_, name_); } + private: - ShmType &shm_; - std::string name_; - Data *pdata_ = nullptr; - - bool IsOk() const { return pdata_; } -protected: - ShmType &shm() { return shm_; } -public: - template <class...Params> - ShmObject(ShmType &segment, const std::string &name, Params&&...t): - shm_(segment), name_(name) - { - pdata_ = shm_.find_or_construct<Data>(DataName(name_).c_str())(t...); - if (!IsOk()) { - throw("shm error: " + name_); - } - } - Data *find(const std::string &name) { return shm_.find<Data>(DataName(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()); } -}; - -template <class D> using Allocator = allocator<D, mshm_t::segment_manager>; - -template <class D> using Circular = boost::circular_buffer<D, Allocator<D> >; - -typedef int 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(MQId id, T&&...t):Super(t...), id_(id) {} - using Super::size; - using Super::capacity; - 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; - } - } -}; - -// 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; - typedef SharedQueue::Data Queue; - ShmMsgQueue(MQId id, ShmType &segment, const std::string &name, const uint32_t len); - 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(); - bool Send(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(); } + ShmType &shm_; + std::string name_; + Data *pdata_ = nullptr; }; } // namespace bhome_shm -- Gitblit v1.8.0