From 58d904a328c0d849769b483e901a0be9426b8209 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期二, 20 七月 2021 20:20:44 +0800
Subject: [PATCH] 调整Request C.BHFree的位置

---
 src/shm.h |  143 +++++++++++++++--------------------------------
 1 files changed, 45 insertions(+), 98 deletions(-)

diff --git a/src/shm.h b/src/shm.h
index 515d856..e005cfc 100644
--- a/src/shm.h
+++ b/src/shm.h
@@ -19,12 +19,10 @@
 #ifndef SHM_6CHO6D6C
 #define SHM_6CHO6D6C
 
-#include "robust.h"
-#include <atomic>
 #include <boost/interprocess/managed_shared_memory.hpp>
 #include <boost/interprocess/sync/interprocess_mutex.hpp>
 #include <boost/noncopyable.hpp>
-#include <thread>
+#include <string>
 
 namespace bhome_shm
 {
@@ -32,70 +30,13 @@
 using namespace boost::interprocess;
 
 typedef managed_shared_memory mshm_t;
-
-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;
+typedef interprocess_mutex Mutex;
+typedef scoped_lock<Mutex> Guard;
 
 class SharedMemory : public mshm_t
 {
 	std::string name_;
+	Mutex *pmutex_ = 0;
 
 	static permissions AllowAll()
 	{
@@ -122,37 +63,33 @@
 	{
 		return construct<T>(name.c_str(), std::nothrow)(std::forward<decltype(params)>(params)...);
 	}
-	void *Alloc(const size_t size) { return allocate(size, std::nothrow); }
+	template <class T>
+	bool Destroy(const std::string &name) { return destroy<T>(name.c_str()); }
+
+	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)...); }
-	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>>;
+enum Mode {
+	eOpen = 0,
+	eCreate = 1,
+	eOpenOrCreate = 2
+};
 
-// ShmObject manages an object in shared memory, but ShmObject itself is not in shared memory.
+// NamedShmObject manages an object in shared memory, but NamedShmObject itself is not in shared memory.
 template <class T>
-class ShmObject : private boost::noncopyable
+class NamedShmObject : private boost::noncopyable
 {
 	static std::string ObjName(const std::string &name) { return "obj" + name; }
 
@@ -162,38 +99,48 @@
 	ShmType &shm() const { return shm_; }
 
 	template <class... Params>
-	ShmObject(ShmType &segment, const std::string &name, Params &&...t) :
+	NamedShmObject(ShmType &segment, const std::string &name, Mode mode, 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 {
+		switch (mode) {
+		case eOpen:
 			pdata_ = shm_.Find<Data>(ObjName(name_));
+			break;
+		case eCreate:
+			pdata_ = shm_.Create<Data>(ObjName(name_), std::forward<decltype(t)>(t)...);
+			break;
+		case eOpenOrCreate:
+			pdata_ = shm_.FindOrCreate<Data>(ObjName(name_), std::forward<decltype(t)>(t)...);
+			break;
+		default: break;
+		}
+		if (!IsOk()) {
+			throw std::runtime_error("Error: shm can not create/open \"" + name_ + "\"");
 		}
 	}
+
+	NamedShmObject(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()); }
+	static bool Remove(SharedMemory &shm, const std::string &name) { return shm.Destroy<Data>(ObjName(name)); }
 	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() {}
+	virtual ~NamedShmObject() {}
 	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