Changeset 371
- Timestamp:
- 04/21/15 15:19:47 (10 years ago)
- Location:
- cpp
- Files:
-
- 4 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 -
cpp/frams/util/extvalue.cpp
r348 r371 124 124 } 125 125 126 THREAD_LOCAL_DEF(ExtObject::Serialization, ExtObject ::serialization);126 THREAD_LOCAL_DEF(ExtObject::Serialization, ExtObject_serialization); 127 127 128 128 void ExtObject::Serialization::begin() … … 185 185 SString ExtObject::serialize_inner() const 186 186 { 187 int ref = tlsGetRef( serialization).add(*this);187 int ref = tlsGetRef(ExtObject_serialization).add(*this); 188 188 if (ref >= 0) 189 189 return SString::sprintf("^%d", ref); … … 216 216 } 217 217 218 tlsGetRef( serialization).remove(*this);//undo nonserializable reference218 tlsGetRef(ExtObject_serialization).remove(*this);//undo nonserializable reference 219 219 SString ret = interfaceName(); 220 220 ret += SString::sprintf("<%p>", object ? object : paraminterface); … … 224 224 SString ExtObject::serialize() const 225 225 { 226 tlsGetRef( serialization).begin();226 tlsGetRef(ExtObject_serialization).begin(); 227 227 SString ret = serialize_inner(); 228 tlsGetRef( serialization).end();228 tlsGetRef(ExtObject_serialization).end(); 229 229 return ret; 230 230 } … … 998 998 VectorObject *vec = new VectorObject; 999 999 ExtObject o(&VectorObject::par, vec); 1000 tlsGetRef(ExtObject ::serialization).add(o);1000 tlsGetRef(ExtObject_serialization).add(o); 1001 1001 const char* p = in + 1; 1002 1002 ExtValue tmp; … … 1029 1029 DictionaryObject *dic = new DictionaryObject; 1030 1030 ExtObject o(&DictionaryObject::par, dic); 1031 tlsGetRef(ExtObject ::serialization).add(o);1031 tlsGetRef(ExtObject_serialization).add(o); 1032 1032 const char* p = in + 1; 1033 1033 ExtValue args[2]/*={value,key}*/, dummy_ret; … … 1080 1080 if (ret && (ref.getType() == TInt)) 1081 1081 { 1082 const ExtObject* o = tlsGetRef(ExtObject ::serialization).get(ref.getInt());1082 const ExtObject* o = tlsGetRef(ExtObject_serialization).get(ref.getInt()); 1083 1083 if (o) 1084 1084 { … … 1105 1105 { 1106 1106 cls->call(m, &tmp, this); 1107 tlsGetRef(ExtObject ::serialization).replace(tmp.getObject(), getObject());1107 tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject()); 1108 1108 return ret; 1109 1109 } … … 1116 1116 { 1117 1117 cls->call(m, &tmp, this); 1118 tlsGetRef(ExtObject ::serialization).replace(tmp.getObject(), getObject());1118 tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject()); 1119 1119 return ret; 1120 1120 } … … 1126 1126 { 1127 1127 cls->call(m, &tmp, this); 1128 tlsGetRef(ExtObject ::serialization).replace(tmp.getObject(), getObject());1128 tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject()); 1129 1129 return ret; 1130 1130 } 1131 1131 } 1132 tlsGetRef(ExtObject ::serialization).remove(tmp.getObject());1132 tlsGetRef(ExtObject_serialization).remove(tmp.getObject()); 1133 1133 setEmpty(); 1134 1134 } … … 1144 1144 const char* ExtValue::deserialize(const char* in) 1145 1145 { 1146 tlsGetRef(ExtObject ::serialization).begin();1146 tlsGetRef(ExtObject_serialization).begin(); 1147 1147 const char* ret = deserialize_inner(in); 1148 tlsGetRef(ExtObject ::serialization).end();1148 tlsGetRef(ExtObject_serialization).end(); 1149 1149 return ret; 1150 1150 } -
cpp/frams/util/extvalue.h
r337 r371 99 99 }; 100 100 101 static THREAD_LOCAL_DECL(Serialization, serialization); 102 }; 101 }; 102 103 extern THREAD_LOCAL_DECL(ExtObject::Serialization, ExtObject_serialization); 103 104 104 105 class ExtValue -
cpp/frams/vm/classes/collectionobj.cpp
r333 r371 149 149 } 150 150 151 static THREAD_LOCAL_DEF(SList, tostring_trace);151 static THREAD_LOCAL_DEF(SList,VectorObject_tostring_trace); 152 152 153 153 void VectorObject::get_toString(ExtValue* ret) … … 155 155 SString out="["; 156 156 //static SListTempl<VectorObject*> trace; 157 if (tlsGetRef( tostring_trace).find(this)>=0)157 if (tlsGetRef(VectorObject_tostring_trace).find(this)>=0) 158 158 out+="..."; 159 159 else 160 160 { 161 tlsGetRef( tostring_trace)+=this;161 tlsGetRef(VectorObject_tostring_trace)+=this; 162 162 for(int i=0;i<data.size();i++) 163 163 { … … 169 169 out+="null"; 170 170 } 171 tlsGetRef( tostring_trace)-=this;171 tlsGetRef(VectorObject_tostring_trace)-=this; 172 172 } 173 173 out+="]"; … … 402 402 SString out="{"; 403 403 //static SListTempl<DictionaryObject*> trace; 404 if (tlsGetRef( tostring_trace).find(this)>=0)404 if (tlsGetRef(VectorObject_tostring_trace).find(this)>=0) 405 405 out+="..."; 406 406 else 407 407 { 408 tlsGetRef( tostring_trace)+=this;408 tlsGetRef(VectorObject_tostring_trace)+=this; 409 409 for(HashEntryIterator it(hash);it.isValid();) 410 410 { … … 418 418 if (it.isValid()) out+=","; 419 419 } 420 tlsGetRef( tostring_trace)-=this;420 tlsGetRef(VectorObject_tostring_trace)-=this; 421 421 } 422 422 out+="}";
Note: See TracChangeset
for help on using the changeset viewer.