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>
34 #include <X11/Xfuncproto.h>
43 * This file should still compile even when THREADS is not defined.
44 * This is to make things easier to deal with on the makefile scene..
52 #define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data"
53 #define GET_TSD_ERROR "_glthread_: failed to get thread specific data"
54 #define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data"
58 * Magic number to determine if a TSD object has been initialized.
59 * Kind of a hack but there doesn't appear to be a better cross-platform
62 #define INIT_MAGIC 0xff8adc98
67 * POSIX Threads -- The best way to go if your platform supports them.
68 * Solaris >= 2.5 have POSIX threads, IRIX >= 6.4 reportedly
69 * has them, and many of the free Unixes now have them.
70 * Be sure to use appropriate -mt or -D_REENTRANT type
71 * compile flags when building.
75 _X_EXPORT unsigned long
78 return (unsigned long) pthread_self();
83 _glthread_InitTSD(_glthread_TSD *tsd)
85 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
86 perror(INIT_TSD_ERROR);
89 tsd->initMagic = INIT_MAGIC;
94 _glthread_GetTSD(_glthread_TSD *tsd)
96 if (tsd->initMagic != (int) INIT_MAGIC) {
97 _glthread_InitTSD(tsd);
99 return pthread_getspecific(tsd->key);
104 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
106 if (tsd->initMagic != (int) INIT_MAGIC) {
107 _glthread_InitTSD(tsd);
109 if (pthread_setspecific(tsd->key, ptr) != 0) {
110 perror(SET_TSD_ERROR);
115 #endif /* PTHREADS */
120 * Solaris/Unix International Threads -- Use only if POSIX threads
121 * aren't available on your Unix platform. Solaris 2.[34] are examples
122 * of platforms where this is the case. Be sure to use -mt and/or
123 * -D_REENTRANT when compiling.
125 #ifdef SOLARIS_THREADS
126 #define USE_LOCK_FOR_KEY /* undef this to try a version without
127 lock for the global key... */
129 _X_EXPORT unsigned long
130 _glthread_GetID(void)
132 OsAbort(); /* XXX not implemented yet */
133 return (unsigned long) 0;
138 _glthread_InitTSD(_glthread_TSD *tsd)
140 if ((errno = mutex_init(&tsd->keylock, 0, NULL)) != 0 ||
141 (errno = thr_keycreate(&(tsd->key), free)) != 0) {
142 perror(INIT_TSD_ERROR);
145 tsd->initMagic = INIT_MAGIC;
150 _glthread_GetTSD(_glthread_TSD *tsd)
153 if (tsd->initMagic != INIT_MAGIC) {
154 _glthread_InitTSD(tsd);
156 #ifdef USE_LOCK_FOR_KEY
157 mutex_lock(&tsd->keylock);
158 thr_getspecific(tsd->key, &ret);
159 mutex_unlock(&tsd->keylock);
161 if ((errno = thr_getspecific(tsd->key, &ret)) != 0) {
162 perror(GET_TSD_ERROR);
171 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
173 if (tsd->initMagic != INIT_MAGIC) {
174 _glthread_InitTSD(tsd);
176 if ((errno = thr_setspecific(tsd->key, ptr)) != 0) {
177 perror(SET_TSD_ERROR);
182 #undef USE_LOCK_FOR_KEY
183 #endif /* SOLARIS_THREADS */
188 * Win32 Threads. The only available option for Windows 95/NT.
189 * Be sure that you compile using the Multithreaded runtime, otherwise
190 * bad things will happen.
194 void FreeTSD(_glthread_TSD *p)
196 if (p->initMagic==INIT_MAGIC) {
202 void InsteadOf_exit(int nCode)
204 DWORD dwErr=GetLastError();
208 _glthread_GetID(void)
210 return GetCurrentThreadId();
215 _glthread_InitTSD(_glthread_TSD *tsd)
217 tsd->key = TlsAlloc();
218 if (tsd->key == TLS_OUT_OF_INDEXES) {
219 perror("Mesa:_glthread_InitTSD");
222 tsd->initMagic = INIT_MAGIC;
227 _glthread_GetTSD(_glthread_TSD *tsd)
229 if (tsd->initMagic != INIT_MAGIC) {
230 _glthread_InitTSD(tsd);
232 return TlsGetValue(tsd->key);
237 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
239 /* the following code assumes that the _glthread_TSD has been initialized
240 to zero at creation */
241 if (tsd->initMagic != INIT_MAGIC) {
242 _glthread_InitTSD(tsd);
244 if (TlsSetValue(tsd->key, ptr) == 0) {
245 perror("Mesa:_glthread_SetTSD");
250 #endif /* WIN32_THREADS */
255 * XFree86 has its own thread wrapper, Xthreads.h
256 * We wrap it again for GL.
260 _X_EXPORT unsigned long
261 _glthread_GetID(void)
263 return (unsigned long) xthread_self();
268 _glthread_InitTSD(_glthread_TSD *tsd)
270 if (xthread_key_create(&tsd->key, NULL) != 0) {
271 perror(INIT_TSD_ERROR);
274 tsd->initMagic = INIT_MAGIC;
279 _glthread_GetTSD(_glthread_TSD *tsd)
282 if (tsd->initMagic != INIT_MAGIC) {
283 _glthread_InitTSD(tsd);
285 xthread_get_specific(tsd->key, &ptr);
291 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
293 if (tsd->initMagic != INIT_MAGIC) {
294 _glthread_InitTSD(tsd);
296 xthread_set_specific(tsd->key, ptr);
309 _glthread_GetID(void)
311 return (unsigned long) find_thread(NULL);
315 _glthread_InitTSD(_glthread_TSD *tsd)
317 tsd->key = tls_allocate();
318 tsd->initMagic = INIT_MAGIC;
322 _glthread_GetTSD(_glthread_TSD *tsd)
324 if (tsd->initMagic != (int) INIT_MAGIC) {
325 _glthread_InitTSD(tsd);
327 return tls_get(tsd->key);
331 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
333 if (tsd->initMagic != (int) INIT_MAGIC) {
334 _glthread_InitTSD(tsd);
336 tls_set(tsd->key, ptr);
339 #endif /* BEOS_THREADS */
350 _X_EXPORT unsigned long
351 _glthread_GetID(void)
358 _glthread_InitTSD(_glthread_TSD *tsd)
365 _glthread_GetTSD(_glthread_TSD *tsd)
373 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)