26 #ifndef STM_COMMON_THREAD_LOCAL_H
27 #define STM_COMMON_THREAD_LOCAL_H
29 #include <stm/config.h>
51 #if defined(STM_TLS_PTHREAD)
52 # define THREAD_LOCAL_DECL_TYPE(X) stm::tls::ThreadLocal<X, sizeof(X)>
53 #elif defined(STM_OS_WINDOWS)
54 # define THREAD_LOCAL_DECL_TYPE(X) __declspec(thread) X
55 #elif defined(STM_CC_GCC) || defined(STM_CC_SUN)
56 # define THREAD_LOCAL_DECL_TYPE(X) __thread X
58 # warning "No thread local implementation defined."
67 #if defined(STM_TLS_PTHREAD)
81 class PThreadLocalImplementation
84 PThreadLocalImplementation(
void*
const v)
86 pthread_key_create(&key, NULL);
87 pthread_setspecific(key, v);
90 virtual ~PThreadLocalImplementation() { pthread_key_delete(key); }
91 void* getValue()
const {
return pthread_getspecific(key); }
92 void setValue(
void*
const v) { pthread_setspecific(key, v); }
99 PThreadLocalImplementation();
100 PThreadLocalImplementation(
const PThreadLocalImplementation&);
102 PThreadLocalImplementation&
103 operator=(
const PThreadLocalImplementation&);
150 template <
typename T,
unsigned S>
151 class ThreadLocal :
public PThreadLocalImplementation
154 ThreadLocal() : PThreadLocalImplementation(new T()) { }
156 ThreadLocal(T t) : PThreadLocalImplementation(new T())
158 __builtin_memcpy(getValue(), &t, S);
161 virtual ~ThreadLocal() {
delete static_cast<T*
>(getValue()); }
163 T* operator&()
const {
return static_cast<T*
>(getValue()); }
169 ThreadLocal(
const ThreadLocal<T, S>&);
170 ThreadLocal<T, S>& operator=(
const ThreadLocal<T, S>&);
178 template <
typename T>
179 class ThreadLocal<T, sizeof(void*)> :
public PThreadLocalImplementation
182 ThreadLocal() : PThreadLocalImplementation(NULL) { }
184 ThreadLocal(T t) : PThreadLocalImplementation(NULL)
186 __builtin_memcpy(getValue(), &t,
sizeof(T));
189 T* operator&()
const {
return static_cast<T*
>(getValue()); }
195 ThreadLocal(
const ThreadLocal<T,
sizeof(
void*)>&);
197 ThreadLocal<T,sizeof(void*)>&
198 operator=(
const ThreadLocal<T,
sizeof(
void*)>&);
218 template <
typename T>
219 class ThreadLocal<T*,
sizeof(
void*)> :
public PThreadLocalImplementation
222 ThreadLocal(T* t = NULL) : PThreadLocalImplementation(t) { }
224 virtual ~ThreadLocal() { }
227 const T& operator*()
const {
return *
static_cast<T*
>(getValue()); }
228 const T* operator->()
const {
return static_cast<T*
>(getValue()); }
229 T& operator*() {
return *
static_cast<T*
>(getValue()); }
230 T* operator->() {
return static_cast<T*
>(getValue()); }
231 operator T*() {
return static_cast<T*
>(getValue()); }
234 ThreadLocal<T*, sizeof(void*)>& operator=(T* rhs) {
239 bool operator==(T* rhs) {
return (getValue() == rhs); }
246 ThreadLocal(
const ThreadLocal<T*,
sizeof(T*)>&);
248 ThreadLocal<T*, sizeof(T*)>&
249 operator=(
const ThreadLocal<T*,
sizeof(T*)>&);
255 #endif // STM_COMMON_THREAD_LOCAL_H
Definition: stm_fraser.c:61