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>
42 * This file should still compile even when THREADS is not defined.
43 * This is to make things easier to deal with on the makefile scene..
51 #define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data"
52 #define GET_TSD_ERROR "_glthread_: failed to get thread specific data"
53 #define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data"
57 * Magic number to determine if a TSD object has been initialized.
58 * Kind of a hack but there doesn't appear to be a better cross-platform
61 #define INIT_MAGIC 0xff8adc98
66 * POSIX Threads -- The best way to go if your platform supports them.
67 * Solaris >= 2.5 have POSIX threads, IRIX >= 6.4 reportedly
68 * has them, and many of the free Unixes now have them.
69 * Be sure to use appropriate -mt or -D_REENTRANT type
70 * compile flags when building.
77 return (unsigned long) pthread_self();
82 _glthread_InitTSD(_glthread_TSD
*tsd
)
84 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
85 perror(INIT_TSD_ERROR
);
88 tsd
->initMagic
= INIT_MAGIC
;
93 _glthread_GetTSD(_glthread_TSD
*tsd
)
95 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
96 _glthread_InitTSD(tsd
);
98 return pthread_getspecific(tsd
->key
);
103 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
105 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
106 _glthread_InitTSD(tsd
);
108 if (pthread_setspecific(tsd
->key
, ptr
) != 0) {
109 perror(SET_TSD_ERROR
);
114 #endif /* PTHREADS */
119 * Solaris/Unix International Threads -- Use only if POSIX threads
120 * aren't available on your Unix platform. Solaris 2.[34] are examples
121 * of platforms where this is the case. Be sure to use -mt and/or
122 * -D_REENTRANT when compiling.
124 #ifdef SOLARIS_THREADS
125 #define USE_LOCK_FOR_KEY /* undef this to try a version without
126 lock for the global key... */
129 _glthread_GetID(void)
131 abort(); /* XXX not implemented yet */
132 return (unsigned long) 0;
137 _glthread_InitTSD(_glthread_TSD
*tsd
)
139 if ((errno
= mutex_init(&tsd
->keylock
, 0, NULL
)) != 0 ||
140 (errno
= thr_keycreate(&(tsd
->key
), free
)) != 0) {
141 perror(INIT_TSD_ERROR
);
144 tsd
->initMagic
= INIT_MAGIC
;
149 _glthread_GetTSD(_glthread_TSD
*tsd
)
152 if (tsd
->initMagic
!= INIT_MAGIC
) {
153 _glthread_InitTSD(tsd
);
155 #ifdef USE_LOCK_FOR_KEY
156 mutex_lock(&tsd
->keylock
);
157 thr_getspecific(tsd
->key
, &ret
);
158 mutex_unlock(&tsd
->keylock
);
160 if ((errno
= thr_getspecific(tsd
->key
, &ret
)) != 0) {
161 perror(GET_TSD_ERROR
);
170 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
172 if (tsd
->initMagic
!= INIT_MAGIC
) {
173 _glthread_InitTSD(tsd
);
175 if ((errno
= thr_setspecific(tsd
->key
, ptr
)) != 0) {
176 perror(SET_TSD_ERROR
);
181 #undef USE_LOCK_FOR_KEY
182 #endif /* SOLARIS_THREADS */
187 * Win32 Threads. The only available option for Windows 95/NT.
188 * Be sure that you compile using the Multithreaded runtime, otherwise
189 * bad things will happen.
193 void FreeTSD(_glthread_TSD
*p
)
195 if (p
->initMagic
==INIT_MAGIC
) {
201 void InsteadOf_exit(int nCode
)
203 DWORD dwErr
=GetLastError();
207 _glthread_GetID(void)
209 return GetCurrentThreadId();
214 _glthread_InitTSD(_glthread_TSD
*tsd
)
216 tsd
->key
= TlsAlloc();
217 if (tsd
->key
== TLS_OUT_OF_INDEXES
) {
218 perror("Mesa:_glthread_InitTSD");
221 tsd
->initMagic
= INIT_MAGIC
;
226 _glthread_GetTSD(_glthread_TSD
*tsd
)
228 if (tsd
->initMagic
!= INIT_MAGIC
) {
229 _glthread_InitTSD(tsd
);
231 return TlsGetValue(tsd
->key
);
236 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
238 /* the following code assumes that the _glthread_TSD has been initialized
239 to zero at creation */
240 if (tsd
->initMagic
!= INIT_MAGIC
) {
241 _glthread_InitTSD(tsd
);
243 if (TlsSetValue(tsd
->key
, ptr
) == 0) {
244 perror("Mesa:_glthread_SetTSD");
249 #endif /* WIN32_THREADS */
254 * XFree86 has its own thread wrapper, Xthreads.h
255 * We wrap it again for GL.
260 _glthread_GetID(void)
262 return (unsigned long) xthread_self();
267 _glthread_InitTSD(_glthread_TSD
*tsd
)
269 if (xthread_key_create(&tsd
->key
, NULL
) != 0) {
270 perror(INIT_TSD_ERROR
);
273 tsd
->initMagic
= INIT_MAGIC
;
278 _glthread_GetTSD(_glthread_TSD
*tsd
)
281 if (tsd
->initMagic
!= INIT_MAGIC
) {
282 _glthread_InitTSD(tsd
);
284 xthread_get_specific(tsd
->key
, &ptr
);
290 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
292 if (tsd
->initMagic
!= INIT_MAGIC
) {
293 _glthread_InitTSD(tsd
);
295 xthread_set_specific(tsd
->key
, ptr
);
308 _glthread_GetID(void)
310 return (unsigned long) find_thread(NULL
);
314 _glthread_InitTSD(_glthread_TSD
*tsd
)
316 tsd
->key
= tls_allocate();
317 tsd
->initMagic
= INIT_MAGIC
;
321 _glthread_GetTSD(_glthread_TSD
*tsd
)
323 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
324 _glthread_InitTSD(tsd
);
326 return tls_get(tsd
->key
);
330 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
332 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
333 _glthread_InitTSD(tsd
);
335 tls_set(tsd
->key
, ptr
);
338 #endif /* BEOS_THREADS */
350 _glthread_GetID(void)
357 _glthread_InitTSD(_glthread_TSD
*tsd
)
364 _glthread_GetTSD(_glthread_TSD
*tsd
)
372 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)