source: cpp/common/threads.h @ 364

Last change on this file since 364 was 348, checked in by Maciej Komosinski, 9 years ago
  • explicit c_str() in SString instead of (const char*) cast
  • genetic converters and GenMan? are now thread-local which enables multi-threaded simulator separation
  • Property svn:eol-style set to native
File size: 3.2 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{
26pthread_key_t mt_key;
27
28  public:
29
30ThreadSingleton()
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
41static void destructor(void* o)
42 {
43 if (o)
44         delete (T*)o;
45 }
46
47T* 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
54T* 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
65T& getref() {return *get();}
66};
67
68template<class T> class ThreadSingletonPtr
69{
70pthread_key_t mt_key;
71
72  public:
73
74ThreadSingletonPtr()
75 {
76 pthread_key_create(&mt_key,NULL);
77 }
78
79T* 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
86T* get()
87 {
88 return (T*)pthread_getspecific(mt_key);
89 }
90};
91
92#else //ifdef MULTITHREADED
93
94template<class T> class ThreadSingleton
95{
96T object;
97
98  public:
99
100T* get() {return &object;}
101T& getref() {return object;}
102};
103
104template<class T> class ThreadSingletonPtr
105{
106T *object;
107
108  public:
109
110ThreadSingletonPtr():object(NULL) {}
111T* get() {return object;}
112T* 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
131template<class T> T* tlsGet(T*& var)
132{
133if (!var)
134        var=new T();
135return var;
136}
137
138template<class T> T* tlsGetPtr(T*& var)
139{
140return var;
141}
142
143#define tlsGetRef(var) (*tlsGet(var))
144
145template<class T> T* tlsSet(T*& var,T* new_o)
146{
147T* o=var;
148var=new_o;
149return 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
Note: See TracBrowser for help on using the repository browser.