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);
58 for (i = 0; i < size; i++) {
59 if (m_structures[i] ==
nullptr) {
62 std::lock_guard<priority_mutex> sync(m_handleMutexes[i]);
63 m_structures[i] = std::make_shared<TStruct>();
64 return static_cast<THandle
>(createHandle(i, enumValue));
67 return HAL_kInvalidHandle;
70 template <
typename THandle,
typename TStruct, int16_t size,
71 HAL_HandleEnum enumValue>
72 std::shared_ptr<TStruct>
73 LimitedHandleResource<THandle, TStruct, size, enumValue>::Get(THandle handle) {
75 int16_t index = getHandleTypedIndex(handle, enumValue);
76 if (index < 0 || index >= size) {
79 std::lock_guard<priority_mutex> sync(m_handleMutexes[index]);
82 return m_structures[index];
85 template <
typename THandle,
typename TStruct, int16_t size,
86 HAL_HandleEnum enumValue>
87 void LimitedHandleResource<THandle, TStruct, size, enumValue>::Free(
90 int16_t index = getHandleTypedIndex(handle, enumValue);
91 if (index < 0 || index >= size)
return;
93 std::lock_guard<priority_mutex> sync(m_allocateMutex);
94 std::lock_guard<priority_mutex> lock(m_handleMutexes[index]);
95 m_structures[index].reset();
The LimitedHandleResource class is a way to track handles.
Definition: LimitedHandleResource.h:34
Definition: priority_mutex.h:53