From 1d6c040dcb9a01648edc66d8c0006c8c9294a705 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期四, 22 四月 2021 18:28:30 +0800
Subject: [PATCH] add mutex timeout limit; use atomic as refcount.

---
 src/msg.h |   27 ++++++++-------------------
 1 files changed, 8 insertions(+), 19 deletions(-)

diff --git a/src/msg.h b/src/msg.h
index e6b0b34..feab5ec 100644
--- a/src/msg.h
+++ b/src/msg.h
@@ -21,6 +21,7 @@
 #include "bh_util.h"
 #include "proto.h"
 #include "shm.h"
+#include <atomic>
 #include <boost/interprocess/offset_ptr.hpp>
 #include <boost/uuid/uuid_generators.hpp>
 #include <functional>
@@ -38,26 +39,14 @@
 // store ref count, msgs shareing the same data should also hold a pointer of the same RefCount object.
 class RefCount : private boost::noncopyable
 {
-public:
-	int Inc()
-	{
-		Guard lk(mutex_);
-		return ++num_;
-	}
-	int Dec()
-	{
-		Guard lk(mutex_);
-		return --num_;
-	}
-	int Get()
-	{
-		Guard lk(mutex_);
-		return num_;
-	}
+	std::atomic<int> num_;
 
-private:
-	Mutex mutex_;
-	int num_ = 1;
+public:
+	RefCount() :
+	    num_(1) { static_assert(std::is_pod<decltype(num_)>::value); }
+	int Inc() { return ++num_; }
+	int Dec() { return --num_; }
+	int Get() { return num_.load(); }
 };
 
 // message content layout: header_size + header + data_size + data

--
Gitblit v1.8.0