mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-06 03:31:43 +00:00
Adds way to reset and version all HAL handles (#545)
Useful in the sim to force a full reset. On roboRIO, the information is still created and added, but is not checked because of speed considerations.
This commit is contained in:
committed by
Peter Johnson
parent
0cd03c66e3
commit
e824b1129e
@@ -31,7 +31,7 @@ namespace hal {
|
||||
*/
|
||||
template <typename THandle, typename TStruct, int16_t size,
|
||||
HAL_HandleEnum enumValue>
|
||||
class LimitedHandleResource {
|
||||
class LimitedHandleResource : public HandleBase {
|
||||
friend class LimitedHandleResourceTest;
|
||||
|
||||
public:
|
||||
@@ -42,6 +42,7 @@ class LimitedHandleResource {
|
||||
THandle Allocate();
|
||||
std::shared_ptr<TStruct> Get(THandle handle);
|
||||
void Free(THandle handle);
|
||||
void ResetHandles() override;
|
||||
|
||||
private:
|
||||
std::array<std::shared_ptr<TStruct>, size> m_structures;
|
||||
@@ -60,7 +61,7 @@ THandle LimitedHandleResource<THandle, TStruct, size, enumValue>::Allocate() {
|
||||
// and allocate it.
|
||||
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[i]);
|
||||
m_structures[i] = std::make_shared<TStruct>();
|
||||
return static_cast<THandle>(createHandle(i, enumValue));
|
||||
return static_cast<THandle>(createHandle(i, enumValue, m_version));
|
||||
}
|
||||
}
|
||||
return HAL_kInvalidHandle;
|
||||
@@ -71,7 +72,7 @@ template <typename THandle, typename TStruct, int16_t size,
|
||||
std::shared_ptr<TStruct>
|
||||
LimitedHandleResource<THandle, TStruct, size, enumValue>::Get(THandle handle) {
|
||||
// get handle index, and fail early if index out of range or wrong handle
|
||||
int16_t index = getHandleTypedIndex(handle, enumValue);
|
||||
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
|
||||
if (index < 0 || index >= size) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -86,11 +87,24 @@ template <typename THandle, typename TStruct, int16_t size,
|
||||
void LimitedHandleResource<THandle, TStruct, size, enumValue>::Free(
|
||||
THandle handle) {
|
||||
// get handle index, and fail early if index out of range or wrong handle
|
||||
int16_t index = getHandleTypedIndex(handle, enumValue);
|
||||
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]);
|
||||
m_structures[index].reset();
|
||||
}
|
||||
|
||||
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);
|
||||
for (int i = 0; i < size; i++) {
|
||||
std::lock_guard<hal::priority_mutex> sync(m_handleMutexes[i]);
|
||||
m_structures[i].reset();
|
||||
}
|
||||
}
|
||||
HandleBase::ResetHandles();
|
||||
}
|
||||
} // namespace hal
|
||||
|
||||
Reference in New Issue
Block a user