Make Object#m_LockOwner std::atomic<std:🧵:id>

refs #7123
This commit is contained in:
Alexander A. Klimov 2019-04-17 18:03:40 +02:00
parent f9f998334d
commit 7e6868bc99
3 changed files with 8 additions and 24 deletions

View 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 */

View 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 */

View 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 */