]> granicus.if.org Git - icinga2/commitdiff
Make Object#m_LockOwner std::atomic<std::thread::id>
authorAlexander A. Klimov <alexander.klimov@icinga.com>
Wed, 17 Apr 2019 16:03:40 +0000 (18:03 +0200)
committerAlexander A. Klimov <alexander.klimov@icinga.com>
Wed, 17 Apr 2019 16:26:23 +0000 (18:26 +0200)
refs #7123

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

index b00891b4c4f8bfb1ad2e62a79c03ab875757ddc6..1d40a753d4dcac63a14b8e136dfb8a09c1fb02ff 100644 (file)
@@ -10,6 +10,7 @@
 #include "base/exception.hpp"
 #include <boost/lexical_cast.hpp>
 #include <boost/thread/recursive_mutex.hpp>
+#include <thread>
 
 using namespace icinga;
 
@@ -27,6 +28,7 @@ static Timer::Ptr l_ObjectCountTimer;
 Object::Object()
 {
        m_References.store(0);
+       m_LockOwner.store(decltype(m_LockOwner.load())());
 }
 
 /**
@@ -53,15 +55,7 @@ String Object::ToString() const
  */
 bool Object::OwnsLock() const
 {
-#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 */
+       return m_LockOwner.load() == std::this_thread::get_id();
 }
 #endif /* I2_DEBUG */
 
index 71e9b0bfa3a83da1916738176a148f9df23a081b..43cf7cee7c51eda9a3116880c5645d83a5dbc4cb 100644 (file)
@@ -9,6 +9,7 @@
 #include <atomic>
 #include <cstddef>
 #include <cstdint>
+#include <thread>
 #include <vector>
 
 using boost::intrusive_ptr;
@@ -193,11 +194,7 @@ private:
        mutable uintptr_t m_Mutex{0};
 
 #ifdef I2_DEBUG
-#      ifndef _WIN32
-       mutable pthread_t m_LockOwner;
-#      else /* _WIN32 */
-       mutable DWORD m_LockOwner;
-#      endif /* _WIN32 */
+       mutable std::atomic<std::thread::id> m_LockOwner;
        mutable size_t m_LockCount = 0;
 #endif /* I2_DEBUG */
 
index c68710c9c39b92562dbf837486d94c7556cfc6dd..5a06488e9c0f6523eb220f81f9a22a993c9078ab 100644 (file)
@@ -2,6 +2,7 @@
 
 #include "base/objectlock.hpp"
 #include <boost/thread/recursive_mutex.hpp>
+#include <thread>
 
 using namespace icinga;
 
@@ -72,11 +73,7 @@ void ObjectLock::Lock()
 
 #ifdef I2_DEBUG
        if (++m_Object->m_LockCount == 1u) {
-#      ifdef _WIN32
-               InterlockedExchange(&m_Object->m_LockOwner, GetCurrentThreadId());
-#      else /* _WIN32 */
-               __sync_lock_test_and_set(&m_Object->m_LockOwner, pthread_self());
-#      endif /* _WIN32 */
+               m_Object->m_LockOwner.store(std::this_thread::get_id());
        }
 #endif /* I2_DEBUG */
 }
@@ -104,11 +101,7 @@ void ObjectLock::Unlock()
 {
 #ifdef I2_DEBUG
        if (m_Locked && !--m_Object->m_LockCount) {
-#      ifdef _WIN32
-               InterlockedExchange(&m_Object->m_LockOwner, 0);
-#      else /* _WIN32 */
-               __sync_lock_release(&m_Object->m_LockOwner);
-#      endif /* _WIN32 */
+               m_Object->m_LockOwner.store(decltype(m_Object->m_LockOwner.load())());
        }
 #endif /* I2_DEBUG */