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.
28 #include "u_compiler.h"
33 * This file should still compile even when THREADS is not defined.
34 * This is to make things easier to deal with on the makefile scene..
42 #define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data"
43 #define GET_TSD_ERROR "_glthread_: failed to get thread specific data"
44 #define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data"
48 * Magic number to determine if a TSD object has been initialized.
49 * Kind of a hack but there doesn't appear to be a better cross-platform
52 #define INIT_MAGIC 0xff8adc98
57 * POSIX Threads -- The best way to go if your platform supports them.
58 * Solaris >= 2.5 have POSIX threads, IRIX >= 6.4 reportedly
59 * has them, and many of the free Unixes now have them.
60 * Be sure to use appropriate -mt or -D_REENTRANT type
61 * compile flags when building.
68 return (unsigned long) pthread_self();
73 u_tsd_init(struct u_tsd *tsd)
75 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
76 perror(INIT_TSD_ERROR);
79 tsd->initMagic = INIT_MAGIC;
84 u_tsd_get(struct u_tsd *tsd)
86 if (tsd->initMagic != (int) INIT_MAGIC) {
89 return pthread_getspecific(tsd->key);
94 u_tsd_set(struct u_tsd *tsd, void *ptr)
96 if (tsd->initMagic != (int) INIT_MAGIC) {
99 if (pthread_setspecific(tsd->key, ptr) != 0) {
100 perror(SET_TSD_ERROR);
105 #endif /* PTHREADS */
110 * Win32 Threads. The only available option for Windows 95/NT.
111 * Be sure that you compile using the Multithreaded runtime, otherwise
112 * bad things will happen.
119 return GetCurrentThreadId();
124 u_tsd_init(struct u_tsd *tsd)
126 tsd->key = TlsAlloc();
127 if (tsd->key == TLS_OUT_OF_INDEXES) {
128 perror(INIT_TSD_ERROR);
131 tsd->initMagic = INIT_MAGIC;
136 u_tsd_destroy(struct u_tsd *tsd)
138 if (tsd->initMagic != INIT_MAGIC) {
142 tsd->initMagic = 0x0;
147 u_tsd_get(struct u_tsd *tsd)
149 if (tsd->initMagic != INIT_MAGIC) {
152 return TlsGetValue(tsd->key);
157 u_tsd_set(struct u_tsd *tsd, void *ptr)
159 /* the following code assumes that the struct u_tsd has been initialized
160 to zero at creation */
161 if (tsd->initMagic != INIT_MAGIC) {
164 if (TlsSetValue(tsd->key, ptr) == 0) {
165 perror(SET_TSD_ERROR);
180 return (unsigned long) find_thread(NULL);
184 u_tsd_init(struct u_tsd *tsd)
186 tsd->key = tls_allocate();
187 tsd->initMagic = INIT_MAGIC;
191 u_tsd_get(struct u_tsd *tsd)
193 if (tsd->initMagic != (int) INIT_MAGIC) {
196 return tls_get(tsd->key);
200 u_tsd_set(struct u_tsd *tsd, void *ptr)
202 if (tsd->initMagic != (int) INIT_MAGIC) {
205 tls_set(tsd->key, ptr);
208 #endif /* BEOS_THREADS */
227 u_tsd_init(struct u_tsd *tsd)
234 u_tsd_get(struct u_tsd *tsd)
242 u_tsd_set(struct u_tsd *tsd, void *ptr)