4#include "Stroika/Foundation/StroikaPreComp.h"
16using namespace Stroika::Foundation::Debug;
18#if qStroika_Foundation_Debug_AssertExternallySynchronizedMutex_Enabled
24void AssertExternallySynchronizedMutex::lock_ () noexcept
27 SharedContext* sharedContext = fSharedContext_.get ();
28 if (sharedContext->fFullLocks_++ == 0) {
30 sharedContext->fThreadWithFullLock_ = this_thread::get_id ();
31 if (not sharedContext->GetSharedLockEmpty_ ()) {
33 Require (sharedContext->CountOfIInSharedLockThreads_ (sharedContext->fThreadWithFullLock_) ==
34 sharedContext->GetSharedLockThreadsCount_ ());
39 if (sharedContext->fThreadWithFullLock_ != this_thread::get_id ()) {
43#if !qCompilerAndStdLib_FormatThreadId_Buggy
44 DbgTrace (
"ATTEMPT TO modify (lock for write) an object which is already in use (debuglocked) in another thread (thisthread={})"_f,
45 this_thread::get_id ());
46 DbgTrace (
"Original thread holding lock: threadID={}, and DbgTraceThreadName={}"_f,
51 Require (sharedContext->fThreadWithFullLock_ == this_thread::get_id ());
59void AssertExternallySynchronizedMutex::unlock_ () noexcept
61 SharedContext* sharedContext = fSharedContext_.get ();
62 Require (sharedContext->fThreadWithFullLock_ == this_thread::get_id ());
63 Require (sharedContext->fFullLocks_ > 0);
64 --sharedContext->fFullLocks_;
69void AssertExternallySynchronizedMutex::lock_shared_ () const noexcept
72 SharedContext* sharedContext = fSharedContext_.get ();
75 if (sharedContext->fFullLocks_ != 0) {
77 if (sharedContext->fThreadWithFullLock_ != this_thread::get_id ()) {
81 DbgTrace (
"ATTEMPT TO shared_lock (lock for READ) an object which is already in use (debuglocked for WRITE) in another thread"_f);
82 DbgTrace (
"Original thread holding (write) lock: threadID={}, and DbgTraceThreadName={}"_f,
83 Characters::ToString (Execution::Thread::FormatThreadID_A (sharedContext->fThreadWithFullLock_)),
86 Require (sharedContext->fThreadWithFullLock_ == this_thread::get_id ());
88 sharedContext->AddSharedLock_ (this_thread::get_id ());
95void AssertExternallySynchronizedMutex::unlock_shared_ () const noexcept
98 SharedContext* sharedContext = fSharedContext_.get ();
99 sharedContext->RemoveSharedLock_ (this_thread::get_id ());
106mutex& AssertExternallySynchronizedMutex::GetSharedLockMutexThreads_ ()
108 static mutex sMutex_;
#define AssertNotReached()
String ToString(T &&t, ARGS... args)
Return a debug-friendly, display version of the argument: not guaranteed parsable or usable except fo...
wstring FormatThreadID(Thread::IDType threadID, const FormatThreadInfo &formatInfo={})