15 #include "HAL/Types.h"
16 #include "HAL/cpp/make_unique.h"
17 #include "HAL/cpp/priority_mutex.h"
18 #include "HandlesInternal.h"
32 template <
typename THandle,
typename TStruct, int16_t size,
33 HAL_HandleEnum enumValue>
35 friend class LimitedHandleResourceTest;
43 std::shared_ptr<TStruct> Get(THandle handle);
44 void Free(THandle handle);
47 std::array<std::shared_ptr<TStruct>, size> m_structures;
48 std::array<priority_mutex, size> m_handleMutexes;
52 template <
typename THandle,
typename TStruct, int16_t size,
53 HAL_HandleEnum enumValue>
56 std::lock_guard<priority_mutex> sync(m_allocateMutex);
57 for (int16_t i = 0; i < size; i++) {
58 if (m_structures[i] ==
nullptr) {
61 std::lock_guard<priority_mutex> sync(m_handleMutexes[i]);
62 m_structures[i] = std::make_shared<TStruct>();
63 return static_cast<THandle
>(createHandle(i, enumValue));
66 return HAL_kInvalidHandle;
69 template <
typename THandle,
typename TStruct, int16_t size,
70 HAL_HandleEnum enumValue>
71 std::shared_ptr<TStruct>
72 LimitedHandleResource<THandle, TStruct, size, enumValue>::Get(THandle handle) {
74 int16_t index = getHandleTypedIndex(handle, enumValue);
75 if (index < 0 || index >= size) {
78 std::lock_guard<priority_mutex> sync(m_handleMutexes[index]);
81 return m_structures[index];
84 template <
typename THandle,
typename TStruct, int16_t size,
85 HAL_HandleEnum enumValue>
86 void LimitedHandleResource<THandle, TStruct, size, enumValue>::Free(
89 int16_t index = getHandleTypedIndex(handle, enumValue);
90 if (index < 0 || index >= size)
return;
92 std::lock_guard<priority_mutex> sync(m_allocateMutex);
93 std::lock_guard<priority_mutex> lock(m_handleMutexes[index]);
94 m_structures[index].reset();
The LimitedHandleResource class is a way to track handles.
Definition: LimitedHandleResource.h:34
Definition: priority_mutex.h:53