From 02645c59ffefe44494a8724c9571a963b77e426c Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Fri, 22 Jun 2018 14:13:09 +0200 Subject: [PATCH] ObjectLock#Unlock(): don't reset m_Object->m_LockOwner too early --- lib/base/object.hpp | 2 ++ lib/base/objectlock.cpp | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/base/object.hpp b/lib/base/object.hpp index a29e4f73b..7836d2644 100644 --- a/lib/base/object.hpp +++ b/lib/base/object.hpp @@ -23,6 +23,7 @@ #include "base/i2-base.hpp" #include "base/debug.hpp" #include +#include #include using boost::intrusive_ptr; @@ -212,6 +213,7 @@ private: # else /* _WIN32 */ mutable DWORD m_LockOwner; # endif /* _WIN32 */ + mutable size_t m_LockCount = 0; #endif /* I2_DEBUG */ friend struct ObjectLock; diff --git a/lib/base/objectlock.cpp b/lib/base/objectlock.cpp index 2abb43fba..c3d3f0522 100644 --- a/lib/base/objectlock.cpp +++ b/lib/base/objectlock.cpp @@ -90,11 +90,13 @@ void ObjectLock::Lock() m_Locked = true; #ifdef I2_DEBUG + if (++m_Object->m_LockCount == 1u) { # ifdef _WIN32 - InterlockedExchange(&m_Object->m_LockOwner, GetCurrentThreadId()); + InterlockedExchange(&m_Object->m_LockOwner, GetCurrentThreadId()); # else /* _WIN32 */ - __sync_lock_test_and_set(&m_Object->m_LockOwner, pthread_self()); + __sync_lock_test_and_set(&m_Object->m_LockOwner, pthread_self()); # endif /* _WIN32 */ + } #endif /* I2_DEBUG */ } @@ -120,7 +122,7 @@ void ObjectLock::Spin(unsigned int it) void ObjectLock::Unlock() { #ifdef I2_DEBUG - if (m_Locked) { + if (m_Locked && !--m_Object->m_LockCount) { # ifdef _WIN32 InterlockedExchange(&m_Object->m_LockOwner, 0); # else /* _WIN32 */