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. First, the glheader.h include must go.
31 #ifdef HAVE_DIX_CONFIG_H
32 #include <dix-config.h>
33 #include "glapi/mesa.h"
35 #include "main/compiler.h"
38 #include "glapi/glthread.h"
42 * This file should still compile even when THREADS is not defined.
43 * This is to make things easier to deal with on the makefile scene..
51 #define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data"
52 #define GET_TSD_ERROR "_glthread_: failed to get thread specific data"
53 #define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data"
57 * Magic number to determine if a TSD object has been initialized.
58 * Kind of a hack but there doesn't appear to be a better cross-platform
61 #define INIT_MAGIC 0xff8adc98
66 * POSIX Threads -- The best way to go if your platform supports them.
67 * Solaris >= 2.5 have POSIX threads, IRIX >= 6.4 reportedly
68 * has them, and many of the free Unixes now have them.
69 * Be sure to use appropriate -mt or -D_REENTRANT type
70 * compile flags when building.
77 return (unsigned long) pthread_self();
82 _glthread_InitTSD(_glthread_TSD
*tsd
)
84 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
85 perror(INIT_TSD_ERROR
);
88 tsd
->initMagic
= INIT_MAGIC
;
93 _glthread_GetTSD(_glthread_TSD
*tsd
)
95 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
96 _glthread_InitTSD(tsd
);
98 return pthread_getspecific(tsd
->key
);
103 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
105 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
106 _glthread_InitTSD(tsd
);
108 if (pthread_setspecific(tsd
->key
, ptr
) != 0) {
109 perror(SET_TSD_ERROR
);
114 #endif /* PTHREADS */
119 * Win32 Threads. The only available option for Windows 95/NT.
120 * Be sure that you compile using the Multithreaded runtime, otherwise
121 * bad things will happen.
125 static void InsteadOf_exit(int nCode
)
127 DWORD dwErr
= GetLastError();
131 _glthread_GetID(void)
133 return GetCurrentThreadId();
138 _glthread_InitTSD(_glthread_TSD
*tsd
)
140 tsd
->key
= TlsAlloc();
141 if (tsd
->key
== TLS_OUT_OF_INDEXES
) {
142 perror(INIT_TSD_ERROR
);
145 tsd
->initMagic
= INIT_MAGIC
;
150 _glthread_DestroyTSD(_glthread_TSD
*tsd
)
152 if (tsd
->initMagic
!= INIT_MAGIC
) {
156 tsd
->initMagic
= 0x0;
161 _glthread_GetTSD(_glthread_TSD
*tsd
)
163 if (tsd
->initMagic
!= INIT_MAGIC
) {
164 _glthread_InitTSD(tsd
);
166 return TlsGetValue(tsd
->key
);
171 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
173 /* the following code assumes that the _glthread_TSD has been initialized
174 to zero at creation */
175 if (tsd
->initMagic
!= INIT_MAGIC
) {
176 _glthread_InitTSD(tsd
);
178 if (TlsSetValue(tsd
->key
, ptr
) == 0) {
179 perror(SET_TSD_ERROR
);
184 #endif /* WIN32_THREADS */
192 _glthread_GetID(void)
194 return (unsigned long) find_thread(NULL
);
198 _glthread_InitTSD(_glthread_TSD
*tsd
)
200 tsd
->key
= tls_allocate();
201 tsd
->initMagic
= INIT_MAGIC
;
205 _glthread_GetTSD(_glthread_TSD
*tsd
)
207 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
208 _glthread_InitTSD(tsd
);
210 return tls_get(tsd
->key
);
214 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
216 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
217 _glthread_InitTSD(tsd
);
219 tls_set(tsd
->key
, ptr
);
222 #endif /* BEOS_THREADS */
234 _glthread_GetID(void)
241 _glthread_InitTSD(_glthread_TSD
*tsd
)
248 _glthread_GetTSD(_glthread_TSD
*tsd
)
256 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)