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.
26 * XXX There's probably some work to do in order to make this file
27 * truly reusable outside of Mesa.
30 #ifdef HAVE_DIX_CONFIG_H
31 #include <dix-config.h>
32 #include <X11/Xfuncproto.h>
40 * This file should still compile even when THREADS is not defined.
41 * This is to make things easier to deal with on the makefile scene..
49 #define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data"
50 #define GET_TSD_ERROR "_glthread_: failed to get thread specific data"
51 #define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data"
54 * Magic number to determine if a TSD object has been initialized.
55 * Kind of a hack but there doesn't appear to be a better cross-platform
58 #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.
69 _X_EXPORT
unsigned long
72 return (unsigned long) pthread_self();
76 _glthread_InitTSD(_glthread_TSD
* tsd
)
78 if (pthread_key_create(&tsd
->key
, NULL
/*free */ ) != 0) {
79 perror(INIT_TSD_ERROR
);
82 tsd
->initMagic
= INIT_MAGIC
;
86 _glthread_GetTSD(_glthread_TSD
* tsd
)
88 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
89 _glthread_InitTSD(tsd
);
91 return pthread_getspecific(tsd
->key
);
95 _glthread_SetTSD(_glthread_TSD
* tsd
, void *ptr
)
97 if (tsd
->initMagic
!= (int) INIT_MAGIC
) {
98 _glthread_InitTSD(tsd
);
100 if (pthread_setspecific(tsd
->key
, ptr
) != 0) {
101 perror(SET_TSD_ERROR
);
106 #endif /* PTHREADS */
109 * Win32 Threads. The only available option for Windows 95/NT.
110 * Be sure that you compile using the Multithreaded runtime, otherwise
111 * bad things will happen.
116 FreeTSD(_glthread_TSD
* p
)
118 if (p
->initMagic
== INIT_MAGIC
) {
125 InsteadOf_exit(int nCode
)
127 DWORD dwErr
= GetLastError();
131 _glthread_GetID(void)
133 return GetCurrentThreadId();
137 _glthread_InitTSD(_glthread_TSD
* tsd
)
139 tsd
->key
= TlsAlloc();
140 if (tsd
->key
== TLS_OUT_OF_INDEXES
) {
141 perror("Mesa:_glthread_InitTSD");
144 tsd
->initMagic
= INIT_MAGIC
;
148 _glthread_GetTSD(_glthread_TSD
* tsd
)
150 if (tsd
->initMagic
!= INIT_MAGIC
) {
151 _glthread_InitTSD(tsd
);
153 return TlsGetValue(tsd
->key
);
157 _glthread_SetTSD(_glthread_TSD
* tsd
, void *ptr
)
159 /* the following code assumes that the _glthread_TSD has been initialized
160 to zero at creation */
161 if (tsd
->initMagic
!= INIT_MAGIC
) {
162 _glthread_InitTSD(tsd
);
164 if (TlsSetValue(tsd
->key
, ptr
) == 0) {
165 perror("Mesa:_glthread_SetTSD");
170 #endif /* WIN32_THREADS */
178 _X_EXPORT
unsigned long
179 _glthread_GetID(void)
185 _glthread_InitTSD(_glthread_TSD
* tsd
)
191 _glthread_GetTSD(_glthread_TSD
* tsd
)
198 _glthread_SetTSD(_glthread_TSD
* tsd
, void *ptr
)