Restored thread checks on database accesses
This commit is contained in:
@@ -24,7 +24,9 @@
|
||||
void
|
||||
RecursiveSharedMutex::lock()
|
||||
{
|
||||
if (_uniqueOwner == std::this_thread::get_id())
|
||||
const auto thisThreadId {std::this_thread::get_id()};
|
||||
|
||||
if (_uniqueOwner == thisThreadId)
|
||||
{
|
||||
// already locked
|
||||
_uniqueCount++;
|
||||
@@ -32,7 +34,7 @@ RecursiveSharedMutex::lock()
|
||||
else
|
||||
{
|
||||
_mutex.lock();
|
||||
_uniqueOwner = std::this_thread::get_id();
|
||||
_uniqueOwner = thisThreadId;
|
||||
assert(_uniqueCount == 0);
|
||||
_uniqueCount = 1;
|
||||
}
|
||||
@@ -53,10 +55,12 @@ RecursiveSharedMutex::unlock()
|
||||
void
|
||||
RecursiveSharedMutex::lock_shared()
|
||||
{
|
||||
if (_uniqueOwner == std::this_thread::get_id())
|
||||
const auto thisThreadId {std::this_thread::get_id()};
|
||||
|
||||
if (_uniqueOwner == thisThreadId )
|
||||
{
|
||||
// alone here, no need to lock
|
||||
_sharedCounts[std::this_thread::get_id()]++;
|
||||
_sharedCounts[thisThreadId]++;
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -66,7 +70,7 @@ RecursiveSharedMutex::lock_shared()
|
||||
{
|
||||
std::scoped_lock lock {_sharedCountMutex};
|
||||
|
||||
auto& sharedCount {_sharedCounts[std::this_thread::get_id()]};
|
||||
auto& sharedCount {_sharedCounts[thisThreadId]};
|
||||
if (sharedCount == 0)
|
||||
needLock = true;
|
||||
else
|
||||
@@ -80,17 +84,19 @@ RecursiveSharedMutex::lock_shared()
|
||||
assert(_uniqueOwner == std::thread::id {});
|
||||
|
||||
std::scoped_lock lock {_sharedCountMutex};
|
||||
_sharedCounts[std::this_thread::get_id()]++;
|
||||
_sharedCounts[thisThreadId]++;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
RecursiveSharedMutex::unlock_shared()
|
||||
{
|
||||
if (_uniqueOwner == std::this_thread::get_id())
|
||||
const auto thisThreadId {std::this_thread::get_id()};
|
||||
|
||||
if (_uniqueOwner == thisThreadId )
|
||||
{
|
||||
// alone here, no need to lock
|
||||
auto& sharedCount {_sharedCounts[std::this_thread::get_id()]};
|
||||
auto& sharedCount {_sharedCounts[thisThreadId ]};
|
||||
assert(sharedCount > 0);
|
||||
--sharedCount;
|
||||
|
||||
@@ -102,7 +108,7 @@ RecursiveSharedMutex::unlock_shared()
|
||||
{
|
||||
std::scoped_lock lock {_sharedCountMutex};
|
||||
|
||||
auto& sharedCount {_sharedCounts[std::this_thread::get_id()]};
|
||||
auto& sharedCount {_sharedCounts[thisThreadId]};
|
||||
assert(sharedCount > 0);
|
||||
needUnlock = (--sharedCount == 0);
|
||||
}
|
||||
@@ -110,3 +116,22 @@ RecursiveSharedMutex::unlock_shared()
|
||||
if (needUnlock)
|
||||
_mutex.unlock_shared();
|
||||
}
|
||||
|
||||
#ifndef NDEBUG
|
||||
bool
|
||||
RecursiveSharedMutex::isUniqueLocked()
|
||||
{
|
||||
return _uniqueOwner == std::this_thread::get_id();
|
||||
}
|
||||
|
||||
bool
|
||||
RecursiveSharedMutex::isSharedLocked()
|
||||
{
|
||||
const auto thisThreadId {std::this_thread::get_id()};
|
||||
if (_uniqueOwner == thisThreadId )
|
||||
return true;
|
||||
|
||||
std::scoped_lock lock {_sharedCountMutex};
|
||||
return _sharedCounts[thisThreadId] > 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -34,6 +34,10 @@ class RecursiveSharedMutex
|
||||
void lock_shared();
|
||||
void unlock_shared();
|
||||
|
||||
#ifndef NDEBUG
|
||||
bool isSharedLocked();
|
||||
bool isUniqueLocked();
|
||||
#endif // NDEBUG
|
||||
private:
|
||||
std::shared_mutex _mutex;
|
||||
std::thread::id _uniqueOwner;
|
||||
|
||||
Reference in New Issue
Block a user