Wrap every place that accesses a dispatch table with a macro. A new script-
[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(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 /* This is temporarilly removed because driver binaries cannot count on
114 * the existance of _gl_DispatchTSD in libGL. It only exists in "new"
115 * libGL. We may be able to ressurect this optimization at some point
116 * for DRI driver or for software Mesa.
117 */
118 #if 0
119 extern struct _glapi_table * _glapi_DispatchTSD;
120 extern _glthread_TSD _gl_DispatchTSD;
121
122 #define GET_DISPATCH() \
123 ((__builtin_expect( _glapi_DispatchTSD != NULL, 1 )) \
124 ? _glapi_DispatchTSD : (struct _glapi_table *) pthread_getspecific(_gl_DispatchTSD.key))
125 #endif
126
127 #endif /* PTHREADS */
128
129
130
131
132 /*
133 * Solaris threads. Use only up to Solaris 2.4.
134 * Solaris 2.5 and higher provide POSIX threads.
135 * Be sure to compile with -mt on the Solaris compilers, or
136 * use -D_REENTRANT if using gcc.
137 */
138 #ifdef SOLARIS_THREADS
139 #include <thread.h>
140
141 typedef struct {
142 thread_key_t key;
143 mutex_t keylock;
144 int initMagic;
145 } _glthread_TSD;
146
147 typedef thread_t _glthread_Thread;
148
149 typedef mutex_t _glthread_Mutex;
150
151 /* XXX need to really implement mutex-related macros */
152 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
153 #define _glthread_INIT_MUTEX(name) (void) name
154 #define _glthread_DESTROY_MUTEX(name) (void) name
155 #define _glthread_LOCK_MUTEX(name) (void) name
156 #define _glthread_UNLOCK_MUTEX(name) (void) name
157
158 #endif /* SOLARIS_THREADS */
159
160
161
162
163 /*
164 * Windows threads. Should work with Windows NT and 95.
165 * IMPORTANT: Link with multithreaded runtime library when THREADS are
166 * used!
167 */
168 #ifdef WIN32_THREADS
169 #include <windows.h>
170
171 typedef struct {
172 DWORD key;
173 int initMagic;
174 } _glthread_TSD;
175
176 typedef HANDLE _glthread_Thread;
177
178 typedef CRITICAL_SECTION _glthread_Mutex;
179
180 /* XXX need to really implement mutex-related macros */
181 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
182 #define _glthread_INIT_MUTEX(name) (void) name
183 #define _glthread_DESTROY_MUTEX(name) (void) name
184 #define _glthread_LOCK_MUTEX(name) (void) name
185 #define _glthread_UNLOCK_MUTEX(name) (void) name
186
187 #endif /* WIN32_THREADS */
188
189
190
191
192 /*
193 * XFree86 has its own thread wrapper, Xthreads.h
194 * We wrap it again for GL.
195 */
196 #ifdef XTHREADS
197 #include <X11/Xthreads.h>
198
199 typedef struct {
200 xthread_key_t key;
201 int initMagic;
202 } _glthread_TSD;
203
204 typedef xthread_t _glthread_Thread;
205
206 typedef xmutex_rec _glthread_Mutex;
207
208 #ifdef XMUTEX_INITIALIZER
209 #define _glthread_DECLARE_STATIC_MUTEX(name) \
210 static _glthread_Mutex name = XMUTEX_INITIALIZER
211 #else
212 #define _glthread_DECLARE_STATIC_MUTEX(name) \
213 static _glthread_Mutex name
214 #endif
215
216 #define _glthread_INIT_MUTEX(name) \
217 xmutex_init(&(name))
218
219 #define _glthread_DESTROY_MUTEX(name) \
220 xmutex_clear(&(name))
221
222 #define _glthread_LOCK_MUTEX(name) \
223 (void) xmutex_lock(&(name))
224
225 #define _glthread_UNLOCK_MUTEX(name) \
226 (void) xmutex_unlock(&(name))
227
228 #endif /* XTHREADS */
229
230
231
232 /*
233 * BeOS threads. R5.x required.
234 */
235 #ifdef BEOS_THREADS
236
237 #include <kernel/OS.h>
238 #include <support/TLS.h>
239
240 typedef struct {
241 int32 key;
242 int initMagic;
243 } _glthread_TSD;
244
245 typedef thread_id _glthread_Thread;
246
247 /* Use Benaphore, aka speeder semaphore */
248 typedef struct {
249 int32 lock;
250 sem_id sem;
251 } benaphore;
252 typedef benaphore _glthread_Mutex;
253
254 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = { 0, 0 }
255 #define _glthread_INIT_MUTEX(name) name.sem = create_sem(0, #name"_benaphore"), name.lock = 0
256 #define _glthread_DESTROY_MUTEX(name) delete_sem(name.sem), name.lock = 0
257 #define _glthread_LOCK_MUTEX(name) if (name.sem == 0) _glthread_INIT_MUTEX(name); \
258 if (atomic_add(&(name.lock), 1) >= 1) acquire_sem(name.sem)
259 #define _glthread_UNLOCK_MUTEX(name) if (atomic_add(&(name.lock), -1) > 1) release_sem(name.sem)
260
261 #endif /* BEOS_THREADS */
262
263
264
265 #ifndef THREADS
266
267 /*
268 * THREADS not defined
269 */
270
271 typedef GLuint _glthread_TSD;
272
273 typedef GLuint _glthread_Thread;
274
275 typedef GLuint _glthread_Mutex;
276
277 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
278
279 #define _glthread_INIT_MUTEX(name) (void) name
280
281 #define _glthread_DESTROY_MUTEX(name) (void) name
282
283 #define _glthread_LOCK_MUTEX(name) (void) name
284
285 #define _glthread_UNLOCK_MUTEX(name) (void) name
286
287 #endif /* THREADS */
288
289
290
291 /*
292 * Platform independent thread specific data API.
293 */
294
295 extern unsigned long
296 _glthread_GetID(void);
297
298
299 extern void
300 _glthread_InitTSD(_glthread_TSD *);
301
302
303 extern void *
304 _glthread_GetTSD(_glthread_TSD *);
305
306
307 extern void
308 _glthread_SetTSD(_glthread_TSD *, void *);
309
310 #if defined(GLX_USE_TLS)
311
312 extern __thread struct _glapi_table * _glapi_tls_Dispatch
313 __attribute__((tls_model("initial-exec")));
314
315 #define GET_DISPATCH() _glapi_tls_Dispatch
316
317 #elif !defined(GL_CALL)
318 # if defined(THREADS)
319 extern struct _glapi_table * _glapi_DispatchTSD;
320 # define GET_DISPATCH() \
321 ((__builtin_expect( _glapi_DispatchTSD != NULL, 1 )) \
322 ? _glapi_DispatchTSD : _glapi_get_dispatch())
323 # else
324 # define GET_DISPATCH() _glapi_Dispatch
325 # endif /* defined(THREADS) */
326 #endif /* ndef GL_CALL */
327
328
329 #endif /* THREADS_H */