mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
[hal] Add GetIndex() to handles classes (#2548)
This provides a lock-free way to get just the handle index.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2016-2020 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
@@ -42,6 +42,9 @@ class DigitalHandleResource : public HandleBase {
|
||||
DigitalHandleResource& operator=(const DigitalHandleResource&) = delete;
|
||||
|
||||
THandle Allocate(int16_t index, HAL_HandleEnum enumValue, int32_t* status);
|
||||
int16_t GetIndex(THandle handle, HAL_HandleEnum enumValue) {
|
||||
return getHandleTypedIndex(handle, enumValue, m_version);
|
||||
}
|
||||
std::shared_ptr<TStruct> Get(THandle handle, HAL_HandleEnum enumValue);
|
||||
void Free(THandle handle, HAL_HandleEnum enumValue);
|
||||
void ResetHandles() override;
|
||||
@@ -73,7 +76,7 @@ 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, m_version);
|
||||
int16_t index = GetIndex(handle, enumValue);
|
||||
if (index < 0 || index >= size) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -87,7 +90,7 @@ 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, m_version);
|
||||
int16_t index = GetIndex(handle, enumValue);
|
||||
if (index < 0 || index >= size) return;
|
||||
// lock and deallocated handle
|
||||
std::scoped_lock lock(m_handleMutexes[index]);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2016-2020 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
@@ -47,6 +47,9 @@ class IndexedClassedHandleResource : public HandleBase {
|
||||
|
||||
THandle Allocate(int16_t index, std::shared_ptr<TStruct> toSet,
|
||||
int32_t* status);
|
||||
int16_t GetIndex(THandle handle) {
|
||||
return getHandleTypedIndex(handle, enumValue, m_version);
|
||||
}
|
||||
std::shared_ptr<TStruct> Get(THandle handle);
|
||||
void Free(THandle handle);
|
||||
void ResetHandles();
|
||||
@@ -82,7 +85,7 @@ std::shared_ptr<TStruct>
|
||||
IndexedClassedHandleResource<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, m_version);
|
||||
int16_t index = GetIndex(handle);
|
||||
if (index < 0 || index >= size) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -97,7 +100,7 @@ template <typename THandle, typename TStruct, int16_t size,
|
||||
void IndexedClassedHandleResource<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, m_version);
|
||||
int16_t index = GetIndex(handle);
|
||||
if (index < 0 || index >= size) return;
|
||||
// lock and deallocated handle
|
||||
std::scoped_lock lock(m_handleMutexes[index]);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2016-2020 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
@@ -43,6 +43,9 @@ class IndexedHandleResource : public HandleBase {
|
||||
IndexedHandleResource& operator=(const IndexedHandleResource&) = delete;
|
||||
|
||||
THandle Allocate(int16_t index, int32_t* status);
|
||||
int16_t GetIndex(THandle handle) {
|
||||
return getHandleTypedIndex(handle, enumValue, m_version);
|
||||
}
|
||||
std::shared_ptr<TStruct> Get(THandle handle);
|
||||
void Free(THandle handle);
|
||||
void ResetHandles() override;
|
||||
@@ -76,7 +79,7 @@ template <typename THandle, typename TStruct, int16_t size,
|
||||
std::shared_ptr<TStruct>
|
||||
IndexedHandleResource<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, m_version);
|
||||
int16_t index = GetIndex(handle);
|
||||
if (index < 0 || index >= size) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -91,7 +94,7 @@ template <typename THandle, typename TStruct, int16_t size,
|
||||
void IndexedHandleResource<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, m_version);
|
||||
int16_t index = GetIndex(handle);
|
||||
if (index < 0 || index >= size) return;
|
||||
// lock and deallocated handle
|
||||
std::scoped_lock lock(m_handleMutexes[index]);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2016-2020 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
@@ -42,6 +42,9 @@ class LimitedClassedHandleResource : public HandleBase {
|
||||
delete;
|
||||
|
||||
THandle Allocate(std::shared_ptr<TStruct> toSet);
|
||||
int16_t GetIndex(THandle handle) {
|
||||
return getHandleTypedIndex(handle, enumValue, m_version);
|
||||
}
|
||||
std::shared_ptr<TStruct> Get(THandle handle);
|
||||
void Free(THandle handle);
|
||||
void ResetHandles() override;
|
||||
@@ -77,7 +80,7 @@ std::shared_ptr<TStruct>
|
||||
LimitedClassedHandleResource<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, m_version);
|
||||
int16_t index = GetIndex(handle);
|
||||
if (index < 0 || index >= size) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -92,7 +95,7 @@ template <typename THandle, typename TStruct, int16_t size,
|
||||
void LimitedClassedHandleResource<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, m_version);
|
||||
int16_t index = GetIndex(handle);
|
||||
if (index < 0 || index >= size) return;
|
||||
// lock and deallocated handle
|
||||
std::scoped_lock allocateLock(m_allocateMutex);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2016-2020 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
@@ -40,6 +40,9 @@ class LimitedHandleResource : public HandleBase {
|
||||
LimitedHandleResource& operator=(const LimitedHandleResource&) = delete;
|
||||
|
||||
THandle Allocate();
|
||||
int16_t GetIndex(THandle handle) {
|
||||
return getHandleTypedIndex(handle, enumValue, m_version);
|
||||
}
|
||||
std::shared_ptr<TStruct> Get(THandle handle);
|
||||
void Free(THandle handle);
|
||||
void ResetHandles() override;
|
||||
@@ -72,7 +75,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, m_version);
|
||||
int16_t index = GetIndex(handle);
|
||||
if (index < 0 || index >= size) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -87,7 +90,7 @@ 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, m_version);
|
||||
int16_t index = GetIndex(handle);
|
||||
if (index < 0 || index >= size) return;
|
||||
// lock and deallocated handle
|
||||
std::scoped_lock allocateLock(m_allocateMutex);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
@@ -44,6 +44,9 @@ class UnlimitedHandleResource : public HandleBase {
|
||||
UnlimitedHandleResource& operator=(const UnlimitedHandleResource&) = delete;
|
||||
|
||||
THandle Allocate(std::shared_ptr<TStruct> structure);
|
||||
int16_t GetIndex(THandle handle) {
|
||||
return getHandleTypedIndex(handle, enumValue, m_version);
|
||||
}
|
||||
std::shared_ptr<TStruct> Get(THandle handle);
|
||||
/* Returns structure previously at that handle (or nullptr if none) */
|
||||
std::shared_ptr<TStruct> Free(THandle handle);
|
||||
@@ -81,7 +84,7 @@ THandle UnlimitedHandleResource<THandle, TStruct, enumValue>::Allocate(
|
||||
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);
|
||||
int16_t index = GetIndex(handle);
|
||||
std::scoped_lock lock(m_handleMutex);
|
||||
if (index < 0 || index >= static_cast<int16_t>(m_structures.size()))
|
||||
return nullptr;
|
||||
@@ -91,7 +94,7 @@ UnlimitedHandleResource<THandle, TStruct, enumValue>::Get(THandle handle) {
|
||||
template <typename THandle, typename TStruct, HAL_HandleEnum enumValue>
|
||||
std::shared_ptr<TStruct>
|
||||
UnlimitedHandleResource<THandle, TStruct, enumValue>::Free(THandle handle) {
|
||||
int16_t index = getHandleTypedIndex(handle, enumValue, m_version);
|
||||
int16_t index = GetIndex(handle);
|
||||
std::scoped_lock lock(m_handleMutex);
|
||||
if (index < 0 || index >= static_cast<int16_t>(m_structures.size()))
|
||||
return nullptr;
|
||||
|
||||
Reference in New Issue
Block a user