From 72bffb0807925a156b076b71f78c848a08d27b87 Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期四, 29 四月 2021 10:55:35 +0800 Subject: [PATCH] refactor mutex. --- src/shm.h | 116 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 95 insertions(+), 21 deletions(-) diff --git a/src/shm.h b/src/shm.h index 22a975b..515d856 100644 --- a/src/shm.h +++ b/src/shm.h @@ -19,12 +19,12 @@ #ifndef SHM_6CHO6D6C #define SHM_6CHO6D6C +#include "robust.h" +#include <atomic> #include <boost/interprocess/managed_shared_memory.hpp> -#include <boost/interprocess/sync/interprocess_condition.hpp> #include <boost/interprocess/sync/interprocess_mutex.hpp> -#include <boost/interprocess/sync/scoped_lock.hpp> #include <boost/noncopyable.hpp> -#include <boost/uuid/uuid.hpp> +#include <thread> namespace bhome_shm { @@ -32,9 +32,66 @@ using namespace boost::interprocess; typedef managed_shared_memory mshm_t; -typedef interprocess_mutex Mutex; -typedef scoped_lock<Mutex> Guard; -typedef interprocess_condition Cond; + +class MutexWithPidCheck +{ + typedef boost::interprocess::interprocess_mutex MutexT; + static pid_t pid() + { + static pid_t val = getpid(); + return val; + } + static bool Killed(pid_t pid) + { + char buf[64] = {0}; + snprintf(buf, sizeof(buf) - 1, "/proc/%d/stat", pid); + return access(buf, F_OK) != 0; + } + bool PidCas(pid_t exp, pid_t val) { return pid_.compare_exchange_strong(exp, val); } + MutexT mutex_; + std::atomic<pid_t> pid_; + +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(); } + MutexWithPidCheck() : + pid_(0) {} + bool try_lock() + { + bool r = false; + if (mutex_.try_lock()) { + auto old = pid_.load(); + r = PidCas(old, pid()); + } else { + auto old = pid_.load(); + if (Killed(old)) { + r = PidCas(old, pid()); + if (r) { + printf("PidCheck captured pid %d -> %d\n", old, pid()); + } + } + } + return r; + } + + void lock() + { + while (!try_lock()) { + std::this_thread::yield(); + } + } + void unlock() + { + auto old = pid_.load(); + if (old == pid()) { + mutex_.unlock(); + } + } +}; + +typedef robust::Mutex Mutex; +typedef robust::Guard<Mutex> Guard; class SharedMemory : public mshm_t { @@ -55,7 +112,16 @@ ~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) { @@ -65,7 +131,7 @@ void Dealloc(offset_ptr<T> ptr) { return Dealloc(ptr.get()); } template <class T, class... Params> - T *New(Params const &...params) { return construct<T>(anonymous_instance, std::nothrow)(params...); } + T *New(Params &&...params) { return construct<T>(anonymous_instance, std::nothrow)(std::forward<decltype(params)>(params)...); } template <class T> void Delete(T *p) { @@ -90,30 +156,33 @@ { static std::string ObjName(const std::string &name) { return "obj" + name; } -protected: +public: typedef T Data; typedef SharedMemory ShmType; - -private: - ShmType &shm_; - std::string name_; - Data *pdata_ = nullptr; - - bool IsOk() const { return pdata_; } - -protected: ShmType &shm() const { 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>(ObjName(name_).c_str(), std::nothrow)(t...); + 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() {} @@ -122,7 +191,12 @@ const Data *data() const { return pdata_; } Data *operator->() { return data(); } const Data *operator->() const { return data(); } - bool Remove() { return shm_.destroy<Data>(ObjName(name_).c_str()); } + bool Remove() { return Remove(shm_, name_); } + +private: + ShmType &shm_; + std::string name_; + Data *pdata_ = nullptr; }; } // namespace bhome_shm -- Gitblit v1.8.0