WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
IndexedHandleResource.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include <array>
10#include <memory>
11
12#include "wpi/hal/Errors.h"
13#include "wpi/hal/Types.h"
15#include "wpi/util/mutex.hpp"
16
17namespace wpi::hal {
18
19/**
20 * The IndexedHandleResource class is a way to track handles. This version
21 * allows a limited number of handles that are allocated by index.
22 * Because they are allocated by index, each individual index holds its own
23 * mutex, which reduces contention heavily.]
24 *
25 * @tparam THandle The Handle Type (Must be typedefed from HAL_Handle)
26 * @tparam TStruct The struct type held by this resource
27 * @tparam size The number of resources allowed to be allocated
28 * @tparam enumValue The type value stored in the handle
29 *
30 */
31template <typename THandle, typename TStruct, int16_t size,
32 HAL_HandleEnum enumValue>
35
36 public:
40
41 std::shared_ptr<TStruct> Allocate(int16_t index, THandle* handle,
42 int32_t* status);
43 int16_t GetIndex(THandle handle) {
44 return getHandleTypedIndex(handle, enumValue, m_version);
45 }
46 std::shared_ptr<TStruct> Get(THandle handle);
47 void Free(THandle handle);
48 void ResetHandles() override;
49
50 private:
51 std::array<std::shared_ptr<TStruct>, size> m_structures;
52 std::array<wpi::util::mutex, size> m_handleMutexes;
53};
54
55template <typename THandle, typename TStruct, int16_t size,
56 HAL_HandleEnum enumValue>
57std::shared_ptr<TStruct>
59 int16_t index, THandle* handle, int32_t* status) {
60 // don't acquire the lock if we can fail early.
61 if (index < 0 || index >= size) {
62 *status = RESOURCE_OUT_OF_RANGE;
63 *handle = HAL_kInvalidHandle;
64 return nullptr;
65 }
66 std::scoped_lock lock(m_handleMutexes[index]);
67 // check for allocation, otherwise allocate and return a valid handle
68 if (m_structures[index] != nullptr) {
69 *status = RESOURCE_IS_ALLOCATED;
70 *handle = HAL_kInvalidHandle;
71 return m_structures[index];
72 }
73 m_structures[index] = std::make_shared<TStruct>();
74 *handle =
75 static_cast<THandle>(wpi::hal::createHandle(index, enumValue, m_version));
76 *status = HAL_SUCCESS;
77 return m_structures[index];
78}
79
80template <typename THandle, typename TStruct, int16_t size,
81 HAL_HandleEnum enumValue>
82std::shared_ptr<TStruct>
84 // get handle index, and fail early if index out of range or wrong handle
85 int16_t index = GetIndex(handle);
86 if (index < 0 || index >= size) {
87 return nullptr;
88 }
89 std::scoped_lock lock(m_handleMutexes[index]);
90 // return structure. Null will propagate correctly, so no need to manually
91 // check.
92 return m_structures[index];
93}
94
95template <typename THandle, typename TStruct, int16_t size,
96 HAL_HandleEnum enumValue>
98 THandle handle) {
99 // get handle index, and fail early if index out of range or wrong handle
100 int16_t index = GetIndex(handle);
101 if (index < 0 || index >= size) {
102 return;
103 }
104 // lock and deallocated handle
105 std::scoped_lock lock(m_handleMutexes[index]);
106 m_structures[index].reset();
107}
108
109template <typename THandle, typename TStruct, int16_t size,
110 HAL_HandleEnum enumValue>
112 for (int i = 0; i < size; i++) {
113 std::scoped_lock lock(m_handleMutexes[i]);
114 m_structures[i].reset();
115 }
117}
118} // namespace wpi::hal
@ index
Definition base.h:690
virtual void ResetHandles()
int16_t m_version
Definition HandlesInternal.hpp:37
void ResetHandles() override
Definition IndexedHandleResource.hpp:111
int16_t GetIndex(THandle handle)
Definition IndexedHandleResource.hpp:43
friend class IndexedHandleResourceTest
Definition IndexedHandleResource.hpp:34
void Free(THandle handle)
Definition IndexedHandleResource.hpp:97
std::shared_ptr< TStruct > Get(THandle handle)
Definition IndexedHandleResource.hpp:83
IndexedHandleResource & operator=(const IndexedHandleResource &)=delete
std::shared_ptr< TStruct > Allocate(int16_t index, THandle *handle, int32_t *status)
Definition IndexedHandleResource.hpp:58
IndexedHandleResource(const IndexedHandleResource &)=delete
#define RESOURCE_OUT_OF_RANGE
Definition Errors.h:84
#define RESOURCE_IS_ALLOCATED
Definition Errors.h:82
#define HAL_SUCCESS
Definition Errors.h:48
#define HAL_kInvalidHandle
Definition Types.h:15
WPILib Hardware Abstraction Layer (HAL) namespace.
Definition Types.hpp:9
HAL_HandleEnum
Enum of HAL handle types.
Definition HandlesInternal.hpp:45
int16_t getHandleTypedIndex(HAL_Handle handle, HAL_HandleEnum enumType, int16_t version)
Get if the handle is a correct type and version.
Definition HandlesInternal.hpp:135
HAL_Handle createHandle(int16_t index, HAL_HandleEnum handleType, int16_t version)
Create a handle for a specific index, type and version.