From 34cd75f77d0ca94dbdba4e6cc9451fe4d33e78b3 Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期三, 19 五月 2021 19:14:13 +0800 Subject: [PATCH] add api BHQueryProcs. --- src/shm.h | 32 ++++++++++++++++++++++++++------ 1 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/shm.h b/src/shm.h index 515d856..b5ec2ea 100644 --- a/src/shm.h +++ b/src/shm.h @@ -19,7 +19,7 @@ #ifndef SHM_6CHO6D6C #define SHM_6CHO6D6C -#include "robust.h" +#include "log.h" #include <atomic> #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/sync/interprocess_mutex.hpp> @@ -68,7 +68,7 @@ if (Killed(old)) { r = PidCas(old, pid()); if (r) { - printf("PidCheck captured pid %d -> %d\n", old, pid()); + LOG_DEBUG() << "PidCheck captured pid " << old << " -> " << pid(); } } } @@ -90,12 +90,14 @@ } }; -typedef robust::Mutex Mutex; -typedef robust::Guard<Mutex> Guard; +typedef interprocess_mutex Mutex; +typedef scoped_lock<Mutex> Guard; +// typedef robust::Guard<Mutex> Guard; class SharedMemory : public mshm_t { std::string name_; + Mutex *pmutex_ = 0; static permissions AllowAll() { @@ -122,19 +124,29 @@ { 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 *Alloc(const size_t size) + { + Guard lock(*pmutex_); + return allocate(size, std::nothrow); + } void Dealloc(void *p) { + Guard lock(*pmutex_); 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)...); } + T *New(Params &&...params) + { + Guard lock(*pmutex_); + return construct<T>(anonymous_instance, std::nothrow)(std::forward<decltype(params)>(params)...); + } template <class T> void Delete(T *p) { + Guard lock(*pmutex_); if (p) { destroy_ptr<T>(p); }; } template <class T> @@ -180,6 +192,12 @@ pdata_ = shm_.Find<Data>(ObjName(name_)); } } + ShmObject(const int64_t offset, ShmType &segment, const std::string &name) : + shm_(segment), name_(name) + { + pdata_ = reinterpret_cast<Data *>(Addr(shm_.get_address()) + offset); + } + bool IsOk() const { return pdata_; } static bool Remove(SharedMemory &shm, const std::string &name) { return shm.destroy<Data>(ObjName(name).c_str()); } @@ -189,11 +207,13 @@ std::string name() const { return name_; } Data *data() { return pdata_; } const Data *data() const { return pdata_; } + int64_t offset() const { return Addr(pdata_) - Addr(shm_.get_address()); } Data *operator->() { return data(); } const Data *operator->() const { return data(); } bool Remove() { return Remove(shm_, name_); } private: + static int64_t Addr(const void *p) { return reinterpret_cast<int64_t>(p); } ShmType &shm_; std::string name_; Data *pdata_ = nullptr; -- Gitblit v1.8.0