Merge branch 'gallium-i915-current' into 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 typedef pthread_cond_t _glthread_Cond;
118
119 #define _glthread_DECLARE_STATIC_COND(name) \
120 static _glthread_Cond name = PTHREAD_COND_INITIALIZER
121
122 #define _glthread_INIT_COND(cond) \
123 pthread_cond_init(&(cond), NULL)
124
125 #define _glthread_DESTROY_COND(name) \
126 pthread_cond_destroy(&(name))
127
128 #define _glthread_COND_WAIT(cond, mutex) \
129 pthread_cond_wait(&(cond), &(mutex))
130
131 #define _glthread_COND_SIGNAL(cond) \
132 pthread_cond_signal(&(cond))
133
134 #define _glthread_COND_BROADCAST(cond) \
135 pthread_cond_broadcast(&(cond))
136
137 #endif /* PTHREADS */
138
139
140
141
142 /*
143 * Solaris threads. Use only up to Solaris 2.4.
144 * Solaris 2.5 and higher provide POSIX threads.
145 * Be sure to compile with -mt on the Solaris compilers, or
146 * use -D_REENTRANT if using gcc.
147 */
148 #ifdef SOLARIS_THREADS
149 #include <thread.h>
150
151 typedef struct {
152 thread_key_t key;
153 mutex_t keylock;
154 int initMagic;
155 } _glthread_TSD;
156
157 typedef thread_t _glthread_Thread;
158
159 typedef mutex_t _glthread_Mutex;
160
161 /* XXX need to really implement mutex-related macros */
162 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
163 #define _glthread_INIT_MUTEX(name) (void) name
164 #define _glthread_DESTROY_MUTEX(name) (void) name
165 #define _glthread_LOCK_MUTEX(name) (void) name
166 #define _glthread_UNLOCK_MUTEX(name) (void) name
167
168 #endif /* SOLARIS_THREADS */
169
170
171
172
173 /*
174 * Windows threads. Should work with Windows NT and 95.
175 * IMPORTANT: Link with multithreaded runtime library when THREADS are
176 * used!
177 */
178 #ifdef WIN32_THREADS
179 #include <windows.h>
180
181 typedef struct {
182 DWORD key;
183 int initMagic;
184 } _glthread_TSD;
185
186 typedef HANDLE _glthread_Thread;
187
188 typedef CRITICAL_SECTION _glthread_Mutex;
189
190 #define _glthread_DECLARE_STATIC_MUTEX(name) /*static*/ _glthread_Mutex name = {0,0,0,0,0,0}
191 #define _glthread_INIT_MUTEX(name) InitializeCriticalSection(&name)
192 #define _glthread_DESTROY_MUTEX(name) DeleteCriticalSection(&name)
193 #define _glthread_LOCK_MUTEX(name) EnterCriticalSection(&name)
194 #define _glthread_UNLOCK_MUTEX(name) LeaveCriticalSection(&name)
195
196 #endif /* WIN32_THREADS */
197
198
199
200
201 /*
202 * XFree86 has its own thread wrapper, Xthreads.h
203 * We wrap it again for GL.
204 */
205 #ifdef USE_XTHREADS
206 #include <X11/Xthreads.h>
207
208 typedef struct {
209 xthread_key_t key;
210 int initMagic;
211 } _glthread_TSD;
212
213 typedef xthread_t _glthread_Thread;
214
215 typedef xmutex_rec _glthread_Mutex;
216
217 #ifdef XMUTEX_INITIALIZER
218 #define _glthread_DECLARE_STATIC_MUTEX(name) \
219 static _glthread_Mutex name = XMUTEX_INITIALIZER
220 #else
221 #define _glthread_DECLARE_STATIC_MUTEX(name) \
222 static _glthread_Mutex name
223 #endif
224
225 #define _glthread_INIT_MUTEX(name) \
226 xmutex_init(&(name))
227
228 #define _glthread_DESTROY_MUTEX(name) \
229 xmutex_clear(&(name))
230
231 #define _glthread_LOCK_MUTEX(name) \
232 (void) xmutex_lock(&(name))
233
234 #define _glthread_UNLOCK_MUTEX(name) \
235 (void) xmutex_unlock(&(name))
236
237 #endif /* USE_XTHREADS */
238
239
240
241 /*
242 * BeOS threads. R5.x required.
243 */
244 #ifdef BEOS_THREADS
245
246 #include <kernel/OS.h>
247 #include <support/TLS.h>
248
249 typedef struct {
250 int32 key;
251 int initMagic;
252 } _glthread_TSD;
253
254 typedef thread_id _glthread_Thread;
255
256 /* Use Benaphore, aka speeder semaphore */
257 typedef struct {
258 int32 lock;
259 sem_id sem;
260 } benaphore;
261 typedef benaphore _glthread_Mutex;
262
263 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = { 0, 0 }
264 #define _glthread_INIT_MUTEX(name) name.sem = create_sem(0, #name"_benaphore"), name.lock = 0
265 #define _glthread_DESTROY_MUTEX(name) delete_sem(name.sem), name.lock = 0
266 #define _glthread_LOCK_MUTEX(name) if (name.sem == 0) _glthread_INIT_MUTEX(name); \
267 if (atomic_add(&(name.lock), 1) >= 1) acquire_sem(name.sem)
268 #define _glthread_UNLOCK_MUTEX(name) if (atomic_add(&(name.lock), -1) > 1) release_sem(name.sem)
269
270 #endif /* BEOS_THREADS */
271
272
273
274 #ifndef THREADS
275
276 /*
277 * THREADS not defined
278 */
279
280 typedef unsigned _glthread_TSD;
281
282 typedef unsigned _glthread_Thread;
283
284 typedef unsigned _glthread_Mutex;
285
286 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
287
288 #define _glthread_INIT_MUTEX(name) (void) name
289
290 #define _glthread_DESTROY_MUTEX(name) (void) name
291
292 #define _glthread_LOCK_MUTEX(name) (void) name
293
294 #define _glthread_UNLOCK_MUTEX(name) (void) name
295
296 typedef unsigned _glthread_Cond;
297
298 #define _glthread_DECLARE_STATIC_COND(name) static _glthread_Cond name = 0
299
300 #define _glthread_INIT_COND(name) (void) name
301
302 #define _glthread_DESTROY_COND(name) (void) name
303
304 #define _glthread_COND_WAIT(name, mutex) (void) name
305
306 #define _glthread_COND_SIGNAL(name) (void) name
307
308 #define _glthread_COND_BROADCAST(name) (void) name
309
310 #endif /* THREADS */
311
312
313
314 /*
315 * Platform independent thread specific data API.
316 */
317
318 extern unsigned long
319 _glthread_GetID(void);
320
321
322 extern void
323 _glthread_InitTSD(_glthread_TSD *);
324
325
326 extern void *
327 _glthread_GetTSD(_glthread_TSD *);
328
329
330 extern void
331 _glthread_SetTSD(_glthread_TSD *, void *);
332
333 #if defined(GLX_USE_TLS)
334
335 extern __thread struct _glapi_table * _glapi_tls_Dispatch
336 __attribute__((tls_model("initial-exec")));
337
338 #define GET_DISPATCH() _glapi_tls_Dispatch
339
340 #elif !defined(GL_CALL)
341 # if defined(THREADS)
342 # define GET_DISPATCH() \
343 ((__builtin_expect( _glapi_Dispatch != NULL, 1 )) \
344 ? _glapi_Dispatch : _glapi_get_dispatch())
345 # else
346 # define GET_DISPATCH() _glapi_Dispatch
347 # endif /* defined(THREADS) */
348 #endif /* ndef GL_CALL */
349
350
351 #endif /* _P_THREAD_H_ */