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 * XXX There's probably some work to do in order to make this file
28 * truly reusable outside of Mesa.
32 #ifdef HAVE_DIX_CONFIG_H
33 #include <dix-config.h>
41 * This file should still compile even when THREADS is not defined.
42 * This is to make things easier to deal with on the makefile scene..
50 #define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data"
51 #define GET_TSD_ERROR "_glthread_: failed to get thread specific data"
52 #define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data"
56 * Magic number to determine if a TSD object has been initialized.
57 * Kind of a hack but there doesn't appear to be a better cross-platform
60 #define INIT_MAGIC 0xff8adc98
65 * POSIX Threads -- The best way to go if your platform supports them.
66 * Solaris >= 2.5 have POSIX threads, IRIX >= 6.4 reportedly
67 * has them, and many of the free Unixes now have them.
68 * Be sure to use appropriate -mt or -D_REENTRANT type
69 * compile flags when building.
76 return (unsigned long) pthread_self();
81 _glthread_InitTSD(_glthread_TSD
*tsd
)
83 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
84 perror(INIT_TSD_ERROR
);
87 tsd
->initMagic
= INIT_MAGIC
;
92 _glthread_GetTSD(_glthread_TSD
*tsd
)
94 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
95 _glthread_InitTSD(tsd
);
97 return pthread_getspecific(tsd
->key
);
102 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
104 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
105 _glthread_InitTSD(tsd
);
107 if (pthread_setspecific(tsd
->key
, ptr
) != 0) {
108 perror(SET_TSD_ERROR
);
113 #endif /* PTHREADS */
118 * Solaris/Unix International Threads -- Use only if POSIX threads
119 * aren't available on your Unix platform. Solaris 2.[34] are examples
120 * of platforms where this is the case. Be sure to use -mt and/or
121 * -D_REENTRANT when compiling.
123 #ifdef SOLARIS_THREADS
124 #define USE_LOCK_FOR_KEY /* undef this to try a version without
125 lock for the global key... */
128 _glthread_GetID(void)
130 abort(); /* XXX not implemented yet */
131 return (unsigned long) 0;
136 _glthread_InitTSD(_glthread_TSD
*tsd
)
138 if ((errno
= mutex_init(&tsd
->keylock
, 0, NULL
)) != 0 ||
139 (errno
= thr_keycreate(&(tsd
->key
), free
)) != 0) {
140 perror(INIT_TSD_ERROR
);
143 tsd
->initMagic
= INIT_MAGIC
;
148 _glthread_GetTSD(_glthread_TSD
*tsd
)
151 if (tsd
->initMagic
!= INIT_MAGIC
) {
152 _glthread_InitTSD(tsd
);
154 #ifdef USE_LOCK_FOR_KEY
155 mutex_lock(&tsd
->keylock
);
156 thr_getspecific(tsd
->key
, &ret
);
157 mutex_unlock(&tsd
->keylock
);
159 if ((errno
= thr_getspecific(tsd
->key
, &ret
)) != 0) {
160 perror(GET_TSD_ERROR
);
169 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
171 if (tsd
->initMagic
!= INIT_MAGIC
) {
172 _glthread_InitTSD(tsd
);
174 if ((errno
= thr_setspecific(tsd
->key
, ptr
)) != 0) {
175 perror(SET_TSD_ERROR
);
180 #undef USE_LOCK_FOR_KEY
181 #endif /* SOLARIS_THREADS */
186 * Win32 Threads. The only available option for Windows 95/NT.
187 * Be sure that you compile using the Multithreaded runtime, otherwise
188 * bad things will happen.
192 void FreeTSD(_glthread_TSD
*p
)
194 if (p
->initMagic
==INIT_MAGIC
) {
200 void InsteadOf_exit(int nCode
)
202 DWORD dwErr
=GetLastError();
206 _glthread_GetID(void)
208 return GetCurrentThreadId();
213 _glthread_InitTSD(_glthread_TSD
*tsd
)
215 tsd
->key
= TlsAlloc();
216 if (tsd
->key
== TLS_OUT_OF_INDEXES
) {
217 perror("Mesa:_glthread_InitTSD");
220 tsd
->initMagic
= INIT_MAGIC
;
225 _glthread_GetTSD(_glthread_TSD
*tsd
)
227 if (tsd
->initMagic
!= INIT_MAGIC
) {
228 _glthread_InitTSD(tsd
);
230 return TlsGetValue(tsd
->key
);
235 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
237 /* the following code assumes that the _glthread_TSD has been initialized
238 to zero at creation */
239 if (tsd
->initMagic
!= INIT_MAGIC
) {
240 _glthread_InitTSD(tsd
);
242 if (TlsSetValue(tsd
->key
, ptr
) == 0) {
243 perror("Mesa:_glthread_SetTSD");
248 #endif /* WIN32_THREADS */
253 * XFree86 has its own thread wrapper, Xthreads.h
254 * We wrap it again for GL.
259 _glthread_GetID(void)
261 return (unsigned long) xthread_self();
266 _glthread_InitTSD(_glthread_TSD
*tsd
)
268 if (xthread_key_create(&tsd
->key
, NULL
) != 0) {
269 perror(INIT_TSD_ERROR
);
272 tsd
->initMagic
= INIT_MAGIC
;
277 _glthread_GetTSD(_glthread_TSD
*tsd
)
280 if (tsd
->initMagic
!= INIT_MAGIC
) {
281 _glthread_InitTSD(tsd
);
283 xthread_get_specific(tsd
->key
, &ptr
);
289 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
291 if (tsd
->initMagic
!= INIT_MAGIC
) {
292 _glthread_InitTSD(tsd
);
294 xthread_set_specific(tsd
->key
, ptr
);
307 _glthread_GetID(void)
309 return (unsigned long) find_thread(NULL
);
313 _glthread_InitTSD(_glthread_TSD
*tsd
)
315 tsd
->key
= tls_allocate();
316 tsd
->initMagic
= INIT_MAGIC
;
320 _glthread_GetTSD(_glthread_TSD
*tsd
)
322 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
323 _glthread_InitTSD(tsd
);
325 return tls_get(tsd
->key
);
329 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
331 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
332 _glthread_InitTSD(tsd
);
334 tls_set(tsd
->key
, ptr
);
337 #endif /* BEOS_THREADS */
349 _glthread_GetID(void)
356 _glthread_InitTSD(_glthread_TSD
*tsd
)
363 _glthread_GetTSD(_glthread_TSD
*tsd
)
371 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)