3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * XXX There's probably some work to do in order to make this file
29 * truly reusable outside of Mesa. First, the glheader.h include must go.
38 * This file should still compile even when THREADS is not defined.
39 * This is to make things easier to deal with on the makefile scene..
47 #define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data"
48 #define GET_TSD_ERROR "_glthread_: failed to get thread specific data"
49 #define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data"
53 * Magic number to determine if a TSD object has been initialized.
54 * Kind of a hack but there doesn't appear to be a better cross-platform
57 #define INIT_MAGIC 0xff8adc98
62 * POSIX Threads -- The best way to go if your platform supports them.
63 * Solaris >= 2.5 have POSIX threads, IRIX >= 6.4 reportedly
64 * has them, and many of the free Unixes now have them.
65 * Be sure to use appropriate -mt or -D_REENTRANT type
66 * compile flags when building.
73 return (unsigned long) pthread_self();
78 _glthread_InitTSD(_glthread_TSD
*tsd
)
80 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
81 perror(INIT_TSD_ERROR
);
84 tsd
->initMagic
= INIT_MAGIC
;
89 _glthread_GetTSD(_glthread_TSD
*tsd
)
91 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
92 _glthread_InitTSD(tsd
);
94 return pthread_getspecific(tsd
->key
);
99 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
101 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
102 _glthread_InitTSD(tsd
);
104 if (pthread_setspecific(tsd
->key
, ptr
) != 0) {
105 perror(SET_TSD_ERROR
);
110 #endif /* PTHREADS */
115 * Solaris/Unix International Threads -- Use only if POSIX threads
116 * aren't available on your Unix platform. Solaris 2.[34] are examples
117 * of platforms where this is the case. Be sure to use -mt and/or
118 * -D_REENTRANT when compiling.
120 #ifdef SOLARIS_THREADS
121 #define USE_LOCK_FOR_KEY /* undef this to try a version without
122 lock for the global key... */
125 _glthread_GetID(void)
127 abort(); /* XXX not implemented yet */
128 return (unsigned long) 0;
133 _glthread_InitTSD(_glthread_TSD
*tsd
)
135 if ((errno
= mutex_init(&tsd
->keylock
, 0, NULL
)) != 0 ||
136 (errno
= thr_keycreate(&(tsd
->key
), free
)) != 0) {
137 perror(INIT_TSD_ERROR
);
140 tsd
->initMagic
= INIT_MAGIC
;
145 _glthread_GetTSD(_glthread_TSD
*tsd
)
148 if (tsd
->initMagic
!= INIT_MAGIC
) {
149 _glthread_InitTSD(tsd
);
151 #ifdef USE_LOCK_FOR_KEY
152 mutex_lock(&tsd
->keylock
);
153 thr_getspecific(tsd
->key
, &ret
);
154 mutex_unlock(&tsd
->keylock
);
156 if ((errno
= thr_getspecific(tsd
->key
, &ret
)) != 0) {
157 perror(GET_TSD_ERROR
);
166 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
168 if (tsd
->initMagic
!= INIT_MAGIC
) {
169 _glthread_InitTSD(tsd
);
171 if ((errno
= thr_setspecific(tsd
->key
, ptr
)) != 0) {
172 perror(SET_TSD_ERROR
);
177 #undef USE_LOCK_FOR_KEY
178 #endif /* SOLARIS_THREADS */
183 * Win32 Threads. The only available option for Windows 95/NT.
184 * Be sure that you compile using the Multithreaded runtime, otherwise
185 * bad things will happen.
190 _glthread_GetID(void)
192 abort(); /* XXX not implemented yet */
193 return (unsigned long) 0;
198 _glthread_InitTSD(_glthread_TSD
*tsd
)
200 tsd
->key
= TlsAlloc();
201 if (tsd
->key
== 0xffffffff) {
202 /* Can Windows handle stderr messages for non-console
203 applications? Does Windows have perror? */
204 /* perror(SET_INIT_ERROR);*/
207 tsd
->initMagic
= INIT_MAGIC
;
212 _glthread_GetTSD(_glthread_TSD
*tsd
)
214 if (tsd
->initMagic
!= INIT_MAGIC
) {
215 _glthread_InitTSD(tsd
);
217 return TlsGetValue(tsd
->key
);
222 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
224 /* the following code assumes that the _glthread_TSD has been initialized
225 to zero at creation */
226 if (tsd
->initMagic
!= INIT_MAGIC
) {
227 _glthread_InitTSD(tsd
);
229 if (TlsSetValue(tsd
->key
, ptr
) == 0) {
230 /* Can Windows handle stderr messages for non-console
231 applications? Does Windows have perror? */
232 /* perror(SET_TSD_ERROR);*/
237 #endif /* WIN32_THREADS */
242 * XFree86 has its own thread wrapper, Xthreads.h
243 * We wrap it again for GL.
248 _glthread_GetID(void)
250 return (unsigned long) xthread_self();
255 _glthread_InitTSD(_glthread_TSD
*tsd
)
257 if (xthread_key_create(&tsd
->key
, NULL
) != 0) {
258 perror(INIT_TSD_ERROR
);
261 tsd
->initMagic
= INIT_MAGIC
;
266 _glthread_GetTSD(_glthread_TSD
*tsd
)
269 if (tsd
->initMagic
!= INIT_MAGIC
) {
270 _glthread_InitTSD(tsd
);
272 xthread_get_specific(tsd
->key
, &ptr
);
278 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
280 if (tsd
->initMagic
!= INIT_MAGIC
) {
281 _glthread_InitTSD(tsd
);
283 xthread_set_specific(tsd
->key
, ptr
);
296 _glthread_GetID(void)
298 return (unsigned long) find_thread(NULL
);
302 _glthread_InitTSD(_glthread_TSD
*tsd
)
304 tsd
->key
= tls_allocate();
305 tsd
->initMagic
= INIT_MAGIC
;
309 _glthread_GetTSD(_glthread_TSD
*tsd
)
311 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
312 _glthread_InitTSD(tsd
);
314 return tls_get(tsd
->key
);
318 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
320 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
321 _glthread_InitTSD(tsd
);
323 tls_set(tsd
->key
, ptr
);
326 #endif /* BEOS_THREADS */
338 _glthread_GetID(void)
345 _glthread_InitTSD(_glthread_TSD
*tsd
)
352 _glthread_GetTSD(_glthread_TSD
*tsd
)
360 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)