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(PTHREADS) || defined(WIN32_THREADS) || defined(BEOS_THREADS)
75 * POSIX threads. This should be your choice in the Unix world
76 * whenever possible. When building with POSIX threads, be sure
77 * to enable any compiler flags which will cause the MT-safe
78 * libc (if one exists) to be used when linking, as well as any
79 * header macros for MT-safe errno, etc. For Solaris, this is the -mt
80 * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable
81 * proper compiling for MT-safe libc etc.
84 #include <pthread.h> /* POSIX threads headers */
91 typedef pthread_t _glthread_Thread
;
93 typedef pthread_mutex_t _glthread_Mutex
;
95 #define _glthread_DECLARE_STATIC_MUTEX(name) \
96 static _glthread_Mutex name = PTHREAD_MUTEX_INITIALIZER
98 #define _glthread_INIT_MUTEX(name) \
99 pthread_mutex_init(&(name), NULL)
101 #define _glthread_DESTROY_MUTEX(name) \
102 pthread_mutex_destroy(&(name))
104 #define _glthread_LOCK_MUTEX(name) \
105 (void) pthread_mutex_lock(&(name))
107 #define _glthread_UNLOCK_MUTEX(name) \
108 (void) pthread_mutex_unlock(&(name))
110 #endif /* PTHREADS */
114 * Windows threads. Should work with Windows NT and 95.
115 * IMPORTANT: Link with multithreaded runtime library when THREADS are
126 typedef HANDLE _glthread_Thread
;
128 typedef CRITICAL_SECTION _glthread_Mutex
;
130 #define _glthread_DECLARE_STATIC_MUTEX(name) \
131 /* static */ _glthread_Mutex name = { 0, 0, 0, 0, 0, 0 }
133 #define _glthread_INIT_MUTEX(name) \
134 InitializeCriticalSection(&name)
136 #define _glthread_DESTROY_MUTEX(name) \
137 DeleteCriticalSection(&name)
139 #define _glthread_LOCK_MUTEX(name) \
140 EnterCriticalSection(&name)
142 #define _glthread_UNLOCK_MUTEX(name) \
143 LeaveCriticalSection(&name)
145 #endif /* WIN32_THREADS */
149 * BeOS threads. R5.x required.
153 /* Problem with OS.h and this file on haiku */
155 #include <kernel/OS.h>
158 #include <support/TLS.h>
160 /* The only two typedefs required here
161 * this is cause of the OS.h problem
164 typedef int32 thread_id
;
165 typedef int32 sem_id
;
173 typedef thread_id _glthread_Thread
;
175 /* Use Benaphore, aka speeder semaphore */
180 typedef benaphore _glthread_Mutex
;
182 #define _glthread_DECLARE_STATIC_MUTEX(name) \
183 static _glthread_Mutex name = { 0, 0 }
185 #define _glthread_INIT_MUTEX(name) \
186 name.sem = create_sem(0, #name"_benaphore"), \
189 #define _glthread_DESTROY_MUTEX(name) \
190 delete_sem(name.sem), \
193 #define _glthread_LOCK_MUTEX(name) \
195 _glthread_INIT_MUTEX(name); \
196 if (atomic_add(&(name.lock), 1) >= 1) \
197 acquire_sem(name.sem)
199 #define _glthread_UNLOCK_MUTEX(name) \
200 if (atomic_add(&(name.lock), -1) > 1) \
201 release_sem(name.sem)
203 #endif /* BEOS_THREADS */
207 * THREADS not defined
211 typedef unsigned _glthread_TSD
;
213 typedef unsigned _glthread_Thread
;
215 typedef unsigned _glthread_Mutex
;
217 #define _glthread_DECLARE_STATIC_MUTEX(name) static _glthread_Mutex name = 0
219 #define _glthread_INIT_MUTEX(name) (void) name
221 #define _glthread_DESTROY_MUTEX(name) (void) name
223 #define _glthread_LOCK_MUTEX(name) (void) name
225 #define _glthread_UNLOCK_MUTEX(name) (void) name
232 * Platform independent thread specific data API.
236 _glthread_GetID(void);
240 _glthread_InitTSD(_glthread_TSD
*);
244 _glthread_DestroyTSD(_glthread_TSD
*); /* WIN32 only */
248 _glthread_GetTSD(_glthread_TSD
*);
252 _glthread_SetTSD(_glthread_TSD
*, void *);
255 #endif /* THREADS_H */