4513853f5a2b1f4e68726f1cc33e5bf0ab9f8f8f
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.
37 * This file should still compile even when THREADS is not defined.
38 * This is to make things easier to deal with on the makefile scene..
46 #define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data"
47 #define GET_TSD_ERROR "_glthread_: failed to get thread specific data"
48 #define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data"
52 * Magic number to determine if a TSD object has been initialized.
53 * Kind of a hack but there doesn't appear to be a better cross-platform
56 #define INIT_MAGIC 0xff8adc98
61 * POSIX Threads -- The best way to go if your platform supports them.
62 * Solaris >= 2.5 have POSIX threads, IRIX >= 6.4 reportedly
63 * has them, and many of the free Unixes now have them.
64 * Be sure to use appropriate -mt or -D_REENTRANT type
65 * compile flags when building.
72 return (unsigned long) pthread_self();
77 _glthread_InitTSD(_glthread_TSD
*tsd
)
79 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
80 perror(INIT_TSD_ERROR
);
83 tsd
->initMagic
= INIT_MAGIC
;
88 _glthread_GetTSD(_glthread_TSD
*tsd
)
90 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
91 _glthread_InitTSD(tsd
);
93 return pthread_getspecific(tsd
->key
);
98 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
100 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
101 _glthread_InitTSD(tsd
);
103 if (pthread_setspecific(tsd
->key
, ptr
) != 0) {
104 perror(SET_TSD_ERROR
);
109 #endif /* PTHREADS */
114 * Solaris/Unix International Threads -- Use only if POSIX threads
115 * aren't available on your Unix platform. Solaris 2.[34] are examples
116 * of platforms where this is the case. Be sure to use -mt and/or
117 * -D_REENTRANT when compiling.
119 #ifdef SOLARIS_THREADS
120 #define USE_LOCK_FOR_KEY /* undef this to try a version without
121 lock for the global key... */
124 _glthread_GetID(void)
126 abort(); /* XXX not implemented yet */
127 return (unsigned long) 0;
132 _glthread_InitTSD(_glthread_TSD
*tsd
)
134 if ((errno
= mutex_init(&tsd
->keylock
, 0, NULL
)) != 0 ||
135 (errno
= thr_keycreate(&(tsd
->key
), free
)) != 0) {
136 perror(INIT_TSD_ERROR
);
139 tsd
->initMagic
= INIT_MAGIC
;
144 _glthread_GetTSD(_glthread_TSD
*tsd
)
147 if (tsd
->initMagic
!= INIT_MAGIC
) {
148 _glthread_InitTSD(tsd
);
150 #ifdef USE_LOCK_FOR_KEY
151 mutex_lock(&tsd
->keylock
);
152 thr_getspecific(tsd
->key
, &ret
);
153 mutex_unlock(&tsd
->keylock
);
155 if ((errno
= thr_getspecific(tsd
->key
, &ret
)) != 0) {
156 perror(GET_TSD_ERROR
);
165 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
167 if (tsd
->initMagic
!= INIT_MAGIC
) {
168 _glthread_InitTSD(tsd
);
170 if ((errno
= thr_setspecific(tsd
->key
, ptr
)) != 0) {
171 perror(SET_TSD_ERROR
);
176 #undef USE_LOCK_FOR_KEY
177 #endif /* SOLARIS_THREADS */
182 * Win32 Threads. The only available option for Windows 95/NT.
183 * Be sure that you compile using the Multithreaded runtime, otherwise
184 * bad things will happen.
188 void FreeTSD(_glthread_TSD
*p
)
190 if (p
->initMagic
==INIT_MAGIC
) {
196 void InsteadOf_exit(int nCode
)
198 DWORD dwErr
=GetLastError();
202 _glthread_GetID(void)
204 return GetCurrentThreadId();
209 _glthread_InitTSD(_glthread_TSD
*tsd
)
211 tsd
->key
= TlsAlloc();
212 if (tsd
->key
== TLS_OUT_OF_INDEXES
) {
213 perror("Mesa:_glthread_InitTSD");
216 tsd
->initMagic
= INIT_MAGIC
;
221 _glthread_GetTSD(_glthread_TSD
*tsd
)
223 if (tsd
->initMagic
!= INIT_MAGIC
) {
224 _glthread_InitTSD(tsd
);
226 return TlsGetValue(tsd
->key
);
231 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
233 /* the following code assumes that the _glthread_TSD has been initialized
234 to zero at creation */
235 if (tsd
->initMagic
!= INIT_MAGIC
) {
236 _glthread_InitTSD(tsd
);
238 if (TlsSetValue(tsd
->key
, ptr
) == 0) {
239 perror("Mesa:_glthread_SetTSD");
244 #endif /* WIN32_THREADS */
249 * XFree86 has its own thread wrapper, Xthreads.h
250 * We wrap it again for GL.
255 _glthread_GetID(void)
257 return (unsigned long) xthread_self();
262 _glthread_InitTSD(_glthread_TSD
*tsd
)
264 if (xthread_key_create(&tsd
->key
, NULL
) != 0) {
265 perror(INIT_TSD_ERROR
);
268 tsd
->initMagic
= INIT_MAGIC
;
273 _glthread_GetTSD(_glthread_TSD
*tsd
)
276 if (tsd
->initMagic
!= INIT_MAGIC
) {
277 _glthread_InitTSD(tsd
);
279 xthread_get_specific(tsd
->key
, &ptr
);
285 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
287 if (tsd
->initMagic
!= INIT_MAGIC
) {
288 _glthread_InitTSD(tsd
);
290 xthread_set_specific(tsd
->key
, ptr
);
303 _glthread_GetID(void)
305 return (unsigned long) find_thread(NULL
);
309 _glthread_InitTSD(_glthread_TSD
*tsd
)
311 tsd
->key
= tls_allocate();
312 tsd
->initMagic
= INIT_MAGIC
;
316 _glthread_GetTSD(_glthread_TSD
*tsd
)
318 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
319 _glthread_InitTSD(tsd
);
321 return tls_get(tsd
->key
);
325 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)
327 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
328 _glthread_InitTSD(tsd
);
330 tls_set(tsd
->key
, ptr
);
333 #endif /* BEOS_THREADS */
345 _glthread_GetID(void)
352 _glthread_InitTSD(_glthread_TSD
*tsd
)
359 _glthread_GetTSD(_glthread_TSD
*tsd
)
367 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
)