[icinga-checkins] icinga.org: icinga2/master: Add missing barriers for Object::m_LockOwner

git at icinga.org git at icinga.org
Wed Nov 12 12:38:12 CET 2014


Module: icinga2
Branch: master
Commit: c5a5d1833b213fe66201a16c9e1ad4cbb58a0748
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=c5a5d1833b213fe66201a16c9e1ad4cbb58a0748

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Wed Nov 12 12:32:14 2014 +0100

Add missing barriers for Object::m_LockOwner

refs #7648

---

 lib/base/object.cpp     |   13 ++++++++++---
 lib/base/object.hpp     |    7 +++++--
 lib/base/objectlock.hpp |   21 ++++++++++++++-------
 lib/base/unix.hpp       |    1 -
 4 files changed, 29 insertions(+), 13 deletions(-)

diff --git a/lib/base/object.cpp b/lib/base/object.cpp
index 5d2b31f..b77175d 100644
--- a/lib/base/object.cpp
+++ b/lib/base/object.cpp
@@ -31,7 +31,7 @@ REGISTER_PRIMITIVE_TYPE(Object);
 Object::Object(void)
 	: m_References(0)
 #ifdef _DEBUG
-	, m_Locked(false)
+	, m_LockOwner(0)
 #endif /* _DEBUG */
 { }
 
@@ -49,8 +49,15 @@ Object::~Object(void)
  */
 bool Object::OwnsLock(void) const
 {
-	// TODO: barrier before reading m_Locked
-	return (m_Locked && m_LockOwner == boost::this_thread::get_id());
+#ifdef _WIN32
+	DWORD tid = InterlockedExchangeAdd(&m_LockOwner, 0);
+
+	return (tid == GetCurrentThreadId());
+#else /* _WIN32 */
+	pthread_t tid = __sync_fetch_and_add(&m_LockOwner, 0);
+
+	return (tid == pthread_self());
+#endif /* _WIN32 */
 }
 #endif /* _DEBUG */
 
diff --git a/lib/base/object.hpp b/lib/base/object.hpp
index fbaaf86..86f9611 100644
--- a/lib/base/object.hpp
+++ b/lib/base/object.hpp
@@ -109,8 +109,11 @@ private:
 	mutable ThinMutex m_Mutex;
 
 #ifdef _DEBUG
-	mutable bool m_Locked;
-	mutable boost::thread::id m_LockOwner;
+#	ifndef _WIN32
+	mutable pthread_t m_LockOwner;
+#	else /* _WIN32 */
+	mutable DWORD m_LockOwner;
+#	endif /* _WIN32 */
 #endif /* _DEBUG */
 
 	friend struct ObjectLock;
diff --git a/lib/base/objectlock.hpp b/lib/base/objectlock.hpp
index 3aa0e39..4fd839c 100644
--- a/lib/base/objectlock.hpp
+++ b/lib/base/objectlock.hpp
@@ -62,18 +62,25 @@ public:
 		m_Locked = true;
 
 #ifdef _DEBUG
-		m_Object->m_Locked = true;
-		// TODO: barrier after writing m_Locked
-		m_Object->m_LockOwner = boost::this_thread::get_id();
+#	ifdef _WIN32
+		InterlockedExchange(&m_Object->m_LockOwner, GetCurrentThreadId());
+#	else /* _WIN32 */
+		__sync_lock_test_and_set(&m_Object->m_LockOwner, pthread_self());
+#	endif /* _WIN32 */
 #endif /* _DEBUG */
 	}
 
 	inline void Unlock(void)
 	{
-	#ifdef _DEBUG
-		if (m_Locked)
-			m_Object->m_Locked = false;
-	#endif /* _DEBUG */
+#ifdef _DEBUG
+		if (m_Locked) {
+#	ifdef _WIN32
+			InterlockedExchange(&m_Object->m_Locked, 0);
+#	else /* _WIN32 */
+			__sync_lock_test_and_set(&m_Object->m_LockOwner, 0);
+#	endif /* _WIN32 */
+		}
+#endif /* _DEBUG */
 
 		if (m_Locked) {
 			m_Object->m_Mutex.Unlock();
diff --git a/lib/base/unix.hpp b/lib/base/unix.hpp
index adb61b2..4163aa2 100644
--- a/lib/base/unix.hpp
+++ b/lib/base/unix.hpp
@@ -36,7 +36,6 @@
 #include <sys/wait.h>
 #include <glob.h>
 #include <dlfcn.h>
-#include <sys/types.h>
 #include <sys/stat.h>
 #include <dirent.h>
 #include <sys/time.h>



More information about the icinga-checkins mailing list