| 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
|---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
|---|
| 3 | // See LICENSE.txt for details. |
|---|
| 4 | |
|---|
| 5 | #ifndef _THREADS_H_ |
|---|
| 6 | #define _THREADS_H_ |
|---|
| 7 | |
|---|
| 8 | #ifdef MULTITHREADED |
|---|
| 9 | |
|---|
| 10 | #include <pthread.h> |
|---|
| 11 | |
|---|
| 12 | int sysGetCPUCount(); |
|---|
| 13 | |
|---|
| 14 | #ifdef LINUX |
|---|
| 15 | //#define USE_CPP_TLS |
|---|
| 16 | //#define CPP_TLS __thread |
|---|
| 17 | #endif |
|---|
| 18 | |
|---|
| 19 | #ifdef __BORLANDC__ //zakladamy ze wszyscy uzywaja pthreadsowych, bo w tych wbudowanych w c++ w obecnym standardzie nie ma destrukcji obiektow (tylko proste struktury) |
|---|
| 20 | //#define USE_CPP_TLS |
|---|
| 21 | //#define CPP_TLS __declspec(thread) |
|---|
| 22 | #endif |
|---|
| 23 | |
|---|
| 24 | template<class T> class ThreadSingleton |
|---|
| 25 | { |
|---|
| 26 | pthread_key_t mt_key; |
|---|
| 27 | |
|---|
| 28 | public: |
|---|
| 29 | |
|---|
| 30 | ThreadSingleton() |
|---|
| 31 | { |
|---|
| 32 | pthread_key_create(&mt_key,&destructor); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | ~ThreadSingleton() |
|---|
| 36 | { |
|---|
| 37 | T* o=set(NULL); |
|---|
| 38 | if (o) delete o; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | static void destructor(void* o) |
|---|
| 42 | { |
|---|
| 43 | if (o) |
|---|
| 44 | delete (T*)o; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | T* set(T* new_o) |
|---|
| 48 | { |
|---|
| 49 | T* o=(T*)pthread_getspecific(mt_key); |
|---|
| 50 | pthread_setspecific(mt_key,new_o); |
|---|
| 51 | return o; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | T* get() |
|---|
| 55 | { |
|---|
| 56 | T* o=(T*)pthread_getspecific(mt_key); |
|---|
| 57 | if (!o) |
|---|
| 58 | { |
|---|
| 59 | o=new T(); |
|---|
| 60 | pthread_setspecific(mt_key,o); |
|---|
| 61 | } |
|---|
| 62 | return o; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | T& getref() {return *get();} |
|---|
| 66 | }; |
|---|
| 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 | |
|---|
| 92 | #else //ifdef MULTITHREADED |
|---|
| 93 | |
|---|
| 94 | template<class T> class ThreadSingleton |
|---|
| 95 | { |
|---|
| 96 | T object; |
|---|
| 97 | |
|---|
| 98 | public: |
|---|
| 99 | |
|---|
| 100 | T* get() {return &object;} |
|---|
| 101 | T& getref() {return object;} |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | template<class T> class ThreadSingletonPtr |
|---|
| 105 | { |
|---|
| 106 | T *object; |
|---|
| 107 | |
|---|
| 108 | public: |
|---|
| 109 | |
|---|
| 110 | ThreadSingletonPtr():object(NULL) {} |
|---|
| 111 | T* get() {return object;} |
|---|
| 112 | T* setptr(T* o) {T* previous=object; object=o; return previous;} |
|---|
| 113 | }; |
|---|
| 114 | |
|---|
| 115 | #endif //ifdef MULTITHREADED |
|---|
| 116 | |
|---|
| 117 | //////////////////////////////////// |
|---|
| 118 | |
|---|
| 119 | // THREAD_LOCAL(cls) - behaves like object of class cls (automatic creation/destruction) |
|---|
| 120 | // THREAD_LOCAL(cls)..._PTR - behaves like pointer to cls (initial NULL, no autocreation/destruction) |
|---|
| 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 | #ifdef USE_CPP_TLS |
|---|
| 123 | |
|---|
| 124 | // use c++ implementation (CPP_TLS must also be defined) |
|---|
| 125 | |
|---|
| 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 |
|---|
| 130 | |
|---|
| 131 | template<class T> T* tlsGet(T*& var) |
|---|
| 132 | { |
|---|
| 133 | if (!var) |
|---|
| 134 | var=new T(); |
|---|
| 135 | return var; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | template<class T> T* tlsGetPtr(T*& var) |
|---|
| 139 | { |
|---|
| 140 | return var; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | #define tlsGetRef(var) (*tlsGet(var)) |
|---|
| 144 | |
|---|
| 145 | template<class T> T* tlsSet(T*& var,T* new_o) |
|---|
| 146 | { |
|---|
| 147 | T* o=var; |
|---|
| 148 | var=new_o; |
|---|
| 149 | return o; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | #define tlsGetSetPtr(var,o) tlsSet(var ## _ptr,o) |
|---|
| 153 | |
|---|
| 154 | #else |
|---|
| 155 | |
|---|
| 156 | // use pthreads implementation |
|---|
| 157 | |
|---|
| 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) |
|---|
| 163 | |
|---|
| 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 | |
|---|
| 169 | #endif |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | #endif |
|---|