mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-29 02:21:44 +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
@@ -33,7 +33,7 @@ namespace hal {
|
||||
*
|
||||
*/
|
||||
template <typename THandle, typename TStruct, int16_t size>
|
||||
class DigitalHandleResource {
|
||||
class DigitalHandleResource : public HandleBase {
|
||||
friend class DigitalHandleResourceTest;
|
||||
|
||||
public:
|
||||
@@ -44,6 +44,7 @@ class DigitalHandleResource {
|
||||
THandle Allocate(int16_t index, HAL_HandleEnum enumValue, int32_t* status);
|
||||
std::shared_ptr<TStruct> Get(THandle handle, HAL_HandleEnum enumValue);
|
||||
void Free(THandle handle, HAL_HandleEnum enumValue);
|
||||
void ResetHandles() override;
|
||||
|
||||
private:
|
||||
std::array<std::shared_ptr<TStruct>, size> m_structures;
|
||||
@@ -65,14 +66,14 @@ THandle DigitalHandleResource<THandle, TStruct, size>::Allocate(
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
m_structures[index] = std::make_shared<TStruct>();
|
||||
return static_cast<THandle>(hal::createHandle(index, enumValue));
|
||||
return static_cast<THandle>(hal::createHandle(index, enumValue, m_version));
|
||||
}
|
||||
|
||||
template <typename THandle, typename TStruct, int16_t size>
|
||||
std::shared_ptr<TStruct> DigitalHandleResource<THandle, TStruct, size>::Get(
|
||||
THandle handle, HAL_HandleEnum enumValue) {
|
||||
// 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,10 +87,19 @@ template <typename THandle, typename TStruct, int16_t size>
|
||||
void DigitalHandleResource<THandle, TStruct, size>::Free(
|
||||
THandle handle, HAL_HandleEnum enumValue) {
|
||||
// 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_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]);
|
||||
m_structures[i].reset();
|
||||
}
|
||||
HandleBase::ResetHandles();
|
||||
}
|
||||
} // namespace hal
|
||||
|
||||
Reference in New Issue
Block a user