From 71c187a1eddfc6c9b652190d83dea7a31a6cb9fb Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 26 Jun 2020 19:56:05 -0700 Subject: [PATCH] [hal] Add GetIndex() to handles classes (#2548) This provides a lock-free way to get just the handle index. --- .../native/include/hal/handles/DigitalHandleResource.h | 9 ++++++--- .../include/hal/handles/IndexedClassedHandleResource.h | 9 ++++++--- .../native/include/hal/handles/IndexedHandleResource.h | 9 ++++++--- .../include/hal/handles/LimitedClassedHandleResource.h | 9 ++++++--- .../native/include/hal/handles/LimitedHandleResource.h | 9 ++++++--- .../native/include/hal/handles/UnlimitedHandleResource.h | 9 ++++++--- 6 files changed, 36 insertions(+), 18 deletions(-) diff --git a/hal/src/main/native/include/hal/handles/DigitalHandleResource.h b/hal/src/main/native/include/hal/handles/DigitalHandleResource.h index dcd4b97caf..3372fe5a27 100644 --- a/hal/src/main/native/include/hal/handles/DigitalHandleResource.h +++ b/hal/src/main/native/include/hal/handles/DigitalHandleResource.h @@ -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 Get(THandle handle, HAL_HandleEnum enumValue); void Free(THandle handle, HAL_HandleEnum enumValue); void ResetHandles() override; @@ -73,7 +76,7 @@ template std::shared_ptr DigitalHandleResource::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 void DigitalHandleResource::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]); diff --git a/hal/src/main/native/include/hal/handles/IndexedClassedHandleResource.h b/hal/src/main/native/include/hal/handles/IndexedClassedHandleResource.h index 2725573d41..7bb0f6b471 100644 --- a/hal/src/main/native/include/hal/handles/IndexedClassedHandleResource.h +++ b/hal/src/main/native/include/hal/handles/IndexedClassedHandleResource.h @@ -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 toSet, int32_t* status); + int16_t GetIndex(THandle handle) { + return getHandleTypedIndex(handle, enumValue, m_version); + } std::shared_ptr Get(THandle handle); void Free(THandle handle); void ResetHandles(); @@ -82,7 +85,7 @@ std::shared_ptr IndexedClassedHandleResource::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 ::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]); diff --git a/hal/src/main/native/include/hal/handles/IndexedHandleResource.h b/hal/src/main/native/include/hal/handles/IndexedHandleResource.h index 2bca4ce8ae..0217e7e036 100644 --- a/hal/src/main/native/include/hal/handles/IndexedHandleResource.h +++ b/hal/src/main/native/include/hal/handles/IndexedHandleResource.h @@ -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 Get(THandle handle); void Free(THandle handle); void ResetHandles() override; @@ -76,7 +79,7 @@ template IndexedHandleResource::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 ::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]); diff --git a/hal/src/main/native/include/hal/handles/LimitedClassedHandleResource.h b/hal/src/main/native/include/hal/handles/LimitedClassedHandleResource.h index a991fc349b..564377a79a 100644 --- a/hal/src/main/native/include/hal/handles/LimitedClassedHandleResource.h +++ b/hal/src/main/native/include/hal/handles/LimitedClassedHandleResource.h @@ -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 toSet); + int16_t GetIndex(THandle handle) { + return getHandleTypedIndex(handle, enumValue, m_version); + } std::shared_ptr Get(THandle handle); void Free(THandle handle); void ResetHandles() override; @@ -77,7 +80,7 @@ std::shared_ptr LimitedClassedHandleResource::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 ::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); diff --git a/hal/src/main/native/include/hal/handles/LimitedHandleResource.h b/hal/src/main/native/include/hal/handles/LimitedHandleResource.h index 07566340ea..2ae3141e23 100644 --- a/hal/src/main/native/include/hal/handles/LimitedHandleResource.h +++ b/hal/src/main/native/include/hal/handles/LimitedHandleResource.h @@ -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 Get(THandle handle); void Free(THandle handle); void ResetHandles() override; @@ -72,7 +75,7 @@ template LimitedHandleResource::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 ::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); diff --git a/hal/src/main/native/include/hal/handles/UnlimitedHandleResource.h b/hal/src/main/native/include/hal/handles/UnlimitedHandleResource.h index 96a91f846c..5f74b88e18 100644 --- a/hal/src/main/native/include/hal/handles/UnlimitedHandleResource.h +++ b/hal/src/main/native/include/hal/handles/UnlimitedHandleResource.h @@ -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 structure); + int16_t GetIndex(THandle handle) { + return getHandleTypedIndex(handle, enumValue, m_version); + } std::shared_ptr Get(THandle handle); /* Returns structure previously at that handle (or nullptr if none) */ std::shared_ptr Free(THandle handle); @@ -81,7 +84,7 @@ THandle UnlimitedHandleResource::Allocate( template std::shared_ptr UnlimitedHandleResource::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(m_structures.size())) return nullptr; @@ -91,7 +94,7 @@ UnlimitedHandleResource::Get(THandle handle) { template std::shared_ptr UnlimitedHandleResource::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(m_structures.size())) return nullptr;