2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * Thread support for gl dispatch.
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
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
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
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
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.
67 #if defined(USE_MGL_NAMESPACE)
68 #define _glapi_Dispatch _mglapi_Dispatch
73 #if (defined(PTHREADS) || defined(SOLARIS_THREADS) ||\
74 defined(WIN32_THREADS) || defined(USE_XTHREADS) || defined(BEOS_THREADS)) \
80 #include <GL/vms_x_fix.h>
84 * POSIX threads. This should be your choice in the Unix world
85 * whenever possible. When building with POSIX threads, be sure
86 * to enable any compiler flags which will cause the MT-safe
87 * libc (if one exists) to be used when linking, as well as any
88 * header macros for MT-safe errno, etc. For Solaris, this is the -mt
89 * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable
90 * proper compiling for MT-safe libc etc.
93 #include <pthread.h> /* POSIX threads headers */
100 typedef pthread_t _glthread_Thread
;
102 typedef pthread_mutex_t _glthread_Mutex
;
104 #define _glthread_DECLARE_STATIC_MUTEX(name) \
105 static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER
107 #define _glthread_INIT_MUTEX(name) \
108 pthread_mutex_init(&(name), NULL)
110 #define _glthread_DESTROY_MUTEX(name) \
111 pthread_mutex_destroy(&(name))
113 #define _glthread_LOCK_MUTEX(name) \
114 (void) pthread_mutex_lock(&(name))
116 #define _glthread_UNLOCK_MUTEX(name) \
117 (void) pthread_mutex_unlock(&(name))
119 typedef pthread_cond_t _glthread_Cond
;
121 #define _glthread_DECLARE_STATIC_COND(name) \
122 static _glthread_Cond name = PTHREAD_COND_INITIALIZER
124 #define _glthread_INIT_COND(cond) \
125 pthread_cond_init(&(cond), NULL)
127 #define _glthread_DESTROY_COND(name) \
128 pthread_cond_destroy(&(name))
130 #define _glthread_COND_WAIT(cond, mutex) \
131 pthread_cond_wait(&(cond), &(mutex))
133 #define _glthread_COND_SIGNAL(cond) \
134 pthread_cond_signal(&(cond))
136 #define _glthread_COND_BROADCAST(cond) \
137 pthread_cond_broadcast(&(cond))
142 typedef unsigned int _glthread_Cond
;
143 #define _glthread_DECLARE_STATIC_COND(name) \
144 // #warning Condition variables not implemented.
146 #define _glthread_INIT_COND(cond) \
149 #define _glthread_DESTROY_COND(name) \
152 #define _glthread_COND_WAIT(cond, mutex) \
155 #define _glthread_COND_SIGNAL(cond) \
158 #define _glthread_COND_BROADCAST(cond) \
165 * Solaris threads. Use only up to Solaris 2.4.
166 * Solaris 2.5 and higher provide POSIX threads.
167 * Be sure to compile with -mt on the Solaris compilers, or
168 * use -D_REENTRANT if using gcc.
170 #ifdef SOLARIS_THREADS
179 typedef thread_t _glthread_Thread
;
181 typedef mutex_t _glthread_Mutex
;
183 /* XXX need to really implement mutex-related macros */
184 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
185 #define _glthread_INIT_MUTEX(name) (void) name
186 #define _glthread_DESTROY_MUTEX(name) (void) name
187 #define _glthread_LOCK_MUTEX(name) (void) name
188 #define _glthread_UNLOCK_MUTEX(name) (void) name
190 #endif /* SOLARIS_THREADS */
196 * Windows threads. Should work with Windows NT and 95.
197 * IMPORTANT: Link with multithreaded runtime library when THREADS are
208 typedef HANDLE _glthread_Thread
;
210 typedef CRITICAL_SECTION _glthread_Mutex
;
212 #define _glthread_DECLARE_STATIC_MUTEX(name) /*static*/ _glthread_Mutex name = {0,0,0,0,0,0}
213 #define _glthread_INIT_MUTEX(name) InitializeCriticalSection(&name)
214 #define _glthread_DESTROY_MUTEX(name) DeleteCriticalSection(&name)
215 #define _glthread_LOCK_MUTEX(name) EnterCriticalSection(&name)
216 #define _glthread_UNLOCK_MUTEX(name) LeaveCriticalSection(&name)
218 #endif /* WIN32_THREADS */
224 * XFree86 has its own thread wrapper, Xthreads.h
225 * We wrap it again for GL.
228 #include <X11/Xthreads.h>
235 typedef xthread_t _glthread_Thread
;
237 typedef xmutex_rec _glthread_Mutex
;
239 #ifdef XMUTEX_INITIALIZER
240 #define _glthread_DECLARE_STATIC_MUTEX(name) \
241 static _glthread_Mutex name = XMUTEX_INITIALIZER
243 #define _glthread_DECLARE_STATIC_MUTEX(name) \
244 static _glthread_Mutex name
247 #define _glthread_INIT_MUTEX(name) \
250 #define _glthread_DESTROY_MUTEX(name) \
251 xmutex_clear(&(name))
253 #define _glthread_LOCK_MUTEX(name) \
254 (void) xmutex_lock(&(name))
256 #define _glthread_UNLOCK_MUTEX(name) \
257 (void) xmutex_unlock(&(name))
259 #endif /* USE_XTHREADS */
264 * BeOS threads. R5.x required.
268 #include <kernel/OS.h>
269 #include <support/TLS.h>
276 typedef thread_id _glthread_Thread
;
278 /* Use Benaphore, aka speeder semaphore */
283 typedef benaphore _glthread_Mutex
;
285 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = { 0, 0 }
286 #define _glthread_INIT_MUTEX(name) name.sem = create_sem(0, #name"_benaphore"), name.lock = 0
287 #define _glthread_DESTROY_MUTEX(name) delete_sem(name.sem), name.lock = 0
288 #define _glthread_LOCK_MUTEX(name) if (name.sem == 0) _glthread_INIT_MUTEX(name); \
289 if (atomic_add(&(name.lock), 1) >= 1) acquire_sem(name.sem)
290 #define _glthread_UNLOCK_MUTEX(name) if (atomic_add(&(name.lock), -1) > 1) release_sem(name.sem)
292 #endif /* BEOS_THREADS */
299 * THREADS not defined
302 typedef int _glthread_TSD
;
304 typedef int _glthread_Thread
;
306 typedef int _glthread_Mutex
;
308 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
310 #define _glthread_INIT_MUTEX(name) (void) name
312 #define _glthread_DESTROY_MUTEX(name) (void) name
314 #define _glthread_LOCK_MUTEX(name) (void) name
316 #define _glthread_UNLOCK_MUTEX(name) (void) name
323 * Platform independent thread specific data API.
327 _glthread_GetID(void);
331 _glthread_InitTSD(_glthread_TSD
*);
335 _glthread_GetTSD(_glthread_TSD
*);
339 _glthread_SetTSD(_glthread_TSD
*, void *);
341 #if defined(GLX_USE_TLS)
343 extern __thread
struct _glapi_table
* _glapi_tls_Dispatch
344 __attribute__((tls_model("initial-exec")));
346 #define GET_DISPATCH() _glapi_tls_Dispatch
348 #elif !defined(GL_CALL)
349 # if defined(THREADS)
350 # define GET_DISPATCH() \
351 ((__builtin_expect( _glapi_Dispatch != NULL, 1 )) \
352 ? _glapi_Dispatch : _glapi_get_dispatch())
354 # define GET_DISPATCH() _glapi_Dispatch
355 # endif /* defined(THREADS) */
356 #endif /* ndef GL_CALL */
359 #endif /* THREADS_H */