minor simplification
[mesa.git] / src / mesa / glapi / glthread.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.3
4 *
5 * Copyright (C) 1999-2003 Brian Paul 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 * Thread support for gl dispatch.
28 *
29 * Initial version by John Stone (j.stone@acm.org) (johns@cs.umr.edu)
30 * and Christoph Poliwoda (poliwoda@volumegraphics.com)
31 * Revised by Keith Whitwell
32 * Adapted for new gl dispatcher by Brian Paul
33 *
34 *
35 *
36 * DOCUMENTATION
37 *
38 * This thread module exports the following types:
39 * _glthread_TSD Thread-specific data area
40 * _glthread_Thread Thread datatype
41 * _glthread_Mutex Mutual exclusion lock
42 *
43 * Macros:
44 * _glthread_DECLARE_STATIC_MUTEX(name) Declare a non-local mutex
45 * _glthread_INIT_MUTEX(name) Initialize a mutex
46 * _glthread_LOCK_MUTEX(name) Lock a mutex
47 * _glthread_UNLOCK_MUTEX(name) Unlock a mutex
48 *
49 * Functions:
50 * _glthread_GetID(v) Get integer thread ID
51 * _glthread_InitTSD() Initialize thread-specific data
52 * _glthread_GetTSD() Get thread-specific data
53 * _glthread_SetTSD() Set thread-specific data
54 *
55 */
56
57 /*
58 * If this file is accidentally included by a non-threaded build,
59 * it should not cause the build to fail, or otherwise cause problems.
60 * In general, it should only be included when needed however.
61 */
62
63 #ifndef GLTHREAD_H
64 #define GLTHREAD_H
65
66
67 #if (defined(PTHREADS) || defined(SOLARIS_THREADS) ||\
68 defined(WIN32_THREADS) || defined(USE_XTHREADS) || defined(BEOS_THREADS)) \
69 && !defined(THREADS)
70 # define THREADS
71 #endif
72
73 #ifdef VMS
74 #include <GL/vms_x_fix.h>
75 #endif
76
77 /*
78 * POSIX threads. This should be your choice in the Unix world
79 * whenever possible. When building with POSIX threads, be sure
80 * to enable any compiler flags which will cause the MT-safe
81 * libc (if one exists) to be used when linking, as well as any
82 * header macros for MT-safe errno, etc. For Solaris, this is the -mt
83 * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable
84 * proper compiling for MT-safe libc etc.
85 */
86 #if defined(PTHREADS)
87 #include <pthread.h> /* POSIX threads headers */
88
89 typedef struct {
90 pthread_key_t key;
91 int initMagic;
92 } _glthread_TSD;
93
94 typedef pthread_t _glthread_Thread;
95
96 typedef pthread_mutex_t _glthread_Mutex;
97
98 #define _glthread_DECLARE_STATIC_MUTEX(name) \
99 static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER
100
101 #define _glthread_INIT_MUTEX(name) \
102 pthread_mutex_init(&(name), NULL)
103
104 #define _glthread_DESTROY_MUTEX(name) \
105 pthread_mutex_destroy(&(name))
106
107 #define _glthread_LOCK_MUTEX(name) \
108 (void) pthread_mutex_lock(&(name))
109
110 #define _glthread_UNLOCK_MUTEX(name) \
111 (void) pthread_mutex_unlock(&(name))
112
113 #endif /* PTHREADS */
114
115
116
117
118 /*
119 * Solaris threads. Use only up to Solaris 2.4.
120 * Solaris 2.5 and higher provide POSIX threads.
121 * Be sure to compile with -mt on the Solaris compilers, or
122 * use -D_REENTRANT if using gcc.
123 */
124 #ifdef SOLARIS_THREADS
125 #include <thread.h>
126
127 typedef struct {
128 thread_key_t key;
129 mutex_t keylock;
130 int initMagic;
131 } _glthread_TSD;
132
133 typedef thread_t _glthread_Thread;
134
135 typedef mutex_t _glthread_Mutex;
136
137 /* XXX need to really implement mutex-related macros */
138 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
139 #define _glthread_INIT_MUTEX(name) (void) name
140 #define _glthread_DESTROY_MUTEX(name) (void) name
141 #define _glthread_LOCK_MUTEX(name) (void) name
142 #define _glthread_UNLOCK_MUTEX(name) (void) name
143
144 #endif /* SOLARIS_THREADS */
145
146
147
148
149 /*
150 * Windows threads. Should work with Windows NT and 95.
151 * IMPORTANT: Link with multithreaded runtime library when THREADS are
152 * used!
153 */
154 #ifdef WIN32_THREADS
155 #include <windows.h>
156
157 typedef struct {
158 DWORD key;
159 int initMagic;
160 } _glthread_TSD;
161
162 typedef HANDLE _glthread_Thread;
163
164 typedef CRITICAL_SECTION _glthread_Mutex;
165
166 /* XXX need to really implement mutex-related macros */
167 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
168 #define _glthread_INIT_MUTEX(name) (void) name
169 #define _glthread_DESTROY_MUTEX(name) (void) name
170 #define _glthread_LOCK_MUTEX(name) (void) name
171 #define _glthread_UNLOCK_MUTEX(name) (void) name
172
173 #endif /* WIN32_THREADS */
174
175
176
177
178 /*
179 * XFree86 has its own thread wrapper, Xthreads.h
180 * We wrap it again for GL.
181 */
182 #ifdef USE_XTHREADS
183 #include <X11/Xthreads.h>
184
185 typedef struct {
186 xthread_key_t key;
187 int initMagic;
188 } _glthread_TSD;
189
190 typedef xthread_t _glthread_Thread;
191
192 typedef xmutex_rec _glthread_Mutex;
193
194 #ifdef XMUTEX_INITIALIZER
195 #define _glthread_DECLARE_STATIC_MUTEX(name) \
196 static _glthread_Mutex name = XMUTEX_INITIALIZER
197 #else
198 #define _glthread_DECLARE_STATIC_MUTEX(name) \
199 static _glthread_Mutex name
200 #endif
201
202 #define _glthread_INIT_MUTEX(name) \
203 xmutex_init(&(name))
204
205 #define _glthread_DESTROY_MUTEX(name) \
206 xmutex_clear(&(name))
207
208 #define _glthread_LOCK_MUTEX(name) \
209 (void) xmutex_lock(&(name))
210
211 #define _glthread_UNLOCK_MUTEX(name) \
212 (void) xmutex_unlock(&(name))
213
214 #endif /* USE_XTHREADS */
215
216
217
218 /*
219 * BeOS threads. R5.x required.
220 */
221 #ifdef BEOS_THREADS
222
223 #include <kernel/OS.h>
224 #include <support/TLS.h>
225
226 typedef struct {
227 int32 key;
228 int initMagic;
229 } _glthread_TSD;
230
231 typedef thread_id _glthread_Thread;
232
233 /* Use Benaphore, aka speeder semaphore */
234 typedef struct {
235 int32 lock;
236 sem_id sem;
237 } benaphore;
238 typedef benaphore _glthread_Mutex;
239
240 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = { 0, 0 }
241 #define _glthread_INIT_MUTEX(name) name.sem = create_sem(0, #name"_benaphore"), name.lock = 0
242 #define _glthread_DESTROY_MUTEX(name) delete_sem(name.sem), name.lock = 0
243 #define _glthread_LOCK_MUTEX(name) if (name.sem == 0) _glthread_INIT_MUTEX(name); \
244 if (atomic_add(&(name.lock), 1) >= 1) acquire_sem(name.sem)
245 #define _glthread_UNLOCK_MUTEX(name) if (atomic_add(&(name.lock), -1) > 1) release_sem(name.sem)
246
247 #endif /* BEOS_THREADS */
248
249
250
251 #ifndef THREADS
252
253 /*
254 * THREADS not defined
255 */
256
257 typedef GLuint _glthread_TSD;
258
259 typedef GLuint _glthread_Thread;
260
261 typedef GLuint _glthread_Mutex;
262
263 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
264
265 #define _glthread_INIT_MUTEX(name) (void) name
266
267 #define _glthread_DESTROY_MUTEX(name) (void) name
268
269 #define _glthread_LOCK_MUTEX(name) (void) name
270
271 #define _glthread_UNLOCK_MUTEX(name) (void) name
272
273 #endif /* THREADS */
274
275
276
277 /*
278 * Platform independent thread specific data API.
279 */
280
281 extern unsigned long
282 _glthread_GetID(void);
283
284
285 extern void
286 _glthread_InitTSD(_glthread_TSD *);
287
288
289 extern void *
290 _glthread_GetTSD(_glthread_TSD *);
291
292
293 extern void
294 _glthread_SetTSD(_glthread_TSD *, void *);
295
296 #if defined(GLX_USE_TLS)
297
298 extern __thread struct _glapi_table * _glapi_tls_Dispatch
299 __attribute__((tls_model("initial-exec")));
300
301 #define GET_DISPATCH() _glapi_tls_Dispatch
302
303 #elif !defined(GL_CALL)
304 # if defined(THREADS)
305 # define GET_DISPATCH() \
306 ((__builtin_expect( _glapi_Dispatch != NULL, 1 )) \
307 ? _glapi_Dispatch : _glapi_get_dispatch())
308 # else
309 # define GET_DISPATCH() _glapi_Dispatch
310 # endif /* defined(THREADS) */
311 #endif /* ndef GL_CALL */
312
313
314 #endif /* THREADS_H */