1 /**************************************************************************
3 * Copyright 1999-2006 Brian Paul
4 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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.
24 **************************************************************************/
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
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
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
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
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.
65 #if defined(USE_MGL_NAMESPACE)
66 #define _glapi_Dispatch _mglapi_Dispatch
71 #if (defined(PTHREADS) || defined(SOLARIS_THREADS) ||\
72 defined(WIN32_THREADS) || defined(USE_XTHREADS) || defined(BEOS_THREADS)) \
78 #include <GL/vms_x_fix.h>
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.
91 #include <pthread.h> /* POSIX threads headers */
98 typedef pthread_t _glthread_Thread
;
100 typedef pthread_mutex_t _glthread_Mutex
;
102 #define _glthread_DECLARE_STATIC_MUTEX(name) \
103 static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER
105 #define _glthread_INIT_MUTEX(name) \
106 pthread_mutex_init(&(name), NULL)
108 #define _glthread_DESTROY_MUTEX(name) \
109 pthread_mutex_destroy(&(name))
111 #define _glthread_LOCK_MUTEX(name) \
112 (void) pthread_mutex_lock(&(name))
114 #define _glthread_UNLOCK_MUTEX(name) \
115 (void) pthread_mutex_unlock(&(name))
117 typedef pthread_cond_t _glthread_Cond
;
119 #define _glthread_DECLARE_STATIC_COND(name) \
120 static _glthread_Cond name = PTHREAD_COND_INITIALIZER
122 #define _glthread_INIT_COND(cond) \
123 pthread_cond_init(&(cond), NULL)
125 #define _glthread_DESTROY_COND(name) \
126 pthread_cond_destroy(&(name))
128 #define _glthread_COND_WAIT(cond, mutex) \
129 pthread_cond_wait(&(cond), &(mutex))
131 #define _glthread_COND_SIGNAL(cond) \
132 pthread_cond_signal(&(cond))
134 #define _glthread_COND_BROADCAST(cond) \
135 pthread_cond_broadcast(&(cond))
137 #endif /* PTHREADS */
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.
148 #ifdef SOLARIS_THREADS
157 typedef thread_t _glthread_Thread
;
159 typedef mutex_t _glthread_Mutex
;
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
168 #endif /* SOLARIS_THREADS */
174 * Windows threads. Should work with Windows NT and 95.
175 * IMPORTANT: Link with multithreaded runtime library when THREADS are
186 typedef HANDLE _glthread_Thread
;
188 typedef CRITICAL_SECTION _glthread_Mutex
;
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)
196 #endif /* WIN32_THREADS */
202 * XFree86 has its own thread wrapper, Xthreads.h
203 * We wrap it again for GL.
206 #include <X11/Xthreads.h>
213 typedef xthread_t _glthread_Thread
;
215 typedef xmutex_rec _glthread_Mutex
;
217 #ifdef XMUTEX_INITIALIZER
218 #define _glthread_DECLARE_STATIC_MUTEX(name) \
219 static _glthread_Mutex name = XMUTEX_INITIALIZER
221 #define _glthread_DECLARE_STATIC_MUTEX(name) \
222 static _glthread_Mutex name
225 #define _glthread_INIT_MUTEX(name) \
228 #define _glthread_DESTROY_MUTEX(name) \
229 xmutex_clear(&(name))
231 #define _glthread_LOCK_MUTEX(name) \
232 (void) xmutex_lock(&(name))
234 #define _glthread_UNLOCK_MUTEX(name) \
235 (void) xmutex_unlock(&(name))
237 #endif /* USE_XTHREADS */
242 * BeOS threads. R5.x required.
246 #include <kernel/OS.h>
247 #include <support/TLS.h>
254 typedef thread_id _glthread_Thread
;
256 /* Use Benaphore, aka speeder semaphore */
261 typedef benaphore _glthread_Mutex
;
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)
270 #endif /* BEOS_THREADS */
277 * THREADS not defined
280 typedef unsigned _glthread_TSD
;
282 typedef unsigned _glthread_Thread
;
284 typedef unsigned _glthread_Mutex
;
286 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
288 #define _glthread_INIT_MUTEX(name) (void) name
290 #define _glthread_DESTROY_MUTEX(name) (void) name
292 #define _glthread_LOCK_MUTEX(name) (void) name
294 #define _glthread_UNLOCK_MUTEX(name) (void) name
296 typedef unsigned _glthread_Cond
;
298 #define _glthread_DECLARE_STATIC_COND(name) static _glthread_Cond name = 0
300 #define _glthread_INIT_COND(name) (void) name
302 #define _glthread_DESTROY_COND(name) (void) name
304 #define _glthread_COND_WAIT(name, mutex) (void) name
306 #define _glthread_COND_SIGNAL(name) (void) name
308 #define _glthread_COND_BROADCAST(name) (void) name
315 * Platform independent thread specific data API.
319 _glthread_GetID(void);
323 _glthread_InitTSD(_glthread_TSD
*);
327 _glthread_GetTSD(_glthread_TSD
*);
331 _glthread_SetTSD(_glthread_TSD
*, void *);
333 #if defined(GLX_USE_TLS)
335 extern __thread
struct _glapi_table
* _glapi_tls_Dispatch
336 __attribute__((tls_model("initial-exec")));
338 #define GET_DISPATCH() _glapi_tls_Dispatch
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())
346 # define GET_DISPATCH() _glapi_Dispatch
347 # endif /* defined(THREADS) */
348 #endif /* ndef GL_CALL */
351 #endif /* _P_THREAD_H_ */