2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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
33 * Modified for use in mapi by Chia-I Wu
37 * If this file is accidentally included by a non-threaded build,
38 * it should not cause the build to fail, or otherwise cause problems.
39 * In general, it should only be included when needed however.
47 #include "u_compiler.h"
49 #if defined(HAVE_PTHREAD)
50 #include <pthread.h> /* POSIX threads headers */
56 #if defined(HAVE_PTHREAD) || defined(_WIN32)
65 #define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data"
66 #define GET_TSD_ERROR "_glthread_: failed to get thread specific data"
67 #define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data"
71 * Magic number to determine if a TSD object has been initialized.
72 * Kind of a hack but there doesn't appear to be a better cross-platform
75 #define INIT_MAGIC 0xff8adc98
83 * POSIX threads. This should be your choice in the Unix world
84 * whenever possible. When building with POSIX threads, be sure
85 * to enable any compiler flags which will cause the MT-safe
86 * libc (if one exists) to be used when linking, as well as any
87 * header macros for MT-safe errno, etc. For Solaris, this is the -mt
88 * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable
89 * proper compiling for MT-safe libc etc.
91 #if defined(HAVE_PTHREAD)
98 typedef pthread_mutex_t u_mutex
;
100 #define u_mutex_declare_static(name) \
101 static u_mutex name = PTHREAD_MUTEX_INITIALIZER
103 #define u_mutex_init(name) pthread_mutex_init(&(name), NULL)
104 #define u_mutex_destroy(name) pthread_mutex_destroy(&(name))
105 #define u_mutex_lock(name) (void) pthread_mutex_lock(&(name))
106 #define u_mutex_unlock(name) (void) pthread_mutex_unlock(&(name))
108 static INLINE
unsigned long
111 return (unsigned long) pthread_self();
116 u_tsd_init(struct u_tsd
*tsd
)
118 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
119 perror(INIT_TSD_ERROR
);
122 tsd
->initMagic
= INIT_MAGIC
;
127 u_tsd_get(struct u_tsd
*tsd
)
129 if (tsd
->initMagic
!= INIT_MAGIC
) {
132 return pthread_getspecific(tsd
->key
);
137 u_tsd_set(struct u_tsd
*tsd
, void *ptr
)
139 if (tsd
->initMagic
!= INIT_MAGIC
) {
142 if (pthread_setspecific(tsd
->key
, ptr
) != 0) {
143 perror(SET_TSD_ERROR
);
148 #endif /* HAVE_PTHREAD */
152 * Windows threads. Should work with Windows NT and 95.
153 * IMPORTANT: Link with multithreaded runtime library when THREADS are
163 typedef CRITICAL_SECTION u_mutex
;
165 /* http://locklessinc.com/articles/pthreads_on_windows/ */
166 #define u_mutex_declare_static(name) \
167 static u_mutex name = {(PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0}
169 #define u_mutex_init(name) InitializeCriticalSection(&name)
170 #define u_mutex_destroy(name) DeleteCriticalSection(&name)
171 #define u_mutex_lock(name) EnterCriticalSection(&name)
172 #define u_mutex_unlock(name) LeaveCriticalSection(&name)
174 static INLINE
unsigned long
177 return GetCurrentThreadId();
182 u_tsd_init(struct u_tsd
*tsd
)
184 tsd
->key
= TlsAlloc();
185 if (tsd
->key
== TLS_OUT_OF_INDEXES
) {
186 perror(INIT_TSD_ERROR
);
189 tsd
->initMagic
= INIT_MAGIC
;
194 u_tsd_destroy(struct u_tsd
*tsd
)
196 if (tsd
->initMagic
!= INIT_MAGIC
) {
200 tsd
->initMagic
= 0x0;
205 u_tsd_get(struct u_tsd
*tsd
)
207 if (tsd
->initMagic
!= INIT_MAGIC
) {
210 return TlsGetValue(tsd
->key
);
215 u_tsd_set(struct u_tsd
*tsd
, void *ptr
)
217 /* the following code assumes that the struct u_tsd has been initialized
218 to zero at creation */
219 if (tsd
->initMagic
!= INIT_MAGIC
) {
222 if (TlsSetValue(tsd
->key
, ptr
) == 0) {
223 perror(SET_TSD_ERROR
);
232 * THREADS not defined
240 typedef unsigned u_mutex
;
242 #define u_mutex_declare_static(name) static u_mutex name = 0
243 #define u_mutex_init(name) (void) name
244 #define u_mutex_destroy(name) (void) name
245 #define u_mutex_lock(name) (void) name
246 #define u_mutex_unlock(name) (void) name
252 static INLINE
unsigned long
260 u_tsd_init(struct u_tsd
*tsd
)
267 u_tsd_get(struct u_tsd
*tsd
)
275 u_tsd_set(struct u_tsd
*tsd
, void *ptr
)
287 #endif /* _U_THREAD_H_ */