Changeset 371 for cpp/common
- Timestamp:
- 04/21/15 15:19:47 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/common/threads.h
r348 r371 92 92 #else //ifdef MULTITHREADED 93 93 94 template<class T> class ThreadSingleton 95 { 96 T object; 94 #define tlsGet(var) (&ThreadLocal_ ## var ## _getref()) 95 #define tlsGetRef(var) ThreadLocal_ ## var ## _getref() 96 #define tlsSet(var,o) var.set(o) 97 97 98 public: 99 100 T* get() {return &object;} 101 T& getref() {return object;} 102 }; 98 #define THREAD_LOCAL_DECL(cls,var) cls& ThreadLocal_ ## var ## _getref() 99 #define THREAD_LOCAL_DEF(cls,var) cls& ThreadLocal_ ## var ## _getref() \ 100 { \ 101 static cls object; \ 102 return object; \ 103 } 103 104 104 105 template<class T> class ThreadSingletonPtr 105 106 { 106 107 T *object; 107 108 108 public: 109 110 109 ThreadSingletonPtr():object(NULL) {} 111 110 T* get() {return object;} … … 119 118 // THREAD_LOCAL(cls) - behaves like object of class cls (automatic creation/destruction) 120 119 // THREAD_LOCAL(cls)..._PTR - behaves like pointer to cls (initial NULL, no autocreation/destruction) 120 // var is a (global) name, can't be a class member anymore because it might be implemented as function 121 121 // _PTR can only be accessed using tls...Ptr() variant of Get/Set, _ptr suffix is internally used in variable name to avoid mistakes 122 122 #ifdef USE_CPP_TLS … … 124 124 // use c++ implementation (CPP_TLS must also be defined) 125 125 126 #define THREAD_LOCAL_DECL(cls,var) CPP_TLS cls* var127 #define THREAD_LOCAL_DEF(cls,var) CPP_TLS cls* var=NULL128 #define THREAD_LOCAL_DECL_PTR(cls,var) CPP_TLS cls* var ## _ptr129 #define THREAD_LOCAL_DEF_PTR(cls,var) CPP_TLS cls* var ## _ptr=NULL126 #define THREAD_LOCAL_DECL(cls,var) CPP_TLS cls* ThreadLocal_ ## var 127 #define THREAD_LOCAL_DEF(cls,var) CPP_TLS cls* ThreadLocal_ ## var=NULL 128 #define THREAD_LOCAL_DECL_PTR(cls,var) CPP_TLS cls* ThreadLocal_ ## var ## _ptr 129 #define THREAD_LOCAL_DEF_PTR(cls,var) CPP_TLS cls* ThreadLocal_ ## var ## _ptr=NULL 130 130 131 template<class T> T* tlsGet(T*& var)131 template<class T> T* _tlsGet(T*& var) 132 132 { 133 133 if (!var) … … 136 136 } 137 137 138 template<class T> T* tlsGetPtr(T*& var)138 template<class T> T* _tlsGetPtr(T*& var) 139 139 { 140 140 return var; 141 141 } 142 142 143 #define tlsGetRef(var) (*tlsGet(var)) 143 #define tlsGet(var) _tlsGet(ThreadLocal_ ## var) 144 #define tlsGetRef(var) (*_tlsGet(ThreadLocal_ ## var)) 144 145 145 template<class T> T* tlsSet(T*& var,T* new_o)146 template<class T> T* _tlsSet(T*& var,T* new_o) 146 147 { 147 148 T* o=var; … … 150 151 } 151 152 152 #define tlsGetSetPtr(var,o) tlsSet(var ## _ptr,o) 153 #define tlsGetPtr(var) _tlsGetPtr(ThreadLocal_ ## var) 154 #define tlsSetPtr(var,o) _tlsSet(ThreadLocal_ ## var ## _ptr,o) 153 155 154 #else 156 #else //#ifdef USE_CPP_TLS 155 157 156 158 // use pthreads implementation 157 159 158 #define THREAD_LOCAL_DECL(cls,var) ThreadSingleton<cls> var 159 #define THREAD_LOCAL_DEF(cls,var) ThreadSingleton<cls> var 160 #define tlsGet(var) var.get() 161 #define tlsGetRef(var) var.getref() 162 #define tlsSet(var,o) var.set(o) 160 #ifdef MULTITHREADED 161 #define THREAD_LOCAL_DECL(cls,var) ThreadSingleton<cls> ThreadLocal_ ## var 162 #define THREAD_LOCAL_DEF(cls,var) ThreadSingleton<cls> ThreadLocal_ ## var 163 #define tlsGet(var) ThreadLocal_ ## var.get() 164 #define tlsGetRef(var) ThreadLocal_ ## var.getref() 165 #define tlsSet(var,o) ThreadLocal_ ## var.set(o) 166 #endif 163 167 164 #define THREAD_LOCAL_DECL_PTR(cls,var) ThreadSingletonPtr<cls>var ## _ptr165 #define THREAD_LOCAL_DEF_PTR(cls,var) ThreadSingletonPtr<cls> var ## _ptr166 #define tlsGetPtr(var) var ## _ptr.get()167 #define tlsSetPtr(var,o) var ## _ptr.setptr(o)168 #define THREAD_LOCAL_DECL_PTR(cls,var) extern ThreadSingletonPtr<cls> ThreadLocal_ ## var ## _ptr 169 #define THREAD_LOCAL_DEF_PTR(cls,var) ThreadSingletonPtr<cls> ThreadLocal_ ## var ## _ptr 170 #define tlsGetPtr(var) ThreadLocal_ ## var ## _ptr.get() 171 #define tlsSetPtr(var,o) ThreadLocal_ ## var ## _ptr.setptr(o) 168 172 169 #endif 173 #endif //#ifdef USE_CPP_TLS 170 174 171 175
Note: See TracChangeset
for help on using the changeset viewer.