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.
116 static void InsteadOf_exit(int nCode
)
118 DWORD dwErr
= GetLastError();
124 return GetCurrentThreadId();
129 u_tsd_init(struct u_tsd
*tsd
)
131 tsd
->key
= TlsAlloc();
132 if (tsd
->key
== TLS_OUT_OF_INDEXES
) {
133 perror(INIT_TSD_ERROR
);
136 tsd
->initMagic
= INIT_MAGIC
;
141 u_tsd_destroy(struct u_tsd
*tsd
)
143 if (tsd
->initMagic
!= INIT_MAGIC
) {
147 tsd
->initMagic
= 0x0;
152 u_tsd_get(struct u_tsd
*tsd
)
154 if (tsd
->initMagic
!= INIT_MAGIC
) {
157 return TlsGetValue(tsd
->key
);
162 u_tsd_set(struct u_tsd
*tsd
, void *ptr
)
164 /* the following code assumes that the struct u_tsd has been initialized
165 to zero at creation */
166 if (tsd
->initMagic
!= INIT_MAGIC
) {
169 if (TlsSetValue(tsd
->key
, ptr
) == 0) {
170 perror(SET_TSD_ERROR
);
175 #endif /* WIN32_THREADS */
185 return (unsigned long) find_thread(NULL
);
189 u_tsd_init(struct u_tsd
*tsd
)
191 tsd
->key
= tls_allocate();
192 tsd
->initMagic
= INIT_MAGIC
;
196 u_tsd_get(struct u_tsd
*tsd
)
198 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
201 return tls_get(tsd
->key
);
205 u_tsd_set(struct u_tsd
*tsd
, void *ptr
)
207 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
210 tls_set(tsd
->key
, ptr
);
213 #endif /* BEOS_THREADS */
225 _glthread_GetID(void)
232 u_tsd_init(struct u_tsd
*tsd
)
239 u_tsd_get(struct u_tsd
*tsd
)
247 u_tsd_set(struct u_tsd
*tsd
, void *ptr
)