Make the shared glapi files not include glheader.h.
[mesa.git] / src / mesa / glapi / glthread.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.1
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25
26 /*
27 * XXX There's probably some work to do in order to make this file
28 * truly reusable outside of Mesa.
29 */
30
31
32 #ifdef HAVE_DIX_CONFIG_H
33 #include <dix-config.h>
34 #endif
35
36 #include <stdlib.h>
37 #include "glthread.h"
38
39
40 /*
41 * This file should still compile even when THREADS is not defined.
42 * This is to make things easier to deal with on the makefile scene..
43 */
44 #ifdef THREADS
45 #include <errno.h>
46
47 /*
48 * Error messages
49 */
50 #define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data"
51 #define GET_TSD_ERROR "_glthread_: failed to get thread specific data"
52 #define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data"
53
54
55 /*
56 * Magic number to determine if a TSD object has been initialized.
57 * Kind of a hack but there doesn't appear to be a better cross-platform
58 * solution.
59 */
60 #define INIT_MAGIC 0xff8adc98
61
62
63
64 /*
65 * POSIX Threads -- The best way to go if your platform supports them.
66 * Solaris >= 2.5 have POSIX threads, IRIX >= 6.4 reportedly
67 * has them, and many of the free Unixes now have them.
68 * Be sure to use appropriate -mt or -D_REENTRANT type
69 * compile flags when building.
70 */
71 #ifdef PTHREADS
72
73 unsigned long
74 _glthread_GetID(void)
75 {
76 return (unsigned long) pthread_self();
77 }
78
79
80 void
81 _glthread_InitTSD(_glthread_TSD *tsd)
82 {
83 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
84 perror(INIT_TSD_ERROR);
85 exit(-1);
86 }
87 tsd->initMagic = INIT_MAGIC;
88 }
89
90
91 void *
92 _glthread_GetTSD(_glthread_TSD *tsd)
93 {
94 if (tsd->initMagic != (int) INIT_MAGIC) {
95 _glthread_InitTSD(tsd);
96 }
97 return pthread_getspecific(tsd->key);
98 }
99
100
101 void
102 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
103 {
104 if (tsd->initMagic != (int) INIT_MAGIC) {
105 _glthread_InitTSD(tsd);
106 }
107 if (pthread_setspecific(tsd->key, ptr) != 0) {
108 perror(SET_TSD_ERROR);
109 exit(-1);
110 }
111 }
112
113 #endif /* PTHREADS */
114
115
116
117 /*
118 * Solaris/Unix International Threads -- Use only if POSIX threads
119 * aren't available on your Unix platform. Solaris 2.[34] are examples
120 * of platforms where this is the case. Be sure to use -mt and/or
121 * -D_REENTRANT when compiling.
122 */
123 #ifdef SOLARIS_THREADS
124 #define USE_LOCK_FOR_KEY /* undef this to try a version without
125 lock for the global key... */
126
127 unsigned long
128 _glthread_GetID(void)
129 {
130 abort(); /* XXX not implemented yet */
131 return (unsigned long) 0;
132 }
133
134
135 void
136 _glthread_InitTSD(_glthread_TSD *tsd)
137 {
138 if ((errno = mutex_init(&tsd->keylock, 0, NULL)) != 0 ||
139 (errno = thr_keycreate(&(tsd->key), free)) != 0) {
140 perror(INIT_TSD_ERROR);
141 exit(-1);
142 }
143 tsd->initMagic = INIT_MAGIC;
144 }
145
146
147 void *
148 _glthread_GetTSD(_glthread_TSD *tsd)
149 {
150 void* ret;
151 if (tsd->initMagic != INIT_MAGIC) {
152 _glthread_InitTSD(tsd);
153 }
154 #ifdef USE_LOCK_FOR_KEY
155 mutex_lock(&tsd->keylock);
156 thr_getspecific(tsd->key, &ret);
157 mutex_unlock(&tsd->keylock);
158 #else
159 if ((errno = thr_getspecific(tsd->key, &ret)) != 0) {
160 perror(GET_TSD_ERROR);
161 exit(-1);
162 }
163 #endif
164 return ret;
165 }
166
167
168 void
169 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
170 {
171 if (tsd->initMagic != INIT_MAGIC) {
172 _glthread_InitTSD(tsd);
173 }
174 if ((errno = thr_setspecific(tsd->key, ptr)) != 0) {
175 perror(SET_TSD_ERROR);
176 exit(-1);
177 }
178 }
179
180 #undef USE_LOCK_FOR_KEY
181 #endif /* SOLARIS_THREADS */
182
183
184
185 /*
186 * Win32 Threads. The only available option for Windows 95/NT.
187 * Be sure that you compile using the Multithreaded runtime, otherwise
188 * bad things will happen.
189 */
190 #ifdef WIN32_THREADS
191
192 void FreeTSD(_glthread_TSD *p)
193 {
194 if (p->initMagic==INIT_MAGIC) {
195 TlsFree(p->key);
196 p->initMagic=0;
197 }
198 }
199
200 void InsteadOf_exit(int nCode)
201 {
202 DWORD dwErr=GetLastError();
203 }
204
205 unsigned long
206 _glthread_GetID(void)
207 {
208 return GetCurrentThreadId();
209 }
210
211
212 void
213 _glthread_InitTSD(_glthread_TSD *tsd)
214 {
215 tsd->key = TlsAlloc();
216 if (tsd->key == TLS_OUT_OF_INDEXES) {
217 perror("Mesa:_glthread_InitTSD");
218 InsteadOf_exit(-1);
219 }
220 tsd->initMagic = INIT_MAGIC;
221 }
222
223
224 void *
225 _glthread_GetTSD(_glthread_TSD *tsd)
226 {
227 if (tsd->initMagic != INIT_MAGIC) {
228 _glthread_InitTSD(tsd);
229 }
230 return TlsGetValue(tsd->key);
231 }
232
233
234 void
235 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
236 {
237 /* the following code assumes that the _glthread_TSD has been initialized
238 to zero at creation */
239 if (tsd->initMagic != INIT_MAGIC) {
240 _glthread_InitTSD(tsd);
241 }
242 if (TlsSetValue(tsd->key, ptr) == 0) {
243 perror("Mesa:_glthread_SetTSD");
244 InsteadOf_exit(-1);
245 }
246 }
247
248 #endif /* WIN32_THREADS */
249
250
251
252 /*
253 * XFree86 has its own thread wrapper, Xthreads.h
254 * We wrap it again for GL.
255 */
256 #ifdef USE_XTHREADS
257
258 unsigned long
259 _glthread_GetID(void)
260 {
261 return (unsigned long) xthread_self();
262 }
263
264
265 void
266 _glthread_InitTSD(_glthread_TSD *tsd)
267 {
268 if (xthread_key_create(&tsd->key, NULL) != 0) {
269 perror(INIT_TSD_ERROR);
270 exit(-1);
271 }
272 tsd->initMagic = INIT_MAGIC;
273 }
274
275
276 void *
277 _glthread_GetTSD(_glthread_TSD *tsd)
278 {
279 void *ptr;
280 if (tsd->initMagic != INIT_MAGIC) {
281 _glthread_InitTSD(tsd);
282 }
283 xthread_get_specific(tsd->key, &ptr);
284 return ptr;
285 }
286
287
288 void
289 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
290 {
291 if (tsd->initMagic != INIT_MAGIC) {
292 _glthread_InitTSD(tsd);
293 }
294 xthread_set_specific(tsd->key, ptr);
295 }
296
297 #endif /* XTHREAD */
298
299
300
301 /*
302 * BeOS threads
303 */
304 #ifdef BEOS_THREADS
305
306 unsigned long
307 _glthread_GetID(void)
308 {
309 return (unsigned long) find_thread(NULL);
310 }
311
312 void
313 _glthread_InitTSD(_glthread_TSD *tsd)
314 {
315 tsd->key = tls_allocate();
316 tsd->initMagic = INIT_MAGIC;
317 }
318
319 void *
320 _glthread_GetTSD(_glthread_TSD *tsd)
321 {
322 if (tsd->initMagic != (int) INIT_MAGIC) {
323 _glthread_InitTSD(tsd);
324 }
325 return tls_get(tsd->key);
326 }
327
328 void
329 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
330 {
331 if (tsd->initMagic != (int) INIT_MAGIC) {
332 _glthread_InitTSD(tsd);
333 }
334 tls_set(tsd->key, ptr);
335 }
336
337 #endif /* BEOS_THREADS */
338
339
340
341 #else /* THREADS */
342
343
344 /*
345 * no-op functions
346 */
347
348 unsigned long
349 _glthread_GetID(void)
350 {
351 return 0;
352 }
353
354
355 void
356 _glthread_InitTSD(_glthread_TSD *tsd)
357 {
358 (void) tsd;
359 }
360
361
362 void *
363 _glthread_GetTSD(_glthread_TSD *tsd)
364 {
365 (void) tsd;
366 return NULL;
367 }
368
369
370 void
371 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
372 {
373 (void) tsd;
374 (void) ptr;
375 }
376
377
378 #endif /* THREADS */