34#define __Lock(name, element) auto __allocator_lock_##name##__ = Util::AllocatorLock(&name, element);
37#define __LockName(allocator, name, element) auto __allocator_lock_##name##__ = Util::AllocatorLock(allocator, element);
49 this->
didAcquire = this->allocator->Acquire(this->element);
63template <
uint MAX_ALLOCS = 0xFFFF,
class ... TYPES>
103 template <
int MEMBER>
107 template <
int MEMBER>
111 template <
int MEMBER>
115 template <
int MEMBER>
119 void Set(
const uint32_t index, TYPES...);
144 std::tuple<Util::PinnedArray<MAX_ALLOCS, TYPES>...>
objects;
153template<
uint MAX_ALLOCS,
class ...TYPES>
164template<
uint MAX_ALLOCS,
class ...TYPES>
168 this->allocationLock.Lock();
169 rhs.allocationLock.Lock();
170 this->objects = rhs.objects;
171 this->size = rhs.size;
174 this->allocationLock.Unlock();
175 rhs.allocationLock.Unlock();
181template<
uint MAX_ALLOCS,
class ...TYPES>
185 this->allocationLock.Lock();
187 this->size = rhs.
size;
188 this->allocationLock.Unlock();
194template<
uint MAX_ALLOCS,
class ...TYPES>
204template<
uint MAX_ALLOCS,
class ...TYPES>
208 this->allocationLock.Lock();
210 this->size = rhs.
size;
211 this->allocationLock.Unlock();
217template<
uint MAX_ALLOCS,
class ...TYPES>
221 this->allocationLock.Lock();
222 rhs.allocationLock.Lock();
223 this->objects = rhs.objects;
224 this->size = rhs.size;
227 this->allocationLock.Unlock();
228 rhs.allocationLock.Unlock();
235template<
uint MAX_ALLOCS,
class ...TYPES>
239 this->allocationLock.Lock();
241 auto i = this->size++;
242 this->owners.Append(Threading::Thread::GetMyThreadId());
243 this->allocationLock.Unlock();
250template<
uint MAX_ALLOCS,
class ...TYPES>
254 n_assert(this->owners[
id] == Threading::Thread::GetMyThreadId());
255 this->allocationLock.Lock();
257 this->allocationLock.Unlock();
264template<
uint MAX_ALLOCS,
class ...TYPES>
268 n_assert(this->owners[
id] == Threading::Thread::GetMyThreadId());
269 this->allocationLock.Lock();
271 this->allocationLock.Unlock();
278template<
uint MAX_ALLOCS,
class ...TYPES>
282 n_assert2(this->numReaders > 0,
"Size requires a read lock");
289template<
uint MAX_ALLOCS,
class ...TYPES>
293 this->allocationLock.Lock();
295 this->allocationLock.Unlock();
302template<
uint MAX_ALLOCS,
class ...TYPES>
306 this->allocationLock.Enter();
309 this->allocationLock.Leave();
315template<
uint MAX_ALLOCS,
class ...TYPES>
320 n_assert(this->owners[index] == Threading::Thread::GetMyThreadId());
321 return std::get<MEMBER>(this->objects)[index];
327template<
uint MAX_ALLOCS,
class ...TYPES>
334 return std::get<MEMBER>(this->objects)[index];
340template<
uint MAX_ALLOCS,
class ...TYPES>
347 return std::get<MEMBER>(this->objects)[index];
353template<
uint MAX_ALLOCS,
class ...TYPES>
358 n_assert(this->owners[index] == Threading::Thread::GetMyThreadId());
359 std::get<MEMBER>(this->objects)[index] = type;
365template<
uint MAX_ALLOCS,
class ...TYPES>
370 return std::get<MEMBER>(this->objects);
376template<
uint MAX_ALLOCS,
class ...TYPES>
381 return std::get<MEMBER>(this->objects);
387template<
uint MAX_ALLOCS,
class ...TYPES>
397template<
uint MAX_ALLOCS,
class ...TYPES>
401 this->size = this->GetArray<0>().Size();
407template<
uint MAX_ALLOCS,
class ...TYPES>
419template<
uint MAX_ALLOCS,
class ...TYPES>
424 if (this->owners[index] == myThread)
430 Threading::Thread::YieldThread();
438template<
uint MAX_ALLOCS,
class ...TYPES>
442 n_assert(this->owners[index] == Threading::Thread::GetMyThreadId());
The ArrayAllocatorSafe provides a thread safe variadic list of types which is to be contained in the ...
Definition arrayallocatorsafe.h:65
bool Acquire(const uint32_t index)
Acquire element, asserts if false and returns true if this call acquired.
Definition arrayallocatorsafe.h:421
uint32_t size
Definition arrayallocatorsafe.h:143
~ArrayAllocatorSafe()
destructor
Definition arrayallocatorsafe.h:196
void operator=(const ArrayAllocatorSafe< MAX_ALLOCS, TYPES... > &rhs)
assign operator
Definition arrayallocatorsafe.h:206
const uint32_t Size() const
get number of used indices
Definition arrayallocatorsafe.h:280
void Set(const uint32_t index, TYPES...)
set for each in tuple
Definition arrayallocatorsafe.h:389
uint32_t Alloc()
allocate a new resource
Definition arrayallocatorsafe.h:237
const tuple_array_t< MEMBER, TYPES... > & ConstGet(const uint32_t index) const
Get const explicitly.
Definition arrayallocatorsafe.h:330
void Reserve(uint32_t size)
grow capacity of arrays to size
Definition arrayallocatorsafe.h:291
Threading::Spinlock allocationLock
Definition arrayallocatorsafe.h:147
void EraseIndex(const uint32_t id)
Erase element for each.
Definition arrayallocatorsafe.h:252
void Set(const uint32_t index, const tuple_array_t< MEMBER, TYPES... > &type)
set single item
Definition arrayallocatorsafe.h:356
void Release(const uint32_t index)
Release an object, the next thread that acquires may use this instance as it fits.
Definition arrayallocatorsafe.h:440
Util::Array< tuple_array_t< MEMBER, TYPES... > > & GetArray()
get array
Definition arrayallocatorsafe.h:379
void UpdateSize()
Any reserve and direct array access might mess with the size.
Definition arrayallocatorsafe.h:399
ArrayAllocatorSafe(const ArrayAllocatorSafe< MAX_ALLOCS, TYPES... > &rhs)
copy constructor
Definition arrayallocatorsafe.h:183
Util::PinnedArray< MAX_ALLOCS, Threading::ThreadId > owners
Definition arrayallocatorsafe.h:145
tuple_array_t< MEMBER, TYPES... > & Get(const uint32_t index)
get single item from resource
Definition arrayallocatorsafe.h:318
std::tuple< Util::PinnedArray< MAX_ALLOCS, TYPES >... > objects
Definition arrayallocatorsafe.h:144
void operator=(ArrayAllocatorSafe< MAX_ALLOCS, TYPES... > &&rhs)
move operator
Definition arrayallocatorsafe.h:219
ArrayAllocatorSafe()
constructor
Definition arrayallocatorsafe.h:155
void EraseIndexSwap(const uint32_t id)
Erase element for each.
Definition arrayallocatorsafe.h:266
const Util::Array< tuple_array_t< MEMBER, TYPES... > > & GetArray() const
get array const reference
Definition arrayallocatorsafe.h:368
void TryAcquire(const uint32_t index)
Spinlock to acquire.
Definition arrayallocatorsafe.h:409
const tuple_array_t< MEMBER, TYPES... > & Get(const uint32_t index) const
same as 32 bit get, but const
Definition arrayallocatorsafe.h:343
void Clear()
clear entire allocator and start from scratch.
Definition arrayallocatorsafe.h:304
ArrayAllocatorSafe(ArrayAllocatorSafe< MAX_ALLOCS, TYPES... > &&rhs)
move constructor
Definition arrayallocatorsafe.h:166
Nebula's dynamic array class.
Definition array.h:60
Definition pinnedarray.h:17
#define n_assert2(exp, msg)
Definition debug.h:51
#define n_assert(exp)
Definition debug.h:50
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
static const ThreadId InvalidThreadId
Definition linuxthreadid.h:16
pthread_t ThreadId
Definition linuxthreadid.h:15
int64_t ThreadIdStorage
Definition linuxthreadid.h:17
A pinned array is an array which manages its own virtual memory.
Definition String.cs:6
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:207
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:294
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:146
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:166
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:274
get_template_type_t< std::tuple_element_t< MEMBER, std::tuple< Util::Array< TYPES >... > > > tuple_array_t
Get type of contained element in Util::Array stored in std::tuple.
Definition tupleutility.h:334
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:231
Definition arrayallocatorsafe.h:44
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
unsigned int uint
Definition types.h:31