Changeset 346 for cpp/common
- Timestamp:
- 04/07/15 04:20:14 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/common/threads.h
r286 r346 66 66 }; 67 67 68 template<class T> class ThreadSingletonPtr 69 { 70 pthread_key_t mt_key; 71 72 public: 73 74 ThreadSingletonPtr() 75 { 76 pthread_key_create(&mt_key,NULL); 77 } 78 79 T* setptr(T* new_o) 80 { 81 T* o=(T*)pthread_getspecific(mt_key); 82 pthread_setspecific(mt_key,new_o); 83 return o; 84 } 85 86 T* get() 87 { 88 return (T*)pthread_getspecific(mt_key); 89 } 90 }; 91 68 92 #else 69 93 … … 82 106 //////////////////////////////////// 83 107 108 // THREAD_LOCAL(cls) - behaves like object of class cls (automatic creation/destruction) 109 // THREAD_LOCAL(cls)..._PTR - behaves like pointer to cls (initial NULL, no autocreation/destruction) 110 // _PTR can only be accessed using tls...Ptr() variant of Get/Set, _ptr suffix is internally used in variable name to avoid mistakes 84 111 #ifdef USE_CPP_TLS 85 112 … … 88 115 #define THREAD_LOCAL_DECL(cls,var) CPP_TLS cls* var 89 116 #define THREAD_LOCAL_DEF(cls,var) CPP_TLS cls* var=NULL 117 #define THREAD_LOCAL_DECL_PTR(cls,var) CPP_TLS cls* var ## _ptr 118 #define THREAD_LOCAL_DEF_PTR(cls,var) CPP_TLS cls* var ## _ptr=NULL 90 119 91 120 template<class T> T* tlsGet(T*& var) … … 93 122 if (!var) 94 123 var=new T(); 124 return var; 125 } 126 127 template<class T> T* tlsGetPtr(T*& var) 128 { 95 129 return var; 96 130 } … … 105 139 } 106 140 141 #define tlsGetSetPtr(var,o) tlsSet(var ## _ptr,o) 142 107 143 #else 108 144 … … 115 151 #define tlsSet(var,o) var.set(o) 116 152 153 #define THREAD_LOCAL_DECL_PTR(cls,var) ThreadSingletonPtr<cls> var ## _ptr 154 #define THREAD_LOCAL_DEF_PTR(cls,var) ThreadSingletonPtr<cls> var ## _ptr 155 #define tlsGetPtr(var) var ## _ptr.get() 156 #define tlsSetPtr(var,o) var ## _ptr.setptr(o) 157 117 158 #endif 118 159
Note: See TracChangeset
for help on using the changeset viewer.