]> granicus.if.org Git - icinga2/commitdiff
Add missing barriers for Object::m_LockOwner
authorGunnar Beutner <gunnar.beutner@netways.de>
Wed, 12 Nov 2014 11:32:14 +0000 (12:32 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Wed, 12 Nov 2014 11:32:14 +0000 (12:32 +0100)
refs #7648

lib/base/object.cpp
lib/base/object.hpp
lib/base/objectlock.hpp
lib/base/unix.hpp

index 5d2b31f7ca9d9daf4bc00bfdf52261e513ce2bae..b77175d8c6300d6393592d3c2d1f2667f914e929 100644 (file)
@@ -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 */
 
index fbaaf8640b57bdef6aaf81e83a4d6db022739b49..86f96119e88bc78b6abf6ac6fa49d6b3ad946f97 100644 (file)
@@ -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;
index 3aa0e39b191fa36c3dfd3e186da0b6eccb5598a4..4fd839c5a20c4117e9f3b6e6d4c4ff26af75e2f4 100644 (file)
@@ -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();
index adb61b2361f208aaa8ad3d72b3019ebc19d1d2dc..4163aa2800a2ce3eea842b3d4d0584db1d124727 100644 (file)
@@ -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>