Skip to content

Commit d96df7b

Browse files
committed
Document data pool concurrency contract
1 parent b7d9aeb commit d96df7b

1 file changed

Lines changed: 57 additions & 64 deletions

File tree

src/Foundations/SystemDataPool.h

Lines changed: 57 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "SystemMemory.h"
55

66
/**
7-
* Represents an handle of a data pool decomposed into index and version.
7+
* Represents a packed data-pool handle decomposed into index and version.
88
*/
99
struct SystemDataPoolHandle
1010
{
@@ -13,118 +13,111 @@ struct SystemDataPoolHandle
1313
};
1414

1515
/**
16-
* A default full structure used as a fallback for the SystemDataPool template when no full data type is specified.
16+
* Default full-data type used when a data pool has no secondary data payload.
1717
*/
1818
struct SystemDataPoolDefaultFull
1919
{
2020
};
2121

22-
/**
23-
* Forward declaration of the SystemDataPoolStorage structure.
24-
* This structure is meant to internally manage storage specifics of a data pool, but its implementation details are abstracted away from the user.
25-
*/
2622
template<typename T, typename TFull>
2723
struct SystemDataPoolStorage;
2824

2925
/**
30-
* Represents a data pool with specific item types and optionally, a fuller version of each item.
31-
* This structure allows for the organization and efficient management of a collection of items of type T,
32-
* with the option to extend each item with additional data of type TFull.
33-
*
34-
* @tparam T The primary type of items stored in the data pool.
35-
* @tparam TFull The full data type associated with each item, providing additional information or properties.
26+
* Lightweight handle to a fixed-capacity data pool.
27+
*
28+
* Data-pool add, remove, lookup, and count operations are thread-safe for a pool created from a
29+
* shared MemoryArena. Index allocation and recycling are synchronized internally, while lookups
30+
* validate the item generation without taking the allocation lock.
31+
*
32+
* A pointer returned by SystemGetDataPoolItem() or SystemGetDataPoolItemFull() does not pin the
33+
* item. The caller must guarantee that the same item is not removed or reused while that pointer
34+
* is being dereferenced. StackMemoryArena-backed pools remain subject to StackMemoryArena's
35+
* thread-local contract.
36+
*
37+
* @tparam T Primary item type.
38+
* @tparam TFull Optional secondary item type.
3639
*/
3740
template<typename T, typename TFull>
3841
struct SystemDataPool
3942
{
40-
SystemDataPoolStorage<T, TFull>* Storage; ///< Pointer to the underlying storage mechanism of the data pool.
43+
SystemDataPoolStorage<T, TFull>* Storage;
4144
};
4245

4346
/**
44-
* Unpack a data pool handle to a struct that contains the index and the version
45-
* @param packedValue The packed value to unpack.
46-
* @return The unpacked handle.
47-
*/
47+
* Unpacks a data-pool handle into its index and generation.
48+
*
49+
* @param packedValue Packed handle value.
50+
* @return Unpacked index and generation.
51+
*/
4852
SystemDataPoolHandle UnpackSystemDataPoolHandle(uint64_t packedValue);
4953

5054
/**
51-
* Creates and initializes a data pool capable of storing items of type T, with an optional fuller version of each item of type TFull.
52-
*
53-
* @tparam T The primary type of items to be stored in the data pool.
54-
* @tparam TFull The full data type associated with each item, defaulting to SystemDataPoolDefaultFull when not specified.
55-
* @param memoryArena The memory arena to use for allocating data pool storage.
56-
* @param maxItems The maximum number of items that the data pool can hold.
57-
* @return An instance of SystemDataPool configured to store items of type T and TFull.
55+
* Creates a fixed-capacity data pool.
56+
*
57+
* The storage is allocated from memoryArena and is not individually freed. maxItems must fit in the
58+
* 32-bit handle index space. The returned pool is empty when its backing storage cannot be created.
59+
*
60+
* @tparam T Primary item type.
61+
* @tparam TFull Optional secondary item type.
62+
* @param memoryArena Arena that owns the pool storage.
63+
* @param maxItems Maximum number of simultaneously allocated items.
64+
* @return Data pool backed by memoryArena, or an empty pool on allocation failure.
5865
*/
5966
template<typename T, typename TFull = SystemDataPoolDefaultFull>
6067
SystemDataPool<T, TFull> SystemCreateDataPool(MemoryArena memoryArena, size_t maxItems);
6168

