Merge branch 'upstream-gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / src / gallium / include / pipe / p_thread.h
1 /**************************************************************************
2 *
3 * Copyright 1999-2006 Brian Paul
4 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 **************************************************************************/
25
26 /**
27 * @file
28 * Thread
29 *
30 * Initial version by John Stone (j.stone@acm.org) (johns@cs.umr.edu)
31 * and Christoph Poliwoda (poliwoda@volumegraphics.com)
32 * Revised by Keith Whitwell
33 * Adapted for new gl dispatcher by Brian Paul
34 *
35 *
36 *
37 * DOCUMENTATION
38 *
39 * This thread module exports the following types:
40 * _glthread_TSD Thread-specific data area
41 * _glthread_Thread Thread datatype
42 * _glthread_Mutex Mutual exclusion lock
43 *
44 * Macros:
45 * _glthread_DECLARE_STATIC_MUTEX(name) Declare a non-local mutex
46 * _glthread_INIT_MUTEX(name) Initialize a mutex
47 * _glthread_LOCK_MUTEX(name) Lock a mutex
48 * _glthread_UNLOCK_MUTEX(name) Unlock a mutex
49 *
50 * Functions:
51 * _glthread_GetID(v) Get integer thread ID
52 * _glthread_InitTSD() Initialize thread-specific data
53 * _glthread_GetTSD() Get thread-specific data
54 * _glthread_SetTSD() Set thread-specific data
55 *
56 * If this file is accidentally included by a non-threaded build,
57 * it should not cause the build to fail, or otherwise cause problems.
58 * In general, it should only be included when needed however.
59 */
60
61 #ifndef _P_THREAD_H_
62 #define _P_THREAD_H_
63
64
65 #if defined(USE_MGL_NAMESPACE)
66 #define _glapi_Dispatch _mglapi_Dispatch
67 #endif
68
69
70
71 #if (defined(PTHREADS) || defined(SOLARIS_THREADS) ||\
72 defined(WIN32_THREADS) || defined(USE_XTHREADS) || defined(BEOS_THREADS)) \
73 && !defined(THREADS)
74 # define THREADS
75 #endif
76
77 #ifdef VMS
78 #include <GL/vms_x_fix.h>
79 #endif
80
81 /*
82 * POSIX threads. This should be your choice in the Unix world
83 * whenever possible. When building with POSIX threads, be sure
84 * to enable any compiler flags which will cause the MT-safe
85 * libc (if one exists) to be used when linking, as well as any
86 * header macros for MT-safe errno, etc. For Solaris, this is the -mt
87 * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable
88 * proper compiling for MT-safe libc etc.
89 */
90 #if defined(PTHREADS)
91 #include <pthread.h> /* POSIX threads headers */
92
93 typedef struct {
94 pthread_key_t key;
95 int initMagic;
96 } _glthread_TSD;
97
98 typedef pthread_t _glthread_Thread;
99
100 typedef pthread_mutex_t _glthread_Mutex;
101
102 #define _glthread_DECLARE_STATIC_MUTEX(name) \
103 static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER
104
105 #define _glthread_INIT_MUTEX(name) \
106 pthread_mutex_init(&(name), NULL)
107
108 #define _glthread_DESTROY_MUTEX(name) \
109 pthread_mutex_destroy(&(name))
110
111 #define _glthread_LOCK_MUTEX(name) \
112 (void) pthread_mutex_lock(&(name))
113
114 #define _glthread_UNLOCK_MUTEX(name) \
115 (void) pthread_mutex_unlock(&(name))
116
117 #endif /* PTHREADS */
118
119
120
121
122 /*
123 * Solaris threads. Use only up to Solaris 2.4.
124 * Solaris 2.5 and higher provide POSIX threads.
125 * Be sure to compile with -mt on the Solaris compilers, or
126 * use -D_REENTRANT if using gcc.
127 */
128 #ifdef SOLARIS_THREADS
129 #include <thread.h>
130
131 typedef struct {
132 thread_key_t key;
133 mutex_t keylock;
134 int initMagic;
135 } _glthread_TSD;
136
137 typedef thread_t _glthread_Thread;
138
139 typedef mutex_t _glthread_Mutex;
140
141 /* XXX need to really implement mutex-related macros */
142 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
143 #define _glthread_INIT_MUTEX(name) (void) name
144 #define _glthread_DESTROY_MUTEX(name) (void) name
145 #define _glthread_LOCK_MUTEX(name) (void) name
146 #define _glthread_UNLOCK_MUTEX(name) (void) name
147
148 #endif /* SOLARIS_THREADS */
149
150
151
152
153 /*
154 * Windows threads. Should work with Windows NT and 95.
155 * IMPORTANT: Link with multithreaded runtime library when THREADS are
156 * used!
157 */
158 #ifdef WIN32_THREADS
159 #include <windows.h>
160
161 typedef struct {
162 DWORD key;
163 int initMagic;
164 } _glthread_TSD;
165
166 typedef HANDLE _glthread_Thread;
167
168 typedef CRITICAL_SECTION _glthread_Mutex;
169
170 #define _glthread_DECLARE_STATIC_MUTEX(name) /*static*/ _glthread_Mutex name = {0,0,0,0,0,0}
171 #define _glthread_INIT_MUTEX(name) InitializeCriticalSection(&name)
172 #define _glthread_DESTROY_MUTEX(name) DeleteCriticalSection(&name)
173 #define _glthread_LOCK_MUTEX(name) EnterCriticalSection(&name)
174 #define _glthread_UNLOCK_MUTEX(name) LeaveCriticalSection(&name)
175
176 #endif /* WIN32_THREADS */
177
178
179
180
181 /*
182 * XFree86 has its own thread wrapper, Xthreads.h
183 * We wrap it again for GL.
184 */
185 #ifdef USE_XTHREADS
186 #include <X11/Xthreads.h>
187
188 typedef struct {
189 xthread_key_t key;
190 int initMagic;
191 } _glthread_TSD;
192
193 typedef xthread_t _glthread_Thread;
194
195 typedef xmutex_rec _glthread_Mutex;
196
197 #ifdef XMUTEX_INITIALIZER
198 #define _glthread_DECLARE_STATIC_MUTEX(name) \
199 static _glthread_Mutex name = XMUTEX_INITIALIZER
200 #else
201 #define _glthread_DECLARE_STATIC_MUTEX(name) \
202 static _glthread_Mutex name
203 #endif
204
205 #define _glthread_INIT_MUTEX(name) \
206 xmutex_init(&(name))
207
208 #define _glthread_DESTROY_MUTEX(name) \
209 xmutex_clear(&(name))
210
211 #define _glthread_LOCK_MUTEX(name) \
212 (void) xmutex_lock(&(name))
213
214 #define _glthread_UNLOCK_MUTEX(name) \
215 (void) xmutex_unlock(&(name))
216
217 #endif /* USE_XTHREADS */
218
219
220
221 /*
222 * BeOS threads. R5.x required.
223 */
224 #ifdef BEOS_THREADS
225
226 #include <kernel/OS.h>
227 #include <support/TLS.h>
228
229 typedef struct {
230 int32 key;
231 int initMagic;
232 } _glthread_TSD;
233
234 typedef thread_id _glthread_Thread;
235
236 /* Use Benaphore, aka speeder semaphore */
237 typedef struct {
238 int32 lock;
239 sem_id sem;
240 } benaphore;
241 typedef benaphore _glthread_Mutex;
242
243 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = { 0, 0 }
244 #define _glthread_INIT_MUTEX(name) name.sem = create_sem(0, #name"_benaphore"), name.lock = 0
245 #define _glthread_DESTROY_MUTEX(name) delete_sem(name.sem), name.lock = 0
246 #define _glthread_LOCK_MUTEX(name) if (name.sem == 0) _glthread_INIT_MUTEX(name); \
247 if (atomic_add(&(name.lock), 1) >= 1) acquire_sem(name.sem)
248 #define _glthread_UNLOCK_MUTEX(name) if (atomic_add(&(name.lock), -1) > 1) release_sem(name.sem)
249
250 #endif /* BEOS_THREADS */
251
252
253
254 #ifndef THREADS
255
256 /*
257 * THREADS not defined
258 */
259
260 typedef unsigned _glthread_TSD;
261
262 typedef unsigned _glthread_Thread;
263
264 typedef unsigned _glthread_Mutex;
265
266 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
267
268 #define _glthread_INIT_MUTEX(name) (void) name
269
270 #define _glthread_DESTROY_MUTEX(name) (void) name
271
272 #define _glthread_LOCK_MUTEX(name) (void) name
273
274 #define _glthread_UNLOCK_MUTEX(name) (void) name
275
276 #endif /* THREADS */
277
278
279
280 /*
281 * Platform independent thread specific data API.
282 */
283
284 extern unsigned long
285 _glthread_GetID(void);
286
287
288 extern void
289 _glthread_InitTSD(_glthread_TSD *);
290
291
292 extern void *
293 _glthread_GetTSD(_glthread_TSD *);
294
295
296 extern void
297 _glthread_SetTSD(_glthread_TSD *, void *);
298
299 #if defined(GLX_USE_TLS)
300
301 extern __thread struct _glapi_table * _glapi_tls_Dispatch
302 __attribute__((tls_model("initial-exec")));
303
304 #define GET_DISPATCH() _glapi_tls_Dispatch
305
306 #elif !defined(GL_CALL)
307 # if defined(THREADS)
308 # define GET_DISPATCH() \
309 ((__builtin_expect( _glapi_Dispatch != NULL, 1 )) \
310 ? _glapi_Dispatch : _glapi_get_dispatch())
311 # else
312 # define GET_DISPATCH() _glapi_Dispatch
313 # endif /* defined(THREADS) */
314 #endif /* ndef GL_CALL */
315
316
317 #endif /* _P_THREAD_H_ */