Nebula
Loading...
Searching...
No Matches
arrayallocatorsafe.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
24//------------------------------------------------------------------------------
25#include "core/types.h"
26#include "util/pinnedarray.h"
28#include "threading/spinlock.h"
29#include <tuple>
30#include "tupleutility.h"
31#include "ids/id.h"
32
33// use this macro to safetly access an array allocator from within a scope
34#define __Lock(name, element) auto __allocator_lock_##name##__ = Util::AllocatorLock(&name, element);
35
36// use this macro when we need to retrieve an allocator and lock it with an explicit name
37#define __LockName(allocator, name, element) auto __allocator_lock_##name##__ = Util::AllocatorLock(&allocator, element);
38
39namespace Util
40{
41
42template<class T>
44{
48 {
49 this->didAcquire = this->allocator->Acquire(this->element);
50 };
51
53 {
54 if (this->didAcquire)
55 this->allocator->Release(this->element);
56 }
57
59 uint32_t element;
61};
62
63template <uint MAX_ALLOCS = 0xFFFF, class ... TYPES>
65{
66 static_assert(soa_names_unique<TYPES...>::value, "NamedSlot names must be unique within an ArrayAllocatorSafe");
67
68public:
71
74
77
80
83
86
88 uint32_t Alloc();
89
91 void EraseIndex(const uint32_t id);
92
94 void EraseIndexSwap(const uint32_t id);
95
97 template <int MEMBER>
98 tuple_array_t<MEMBER, TYPES...>& Get(const uint32_t index);
99
101 template <int MEMBER>
102 const tuple_array_t<MEMBER, TYPES...>& ConstGet(const uint32_t index) const;
103
105 template <int MEMBER>
106 const tuple_array_t<MEMBER, TYPES...>& Get(const uint32_t index) const;
107
109 template <int MEMBER>
110 void Set(const uint32_t index, const tuple_array_t<MEMBER, TYPES...>& type);
111
113 template <int MEMBER>
114 const Util::Array<tuple_array_t<MEMBER, TYPES...>>& GetArray() const;
115
117 template <int MEMBER>
118 Util::Array<tuple_array_t<MEMBER, TYPES...>>& GetArray();
119
121 void Set(const uint32_t index, TYPES...);
122
124 const uint32_t Size() const;
125
127 void Reserve(uint32_t size);
128
130 void Clear();
131
135
137 void TryAcquire(const uint32_t index);
139 bool Acquire(const uint32_t index);
141 void Release(const uint32_t index);
142
143protected:
144
145 uint32_t size;
146 std::tuple<Util::PinnedArray<MAX_ALLOCS, soa_value_type_t<TYPES>>...> objects;
148
150};
151
152//------------------------------------------------------------------------------
155template<uint MAX_ALLOCS, class ...TYPES>
156inline
162
163//------------------------------------------------------------------------------
166template<uint MAX_ALLOCS, class ...TYPES>
167inline
169{
170 bool locked = this->allocationLock.Lock();
171 bool rhsLocked = rhs.allocationLock.Lock();
172 this->objects = rhs.objects;
173 this->size = rhs.size;
174 rhs.Clear();
175
176 if (locked) this->allocationLock.Unlock();
177 if (rhsLocked) rhs.allocationLock.Unlock();
178}
179
180//------------------------------------------------------------------------------
183template<uint MAX_ALLOCS, class ...TYPES>
184inline
186{
187 bool locked = this->allocationLock.Lock();
188 this->objects = rhs.objects;
189 this->size = rhs.size;
190 if (locked) this->allocationLock.Unlock();
191}
192
193//------------------------------------------------------------------------------
196template<uint MAX_ALLOCS, class ...TYPES>
197inline
202
203//------------------------------------------------------------------------------
206template<uint MAX_ALLOCS, class ...TYPES>
207inline void
209{
210 bool locked = this->allocationLock.Lock();
211 this->objects = rhs.objects;
212 this->size = rhs.size;
213 if (locked) this->allocationLock.Unlock();
214}
215
216//------------------------------------------------------------------------------
219template<uint MAX_ALLOCS, class ...TYPES>
220inline void
222{
223 bool locked = this->allocationLock.Lock();
224 bool rhsLocked = rhs.allocationLock.Lock();
225 this->objects = rhs.objects;
226 this->size = rhs.size;
227 rhs.Clear();
228
229 if (locked) this->allocationLock.Unlock();
230 if (rhsLocked) rhs.allocationLock.Unlock();
231}
232
233//------------------------------------------------------------------------------
237template<uint MAX_ALLOCS, class ...TYPES>
238inline uint32_t
240{
241 bool locked = this->allocationLock.Lock();
243 auto i = this->size++;
244 this->owners.Append(Threading::Thread::GetMyThreadId());
245 if (locked) this->allocationLock.Unlock();
246 return i;
247}
248
249//------------------------------------------------------------------------------
252template<uint MAX_ALLOCS, class ...TYPES>
253inline void
255{
256 n_assert(this->owners[id] == Threading::Thread::GetMyThreadId());
257 this->allocationLock.Lock();
259 this->allocationLock.Unlock();
260 this->size--;
261}
262
263//------------------------------------------------------------------------------
266template<uint MAX_ALLOCS, class ...TYPES>
267inline void
269{
270 n_assert(this->owners[id] == Threading::Thread::GetMyThreadId());
271 this->allocationLock.Lock();
273 this->allocationLock.Unlock();
274 this->size--;
275}
276
277//------------------------------------------------------------------------------
280template<uint MAX_ALLOCS, class ...TYPES>
281inline const uint32_t
283{
284 return this->size;
285}
286
287//------------------------------------------------------------------------------
290template<uint MAX_ALLOCS, class ...TYPES>
291inline void
293{
294 this->allocationLock.Lock();
296 this->allocationLock.Unlock();
297 // Size is still the same.
298}
299
300//------------------------------------------------------------------------------
303template<uint MAX_ALLOCS, class ...TYPES>
304inline void
306{
307 this->allocationLock.Lock();
309 this->size = 0;
310 this->allocationLock.Unlock();
311}
312
313//------------------------------------------------------------------------------
316template<uint MAX_ALLOCS, class ...TYPES>
317template<int MEMBER>
318inline tuple_array_t<MEMBER, TYPES...>&
320{
321 n_assert(this->owners[index] == Threading::Thread::GetMyThreadId());
322 return std::get<soa_tuple_index_v<MEMBER, TYPES...>>(this->objects)[index];
323}
324
325//------------------------------------------------------------------------------
328template<uint MAX_ALLOCS, class ...TYPES>
329template<int MEMBER>
330inline const tuple_array_t<MEMBER, TYPES...>&
332{
333 // Allow const get when no thread is owning the element as well
334 n_assert(this->owners[index] == Threading::Thread::GetMyThreadId() || this->owners[index] == Threading::InvalidThreadId);
335 return std::get<soa_tuple_index_v<MEMBER, TYPES...>>(this->objects)[index];
336}
337
338//------------------------------------------------------------------------------
341template<uint MAX_ALLOCS, class ...TYPES>
342template<int MEMBER>
343inline const tuple_array_t<MEMBER, TYPES...>&
345{
346 // Allow const get when no thread is owning the element as well
347 n_assert(this->owners[index] == Threading::Thread::GetMyThreadId() || this->owners[index] == Threading::InvalidThreadId);
348 return std::get<soa_tuple_index_v<MEMBER, TYPES...>>(this->objects)[index];
349}
350
351//------------------------------------------------------------------------------
354template<uint MAX_ALLOCS, class ...TYPES>
355template<int MEMBER>
356inline void
358{
359 n_assert(this->owners[index] == Threading::Thread::GetMyThreadId());
360 std::get<soa_tuple_index_v<MEMBER, TYPES...>>(this->objects)[index] = type;
361}
362
363//------------------------------------------------------------------------------
366template<uint MAX_ALLOCS, class ...TYPES>
367template<int MEMBER>
368inline const Util::Array<tuple_array_t<MEMBER, TYPES...>>&
370{
371 return std::get<soa_tuple_index_v<MEMBER, TYPES...>>(this->objects);
372}
373
374//------------------------------------------------------------------------------
377template<uint MAX_ALLOCS, class ...TYPES>
378template<int MEMBER>
379inline Util::Array<tuple_array_t<MEMBER, TYPES...>>&
381{
382 return std::get<soa_tuple_index_v<MEMBER, TYPES...>>(this->objects);
383}
384
385//------------------------------------------------------------------------------
388template<uint MAX_ALLOCS, class ...TYPES>
389void
390ArrayAllocatorSafe<MAX_ALLOCS, TYPES...>::Set(const uint32_t index, TYPES... values)
391{
392 set_for_each_in_tuple(this->objects, index, values...);
393}
394
395//------------------------------------------------------------------------------
398template<uint MAX_ALLOCS, class ...TYPES>
399void
404
405//------------------------------------------------------------------------------
408template<uint MAX_ALLOCS, class ...TYPES>
409void
411{
412 Threading::ThreadId myThread = Threading::Thread::GetMyThreadId();
413 Threading::ThreadId currentThread = Threading::Interlocked::CompareExchange((volatile Threading::ThreadIdStorage*)&this->owners[index], myThread, Threading::InvalidThreadId);
414 n_assert(currentThread == Threading::InvalidThreadId);
415}
416
417//------------------------------------------------------------------------------
420template<uint MAX_ALLOCS, class ...TYPES>
421bool
423{
424 Threading::ThreadId myThread = Threading::Thread::GetMyThreadId();
425 if (this->owners[index] == myThread)
426 return false;
427
428 // Spinlock
429 while (Threading::Interlocked::CompareExchange((volatile Threading::ThreadIdStorage*)&this->owners[index], myThread, Threading::InvalidThreadId) != Threading::InvalidThreadId)
430 {
431 Threading::Thread::YieldThread();
432 };
433 return true;
434}
435
436//------------------------------------------------------------------------------
439template<uint MAX_ALLOCS, class ...TYPES>
440void
442{
443 n_assert(this->owners[index] == Threading::Thread::GetMyThreadId());
444 Threading::Interlocked::Exchange((volatile Threading::ThreadIdStorage*)&this->owners[index], Threading::InvalidThreadId);
445}
446
447} // namespace Util
Definition spinlock.h:21
bool Acquire(const uint32_t index)
Acquire element, asserts if false and returns true if this call acquired.
Definition arrayallocatorsafe.h:422
uint32_t size
Definition arrayallocatorsafe.h:145
~ArrayAllocatorSafe()
destructor
Definition arrayallocatorsafe.h:198
void operator=(const ArrayAllocatorSafe< MAX_ALLOCS, TYPES... > &rhs)
assign operator
Definition arrayallocatorsafe.h:208
const uint32_t Size() const
get number of used indices
Definition arrayallocatorsafe.h:282
uint32_t Alloc()
allocate a new resource
Definition arrayallocatorsafe.h:239
const tuple_array_t< MEMBER, TYPES... > & ConstGet(const uint32_t index) const
Get const explicitly.
Definition arrayallocatorsafe.h:331
void Reserve(uint32_t size)
grow capacity of arrays to size
Definition arrayallocatorsafe.h:292
Threading::Spinlock allocationLock
Definition arrayallocatorsafe.h:149
void EraseIndex(const uint32_t id)
Erase element for each.
Definition arrayallocatorsafe.h:254
void Set(const uint32_t index, const tuple_array_t< MEMBER, TYPES... > &type)
set single item
Definition arrayallocatorsafe.h:357
void Release(const uint32_t index)
Release an object, the next thread that acquires may use this instance as it fits.
Definition arrayallocatorsafe.h:441
void UpdateSize()
Any reserve and direct array access might mess with the size.
Definition arrayallocatorsafe.h:400
Util::PinnedArray< MAX_ALLOCS, Threading::ThreadId > owners
Definition arrayallocatorsafe.h:147
tuple_array_t< MEMBER, TYPES... > & Get(const uint32_t index)
get single item from resource
Definition arrayallocatorsafe.h:319
ArrayAllocatorSafe()
constructor
Definition arrayallocatorsafe.h:157
void EraseIndexSwap(const uint32_t id)
Erase element for each.
Definition arrayallocatorsafe.h:268
const Util::Array< tuple_array_t< MEMBER, TYPES... > > & GetArray() const
get array const reference
Definition arrayallocatorsafe.h:369
void TryAcquire(const uint32_t index)
Spinlock to acquire.
Definition arrayallocatorsafe.h:410
void Clear()
clear entire allocator and start from scratch.
Definition arrayallocatorsafe.h:305
std::tuple< Util::PinnedArray< MAX_ALLOCS, soa_value_type_t< TYPES > >... > objects
Definition arrayallocatorsafe.h:146
Nebula's dynamic array class.
Definition array.h:61
Definition pinnedarray.h:17
Set()
default constructor
Definition set.h:79
#define n_assert(exp)
Definition debug.h:58
int Exchange(int volatile *dest, int value)
interlocked exchange
Definition gccinterlocked.cc:94
int CompareExchange(int volatile *dest, int exchange, int comparand)
interlocked compare-exchange
Definition gccinterlocked.cc:112
pthread_t ThreadId
Definition linuxthreadid.h:15
int64_t ThreadIdStorage
Definition linuxthreadid.h:17
A quad tree designed to return regions of free 2D space.
Definition Random.cs:5
void erase_index_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t i, std::index_sequence< Is... >)
Entry point for erasing an element.
Definition tupleutility.h:368
void reserve_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t size, std::index_sequence< Is... >)
Entry point for reserving in each array.
Definition tupleutility.h:455
void alloc_for_each_in_tuple(std::tuple< Ts... > &tuple, std::index_sequence< Is... >)
Unpacks allocations for each member in a tuble.
Definition tupleutility.h:307
void clear_for_each_in_tuple(std::tuple< Ts... > &tuple, std::index_sequence< Is... >)
Unpacks allocations for each member in a tuple.
Definition tupleutility.h:327
void set_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t i, std::index_sequence< Is... >, TYPES const &... values)
Entry point for setting values in each array at an index.
Definition tupleutility.h:435
constexpr int soa_tuple_index_v
Definition tupleutility.h:167
void erase_index_swap_for_each_in_tuple(std::tuple< Ts... > &tuple, uint32_t i, std::index_sequence< Is... >)
Entry point for erasing an element by swapping with the last and reducing size.
Definition tupleutility.h:392
soa_value_type_t< std::tuple_element_t< soa_tuple_index_v< MEMBER, TYPES... >, std::tuple< TYPES... > > > tuple_array_t
Get type of contained element in Util::Array stored in std::tuple.
Definition tupleutility.h:495
T * allocator
Definition arrayallocatorsafe.h:60
bool didAcquire
Definition arrayallocatorsafe.h:58
uint32_t element
Definition arrayallocatorsafe.h:59
~AllocatorLock()
Definition arrayallocatorsafe.h:52
AllocatorLock(T *allocator, uint32_t element)
Definition arrayallocatorsafe.h:45
Definition tupleutility.h:121
unsigned int uint
Definition types.h:41