Changeset 371


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.

Location:
cpp
Files:
4 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
  • cpp/frams/util/extvalue.cpp

    r348 r371  
    124124}
    125125
    126 THREAD_LOCAL_DEF(ExtObject::Serialization, ExtObject::serialization);
     126THREAD_LOCAL_DEF(ExtObject::Serialization, ExtObject_serialization);
    127127
    128128void ExtObject::Serialization::begin()
     
    185185SString ExtObject::serialize_inner() const
    186186{
    187         int ref = tlsGetRef(serialization).add(*this);
     187        int ref = tlsGetRef(ExtObject_serialization).add(*this);
    188188        if (ref >= 0)
    189189                return SString::sprintf("^%d", ref);
     
    216216        }
    217217
    218         tlsGetRef(serialization).remove(*this);//undo nonserializable reference
     218        tlsGetRef(ExtObject_serialization).remove(*this);//undo nonserializable reference
    219219        SString ret = interfaceName();
    220220        ret += SString::sprintf("<%p>", object ? object : paraminterface);
     
    224224SString ExtObject::serialize() const
    225225{
    226         tlsGetRef(serialization).begin();
     226        tlsGetRef(ExtObject_serialization).begin();
    227227        SString ret = serialize_inner();
    228         tlsGetRef(serialization).end();
     228        tlsGetRef(ExtObject_serialization).end();
    229229        return ret;
    230230}
     
    998998                VectorObject *vec = new VectorObject;
    999999                ExtObject o(&VectorObject::par, vec);
    1000                 tlsGetRef(ExtObject::serialization).add(o);
     1000                tlsGetRef(ExtObject_serialization).add(o);
    10011001                const char* p = in + 1;
    10021002                ExtValue tmp;
     
    10291029                DictionaryObject *dic = new DictionaryObject;
    10301030                ExtObject o(&DictionaryObject::par, dic);
    1031                 tlsGetRef(ExtObject::serialization).add(o);
     1031                tlsGetRef(ExtObject_serialization).add(o);
    10321032                const char* p = in + 1;
    10331033                ExtValue args[2]/*={value,key}*/, dummy_ret;
     
    10801080                if (ret && (ref.getType() == TInt))
    10811081                {
    1082                         const ExtObject* o = tlsGetRef(ExtObject::serialization).get(ref.getInt());
     1082                        const ExtObject* o = tlsGetRef(ExtObject_serialization).get(ref.getInt());
    10831083                        if (o)
    10841084                        {
     
    11051105                                {
    11061106                                        cls->call(m, &tmp, this);
    1107                                         tlsGetRef(ExtObject::serialization).replace(tmp.getObject(), getObject());
     1107                                        tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject());
    11081108                                        return ret;
    11091109                                }
     
    11161116                                {
    11171117                                        cls->call(m, &tmp, this);
    1118                                         tlsGetRef(ExtObject::serialization).replace(tmp.getObject(), getObject());
     1118                                        tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject());
    11191119                                        return ret;
    11201120                                }
     
    11261126                                {
    11271127                                        cls->call(m, &tmp, this);
    1128                                         tlsGetRef(ExtObject::serialization).replace(tmp.getObject(), getObject());
     1128                                        tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject());
    11291129                                        return ret;
    11301130                                }
    11311131                        }
    1132                         tlsGetRef(ExtObject::serialization).remove(tmp.getObject());
     1132                        tlsGetRef(ExtObject_serialization).remove(tmp.getObject());
    11331133                        setEmpty();
    11341134                }
     
    11441144const char* ExtValue::deserialize(const char* in)
    11451145{
    1146         tlsGetRef(ExtObject::serialization).begin();
     1146        tlsGetRef(ExtObject_serialization).begin();
    11471147        const char* ret = deserialize_inner(in);
    1148         tlsGetRef(ExtObject::serialization).end();
     1148        tlsGetRef(ExtObject_serialization).end();
    11491149        return ret;
    11501150}
  • cpp/frams/util/extvalue.h

    r337 r371  
    9999        };
    100100
    101         static THREAD_LOCAL_DECL(Serialization, serialization);
    102 };
     101};
     102
     103extern THREAD_LOCAL_DECL(ExtObject::Serialization, ExtObject_serialization);
    103104
    104105class ExtValue
  • cpp/frams/vm/classes/collectionobj.cpp

    r333 r371  
    149149}
    150150
    151 static THREAD_LOCAL_DEF(SList,tostring_trace);
     151static THREAD_LOCAL_DEF(SList,VectorObject_tostring_trace);
    152152
    153153void VectorObject::get_toString(ExtValue* ret)
     
    155155SString out="[";
    156156//static SListTempl<VectorObject*> trace;
    157 if (tlsGetRef(tostring_trace).find(this)>=0)
     157if (tlsGetRef(VectorObject_tostring_trace).find(this)>=0)
    158158        out+="...";
    159159else
    160160        {
    161         tlsGetRef(tostring_trace)+=this;
     161        tlsGetRef(VectorObject_tostring_trace)+=this;
    162162        for(int i=0;i<data.size();i++)
    163163                {
     
    169169                        out+="null";
    170170                }
    171         tlsGetRef(tostring_trace)-=this;
     171        tlsGetRef(VectorObject_tostring_trace)-=this;
    172172        }
    173173out+="]";
     
    402402SString out="{";
    403403//static SListTempl<DictionaryObject*> trace;
    404 if (tlsGetRef(tostring_trace).find(this)>=0)
     404if (tlsGetRef(VectorObject_tostring_trace).find(this)>=0)
    405405        out+="...";
    406406else
    407407        {
    408         tlsGetRef(tostring_trace)+=this;
     408        tlsGetRef(VectorObject_tostring_trace)+=this;
    409409        for(HashEntryIterator it(hash);it.isValid();)
    410410                {
     
    418418                if (it.isValid()) out+=",";
    419419                }
    420         tlsGetRef(tostring_trace)-=this;
     420        tlsGetRef(VectorObject_tostring_trace)-=this;
    421421        }
    422422out+="}";
Note: See TracChangeset for help on using the changeset viewer.