Merge branch 'master' of git+ssh://git.freedesktop.org/git/mesa/mesa into gallium-0.2
[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(PTHREADS) || defined(SOLARIS_THREADS) ||\
66 defined(WIN32_THREADS) || defined(USE_XTHREADS) || defined(BEOS_THREADS)) \
67 && !defined(THREADS)
68 # define THREADS
69 #endif
70
71 #ifdef VMS
72 #include <GL/vms_x_fix.h>
73 #endif
74
75 /*
76 * POSIX threads. This should be your choice in the Unix world
77 * whenever possible. When building with POSIX threads, be sure
78 * to enable any compiler flags which will cause the MT-safe
79 * libc (if one exists) to be used when linking, as well as any
80 * header macros for MT-safe errno, etc. For Solaris, this is the -mt
81 * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable
82 * proper compiling for MT-safe libc etc.
83 */
84 #if defined(PTHREADS)
85 #include <pthread.h> /* POSIX threads headers */
86
87 typedef struct {
88 pthread_key_t key;
89 int initMagic;
90 } _glthread_TSD;
91
92 typedef pthread_t _glthread_Thread;
93
94 typedef pthread_mutex_t _glthread_Mutex;
95
96 #define _glthread_DECLARE_STATIC_MUTEX(name) \
97 static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER
98
99 #define _glthread_INIT_MUTEX(name) \
100 pthread_mutex_init(&(name), NULL)
101
102 #define _glthread_DESTROY_MUTEX(name) \
103 pthread_mutex_destroy(&(name))
104
105 #define _glthread_LOCK_MUTEX(name) \
106 (void) pthread_mutex_lock(&(name))
107
108 #define _glthread_UNLOCK_MUTEX(name) \
109 (void) pthread_mutex_unlock(&(name))
110
111 typedef pthread_cond_t _glthread_Cond;
112
113 #define _glthread_DECLARE_STATIC_COND(name) \
114 static _glthread_Cond name = PTHREAD_COND_INITIALIZER
115
116 #define _glthread_INIT_COND(cond) \
117 pthread_cond_init(&(cond), NULL)
118
119 #define _glthread_DESTROY_COND(name) \
120 pthread_cond_destroy(&(name))
121
122 #define _glthread_COND_WAIT(cond, mutex) \
123 pthread_cond_wait(&(cond), &(mutex))
124
125 #define _glthread_COND_SIGNAL(cond) \
126 pthread_cond_signal(&(cond))
127
128 #define _glthread_COND_BROADCAST(cond) \
129 pthread_cond_broadcast(&(cond))
130
131 #endif /* PTHREADS */
132
133
134
135
136 /*
137 * Solaris threads. Use only up to Solaris 2.4.
138 * Solaris 2.5 and higher provide POSIX threads.
139 * Be sure to compile with -mt on the Solaris compilers, or
140 * use -D_REENTRANT if using gcc.
141 */
142 #ifdef SOLARIS_THREADS
143 #include <thread.h>
144
145 typedef struct {
146 thread_key_t key;
147 mutex_t keylock;
148 int initMagic;
149 } _glthread_TSD;
150
151 typedef thread_t _glthread_Thread;
152
153 typedef mutex_t _glthread_Mutex;
154
155 /* XXX need to really implement mutex-related macros */
156 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
157 #define _glthread_INIT_MUTEX(name) (void) name
158 #define _glthread_DESTROY_MUTEX(name) (void) name
159 #define _glthread_LOCK_MUTEX(name) (void) name
160 #define _glthread_UNLOCK_MUTEX(name) (void) name
161
162 #endif /* SOLARIS_THREADS */
163
164
165
166
167 /*
168 * Windows threads. Should work with Windows NT and 95.
169 * IMPORTANT: Link with multithreaded runtime library when THREADS are
170 * used!
171 */
172 #ifdef WIN32_THREADS
173 #include <windows.h>
174
175 typedef struct {
176 DWORD key;
177 int initMagic;
178 } _glthread_TSD;
179
180 typedef HANDLE _glthread_Thread;
181
182 typedef CRITICAL_SECTION _glthread_Mutex;
183
184 #define _glthread_DECLARE_STATIC_MUTEX(name) /*static*/ _glthread_Mutex name = {0,0,0,0,0,0}
185 #define _glthread_INIT_MUTEX(name) InitializeCriticalSection(&name)
186 #define _glthread_DESTROY_MUTEX(name) DeleteCriticalSection(&name)
187 #define _glthread_LOCK_MUTEX(name) EnterCriticalSection(&name)
188 #define _glthread_UNLOCK_MUTEX(name) LeaveCriticalSection(&name)
189
190 #endif /* WIN32_THREADS */
191
192
193
194
195 /*
196 * XFree86 has its own thread wrapper, Xthreads.h
197 * We wrap it again for GL.
198 */
199 #ifdef USE_XTHREADS
200 #include <X11/Xthreads.h>
201
202 typedef struct {
203 xthread_key_t key;
204 int initMagic;
205 } _glthread_TSD;
206
207 typedef xthread_t _glthread_Thread;
208
209 typedef xmutex_rec _glthread_Mutex;
210
211 #ifdef XMUTEX_INITIALIZER
212 #define _glthread_DECLARE_STATIC_MUTEX(name) \
213 static _glthread_Mutex name = XMUTEX_INITIALIZER
214 #else
215 #define _glthread_DECLARE_STATIC_MUTEX(name) \
216 static _glthread_Mutex name
217 #endif
218
219 #define _glthread_INIT_MUTEX(name) \
220 xmutex_init(&(name))
221
222 #define _glthread_DESTROY_MUTEX(name) \
223 xmutex_clear(&(name))
224
225 #define _glthread_LOCK_MUTEX(name) \
226 (void) xmutex_lock(&(name))
227
228 #define _glthread_UNLOCK_MUTEX(name) \
229 (void) xmutex_unlock(&(name))
230
231 #endif /* USE_XTHREADS */
232
233
234
235 /*
236 * BeOS threads. R5.x required.
237 */
238 #ifdef BEOS_THREADS
239
240 #include <kernel/OS.h>
241 #include <support/TLS.h>
242
243 typedef struct {
244 int32 key;
245 int initMagic;
246 } _glthread_TSD;
247
248 typedef thread_id _glthread_Thread;
249
250 /* Use Benaphore, aka speeder semaphore */
251 typedef struct {
252 int32 lock;
253 sem_id sem;
254 } benaphore;
255 typedef benaphore _glthread_Mutex;
256
257 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = { 0, 0 }
258 #define _glthread_INIT_MUTEX(name) name.sem = create_sem(0, #name"_benaphore"), name.lock = 0
259 #define _glthread_DESTROY_MUTEX(name) delete_sem(name.sem), name.lock = 0
260 #define _glthread_LOCK_MUTEX(name) if (name.sem == 0) _glthread_INIT_MUTEX(name); \
261 if (atomic_add(&(name.lock), 1) >= 1) acquire_sem(name.sem)
262 #define _glthread_UNLOCK_MUTEX(name) if (atomic_add(&(name.lock), -1) > 1) release_sem(name.sem)
263
264 #endif /* BEOS_THREADS */
265
266
267
268 #ifndef THREADS
269
270 /*
271 * THREADS not defined
272 */
273
274 typedef unsigned _glthread_TSD;
275
276 typedef unsigned _glthread_Thread;
277
278 typedef unsigned _glthread_Mutex;
279
280 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
281
282 #define _glthread_INIT_MUTEX(name) (void) name
283
284 #define _glthread_DESTROY_MUTEX(name) (void) name
285
286 #define _glthread_LOCK_MUTEX(name) (void) name
287
288 #define _glthread_UNLOCK_MUTEX(name) (void) name
289
290 typedef unsigned _glthread_Cond;
291
292 #define _glthread_DECLARE_STATIC_COND(name) static _glthread_Cond name = 0
293
294 #define _glthread_INIT_COND(name) (void) name
295
296 #define _glthread_DESTROY_COND(name) (void) name
297
298 #define _glthread_COND_WAIT(name, mutex) (void) name
299
300 #define _glthread_COND_SIGNAL(name) (void) name
301
302 #define _glthread_COND_BROADCAST(name) (void) name
303
304 #endif /* THREADS */
305
306
307
308 /*
309 * Platform independent thread specific data API.
310 */
311
312 extern unsigned long
313 _glthread_GetID(void);
314
315
316 extern void
317 _glthread_InitTSD(_glthread_TSD *);
318
319
320 extern void *
321 _glthread_GetTSD(_glthread_TSD *);
322
323
324 extern void
325 _glthread_SetTSD(_glthread_TSD *, void *);
326
327
328
329 #endif /* _P_THREAD_H_ */