6269
/**
63-
* Adds an item of type T to the specified data pool and returns a handle to the newly added item.
64-
*
65-
* @tparam T The type of the item to add to the data pool.
66-
* @tparam TFull The full data type associated with each item in the pool.
67-
* @param dataPool The data pool to which the item will be added.
68-
* @param data The item to add to the data pool.
69-
* @return A handle to the newly added item within the data pool.
70+
* Adds an item to the pool.
71+
*
72+
* The operation is thread-safe. A recycled slot receives the generation established by its previous
73+
* removal, so stale handles do not resolve to the new item.
74+
*
75+
* @return Handle to the added item, or ELEM_HANDLE_NULL when the pool is full.
7076
*/
7177
template<typename T, typename TFull>
7278
ElemHandle SystemAddDataPoolItem(SystemDataPool<T, TFull> dataPool, T data);
7379

7480
/**
75-
* Adds or updates the fuller version of an item in the data pool, identified by a given handle.
76-
*
77-
* @tparam T The primary type of items stored in the data pool.
78-
* @tparam TFull The full data type associated with each item.
79-
* @param dataPool The data pool containing the item.
80-
* @param handle The handle identifying the item to be extended with fuller data.
81-
* @param data The fuller version of the item to add or update in the data pool.
81+
* Writes the optional secondary data associated with an existing item.
82+
*
83+
* The handle generation is validated before the write. This function does not pin the item after
84+
* validation; the caller must not remove or reuse the same item concurrently with this write.
8285
*/
8386
template<typename T, typename TFull>
8487
void SystemAddDataPoolItemFull(SystemDataPool<T, TFull> dataPool, ElemHandle handle, TFull data);
8588

8689
/**
87-
* Removes an item from the data pool, identified by a given handle.
88-
*
89-
* @tparam T The primary type of items stored in the data pool.
90-
* @tparam TFull The full data type associated with each item.
91-
* @param dataPool The data pool from which the item will be removed.
92-
* @param handle The handle identifying the item to remove.
90+
* Removes an item and makes its slot available for reuse.
91+
*
92+
* The operation is thread-safe. Concurrent attempts to remove the same generation only free the
93+
* slot once; later attempts observe the generation change and are ignored.
9394
*/
9495
template<typename T, typename TFull>
9596
void SystemRemoveDataPoolItem(SystemDataPool<T, TFull> dataPool, ElemHandle handle);
9697

9798
/**
98-
* Retrieves a pointer to an item in the data pool, identified by a given handle.
99-
*
100-
* @tparam T The primary type of items stored in the data pool.
101-
* @tparam TFull The full data type associated with each item.
102-
* @param dataPool The data pool containing the item.
103-
* @param handle The handle identifying the item to retrieve.
104-
* @return A pointer to the item associated with the given handle, or nullptr if the item does not exist.
99+
* Resolves a handle to its primary item.
100+
*
101+
* The lookup itself is thread-safe and returns nullptr for a stale handle. The returned pointer is
102+
* non-owning and is not lifetime-protected against a later concurrent removal/reuse of the same item.
105103
*/
106104
template<typename T, typename TFull>
107105
T* SystemGetDataPoolItem(SystemDataPool<T, TFull> dataPool, ElemHandle handle);
108106

109107
/**
110-
* Retrieves a pointer to the fuller version of an item in the data pool, identified by a given handle.
111-
*
112-
* @tparam T The primary type of items stored in the data pool.
113-
* @tparam TFull The full data type associated with each item.
114-
* @param dataPool The data pool containing the item.
115-
* @param handle The handle identifying the item to retrieve its fuller version.
116-
* @return A pointer to the fuller version of the item associated with the given handle, or nullptr if the fuller data does not exist.
108+
* Resolves a handle to its secondary item data.
109+
*
110+
* The lookup itself is thread-safe and returns nullptr for a stale handle. The returned pointer is
111+
* non-owning and is not lifetime-protected against a later concurrent removal/reuse of the same item.
117112
*/
118113
template<typename T, typename TFull>
119114
TFull* SystemGetDataPoolItemFull(SystemDataPool<T, TFull> dataPool, ElemHandle handle);
120115

121116
/**
122-
* Counts the number of items in the data pool.
123-
*
124-
* @tparam T Primary type of items in the data pool.
125-
* @tparam TFull Full data type associated with each item.
126-
* @param dataPool The data pool whose items are to be counted.
127-
* @return The total count of items in the data pool.
117+
* Returns the current number of live items.
118+
*
119+
* The count is read atomically and may change immediately after the function returns when the pool
120+
* is being modified concurrently.
128121
*/
129122
template<typename T, typename TFull>
130123
size_t SystemGetDataPoolItemCount(SystemDataPool<T, TFull> dataPool);

0 commit comments

Comments
 (0)