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. First, the glheader.h include must go.
31 #ifdef HAVE_DIX_CONFIG_H
32 #include <dix-config.h>
35 #include "main/glheader.h"
40 * This file should still compile even when THREADS is not defined.
41 * This is to make things easier to deal with on the makefile scene..
49 #define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data"
50 #define GET_TSD_ERROR "_glthread_: failed to get thread specific data"
51 #define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data"
55 * Magic number to determine if a TSD object has been initialized.
56 * Kind of a hack but there doesn't appear to be a better cross-platform
59 #define INIT_MAGIC 0xff8adc98
64 * POSIX Threads -- The best way to go if your platform supports them.
65 * Solaris >= 2.5 have POSIX threads, IRIX >= 6.4 reportedly
66 * has them, and many of the free Unixes now have them.
67 * Be sure to use appropriate -mt or -D_REENTRANT type
68 * compile flags when building.
75 return (unsigned long) pthread_self();
80 _glthread_InitTSD(_glthread_TSD
*tsd
)
82 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
83 perror(INIT_TSD_ERROR
);
86 tsd
->initMagic
= INIT_MAGIC
;
91 _glthread_GetTSD(_glthread_TSD
*tsd
)
93 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
94 _glthread_InitTSD(tsd
);
96 return pthread_getspecific(tsd
->key
);
101 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
103 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
104 _glthread_InitTSD(tsd
);
106 if (pthread_setspecific(tsd
->key
, ptr
) != 0) {
107 perror(SET_TSD_ERROR
);
112 #endif /* PTHREADS */
117 * Solaris/Unix International Threads -- Use only if POSIX threads
118 * aren't available on your Unix platform. Solaris 2.[34] are examples
119 * of platforms where this is the case. Be sure to use -mt and/or
120 * -D_REENTRANT when compiling.
122 #ifdef SOLARIS_THREADS
123 #define USE_LOCK_FOR_KEY /* undef this to try a version without
124 lock for the global key... */
127 _glthread_GetID(void)
129 abort(); /* XXX not implemented yet */
130 return (unsigned long) 0;
135 _glthread_InitTSD(_glthread_TSD
*tsd
)
137 if ((errno
= mutex_init(&tsd
->keylock
, 0, NULL
)) != 0 ||
138 (errno
= thr_keycreate(&(tsd
->key
), free
)) != 0) {
139 perror(INIT_TSD_ERROR
);
142 tsd
->initMagic
= INIT_MAGIC
;
147 _glthread_GetTSD(_glthread_TSD
*tsd
)
150 if (tsd
->initMagic
!= INIT_MAGIC
) {
151 _glthread_InitTSD(tsd
);
153 #ifdef USE_LOCK_FOR_KEY
154 mutex_lock(&tsd
->keylock
);
155 thr_getspecific(tsd
->key
, &ret
);
156 mutex_unlock(&tsd
->keylock
);
158 if ((errno
= thr_getspecific(tsd
->key
, &ret
)) != 0) {
159 perror(GET_TSD_ERROR
);
168 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
170 if (tsd
->initMagic
!= INIT_MAGIC
) {
171 _glthread_InitTSD(tsd
);
173 if ((errno
= thr_setspecific(tsd
->key
, ptr
)) != 0) {
174 perror(SET_TSD_ERROR
);
179 #undef USE_LOCK_FOR_KEY
180 #endif /* SOLARIS_THREADS */
185 * Win32 Threads. The only available option for Windows 95/NT.
186 * Be sure that you compile using the Multithreaded runtime, otherwise
187 * bad things will happen.
191 void FreeTSD(_glthread_TSD
*p
)
193 if (p
->initMagic
==INIT_MAGIC
) {
199 void InsteadOf_exit(int nCode
)
201 DWORD dwErr
=GetLastError();
205 _glthread_GetID(void)
207 return GetCurrentThreadId();
212 _glthread_InitTSD(_glthread_TSD
*tsd
)
214 tsd
->key
= TlsAlloc();
215 if (tsd
->key
== TLS_OUT_OF_INDEXES
) {
216 perror("Mesa:_glthread_InitTSD");
219 tsd
->initMagic
= INIT_MAGIC
;
224 _glthread_GetTSD(_glthread_TSD
*tsd
)
226 if (tsd
->initMagic
!= INIT_MAGIC
) {
227 _glthread_InitTSD(tsd
);
229 return TlsGetValue(tsd
->key
);
234 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
236 /* the following code assumes that the _glthread_TSD has been initialized
237 to zero at creation */
238 if (tsd
->initMagic
!= INIT_MAGIC
) {
239 _glthread_InitTSD(tsd
);
241 if (TlsSetValue(tsd
->key
, ptr
) == 0) {
242 perror("Mesa:_glthread_SetTSD");
247 #endif /* WIN32_THREADS */
252 * XFree86 has its own thread wrapper, Xthreads.h
253 * We wrap it again for GL.
258 _glthread_GetID(void)
260 return (unsigned long) xthread_self();
265 _glthread_InitTSD(_glthread_TSD
*tsd
)
267 if (xthread_key_create(&tsd
->key
, NULL
) != 0) {
268 perror(INIT_TSD_ERROR
);
271 tsd
->initMagic
= INIT_MAGIC
;
276 _glthread_GetTSD(_glthread_TSD
*tsd
)
279 if (tsd
->initMagic
!= INIT_MAGIC
) {
280 _glthread_InitTSD(tsd
);
282 xthread_get_specific(tsd
->key
, &ptr
);
288 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
290 if (tsd
->initMagic
!= INIT_MAGIC
) {
291 _glthread_InitTSD(tsd
);
293 xthread_set_specific(tsd
->key
, ptr
);
306 _glthread_GetID(void)
308 return (unsigned long) find_thread(NULL
);
312 _glthread_InitTSD(_glthread_TSD
*tsd
)
314 tsd
->key
= tls_allocate();
315 tsd
->initMagic
= INIT_MAGIC
;
319 _glthread_GetTSD(_glthread_TSD
*tsd
)
321 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
322 _glthread_InitTSD(tsd
);
324 return tls_get(tsd
->key
);
328 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
330 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
331 _glthread_InitTSD(tsd
);
333 tls_set(tsd
->key
, ptr
);
336 #endif /* BEOS_THREADS */
348 _glthread_GetID(void)
355 _glthread_InitTSD(_glthread_TSD
*tsd
)
362 _glthread_GetTSD(_glthread_TSD
*tsd
)
370 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)