mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
Use wpi::mutex instead of std::mutex. (#730)
This uses a priority-aware mutex on Linux platforms. Fixes #729.
This commit is contained in:
@@ -12,6 +12,8 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include <support/mutex.h>
|
||||
|
||||
#include "HAL/Errors.h"
|
||||
#include "HAL/Types.h"
|
||||
#include "HAL/cpp/make_unique.h"
|
||||
@@ -51,7 +53,7 @@ class IndexedClassedHandleResource : public HandleBase {
|
||||
|
||||
private:
|
||||
std::array<std::shared_ptr<TStruct>[], size> m_structures;
|
||||
std::array<std::mutex[], size> m_handleMutexes;
|
||||
std::array<wpi::mutex[], size> m_handleMutexes;
|
||||
};
|
||||
|
||||
template <typename THandle, typename TStruct, int16_t size,
|
||||
@@ -59,7 +61,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<std::mutex[]>(size);
|
||||
m_handleMutexes = std::make_unique<wpi::mutex[]>(size);
|
||||
}
|
||||
|
||||
template <typename THandle, typename TStruct, int16_t size,
|
||||
@@ -72,7 +74,7 @@ IndexedClassedHandleResource<THandle, TStruct, size, enumValue>::Allocate(
|
||||
*status = RESOURCE_OUT_OF_RANGE;
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
std::lock_guard<std::mutex> sync(m_handleMutexes[index]);
|
||||
std::lock_guard<wpi::mutex> sync(m_handleMutexes[index]);
|
||||
// check for allocation, otherwise allocate and return a valid handle
|
||||
if (m_structures[index] != nullptr) {
|
||||
*status = RESOURCE_IS_ALLOCATED;
|
||||
@@ -92,7 +94,7 @@ IndexedClassedHandleResource<THandle, TStruct, size, enumValue>::Get(
|
||||
if (index < 0 || index >= size) {
|
||||
return nullptr;
|
||||
}
|
||||
std::lock_guard<std::mutex> sync(m_handleMutexes[index]);
|
||||
std::lock_guard<wpi::mutex> sync(m_handleMutexes[index]);
|
||||
// return structure. Null will propogate correctly, so no need to manually
|
||||
// check.
|
||||
return m_structures[index];
|
||||
@@ -106,7 +108,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<std::mutex> sync(m_handleMutexes[index]);
|
||||
std::lock_guard<wpi::mutex> sync(m_handleMutexes[index]);
|
||||
m_structures[index].reset();
|
||||
}
|
||||
|
||||
@@ -115,7 +117,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<std::mutex> sync(m_handleMutexes[i]);
|
||||
std::lock_guard<wpi::mutex> sync(m_handleMutexes[i]);
|
||||
m_structures[i].reset();
|
||||
}
|
||||
HandleBase::ResetHandles();
|
||||
|
||||
Reference in New Issue
Block a user