diff --git a/lib/base/object.cpp b/lib/base/object.cpp index b00891b4c..1d40a753d 100644 --- a/lib/base/object.cpp +++ b/lib/base/object.cpp @@ -10,6 +10,7 @@ #include "base/exception.hpp" #include #include +#include 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 */ diff --git a/lib/base/object.hpp b/lib/base/object.hpp index 71e9b0bfa..43cf7cee7 100644 --- a/lib/base/object.hpp +++ b/lib/base/object.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include 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 m_LockOwner; mutable size_t m_LockCount = 0; #endif /* I2_DEBUG */ diff --git a/lib/base/objectlock.cpp b/lib/base/objectlock.cpp index c68710c9c..5a06488e9 100644 --- a/lib/base/objectlock.cpp +++ b/lib/base/objectlock.cpp @@ -2,6 +2,7 @@ #include "base/objectlock.hpp" #include +#include 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 */