mesa: #include <stdio.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 <stdio.h>
38 #include "glthread.h"
39
40
41 /*
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..
44 */
45 #ifdef THREADS
46 #include <errno.h>
47
48 /*
49 * Error messages
50 */
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"
54
55
56 /*
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
59 * solution.
60 */
61 #define INIT_MAGIC 0xff8adc98
62
63
64
65 /*
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.
71 */
72 #ifdef PTHREADS
73
74 unsigned long
75 _glthread_GetID(void)
76 {
77 return (unsigned long) pthread_self();
78 }
79
80
81 void
82 _glthread_InitTSD(_glthread_TSD *tsd)
83 {
84 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
85 perror(INIT_TSD_ERROR);
86 exit(-1);
87 }
88 tsd->initMagic = INIT_MAGIC;
89 }
90
91
92 void *
93 _glthread_GetTSD(_glthread_TSD *tsd)
94 {
95 if (tsd->initMagic != (int) INIT_MAGIC) {
96 _glthread_InitTSD(tsd);
97 }
98 return pthread_getspecific(tsd->key);
99 }
100
101
102 void
103 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
104 {
105 if (tsd->initMagic != (int) INIT_MAGIC) {
106 _glthread_InitTSD(tsd);
107 }
108 if (pthread_setspecific(tsd->key, ptr) != 0) {
109 perror(SET_TSD_ERROR);
110 exit(-1);
111 }
112 }
113
114 #endif /* PTHREADS */
115
116
117
118 /*
119 * Solaris/Unix International Threads -- Use only if POSIX threads
120 * aren't available on your Unix platform. Solaris 2.[34] are examples
121 * of platforms where this is the case. Be sure to use -mt and/or
122 * -D_REENTRANT when compiling.
123 */
124 #ifdef SOLARIS_THREADS
125 #define USE_LOCK_FOR_KEY /* undef this to try a version without
126 lock for the global key... */
127
128 unsigned long
129 _glthread_GetID(void)
130 {
131 abort(); /* XXX not implemented yet */
132 return (unsigned long) 0;
133 }
134
135
136 void
137 _glthread_InitTSD(_glthread_TSD *tsd)
138 {
139 if ((errno = mutex_init(&tsd->keylock, 0, NULL)) != 0 ||
140 (errno = thr_keycreate(&(tsd->key), free)) != 0) {
141 perror(INIT_TSD_ERROR);
142 exit(-1);
143 }
144 tsd->initMagic = INIT_MAGIC;
145 }
146
147
148 void *
149 _glthread_GetTSD(_glthread_TSD *tsd)
150 {
151 void* ret;
152 if (tsd->initMagic != INIT_MAGIC) {
153 _glthread_InitTSD(tsd);
154 }
155 #ifdef USE_LOCK_FOR_KEY
156 mutex_lock(&tsd->keylock);
157 thr_getspecific(tsd->key, &ret);
158 mutex_unlock(&tsd->keylock);
159 #else
160 if ((errno = thr_getspecific(tsd->key, &ret)) != 0) {
161 perror(GET_TSD_ERROR);
162 exit(-1);
163 }
164 #endif
165 return ret;
166 }
167
168
169 void
170 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
171 {
172 if (tsd->initMagic != INIT_MAGIC) {
173 _glthread_InitTSD(tsd);
174 }
175 if ((errno = thr_setspecific(tsd->key, ptr)) != 0) {
176 perror(SET_TSD_ERROR);
177 exit(-1);
178 }
179 }
180
181 #undef USE_LOCK_FOR_KEY
182 #endif /* SOLARIS_THREADS */
183
184
185
186 /*
187 * Win32 Threads. The only available option for Windows 95/NT.
188 * Be sure that you compile using the Multithreaded runtime, otherwise
189 * bad things will happen.
190 */
191 #ifdef WIN32_THREADS
192
193 void FreeTSD(_glthread_TSD *p)
194 {
195 if (p->initMagic==INIT_MAGIC) {
196 TlsFree(p->key);
197 p->initMagic=0;
198 }
199 }
200
201 void InsteadOf_exit(int nCode)
202 {
203 DWORD dwErr=GetLastError();
204 }
205
206 unsigned long
207 _glthread_GetID(void)
208 {
209 return GetCurrentThreadId();
210 }
211
212
213 void
214 _glthread_InitTSD(_glthread_TSD *tsd)
215 {
216 tsd->key = TlsAlloc();
217 if (tsd->key == TLS_OUT_OF_INDEXES) {
218 perror("Mesa:_glthread_InitTSD");
219 InsteadOf_exit(-1);
220 }
221 tsd->initMagic = INIT_MAGIC;
222 }
223
224
225 void *
226 _glthread_GetTSD(_glthread_TSD *tsd)
227 {
228 if (tsd->initMagic != INIT_MAGIC) {
229 _glthread_InitTSD(tsd);
230 }
231 return TlsGetValue(tsd->key);
232 }
233
234
235 void
236 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
237 {
238 /* the following code assumes that the _glthread_TSD has been initialized
239 to zero at creation */
240 if (tsd->initMagic != INIT_MAGIC) {
241 _glthread_InitTSD(tsd);
242 }
243 if (TlsSetValue(tsd->key, ptr) == 0) {
244 perror("Mesa:_glthread_SetTSD");
245 InsteadOf_exit(-1);
246 }
247 }
248
249 #endif /* WIN32_THREADS */
250
251
252
253 /*
254 * XFree86 has its own thread wrapper, Xthreads.h
255 * We wrap it again for GL.
256 */
257 #ifdef USE_XTHREADS
258
259 unsigned long
260 _glthread_GetID(void)
261 {
262 return (unsigned long) xthread_self();
263 }
264
265
266 void
267 _glthread_InitTSD(_glthread_TSD *tsd)
268 {
269 if (xthread_key_create(&tsd->key, NULL) != 0) {
270 perror(INIT_TSD_ERROR);
271 exit(-1);
272 }
273 tsd->initMagic = INIT_MAGIC;
274 }
275
276
277 void *
278 _glthread_GetTSD(_glthread_TSD *tsd)
279 {
280 void *ptr;
281 if (tsd->initMagic != INIT_MAGIC) {
282 _glthread_InitTSD(tsd);
283 }
284 xthread_get_specific(tsd->key, &ptr);
285 return ptr;
286 }
287
288
289 void
290 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
291 {
292 if (tsd->initMagic != INIT_MAGIC) {
293 _glthread_InitTSD(tsd);
294 }
295 xthread_set_specific(tsd->key, ptr);
296 }
297
298 #endif /* XTHREAD */
299
300
301
302 /*
303 * BeOS threads
304 */
305 #ifdef BEOS_THREADS
306
307 unsigned long
308 _glthread_GetID(void)
309 {
310 return (unsigned long) find_thread(NULL);
311 }
312
313 void
314 _glthread_InitTSD(_glthread_TSD *tsd)
315 {
316 tsd->key = tls_allocate();
317 tsd->initMagic = INIT_MAGIC;
318 }
319
320 void *
321 _glthread_GetTSD(_glthread_TSD *tsd)
322 {
323 if (tsd->initMagic != (int) INIT_MAGIC) {
324 _glthread_InitTSD(tsd);
325 }
326 return tls_get(tsd->key);
327 }
328
329 void
330 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
331 {
332 if (tsd->initMagic != (int) INIT_MAGIC) {
333 _glthread_InitTSD(tsd);
334 }
335 tls_set(tsd->key, ptr);
336 }
337
338 #endif /* BEOS_THREADS */
339
340
341
342 #else /* THREADS */
343
344
345 /*
346 * no-op functions
347 */
348
349 unsigned long
350 _glthread_GetID(void)
351 {
352 return 0;
353 }
354
355
356 void
357 _glthread_InitTSD(_glthread_TSD *tsd)
358 {
359 (void) tsd;
360 }
361
362
363 void *
364 _glthread_GetTSD(_glthread_TSD *tsd)
365 {
366 (void) tsd;
367 return NULL;
368 }
369
370
371 void
372 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
373 {
374 (void) tsd;
375 (void) ptr;
376 }
377
378
379 #endif /* THREADS */