WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
LimitedHandleResource.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/Types.h"
14#include "wpi/util/mutex.hpp"
15
16namespace wpi::hal {
17
18/**
19 * The LimitedHandleResource class is a way to track handles. This version
20 * allows a limited number of handles that are allocated sequentially.
21 *
22 * @tparam THandle The Handle Type (Must be typedefed from HAL_Handle)
23 * @tparam TStruct The struct type held by this resource
24 * @tparam size The number of resources allowed to be allocated
25 * @tparam enumValue The type value stored in the handle
26 *
27 */
28template <typename THandle, typename TStruct, int16_t size,
29 HAL_HandleEnum enumValue>
32
33 public:
37
38 THandle Allocate();
39 int16_t GetIndex(THandle handle) {
40 return getHandleTypedIndex(handle, enumValue, m_version);
41 }
42 std::shared_ptr<TStruct> Get(THandle handle);
43 void Free(THandle handle);
44 void ResetHandles() override;
45
46 private:
47 std::array<std::shared_ptr<TStruct>, size> m_structures;
48 std::array<wpi::util::mutex, size> m_handleMutexes;
49 wpi::util::mutex m_allocateMutex;
50};
51
52template <typename THandle, typename TStruct, int16_t size,
53 HAL_HandleEnum enumValue>
55 // globally lock to loop through indices
56 std::scoped_lock lock(m_allocateMutex);
57 for (int16_t i = 0; i < size; i++) {
58 if (m_structures[i] == nullptr) {
59 // if a false index is found, grab its specific mutex
60 // and allocate it.
61 std::scoped_lock lock(m_handleMutexes[i]);
62 m_structures[i] = std::make_shared<TStruct>();
63 return static_cast<THandle>(createHandle(i, enumValue, m_version));
64 }
65 }
66 return HAL_kInvalidHandle;
67}
68
69template <typename THandle, typename TStruct, int16_t size,
70 HAL_HandleEnum enumValue>
71std::shared_ptr<TStruct>
73 // get handle index, and fail early if index out of range or wrong handle
74 int16_t index = GetIndex(handle);
75 if (index < 0 || index >= size) {
76 return nullptr;
77 }
78 std::scoped_lock lock(m_handleMutexes[index]);
79 // return structure. Null will propagate correctly, so no need to manually
80 // check.
81 return m_structures[index];
82}
83
84template <typename THandle, typename TStruct, int16_t size,
85 HAL_HandleEnum enumValue>
87 THandle handle) {
88 // get handle index, and fail early if index out of range or wrong handle
89 int16_t index = GetIndex(handle);
90 if (index < 0 || index >= size) {
91 return;
92 }
93 // lock and deallocated handle
94 std::scoped_lock allocateLock(m_allocateMutex);
95 std::scoped_lock handleLock(m_handleMutexes[index]);
96 m_structures[index].reset();
97}
98
99template <typename THandle, typename TStruct, int16_t size,
100 HAL_HandleEnum enumValue>
102 {
103 std::scoped_lock allocateLock(m_allocateMutex);
104 for (int i = 0; i < size; i++) {
105 std::scoped_lock handleLock(m_handleMutexes[i]);
106 m_structures[i].reset();
107 }
108 }
110}
111} // namespace wpi::hal
@ index
Definition base.h:690
virtual void ResetHandles()
int16_t m_version
Definition HandlesInternal.hpp:37
std::shared_ptr< TStruct > Get(THandle handle)
Definition LimitedHandleResource.hpp:72
LimitedHandleResource(const LimitedHandleResource &)=delete
int16_t GetIndex(THandle handle)
Definition LimitedHandleResource.hpp:39
void ResetHandles() override
Definition LimitedHandleResource.hpp:101
void Free(THandle handle)
Definition LimitedHandleResource.hpp:86
LimitedHandleResource & operator=(const LimitedHandleResource &)=delete
THandle Allocate()
Definition LimitedHandleResource.hpp:54
friend class LimitedHandleResourceTest
Definition LimitedHandleResource.hpp:31
#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.
::std::mutex mutex
Definition mutex.hpp:17