Fixes static initialization of HAL handles (#565)

This commit is contained in:
Thad House
2017-07-08 22:04:01 -07:00
committed by Peter Johnson
parent abbe630274
commit 237685d4a5

View File

@@ -14,10 +14,13 @@
namespace hal {
static llvm::SmallVector<HandleBase*, 32> globalHandles;
static priority_mutex globalHandleMutex;
static priority_mutex& GetGlobalHandleMutex() {
static priority_mutex globalHandleMutex;
return globalHandleMutex;
}
HandleBase::HandleBase() {
std::lock_guard<priority_mutex> lock(globalHandleMutex);
std::lock_guard<priority_mutex> 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<priority_mutex> lock(globalHandleMutex);
std::lock_guard<priority_mutex> 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<priority_mutex> lock(globalHandleMutex);
std::unique_lock<priority_mutex> lock(GetGlobalHandleMutex());
for (auto&& i : globalHandles) {
if (i != nullptr) {
lock.unlock();