diff --git a/hal/lib/shared/handles/HandlesInternal.cpp b/hal/lib/shared/handles/HandlesInternal.cpp index a1e9c2e5f7..f91a028b69 100644 --- a/hal/lib/shared/handles/HandlesInternal.cpp +++ b/hal/lib/shared/handles/HandlesInternal.cpp @@ -14,10 +14,13 @@ namespace hal { static llvm::SmallVector globalHandles; -static priority_mutex globalHandleMutex; +static priority_mutex& GetGlobalHandleMutex() { + static priority_mutex globalHandleMutex; + return globalHandleMutex; +} HandleBase::HandleBase() { - std::lock_guard lock(globalHandleMutex); + std::lock_guard lock(GetGlobalHandleMutex()); auto index = std::find(globalHandles.begin(), globalHandles.end(), this); if (index == globalHandles.end()) { globalHandles.push_back(this); @@ -27,7 +30,7 @@ HandleBase::HandleBase() { } HandleBase::~HandleBase() { - std::lock_guard lock(globalHandleMutex); + std::lock_guard lock(GetGlobalHandleMutex()); auto index = std::find(globalHandles.begin(), globalHandles.end(), this); if (index != globalHandles.end()) { *index = nullptr; @@ -42,7 +45,7 @@ void HandleBase::ResetHandles() { } void HandleBase::ResetGlobalHandles() { - std::unique_lock lock(globalHandleMutex); + std::unique_lock lock(GetGlobalHandleMutex()); for (auto&& i : globalHandles) { if (i != nullptr) { lock.unlock();