From a6f67b4249525089fb97eb9418c7014f66c2a000 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期三, 28 四月 2021 19:29:17 +0800
Subject: [PATCH] use new robust mutex, circurar; rm timeout mutex.

---
 src/shm.h |   88 ++++++++++++++++++++++++-------------------
 1 files changed, 49 insertions(+), 39 deletions(-)

diff --git a/src/shm.h b/src/shm.h
index 7773ceb..17352fe 100644
--- a/src/shm.h
+++ b/src/shm.h
@@ -19,13 +19,11 @@
 #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 <chrono>
 #include <thread>
 
 namespace bhome_shm
@@ -35,53 +33,65 @@
 
 typedef managed_shared_memory mshm_t;
 
-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
+class MutexWithPidCheck
 {
 	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_;
+	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;
+	}
 
-	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();
+	void lock()
+	{
+		while (!try_lock()) {
+			std::this_thread::yield();
+		}
+	}
+	void unlock()
+	{
+		auto old = pid_.load();
+		if (old == pid()) {
+			mutex_.unlock();
+		}
+	}
 };
 
-// typedef boost::interprocess::interprocess_mutex Mutex;
-typedef MutexWithTimeLimit Mutex;
-typedef scoped_lock<Mutex> Guard;
-typedef interprocess_condition Cond;
+typedef robust::CasMutex<true> Mutex;
+typedef robust::Guard<Mutex> Guard;
 
 class SharedMemory : public mshm_t
 {

--
Gitblit v1.8.0