Skip to content

Commit b09dab0

Browse files
committed
Prevent mutex lock fail even if --enable-debug
This PR intends to resolve #15227. "configure --debug-enabled" enables "#ifdef DEBUG_LOCKORDER". Then "lockdata" (in sync.cpp) will be initialized same as other static objects. But unfortunately, lockdata.push_lock() was called before its initialization (via initializing signatureCache which is declared in script/sigcache.cpp) on macOS. This PR apply the "Construct On First Use Idiom" to "lockdata" to prevent it.
1 parent 94167e2 commit b09dab0

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/sync.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ struct LockData {
7373
LockOrders lockorders;
7474
InvLockOrders invlockorders;
7575
std::mutex dd_mutex;
76-
} static lockdata;
76+
};
77+
LockData& GetLockData() {
78+
static LockData lockdata;
79+
return lockdata;
80+
}
7781

7882
static thread_local LockStack g_lockstack;
7983

@@ -109,6 +113,7 @@ static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch,
109113

110114
static void push_lock(void* c, const CLockLocation& locklocation)
111115
{
116+
LockData& lockdata = GetLockData();
112117
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
113118

114119
g_lockstack.push_back(std::make_pair(c, locklocation));
@@ -173,6 +178,7 @@ void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLi
173178

174179
void DeleteLock(void* cs)
175180
{
181+
LockData& lockdata = GetLockData();
176182
if (!lockdata.available) {
177183
// We're already shutting down.
178184
return;

0 commit comments

Comments
 (0)