From 02ba913dc7bb5d711471b27f2ea23a897d0f2e28 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期五, 23 四月 2021 15:34:26 +0800
Subject: [PATCH] bind msgi to shm, change offset_ptr to abs offset.

---
 src/shm.h |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/src/shm.h b/src/shm.h
index 22a975b..5bf8e41 100644
--- a/src/shm.h
+++ b/src/shm.h
@@ -19,12 +19,15 @@
 #ifndef SHM_6CHO6D6C
 #define SHM_6CHO6D6C
 
+#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 <chrono>
+#include <thread>
 
 namespace bhome_shm
 {
@@ -32,7 +35,52 @@
 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() { mutex_.unlock(); }
+};
+
+// typedef boost::interprocess::interprocess_mutex Mutex;
+typedef MutexWithTimeLimit Mutex;
 typedef scoped_lock<Mutex> Guard;
 typedef interprocess_condition Cond;
 
@@ -114,6 +162,7 @@
 			throw("Error: Not enough memory, can not allocate \"" + name_ + "\"");
 		}
 	}
+	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 +171,7 @@
 	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_); }
 };
 
 } // namespace bhome_shm

--
Gitblit v1.8.0