1 /* $Id: glthread.c,v 1.10 2002/10/24 23:57:20 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * XXX There's probably some work to do in order to make this file
30 * truly reusable outside of Mesa. First, the glheader.h include must go.
39 * This file should still compile even when THREADS is not defined.
40 * This is to make things easier to deal with on the makefile scene..
48 #define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data"
49 #define GET_TSD_ERROR "_glthread_: failed to get thread specific data"
50 #define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data"
54 * Magic number to determine if a TSD object has been initialized.
55 * Kind of a hack but there doesn't appear to be a better cross-platform
58 #define INIT_MAGIC 0xff8adc98
63 * POSIX Threads -- The best way to go if your platform supports them.
64 * Solaris >= 2.5 have POSIX threads, IRIX >= 6.4 reportedly
65 * has them, and many of the free Unixes now have them.
66 * Be sure to use appropriate -mt or -D_REENTRANT type
67 * compile flags when building.
74 return (unsigned long) pthread_self();
79 _glthread_InitTSD(_glthread_TSD
*tsd
)
81 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
82 perror(INIT_TSD_ERROR
);
85 tsd
->initMagic
= INIT_MAGIC
;
90 _glthread_GetTSD(_glthread_TSD
*tsd
)
92 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
93 _glthread_InitTSD(tsd
);
95 return pthread_getspecific(tsd
->key
);
100 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
102 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
103 _glthread_InitTSD(tsd
);
105 if (pthread_setspecific(tsd
->key
, ptr
) != 0) {
106 perror(SET_TSD_ERROR
);
111 #endif /* PTHREADS */
116 * Solaris/Unix International Threads -- Use only if POSIX threads
117 * aren't available on your Unix platform. Solaris 2.[34] are examples
118 * of platforms where this is the case. Be sure to use -mt and/or
119 * -D_REENTRANT when compiling.
121 #ifdef SOLARIS_THREADS
122 #define USE_LOCK_FOR_KEY /* undef this to try a version without
123 lock for the global key... */
126 _glthread_GetID(void)
128 abort(); /* XXX not implemented yet */
129 return (unsigned long) 0;
134 _glthread_InitTSD(_glthread_TSD
*tsd
)
136 if ((errno
= mutex_init(&tsd
->keylock
, 0, NULL
)) != 0 ||
137 (errno
= thr_keycreate(&(tsd
->key
), free
)) != 0) {
138 perror(INIT_TSD_ERROR
);
141 tsd
->initMagic
= INIT_MAGIC
;
146 _glthread_GetTSD(_glthread_TSD
*tsd
)
149 if (tsd
->initMagic
!= INIT_MAGIC
) {
150 _glthread_InitTSD(tsd
);
152 #ifdef USE_LOCK_FOR_KEY
153 mutex_lock(&tsd
->keylock
);
154 thr_getspecific(tsd
->key
, &ret
);
155 mutex_unlock(&tsd
->keylock
);
157 if ((errno
= thr_getspecific(tsd
->key
, &ret
)) != 0) {
158 perror(GET_TSD_ERROR
);
167 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
169 if (tsd
->initMagic
!= INIT_MAGIC
) {
170 _glthread_InitTSD(tsd
);
172 if ((errno
= thr_setspecific(tsd
->key
, ptr
)) != 0) {
173 perror(SET_TSD_ERROR
);
178 #undef USE_LOCK_FOR_KEY
179 #endif /* SOLARIS_THREADS */
184 * Win32 Threads. The only available option for Windows 95/NT.
185 * Be sure that you compile using the Multithreaded runtime, otherwise
186 * bad things will happen.
191 _glthread_GetID(void)
193 abort(); /* XXX not implemented yet */
194 return (unsigned long) 0;
199 _glthread_InitTSD(_glthread_TSD
*tsd
)
201 tsd
->key
= TlsAlloc();
202 if (tsd
->key
== 0xffffffff) {
203 /* Can Windows handle stderr messages for non-console
204 applications? Does Windows have perror? */
205 /* perror(SET_INIT_ERROR);*/
208 tsd
->initMagic
= INIT_MAGIC
;
213 _glthread_GetTSD(_glthread_TSD
*tsd
)
215 if (tsd
->initMagic
!= INIT_MAGIC
) {
216 _glthread_InitTSD(tsd
);
218 return TlsGetValue(tsd
->key
);
223 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
225 /* the following code assumes that the _glthread_TSD has been initialized
226 to zero at creation */
227 if (tsd
->initMagic
!= INIT_MAGIC
) {
228 _glthread_InitTSD(tsd
);
230 if (TlsSetValue(tsd
->key
, ptr
) == 0) {
231 /* Can Windows handle stderr messages for non-console
232 applications? Does Windows have perror? */
233 /* perror(SET_TSD_ERROR);*/
238 #endif /* WIN32_THREADS */
243 * XFree86 has its own thread wrapper, Xthreads.h
244 * We wrap it again for GL.
249 _glthread_GetID(void)
251 return (unsigned long) xthread_self();
256 _glthread_InitTSD(_glthread_TSD
*tsd
)
258 if (xthread_key_create(&tsd
->key
, NULL
) != 0) {
259 perror(INIT_TSD_ERROR
);
262 tsd
->initMagic
= INIT_MAGIC
;
267 _glthread_GetTSD(_glthread_TSD
*tsd
)
270 if (tsd
->initMagic
!= INIT_MAGIC
) {
271 _glthread_InitTSD(tsd
);
273 xthread_get_specific(tsd
->key
, &ptr
);
279 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
281 if (tsd
->initMagic
!= INIT_MAGIC
) {
282 _glthread_InitTSD(tsd
);
284 xthread_set_specific(tsd
->key
, ptr
);
297 _glthread_GetID(void)
299 return (unsigned long) find_thread(NULL
);
303 _glthread_InitTSD(_glthread_TSD
*tsd
)
305 tsd
->key
= tls_allocate();
306 tsd
->initMagic
= INIT_MAGIC
;
310 _glthread_GetTSD(_glthread_TSD
*tsd
)
312 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
313 _glthread_InitTSD(tsd
);
315 return tls_get(tsd
->key
);
319 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
321 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
322 _glthread_InitTSD(tsd
);
324 tls_set(tsd
->key
, ptr
);
327 #endif /* BEOS_THREADS */
339 _glthread_GetID(void)
346 _glthread_InitTSD(_glthread_TSD
*tsd
)
353 _glthread_GetTSD(_glthread_TSD
*tsd
)
361 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)