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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * 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
34 * Modified for use in mapi by Chia-I Wu
38 * If this file is accidentally included by a non-threaded build,
39 * it should not cause the build to fail, or otherwise cause problems.
40 * In general, it should only be included when needed however.
48 #include "u_compiler.h"
50 #if defined(HAVE_PTHREAD)
51 #include <pthread.h> /* POSIX threads headers */
57 #if defined(HAVE_PTHREAD) || defined(_WIN32)
66 #define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data"
67 #define GET_TSD_ERROR "_glthread_: failed to get thread specific data"
68 #define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data"
72 * Magic number to determine if a TSD object has been initialized.
73 * Kind of a hack but there doesn't appear to be a better cross-platform
76 #define INIT_MAGIC 0xff8adc98
84 * POSIX threads. This should be your choice in the Unix world
85 * whenever possible. When building with POSIX threads, be sure
86 * to enable any compiler flags which will cause the MT-safe
87 * libc (if one exists) to be used when linking, as well as any
88 * header macros for MT-safe errno, etc. For Solaris, this is the -mt
89 * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable
90 * proper compiling for MT-safe libc etc.
92 #if defined(HAVE_PTHREAD)
99 typedef pthread_mutex_t u_mutex
;
101 #define u_mutex_declare_static(name) \
102 static u_mutex name = PTHREAD_MUTEX_INITIALIZER
104 #define u_mutex_init(name) pthread_mutex_init(&(name), NULL)
105 #define u_mutex_destroy(name) pthread_mutex_destroy(&(name))
106 #define u_mutex_lock(name) (void) pthread_mutex_lock(&(name))
107 #define u_mutex_unlock(name) (void) pthread_mutex_unlock(&(name))
109 static INLINE
unsigned long
112 return (unsigned long) pthread_self();
117 u_tsd_init(struct u_tsd
*tsd
)
119 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
120 perror(INIT_TSD_ERROR
);
123 tsd
->initMagic
= INIT_MAGIC
;
128 u_tsd_get(struct u_tsd
*tsd
)
130 if (tsd
->initMagic
!= INIT_MAGIC
) {
133 return pthread_getspecific(tsd
->key
);
138 u_tsd_set(struct u_tsd
*tsd
, void *ptr
)
140 if (tsd
->initMagic
!= INIT_MAGIC
) {
143 if (pthread_setspecific(tsd
->key
, ptr
) != 0) {
144 perror(SET_TSD_ERROR
);
149 #endif /* HAVE_PTHREAD */
153 * Windows threads. Should work with Windows NT and 95.
154 * IMPORTANT: Link with multithreaded runtime library when THREADS are
164 typedef CRITICAL_SECTION u_mutex
;
166 /* http://locklessinc.com/articles/pthreads_on_windows/ */
167 #define u_mutex_declare_static(name) \
168 static u_mutex name = {(PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0}
170 #define u_mutex_init(name) InitializeCriticalSection(&name)
171 #define u_mutex_destroy(name) DeleteCriticalSection(&name)
172 #define u_mutex_lock(name) EnterCriticalSection(&name)
173 #define u_mutex_unlock(name) LeaveCriticalSection(&name)
175 static INLINE
unsigned long
178 return GetCurrentThreadId();
183 u_tsd_init(struct u_tsd
*tsd
)
185 tsd
->key
= TlsAlloc();
186 if (tsd
->key
== TLS_OUT_OF_INDEXES
) {
187 perror(INIT_TSD_ERROR
);
190 tsd
->initMagic
= INIT_MAGIC
;
195 u_tsd_destroy(struct u_tsd
*tsd
)
197 if (tsd
->initMagic
!= INIT_MAGIC
) {
201 tsd
->initMagic
= 0x0;
206 u_tsd_get(struct u_tsd
*tsd
)
208 if (tsd
->initMagic
!= INIT_MAGIC
) {
211 return TlsGetValue(tsd
->key
);
216 u_tsd_set(struct u_tsd
*tsd
, void *ptr
)
218 /* the following code assumes that the struct u_tsd has been initialized
219 to zero at creation */
220 if (tsd
->initMagic
!= INIT_MAGIC
) {
223 if (TlsSetValue(tsd
->key
, ptr
) == 0) {
224 perror(SET_TSD_ERROR
);
233 * THREADS not defined
241 typedef unsigned u_mutex
;
243 #define u_mutex_declare_static(name) static u_mutex name = 0
244 #define u_mutex_init(name) (void) name
245 #define u_mutex_destroy(name) (void) name
246 #define u_mutex_lock(name) (void) name
247 #define u_mutex_unlock(name) (void) name
253 static INLINE
unsigned long
261 u_tsd_init(struct u_tsd
*tsd
)
268 u_tsd_get(struct u_tsd
*tsd
)
276 u_tsd_set(struct u_tsd
*tsd
, void *ptr
)
288 #endif /* _U_THREAD_H_ */