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 #endif /* PTHREADS */
123 * Solaris threads. Use only up to Solaris 2.4.
124 * Solaris 2.5 and higher provide POSIX threads.
125 * Be sure to compile with -mt on the Solaris compilers, or
126 * use -D_REENTRANT if using gcc.
128 #ifdef SOLARIS_THREADS
137 typedef thread_t _glthread_Thread
;
139 typedef mutex_t _glthread_Mutex
;
141 /* XXX need to really implement mutex-related macros */
142 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
143 #define _glthread_INIT_MUTEX(name) (void) name
144 #define _glthread_DESTROY_MUTEX(name) (void) name
145 #define _glthread_LOCK_MUTEX(name) (void) name
146 #define _glthread_UNLOCK_MUTEX(name) (void) name
148 #endif /* SOLARIS_THREADS */
154 * Windows threads. Should work with Windows NT and 95.
155 * IMPORTANT: Link with multithreaded runtime library when THREADS are
166 typedef HANDLE _glthread_Thread
;
168 typedef CRITICAL_SECTION _glthread_Mutex
;
170 #define _glthread_DECLARE_STATIC_MUTEX(name) /*static*/ _glthread_Mutex name = {0,0,0,0,0,0}
171 #define _glthread_INIT_MUTEX(name) InitializeCriticalSection(&name)
172 #define _glthread_DESTROY_MUTEX(name) DeleteCriticalSection(&name)
173 #define _glthread_LOCK_MUTEX(name) EnterCriticalSection(&name)
174 #define _glthread_UNLOCK_MUTEX(name) LeaveCriticalSection(&name)
176 #endif /* WIN32_THREADS */
182 * XFree86 has its own thread wrapper, Xthreads.h
183 * We wrap it again for GL.
186 #include <X11/Xthreads.h>
193 typedef xthread_t _glthread_Thread
;
195 typedef xmutex_rec _glthread_Mutex
;
197 #ifdef XMUTEX_INITIALIZER
198 #define _glthread_DECLARE_STATIC_MUTEX(name) \
199 static _glthread_Mutex name = XMUTEX_INITIALIZER
201 #define _glthread_DECLARE_STATIC_MUTEX(name) \
202 static _glthread_Mutex name
205 #define _glthread_INIT_MUTEX(name) \
208 #define _glthread_DESTROY_MUTEX(name) \
209 xmutex_clear(&(name))
211 #define _glthread_LOCK_MUTEX(name) \
212 (void) xmutex_lock(&(name))
214 #define _glthread_UNLOCK_MUTEX(name) \
215 (void) xmutex_unlock(&(name))
217 #endif /* USE_XTHREADS */
222 * BeOS threads. R5.x required.
226 #include <kernel/OS.h>
227 #include <support/TLS.h>
234 typedef thread_id _glthread_Thread
;
236 /* Use Benaphore, aka speeder semaphore */
241 typedef benaphore _glthread_Mutex
;
243 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = { 0, 0 }
244 #define _glthread_INIT_MUTEX(name) name.sem = create_sem(0, #name"_benaphore"), name.lock = 0
245 #define _glthread_DESTROY_MUTEX(name) delete_sem(name.sem), name.lock = 0
246 #define _glthread_LOCK_MUTEX(name) if (name.sem == 0) _glthread_INIT_MUTEX(name); \
247 if (atomic_add(&(name.lock), 1) >= 1) acquire_sem(name.sem)
248 #define _glthread_UNLOCK_MUTEX(name) if (atomic_add(&(name.lock), -1) > 1) release_sem(name.sem)
250 #endif /* BEOS_THREADS */
257 * THREADS not defined
260 typedef unsigned _glthread_TSD
;
262 typedef unsigned _glthread_Thread
;
264 typedef unsigned _glthread_Mutex
;
266 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
268 #define _glthread_INIT_MUTEX(name) (void) name
270 #define _glthread_DESTROY_MUTEX(name) (void) name
272 #define _glthread_LOCK_MUTEX(name) (void) name
274 #define _glthread_UNLOCK_MUTEX(name) (void) name
281 * Platform independent thread specific data API.
285 _glthread_GetID(void);
289 _glthread_InitTSD(_glthread_TSD
*);
293 _glthread_GetTSD(_glthread_TSD
*);
297 _glthread_SetTSD(_glthread_TSD
*, void *);
299 #if defined(GLX_USE_TLS)
301 extern __thread
struct _glapi_table
* _glapi_tls_Dispatch
302 __attribute__((tls_model("initial-exec")));
304 #define GET_DISPATCH() _glapi_tls_Dispatch
306 #elif !defined(GL_CALL)
307 # if defined(THREADS)
308 # define GET_DISPATCH() \
309 ((__builtin_expect( _glapi_Dispatch != NULL, 1 )) \
310 ? _glapi_Dispatch : _glapi_get_dispatch())
312 # define GET_DISPATCH() _glapi_Dispatch
313 # endif /* defined(THREADS) */
314 #endif /* ndef GL_CALL */
317 #endif /* _P_THREAD_H_ */