24 #ifndef _GEMA_THREAD_LOCAL_STORAGE_H_ 25 #define _GEMA_THREAD_LOCAL_STORAGE_H_ 35 template <
class T>
struct GmTLS_DataT<T, false> {
typedef T BaseT; T _value; };
40 template <
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");
80 GmConstructAlignedObjectsInMemory<typename DataT::BaseT>((
typename DataT::BaseT*)ptr,
GM_CACHE_LINE_SIZE, nt);
85 static void free(DataT* ptr)
90 GmDestroyAlignedObjectsInMemory<typename DataT::BaseT>((
typename DataT::BaseT*)ptr,
GM_CACHE_LINE_SIZE, nt);
131 template <
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)
#define GM_CACHE_LINE_SIZE
Cache line size. TODO: Get this from a configuration parameter.
Definition: gmMemory.h:35
void * GmAlignedMallocThrow(size_t size, size_t align)
A version of GmAlignedMalloc that throws a bad_alloc exception on failure.
Definition: gmMemory.h:65
~GmTLS()
Destructor.
Definition: gmThreadLocalStorage.h:150
A class that works together with GmThreadManager to provide thread local storage.
Definition: gmThreadLocalStorage.h:131
void setLocalData(const T &data)
Updates the current thread local data.
Definition: gmThreadLocalStorage.h:186
bool GmIsCacheAligned(void *ptr)
Checks if a pointer is cache aligned or not.
Definition: gmMemory.h:81
Declaration of the GmThreadManager class.
GmTLS(const T &defVal)
Creates a new thread local storage with values initialized to the given default value.
Definition: gmThreadLocalStorage.h:147
T & localData(int tid)
Returns the given thread local data as a modifiable reference.
Definition: gmThreadLocalStorage.h:163
GmTLS()
Creates a new thread local storage, with default constructed values.
Definition: gmThreadLocalStorage.h:144
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:166
void init(const T &val)
Initializes the value for ALL threads with the given value.
Definition: gmThreadLocalStorage.h:153
static void free(DataT *ptr)
Releases the memory allocated by alloc.
Definition: gmThreadLocalStorage.h:62
const T & localData(int tid) const
Returns the given thread local data as a const reference.
Definition: gmThreadLocalStorage.h:170
static DataT * alloc()
Alloc a DataT vector of size equal to the number of threads + 1.
Definition: gmThreadLocalStorage.h:71
void setLocalData(int tid, const T &data)
Updates the given thread local data.
Definition: gmThreadLocalStorage.h:183
T & localData()
Returns the current thread local data as a modifiable reference.
Definition: gmThreadLocalStorage.h:177
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:85
Aux structure to define the data type, cache aligned or not, for the GmTLS class.
Definition: gmThreadLocalStorage.h:32
Implementation of the custom allocator used by Armadillo when memory is needed for matrices and vecto...
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
const T & localData() const
Returns the current thread local data as a const reference.
Definition: gmThreadLocalStorage.h:180
static int maxWorkerThreads()
Returns the maximum number of allowed working threads.
Definition: gmThreadManager.h:153
Aux structure to define the allocation strategy for GmTLS.
Definition: gmThreadLocalStorage.h:47