1 /* $Id: glthread.h,v 1.12 2002/12/12 13:03:15 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * Thread support for gl dispatch.
31 * Initial version by John Stone (j.stone@acm.org) (johns@cs.umr.edu)
32 * and Christoph Poliwoda (poliwoda@volumegraphics.com)
33 * Revised by Keith Whitwell
34 * Adapted for new gl dispatcher by Brian Paul
40 * This thread module exports the following types:
41 * _glthread_TSD Thread-specific data area
42 * _glthread_Thread Thread datatype
43 * _glthread_Mutex Mutual exclusion lock
46 * _glthread_DECLARE_STATIC_MUTEX(name) Declare a non-local mutex
47 * _glthread_INIT_MUTEX(name) Initialize a mutex
48 * _glthread_LOCK_MUTEX(name) Lock a mutex
49 * _glthread_UNLOCK_MUTEX(name) Unlock a mutex
52 * _glthread_GetID(v) Get integer thread ID
53 * _glthread_InitTSD() Initialize thread-specific data
54 * _glthread_GetTSD() Get thread-specific data
55 * _glthread_SetTSD() Set thread-specific data
60 * If this file is accidentally included by a non-threaded build,
61 * it should not cause the build to fail, or otherwise cause problems.
62 * In general, it should only be included when needed however.
69 #if defined(PTHREADS) || defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || defined(XTHREADS)
74 #include <GL/vms_x_fix.h>
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.
87 #include <pthread.h> /* POSIX threads headers */
94 typedef pthread_t _glthread_Thread
;
96 typedef pthread_mutex_t _glthread_Mutex
;
98 #define _glthread_DECLARE_STATIC_MUTEX(name) \
99 static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER
101 #define _glthread_INIT_MUTEX(name) \
102 pthread_mutex_init(&(name), NULL)
104 #define _glthread_DESTROY_MUTEX(name) \
105 pthread_mutex_destroy(&(name))
107 #define _glthread_LOCK_MUTEX(name) \
108 (void) pthread_mutex_lock(&(name))
110 #define _glthread_UNLOCK_MUTEX(name) \
111 (void) pthread_mutex_unlock(&(name))
113 #endif /* PTHREADS */
119 * Solaris threads. Use only up to Solaris 2.4.
120 * Solaris 2.5 and higher provide POSIX threads.
121 * Be sure to compile with -mt on the Solaris compilers, or
122 * use -D_REENTRANT if using gcc.
124 #ifdef SOLARIS_THREADS
133 typedef thread_t _glthread_Thread
;
135 typedef mutex_t _glthread_Mutex
;
137 /* XXX need to really implement mutex-related macros */
138 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
139 #define _glthread_INIT_MUTEX(name) (void) name
140 #define _glthread_DESTROY_MUTEX(name) (void) name
141 #define _glthread_LOCK_MUTEX(name) (void) name
142 #define _glthread_UNLOCK_MUTEX(name) (void) name
144 #endif /* SOLARIS_THREADS */
150 * Windows threads. Should work with Windows NT and 95.
151 * IMPORTANT: Link with multithreaded runtime library when THREADS are
162 typedef HANDLE _glthread_Thread
;
164 typedef CRITICAL_SECTION _glthread_Mutex
;
166 /* XXX need to really implement mutex-related macros */
167 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
168 #define _glthread_INIT_MUTEX(name) (void) name
169 #define _glthread_DESTROY_MUTEX(name) (void) name
170 #define _glthread_LOCK_MUTEX(name) (void) name
171 #define _glthread_UNLOCK_MUTEX(name) (void) name
173 #endif /* WIN32_THREADS */
179 * XFree86 has its own thread wrapper, Xthreads.h
180 * We wrap it again for GL.
183 #include "Xthreads.h"
190 typedef xthread_t _glthread_Thread
;
192 typedef xmutex_rec _glthread_Mutex
;
194 #ifdef XMUTEX_INITIALIZER
195 #define _glthread_DECLARE_STATIC_MUTEX(name) \
196 static _glthread_Mutex name = XMUTEX_INITIALIZER
198 #define _glthread_DECLARE_STATIC_MUTEX(name) \
199 static _glthread_Mutex name
202 #define _glthread_INIT_MUTEX(name) \
205 #define _glthread_DESTROY_MUTEX(name) \
206 xmutex_clear(&(name))
208 #define _glthread_LOCK_MUTEX(name) \
209 (void) xmutex_lock(&(name))
211 #define _glthread_UNLOCK_MUTEX(name) \
212 (void) xmutex_unlock(&(name))
214 #endif /* XTHREADS */
219 * BeOS threads. R5.x required.
222 #include <kernel/OS.h>
223 #include <support/TLS.h>
230 typedef thread_id _glthread_Thread
;
232 /* Use Benaphore, aka speeder semaphore */
237 typedef benaphore _glthread_Mutex
;
239 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = { 0,
240 create_sem(0, #name"_benaphore") }
241 #define _glthread_INIT_MUTEX(name) name.sem = create_sem(0, #name"_benaphore"), name.lock = 0
242 #define _glthread_LOCK_MUTEX(name) if((atomic_add(&(name.lock), 1)) >= 1) acquire_sem(name.sem)
243 #define _glthread_UNLOCK_MUTEX(name) if((atomic_add(&(name.lock), -1)) > 1) release_sem(name.sem)
245 #endif /* BEOS_THREADS */
252 * THREADS not defined
255 typedef GLuint _glthread_TSD
;
257 typedef GLuint _glthread_Thread
;
259 typedef GLuint _glthread_Mutex
;
261 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
263 #define _glthread_INIT_MUTEX(name) (void) name
265 #define _glthread_DESTROY_MUTEX(name) (void) name
267 #define _glthread_LOCK_MUTEX(name) (void) name
269 #define _glthread_UNLOCK_MUTEX(name) (void) name
276 * Platform independent thread specific data API.
280 _glthread_GetID(void);
284 _glthread_InitTSD(_glthread_TSD
*);
288 _glthread_GetTSD(_glthread_TSD
*);
292 _glthread_SetTSD(_glthread_TSD
*, void *);
296 #endif /* THREADS_H */