Fixes cross module base class statics (#779)

This commit is contained in:
Thad House
2017-12-03 00:05:05 -08:00
committed by Peter Johnson
parent 0b8d3f0260
commit 0994364591

View File

@@ -13,7 +13,11 @@
#include <support/mutex.h>
namespace hal {
static llvm::SmallVector<HandleBase*, 32> globalHandles;
static llvm::SmallVector<HandleBase*, 32>& GetGlobalHandles() {
static llvm::SmallVector<HandleBase*, 32> globalHandles;
return globalHandles;
}
static wpi::mutex& GetGlobalHandleMutex() {
static wpi::mutex globalHandleMutex;
return globalHandleMutex;
@@ -21,6 +25,7 @@ static wpi::mutex& GetGlobalHandleMutex() {
HandleBase::HandleBase() {
std::lock_guard<wpi::mutex> lock(GetGlobalHandleMutex());
auto& globalHandles = GetGlobalHandles();
auto index = std::find(globalHandles.begin(), globalHandles.end(), this);
if (index == globalHandles.end()) {
globalHandles.push_back(this);
@@ -31,6 +36,7 @@ HandleBase::HandleBase() {
HandleBase::~HandleBase() {
std::lock_guard<wpi::mutex> lock(GetGlobalHandleMutex());
auto& globalHandles = GetGlobalHandles();
auto index = std::find(globalHandles.begin(), globalHandles.end(), this);
if (index != globalHandles.end()) {
*index = nullptr;
@@ -46,6 +52,7 @@ void HandleBase::ResetHandles() {
void HandleBase::ResetGlobalHandles() {
std::unique_lock<wpi::mutex> lock(GetGlobalHandleMutex());
auto& globalHandles = GetGlobalHandles();
for (auto&& i : globalHandles) {
if (i != nullptr) {
lock.unlock();