24#ifndef _GEMA_THREAD_LOCAL_STORAGE_H_
25#define _GEMA_THREAD_LOCAL_STORAGE_H_
35template <
class T>
struct GmTLS_DataT<T, false> {
typedef T BaseT; T _value; };
40template <
class T>
struct GmTLS_DataT<T, true> {
typedef T BaseT; GM_CACHE_ALIGNED T _value; };
52 static_assert(
sizeof(DataT) ==
sizeof(
typename DataT::BaseT),
"Unexpected type size for unaligned GmTLS_Alloc");
62 static void free(DataT* ptr) {
delete[] ptr; }
68 static_assert(
sizeof(DataT) %
GM_CACHE_LINE_SIZE == 0,
"Unexpected type size for aligned GmTLS_Alloc");
85 static void free(DataT* ptr)
131template <
class T,
bool Align = false>
class GmTLS
147 GmTLS(
const T& defVal) { _data = Alloc::alloc();
init(defVal); }
156 _data[i]._value = val;
166 return _data[tid]._value;
173 return _data[tid]._value;
183 void setLocalData(
int tid,
const T& data) { _data[tid]._value = data; }
189 Q_DISABLE_COPY(
GmTLS)
A class that works together with GmThreadManager to provide thread local storage.
Definition gmThreadLocalStorage.h:132
~GmTLS()
Destructor.
Definition gmThreadLocalStorage.h:150
T & localData(int tid)
Returns the given thread local data as a modifiable reference.
Definition gmThreadLocalStorage.h:163
const T & localData() const
Returns the current thread local data as a const reference.
Definition gmThreadLocalStorage.h:180
void setLocalData(int tid, const T &data)
Updates the given thread local data.
Definition gmThreadLocalStorage.h:183
GmTLS()
Creates a new thread local storage, with default constructed values.
Definition gmThreadLocalStorage.h:144
int size() const
Returns the number of values stored in the TLS object. Equal to the max number of threads + 1 (for th...
Definition gmThreadLocalStorage.h:160
void init(const T &val)
Initializes the value for ALL threads with the given value.
Definition gmThreadLocalStorage.h:153
T & localData()
Returns the current thread local data as a modifiable reference.
Definition gmThreadLocalStorage.h:177
GmTLS(const T &defVal)
Creates a new thread local storage with values initialized to the given default value.
Definition gmThreadLocalStorage.h:147
void setLocalData(const T &data)
Updates the current thread local data.
Definition gmThreadLocalStorage.h:186
const T & localData(int tid) const
Returns the given thread local data as a const reference.
Definition gmThreadLocalStorage.h:170
static int currentId()
Returns the id of the current thread, which MUST be either the main thread or a thread created by the...
Definition gmThreadManager.h:171
static int maxWorkerThreads()
Returns the maximum number of allowed working threads.
Definition gmThreadManager.h:158
Implementation of the custom allocator used by Armadillo when memory is needed for matrices and vecto...
#define GM_CACHE_LINE_SIZE
Cache line size. TODO: Get this from a configuration parameter.
Definition gmMemory.h:35
void GmDestroyAlignedObjectsInMemory(T *ptr, int align, int n)
Calls T destructor for 'n' aligned objects in the memory area pointed by ptr.
Definition gmMemory.h:154
void GmConstructAlignedObjectsInMemory(T *ptr, int align, int n)
Calls the default T constructor for creating 'n' aligned objects in the memory area pointed by ptr.
Definition gmMemory.h:129
bool GmIsCacheAligned(void *ptr)
Checks if a pointer is cache aligned or not.
Definition gmMemory.h:81
void * GmAlignedMallocThrow(size_t size, size_t align)
A version of GmAlignedMalloc that throws a bad_alloc exception on failure.
Definition gmMemory.h:65
Declaration of the GmThreadManager class.
static DataT * alloc()
Alloc a DataT vector of size equal to the number of threads + 1.
Definition gmThreadLocalStorage.h:55
static void free(DataT *ptr)
Releases the memory allocated by alloc.
Definition gmThreadLocalStorage.h:62
static void free(DataT *ptr)
Releases the memory allocated by alloc.
Definition gmThreadLocalStorage.h:85
static DataT * alloc()
Alloc a DataT vector of size equal to the number of threads + 1.
Definition gmThreadLocalStorage.h:71
Aux structure to define the allocation strategy for GmTLS.
Definition gmThreadLocalStorage.h:47
Aux structure to define the data type, cache aligned or not, for the GmTLS class.
Definition gmThreadLocalStorage.h:32