From 237685d4a57dc9fde9f1521bd5f83f0b088eeec7 Mon Sep 17 00:00:00 2001 From: Thad House Date: Sat, 8 Jul 2017 22:04:01 -0700 Subject: [PATCH] Fixes static initialization of HAL handles (#565) --- hal/lib/shared/handles/HandlesInternal.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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();