Remove priority mutex (#644)

* Removed hal::priority_condition_variable

* Replaced uses of priority mutexes with std::mutex and std::recursive_mutex

This allowed replacing a use of std::condition_variable_any with
std::condition_variable.

* Replaced all uses of std::recursive_mutex with std::mutex equivalents
This commit is contained in:
Tyler Veness
2017-09-28 23:32:35 -07:00
committed by Peter Johnson
parent 19addb04cf
commit dd66b23845
44 changed files with 390 additions and 746 deletions

View File

@@ -11,11 +11,11 @@
#include <array>
#include <memory>
#include <mutex>
#include "HAL/Errors.h"
#include "HAL/Types.h"
#include "HAL/cpp/make_unique.h"
#include "HAL/cpp/priority_mutex.h"
#include "HAL/handles/HandlesInternal.h"
namespace hal {
@@ -48,7 +48,7 @@ class DigitalHandleResource : public HandleBase {
private:
std::array<std::shared_ptr<TStruct>, size> m_structures;
std::array<hal::priority_mutex, size> m_handleMutexes;
std::array<std::mutex, size> m_handleMutexes;
};
template <typename THandle, typename TStruct, int16_t size>
@@ -59,7 +59,7 @@ THandle DigitalHandleResource<THandle, TStruct, size>::Allocate(
*status = RESOURCE_OUT_OF_RANGE;
return HAL_kInvalidHandle;
}
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
std::lock_guard<std::mutex> sync(m_handleMutexes[index]);
// check for allocation, otherwise allocate and return a valid handle
if (m_structures[index] != nullptr) {
*status = RESOURCE_IS_ALLOCATED;
@@ -77,7 +77,7 @@ std::shared_ptr<TStruct> DigitalHandleResource<THandle, TStruct, size>::Get(
if (index < 0 || index >= size) {
return nullptr;
}
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
std::lock_guard<std::mutex> sync(m_handleMutexes[index]);
// return structure. Null will propogate correctly, so no need to manually
// check.
return m_structures[index];
@@ -90,14 +90,14 @@ void DigitalHandleResource<THandle, TStruct, size>::Free(
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
if (index < 0 || index >= size) return;
// lock and deallocated handle
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
std::lock_guard<std::mutex> sync(m_handleMutexes[index]);
m_structures[index].reset();
}
template <typename THandle, typename TStruct, int16_t size>
void DigitalHandleResource<THandle, TStruct, size>::ResetHandles() {
for (int i = 0; i < size; i++) {
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[i]);
std::lock_guard<std::mutex> sync(m_handleMutexes[i]);
m_structures[i].reset();
}
HandleBase::ResetHandles();

View File

@@ -51,7 +51,7 @@ class IndexedClassedHandleResource : public HandleBase {
private:
std::array<std::shared_ptr<TStruct>[], size> m_structures;
std::array<hal::priority_mutex[], size> m_handleMutexes;
std::array<std::mutex[], size> m_handleMutexes;
};
template <typename THandle, typename TStruct, int16_t size,
@@ -59,7 +59,7 @@ template <typename THandle, typename TStruct, int16_t size,
IndexedClassedHandleResource<THandle, TStruct, size,
enumValue>::IndexedClassedHandleResource() {
m_structures = std::make_unique<std::shared_ptr<TStruct>[]>(size);
m_handleMutexes = std::make_unique<hal::priority_mutex[]>(size);
m_handleMutexes = std::make_unique<std::mutex[]>(size);
}
template <typename THandle, typename TStruct, int16_t size,
@@ -72,7 +72,7 @@ IndexedClassedHandleResource<THandle, TStruct, size, enumValue>::Allocate(
*status = RESOURCE_OUT_OF_RANGE;
return HAL_kInvalidHandle;
}
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
std::lock_guard<std::mutex> sync(m_handleMutexes[index]);
// check for allocation, otherwise allocate and return a valid handle
if (m_structures[index] != nullptr) {
*status = RESOURCE_IS_ALLOCATED;
@@ -91,7 +91,7 @@ std::shared_ptr<TStruct> IndexedClassedHandleResource<
if (index < 0 || index >= size) {
return nullptr;
}
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
std::lock_guard<std::mutex> sync(m_handleMutexes[index]);
// return structure. Null will propogate correctly, so no need to manually
// check.
return m_structures[index];
@@ -105,7 +105,7 @@ void IndexedClassedHandleResource<THandle, TStruct, size, enumValue>::Free(
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
if (index < 0 || index >= size) return;
// lock and deallocated handle
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
std::lock_guard<std::mutex> sync(m_handleMutexes[index]);
m_structures[index].reset();
}
@@ -114,7 +114,7 @@ template <typename THandle, typename TStruct, int16_t size,
void IndexedClassedHandleResource<THandle, TStruct, size,
enumValue>::ResetHandles() {
for (int i = 0; i < size; i++) {
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[i]);
std::lock_guard<std::mutex> sync(m_handleMutexes[i]);
m_structures[i].reset();
}
HandleBase::ResetHandles();

View File

@@ -11,11 +11,11 @@
#include <array>
#include <memory>
#include <mutex>
#include "HAL/Errors.h"
#include "HAL/Types.h"
#include "HAL/cpp/make_unique.h"
#include "HAL/cpp/priority_mutex.h"
#include "HAL/handles/HandlesInternal.h"
namespace hal {
@@ -49,7 +49,7 @@ class IndexedHandleResource : public HandleBase {
private:
std::array<std::shared_ptr<TStruct>, size> m_structures;
std::array<hal::priority_mutex, size> m_handleMutexes;
std::array<std::mutex, size> m_handleMutexes;
};
template <typename THandle, typename TStruct, int16_t size,
@@ -61,7 +61,7 @@ THandle IndexedHandleResource<THandle, TStruct, size, enumValue>::Allocate(
*status = RESOURCE_OUT_OF_RANGE;
return HAL_kInvalidHandle;
}
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
std::lock_guard<std::mutex> sync(m_handleMutexes[index]);
// check for allocation, otherwise allocate and return a valid handle
if (m_structures[index] != nullptr) {
*status = RESOURCE_IS_ALLOCATED;
@@ -80,7 +80,7 @@ IndexedHandleResource<THandle, TStruct, size, enumValue>::Get(THandle handle) {
if (index < 0 || index >= size) {
return nullptr;
}
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
std::lock_guard<std::mutex> sync(m_handleMutexes[index]);
// return structure. Null will propogate correctly, so no need to manually
// check.
return m_structures[index];
@@ -94,7 +94,7 @@ void IndexedHandleResource<THandle, TStruct, size, enumValue>::Free(
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
if (index < 0 || index >= size) return;
// lock and deallocated handle
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
std::lock_guard<std::mutex> sync(m_handleMutexes[index]);
m_structures[index].reset();
}
@@ -102,7 +102,7 @@ template <typename THandle, typename TStruct, int16_t size,
HAL_HandleEnum enumValue>
void IndexedHandleResource<THandle, TStruct, size, enumValue>::ResetHandles() {
for (int i = 0; i < size; i++) {
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[i]);
std::lock_guard<std::mutex> sync(m_handleMutexes[i]);
m_structures[i].reset();
}
HandleBase::ResetHandles();

View File

@@ -11,10 +11,10 @@
#include <array>
#include <memory>
#include <mutex>
#include "HAL/Types.h"
#include "HAL/cpp/make_unique.h"
#include "HAL/cpp/priority_mutex.h"
#include "HAL/handles/HandlesInternal.h"
namespace hal {
@@ -48,8 +48,8 @@ class LimitedClassedHandleResource : public HandleBase {
private:
std::array<std::shared_ptr<TStruct>, size> m_structures;
std::array<hal::priority_mutex, size> m_handleMutexes;
hal::priority_mutex m_allocateMutex;
std::array<std::mutex, size> m_handleMutexes;
std::mutex m_allocateMutex;
};
template <typename THandle, typename TStruct, int16_t size,
@@ -58,12 +58,12 @@ THandle
LimitedClassedHandleResource<THandle, TStruct, size, enumValue>::Allocate(
std::shared_ptr<TStruct> toSet) {
// globally lock to loop through indices
std::lock_guard<hal::priority_mutex> sync(m_allocateMutex);
std::lock_guard<std::mutex> sync(m_allocateMutex);
for (int16_t i = 0; i < size; i++) {
if (m_structures[i] == nullptr) {
// if a false index is found, grab its specific mutex
// and allocate it.
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[i]);
std::lock_guard<std::mutex> sync(m_handleMutexes[i]);
m_structures[i] = toSet;
return static_cast<THandle>(createHandle(i, enumValue, m_version));
}
@@ -80,7 +80,7 @@ std::shared_ptr<TStruct> LimitedClassedHandleResource<
if (index < 0 || index >= size) {
return nullptr;
}
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
std::lock_guard<std::mutex> sync(m_handleMutexes[index]);
// return structure. Null will propogate correctly, so no need to manually
// check.
return m_structures[index];
@@ -94,8 +94,8 @@ void LimitedClassedHandleResource<THandle, TStruct, size, enumValue>::Free(
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
if (index < 0 || index >= size) return;
// lock and deallocated handle
std::lock_guard<hal::priority_mutex> sync(m_allocateMutex);
std::lock_guard<hal::priority_mutex> lock(m_handleMutexes[index]);
std::lock_guard<std::mutex> sync(m_allocateMutex);
std::lock_guard<std::mutex> lock(m_handleMutexes[index]);
m_structures[index].reset();
}
@@ -104,9 +104,9 @@ template <typename THandle, typename TStruct, int16_t size,
void LimitedClassedHandleResource<THandle, TStruct, size,
enumValue>::ResetHandles() {
{
std::lock_guard<hal::priority_mutex> lock(m_allocateMutex);
std::lock_guard<std::mutex> lock(m_allocateMutex);
for (int i = 0; i < size; i++) {
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[i]);
std::lock_guard<std::mutex> sync(m_handleMutexes[i]);
m_structures[i].reset();
}
}

View File

@@ -11,10 +11,10 @@
#include <array>
#include <memory>
#include <mutex>
#include "HAL/Types.h"
#include "HAL/cpp/make_unique.h"
#include "HAL/cpp/priority_mutex.h"
#include "HandlesInternal.h"
namespace hal {
@@ -46,20 +46,20 @@ class LimitedHandleResource : public HandleBase {
private:
std::array<std::shared_ptr<TStruct>, size> m_structures;
std::array<hal::priority_mutex, size> m_handleMutexes;
hal::priority_mutex m_allocateMutex;
std::array<std::mutex, size> m_handleMutexes;
std::mutex m_allocateMutex;
};
template <typename THandle, typename TStruct, int16_t size,
HAL_HandleEnum enumValue>
THandle LimitedHandleResource<THandle, TStruct, size, enumValue>::Allocate() {
// globally lock to loop through indices
std::lock_guard<hal::priority_mutex> sync(m_allocateMutex);
std::lock_guard<std::mutex> sync(m_allocateMutex);
for (int16_t i = 0; i < size; i++) {
if (m_structures[i] == nullptr) {
// if a false index is found, grab its specific mutex
// and allocate it.
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[i]);
std::lock_guard<std::mutex> sync(m_handleMutexes[i]);
m_structures[i] = std::make_shared<TStruct>();
return static_cast<THandle>(createHandle(i, enumValue, m_version));
}
@@ -76,7 +76,7 @@ LimitedHandleResource<THandle, TStruct, size, enumValue>::Get(THandle handle) {
if (index < 0 || index >= size) {
return nullptr;
}
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[index]);
std::lock_guard<std::mutex> sync(m_handleMutexes[index]);
// return structure. Null will propogate correctly, so no need to manually
// check.
return m_structures[index];
@@ -90,8 +90,8 @@ void LimitedHandleResource<THandle, TStruct, size, enumValue>::Free(
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
if (index < 0 || index >= size) return;
// lock and deallocated handle
std::lock_guard<hal::priority_mutex> sync(m_allocateMutex);
std::lock_guard<hal::priority_mutex> lock(m_handleMutexes[index]);
std::lock_guard<std::mutex> sync(m_allocateMutex);
std::lock_guard<std::mutex> lock(m_handleMutexes[index]);
m_structures[index].reset();
}
@@ -99,9 +99,9 @@ template <typename THandle, typename TStruct, int16_t size,
HAL_HandleEnum enumValue>
void LimitedHandleResource<THandle, TStruct, size, enumValue>::ResetHandles() {
{
std::lock_guard<hal::priority_mutex> lock(m_allocateMutex);
std::lock_guard<std::mutex> lock(m_allocateMutex);
for (int i = 0; i < size; i++) {
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[i]);
std::lock_guard<std::mutex> sync(m_handleMutexes[i]);
m_structures[i].reset();
}
}

View File

@@ -10,10 +10,10 @@
#include <stdint.h>
#include <memory>
#include <mutex>
#include <vector>
#include "HAL/Types.h"
#include "HAL/cpp/priority_mutex.h"
#include "HAL/handles/HandlesInternal.h"
namespace hal {
@@ -48,13 +48,13 @@ class UnlimitedHandleResource : public HandleBase {
private:
std::vector<std::shared_ptr<TStruct>> m_structures;
hal::priority_mutex m_handleMutex;
std::mutex m_handleMutex;
};
template <typename THandle, typename TStruct, HAL_HandleEnum enumValue>
THandle UnlimitedHandleResource<THandle, TStruct, enumValue>::Allocate(
std::shared_ptr<TStruct> structure) {
std::lock_guard<hal::priority_mutex> sync(m_handleMutex);
std::lock_guard<std::mutex> sync(m_handleMutex);
size_t i;
for (i = 0; i < m_structures.size(); i++) {
if (m_structures[i] == nullptr) {
@@ -73,7 +73,7 @@ template <typename THandle, typename TStruct, HAL_HandleEnum enumValue>
std::shared_ptr<TStruct>
UnlimitedHandleResource<THandle, TStruct, enumValue>::Get(THandle handle) {
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
std::lock_guard<hal::priority_mutex> sync(m_handleMutex);
std::lock_guard<std::mutex> sync(m_handleMutex);
if (index < 0 || index >= static_cast<int16_t>(m_structures.size()))
return nullptr;
return m_structures[index];
@@ -83,7 +83,7 @@ template <typename THandle, typename TStruct, HAL_HandleEnum enumValue>
void UnlimitedHandleResource<THandle, TStruct, enumValue>::Free(
THandle handle) {
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
std::lock_guard<hal::priority_mutex> sync(m_handleMutex);
std::lock_guard<std::mutex> sync(m_handleMutex);
if (index < 0 || index >= static_cast<int16_t>(m_structures.size())) return;
m_structures[index].reset();
}
@@ -91,7 +91,7 @@ void UnlimitedHandleResource<THandle, TStruct, enumValue>::Free(
template <typename THandle, typename TStruct, HAL_HandleEnum enumValue>
void UnlimitedHandleResource<THandle, TStruct, enumValue>::ResetHandles() {
{
std::lock_guard<hal::priority_mutex> lock(m_handleMutex);
std::lock_guard<std::mutex> lock(m_handleMutex);
for (size_t i = 0; i < m_structures.size(); i++) {
m_structures[i].reset();
}