source: cpp/common/threads.h @ 346

Last change on this file since 346 was 346, checked in by Maciej Komosinski, 9 years ago

Each thread can use its own simulator's GenMan?

  • Property svn:eol-style set to native
File size: 3.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{
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
93
94template<class T> class ThreadSingleton
95{
96T object;
97
98  public:
99
100T* get() {return &object;}
101T& getref() {return object;}
102};
103
104#endif
105
106////////////////////////////////////
107
108// THREAD_LOCAL(cls) - behaves like object of class cls (automatic creation/destruction)
109// THREAD_LOCAL(cls)..._PTR - behaves like pointer to cls (initial NULL, no autocreation/destruction)
110// _PTR can only be accessed using tls...Ptr() variant of Get/Set, _ptr suffix is internally used in variable name to avoid mistakes
111#ifdef USE_CPP_TLS
112
113// use c++ implementation (CPP_TLS must also be defined)
114
115#define THREAD_LOCAL_DECL(cls,var) CPP_TLS cls* var
116#define THREAD_LOCAL_DEF(cls,var) CPP_TLS cls* var=NULL
117#define THREAD_LOCAL_DECL_PTR(cls,var) CPP_TLS cls* var ## _ptr
118#define THREAD_LOCAL_DEF_PTR(cls,var) CPP_TLS cls* var ## _ptr=NULL
119
120template<class T> T* tlsGet(T*& var)
121{
122if (!var)
123        var=new T();
124return var;
125}
126
127template<class T> T* tlsGetPtr(T*& var)
128{
129return var;
130}
131
132#define tlsGetRef(var) (*tlsGet(var))
133
134template<class T> T* tlsSet(T*& var,T* new_o)
135{
136T* o=var;
137var=new_o;
138return o;
139}
140
141#define tlsGetSetPtr(var,o) tlsSet(var ## _ptr,o)
142
143#else
144
145// use pthreads implementation
146
147#define THREAD_LOCAL_DECL(cls,var) ThreadSingleton<cls> var
148#define THREAD_LOCAL_DEF(cls,var) ThreadSingleton<cls> var
149#define tlsGet(var) var.get()
150#define tlsGetRef(var) var.getref()
151#define tlsSet(var,o) var.set(o)
152
153#define THREAD_LOCAL_DECL_PTR(cls,var) ThreadSingletonPtr<cls> var ## _ptr
154#define THREAD_LOCAL_DEF_PTR(cls,var) ThreadSingletonPtr<cls> var ## _ptr
155#define tlsGetPtr(var) var ## _ptr.get()
156#define tlsSetPtr(var,o) var ## _ptr.setptr(o)
157
158#endif
159
160
161#endif
Note: See TracBrowser for help on using the repository browser.