source: cpp/common/threads.h @ 383

Last change on this file since 383 was 377, checked in by Maciej Komosinski, 10 years ago

Included header for NULL

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
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
12int 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
24template<class T> class ThreadSingleton
25{
26        pthread_key_t mt_key;
27
28public:
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
68template<class T> class ThreadSingletonPtr
69{
70        pthread_key_t mt_key;
71
72public:
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#include <cstddef> //NULL
95
96#define tlsGet(var) (&ThreadLocal_ ## var ## _getref())
97#define tlsGetRef(var) ThreadLocal_ ## var ## _getref()
98#define tlsSet(var,o) var.set(o)
99
100#define THREAD_LOCAL_DECL(cls,var) cls& ThreadLocal_ ## var ## _getref()
101#define THREAD_LOCAL_DEF(cls,var) cls& ThreadLocal_ ## var ## _getref() \
102        { \
103        static cls object; \
104        return object; \
105        }
106
107template<class T> class ThreadSingletonPtr
108{
109        T *object;
110public:
111        ThreadSingletonPtr():object(NULL) {}
112        T* get() {return object;}
113        T* setptr(T* o) {T* previous=object; object=o; return previous;}
114};
115
116#endif //ifdef MULTITHREADED
117
118////////////////////////////////////
119
120// THREAD_LOCAL(cls) - behaves like object of class cls (automatic creation/destruction)
121// THREAD_LOCAL(cls)..._PTR - behaves like pointer to cls (initial NULL, no autocreation/destruction)
122// var is a (global) name, can't be a class member anymore because it might be implemented as function
123// _PTR can only be accessed using tls...Ptr() variant of Get/Set, _ptr suffix is internally used in variable name to avoid mistakes
124#ifdef USE_CPP_TLS
125
126// use c++ implementation (CPP_TLS must also be defined)
127
128#define THREAD_LOCAL_DECL(cls,var) CPP_TLS cls* ThreadLocal_ ## var
129#define THREAD_LOCAL_DEF(cls,var) CPP_TLS cls* ThreadLocal_ ## var=NULL
130#define THREAD_LOCAL_DECL_PTR(cls,var) CPP_TLS cls* ThreadLocal_ ## var ## _ptr
131#define THREAD_LOCAL_DEF_PTR(cls,var) CPP_TLS cls* ThreadLocal_ ## var ## _ptr=NULL
132
133template<class T> T* _tlsGet(T*& var)
134{
135        if (!var)
136                var=new T();
137        return var;
138}
139
140template<class T> T* _tlsGetPtr(T*& var)
141{
142        return var;
143}
144
145#define tlsGet(var) _tlsGet(ThreadLocal_ ## var)
146#define tlsGetRef(var) (*_tlsGet(ThreadLocal_ ## var))
147
148template<class T> T* _tlsSet(T*& var,T* new_o)
149{
150        T* o=var;
151        var=new_o;
152        return o;
153}
154
155#define tlsGetPtr(var) _tlsGetPtr(ThreadLocal_ ## var)
156#define tlsSetPtr(var,o) _tlsSet(ThreadLocal_ ## var ## _ptr,o)
157
158#else //#ifdef USE_CPP_TLS
159
160// use pthreads implementation
161
162#ifdef MULTITHREADED
163#define THREAD_LOCAL_DECL(cls,var) ThreadSingleton<cls> ThreadLocal_ ## var
164#define THREAD_LOCAL_DEF(cls,var) ThreadSingleton<cls> ThreadLocal_ ## var
165#define tlsGet(var) ThreadLocal_ ## var.get()
166#define tlsGetRef(var) ThreadLocal_ ## var.getref()
167#define tlsSet(var,o) ThreadLocal_ ## var.set(o)
168#endif
169
170#define THREAD_LOCAL_DECL_PTR(cls,var) extern ThreadSingletonPtr<cls> ThreadLocal_ ## var ## _ptr
171#define THREAD_LOCAL_DEF_PTR(cls,var) ThreadSingletonPtr<cls> ThreadLocal_ ## var ## _ptr
172#define tlsGetPtr(var) ThreadLocal_ ## var ## _ptr.get()
173#define tlsSetPtr(var,o) ThreadLocal_ ## var ## _ptr.setptr(o)
174
175#endif //#ifdef USE_CPP_TLS
176
177
178#endif
Note: See TracBrowser for help on using the repository browser.