Changeset 371 for cpp/common


Ignore:
Timestamp:
04/21/15 15:19:47 (10 years ago)
Author:
Maciej Komosinski
Message:

THREAD_LOCAL uses ThreadLocal_ prefix for variables. The static object inside the single-threaded implementation is no longer associated with a class.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/threads.h

    r348 r371  
    9292#else //ifdef MULTITHREADED
    9393
    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)
    9797
    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        }
    103104
    104105template<class T> class ThreadSingletonPtr
    105106{
    106107T *object;
    107 
    108108  public:
    109 
    110109ThreadSingletonPtr():object(NULL) {}
    111110T* get() {return object;}
     
    119118// THREAD_LOCAL(cls) - behaves like object of class cls (automatic creation/destruction)
    120119// 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
    121121// _PTR can only be accessed using tls...Ptr() variant of Get/Set, _ptr suffix is internally used in variable name to avoid mistakes
    122122#ifdef USE_CPP_TLS
     
    124124// use c++ implementation (CPP_TLS must also be defined)
    125125
    126 #define THREAD_LOCAL_DECL(cls,var) CPP_TLS cls* var
    127 #define THREAD_LOCAL_DEF(cls,var) CPP_TLS cls* var=NULL
    128 #define THREAD_LOCAL_DECL_PTR(cls,var) CPP_TLS cls* var ## _ptr
    129 #define THREAD_LOCAL_DEF_PTR(cls,var) CPP_TLS cls* var ## _ptr=NULL
     126#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
    130130
    131 template<class T> T* tlsGet(T*& var)
     131template<class T> T* _tlsGet(T*& var)
    132132{
    133133if (!var)
     
    136136}
    137137
    138 template<class T> T* tlsGetPtr(T*& var)
     138template<class T> T* _tlsGetPtr(T*& var)
    139139{
    140140return var;
    141141}
    142142
    143 #define tlsGetRef(var) (*tlsGet(var))
     143#define tlsGet(var) _tlsGet(ThreadLocal_ ## var)
     144#define tlsGetRef(var) (*_tlsGet(ThreadLocal_ ## var))
    144145
    145 template<class T> T* tlsSet(T*& var,T* new_o)
     146template<class T> T* _tlsSet(T*& var,T* new_o)
    146147{
    147148T* o=var;
     
    150151}
    151152
    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)
    153155
    154 #else
     156#else //#ifdef USE_CPP_TLS
    155157
    156158// use pthreads implementation
    157159
    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
    163167
    164 #define THREAD_LOCAL_DECL_PTR(cls,var) ThreadSingletonPtr<cls> var ## _ptr
    165 #define THREAD_LOCAL_DEF_PTR(cls,var) ThreadSingletonPtr<cls> var ## _ptr
    166 #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)
    168172
    169 #endif
     173#endif //#ifdef USE_CPP_TLS
    170174
    171175
Note: See TracChangeset for help on using the changeset viewer.