3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Thread support for gl dispatch.
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
59 * If this file is accidentally included by a non-threaded build,
60 * it should not cause the build to fail, or otherwise cause problems.
61 * In general, it should only be included when needed however.
68 #if defined(PTHREADS) || defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || defined(XTHREADS)
73 #include <GL/vms_x_fix.h>
77 * POSIX threads. This should be your choice in the Unix world
78 * whenever possible. When building with POSIX threads, be sure
79 * to enable any compiler flags which will cause the MT-safe
80 * libc (if one exists) to be used when linking, as well as any
81 * header macros for MT-safe errno, etc. For Solaris, this is the -mt
82 * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable
83 * proper compiling for MT-safe libc etc.
86 #include <pthread.h> /* POSIX threads headers */
93 typedef pthread_t _glthread_Thread
;
95 typedef pthread_mutex_t _glthread_Mutex
;
97 #define _glthread_DECLARE_STATIC_MUTEX(name) \
98 static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER
100 #define _glthread_INIT_MUTEX(name) \
101 pthread_mutex_init(&(name), NULL)
103 #define _glthread_DESTROY_MUTEX(name) \
104 pthread_mutex_destroy(&(name))
106 #define _glthread_LOCK_MUTEX(name) \
107 (void) pthread_mutex_lock(&(name))
109 #define _glthread_UNLOCK_MUTEX(name) \
110 (void) pthread_mutex_unlock(&(name))
112 #endif /* PTHREADS */
118 * Solaris threads. Use only up to Solaris 2.4.
119 * Solaris 2.5 and higher provide POSIX threads.
120 * Be sure to compile with -mt on the Solaris compilers, or
121 * use -D_REENTRANT if using gcc.
123 #ifdef SOLARIS_THREADS
132 typedef thread_t _glthread_Thread
;
134 typedef mutex_t _glthread_Mutex
;
136 /* XXX need to really implement mutex-related macros */
137 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
138 #define _glthread_INIT_MUTEX(name) (void) name
139 #define _glthread_DESTROY_MUTEX(name) (void) name
140 #define _glthread_LOCK_MUTEX(name) (void) name
141 #define _glthread_UNLOCK_MUTEX(name) (void) name
143 #endif /* SOLARIS_THREADS */
149 * Windows threads. Should work with Windows NT and 95.
150 * IMPORTANT: Link with multithreaded runtime library when THREADS are
161 typedef HANDLE _glthread_Thread
;
163 typedef CRITICAL_SECTION _glthread_Mutex
;
165 /* XXX need to really implement mutex-related macros */
166 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
167 #define _glthread_INIT_MUTEX(name) (void) name
168 #define _glthread_DESTROY_MUTEX(name) (void) name
169 #define _glthread_LOCK_MUTEX(name) (void) name
170 #define _glthread_UNLOCK_MUTEX(name) (void) name
172 #endif /* WIN32_THREADS */
178 * XFree86 has its own thread wrapper, Xthreads.h
179 * We wrap it again for GL.
182 #include "Xthreads.h"
189 typedef xthread_t _glthread_Thread
;
191 typedef xmutex_rec _glthread_Mutex
;
193 #ifdef XMUTEX_INITIALIZER
194 #define _glthread_DECLARE_STATIC_MUTEX(name) \
195 static _glthread_Mutex name = XMUTEX_INITIALIZER
197 #define _glthread_DECLARE_STATIC_MUTEX(name) \
198 static _glthread_Mutex name
201 #define _glthread_INIT_MUTEX(name) \
204 #define _glthread_DESTROY_MUTEX(name) \
205 xmutex_clear(&(name))
207 #define _glthread_LOCK_MUTEX(name) \
208 (void) xmutex_lock(&(name))
210 #define _glthread_UNLOCK_MUTEX(name) \
211 (void) xmutex_unlock(&(name))
213 #endif /* XTHREADS */
218 * BeOS threads. R5.x required.
221 #include <kernel/OS.h>
222 #include <support/TLS.h>
229 typedef thread_id _glthread_Thread
;
231 /* Use Benaphore, aka speeder semaphore */
236 typedef benaphore _glthread_Mutex
;
238 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = { 0,
239 create_sem(0, #name"_benaphore") }
240 #define _glthread_INIT_MUTEX(name) name.sem = create_sem(0, #name"_benaphore"), name.lock = 0
241 #define _glthread_LOCK_MUTEX(name) if((atomic_add(&(name.lock), 1)) >= 1) acquire_sem(name.sem)
242 #define _glthread_UNLOCK_MUTEX(name) if((atomic_add(&(name.lock), -1)) > 1) release_sem(name.sem)
244 #endif /* BEOS_THREADS */
251 * THREADS not defined
254 typedef GLuint _glthread_TSD
;
256 typedef GLuint _glthread_Thread
;
258 typedef GLuint _glthread_Mutex
;
260 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
262 #define _glthread_INIT_MUTEX(name) (void) name
264 #define _glthread_DESTROY_MUTEX(name) (void) name
266 #define _glthread_LOCK_MUTEX(name) (void) name
268 #define _glthread_UNLOCK_MUTEX(name) (void) name
275 * Platform independent thread specific data API.
279 _glthread_GetID(void);
283 _glthread_InitTSD(_glthread_TSD
*);
287 _glthread_GetTSD(_glthread_TSD
*);
291 _glthread_SetTSD(_glthread_TSD
*, void *);
295 #endif /* THREADS_H */