blob: dce8a8a837e42f2282a4af1a769f0bf01cd5459a [file] [log] [blame]
Brian Paul3c634522002-10-24 23:57:19 +00001/* $Id: glthread.c,v 1.10 2002/10/24 23:57:20 brianp Exp $ */
Brian Paulc11371a1999-12-16 17:31:06 +00002
3/*
4 * Mesa 3-D graphics library
Brian Paul3c634522002-10-24 23:57:19 +00005 * Version: 4.1
Gareth Hughes22144ab2001-03-12 00:48:37 +00006 *
Brian Paul3c634522002-10-24 23:57:19 +00007 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
Gareth Hughes22144ab2001-03-12 00:48:37 +00008 *
Brian Paulc11371a1999-12-16 17:31:06 +00009 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
Gareth Hughes22144ab2001-03-12 00:48:37 +000015 *
Brian Paulc11371a1999-12-16 17:31:06 +000016 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
Gareth Hughes22144ab2001-03-12 00:48:37 +000018 *
Brian Paulc11371a1999-12-16 17:31:06 +000019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28/*
Brian Paul8d365ab2000-01-28 18:57:56 +000029 * XXX There's probably some work to do in order to make this file
30 * truly reusable outside of Mesa. First, the glheader.h include must go.
Brian Paulc11371a1999-12-16 17:31:06 +000031 */
32
33
Brian Paulc11371a1999-12-16 17:31:06 +000034#include "glheader.h"
Gareth Hughes22144ab2001-03-12 00:48:37 +000035#include "glthread.h"
Brian Paulc11371a1999-12-16 17:31:06 +000036
37
Brian Paulc11371a1999-12-16 17:31:06 +000038/*
39 * This file should still compile even when THREADS is not defined.
40 * This is to make things easier to deal with on the makefile scene..
41 */
42#ifdef THREADS
43#include <errno.h>
Brian Paulc11371a1999-12-16 17:31:06 +000044
45/*
46 * Error messages
47 */
48#define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data"
49#define GET_TSD_ERROR "_glthread_: failed to get thread specific data"
50#define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data"
51
52
53/*
Brian Paula9601f12000-02-10 21:27:25 +000054 * 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
56 * solution.
Brian Paulc11371a1999-12-16 17:31:06 +000057 */
Brian Paula9601f12000-02-10 21:27:25 +000058#define INIT_MAGIC 0xff8adc98
Brian Paulc11371a1999-12-16 17:31:06 +000059
60
61
62/*
63 * POSIX Threads -- The best way to go if your platform supports them.
64 * Solaris >= 2.5 have POSIX threads, IRIX >= 6.4 reportedly
65 * has them, and many of the free Unixes now have them.
66 * Be sure to use appropriate -mt or -D_REENTRANT type
67 * compile flags when building.
68 */
69#ifdef PTHREADS
70
71unsigned long
72_glthread_GetID(void)
73{
74 return (unsigned long) pthread_self();
75}
76
77
78void
79_glthread_InitTSD(_glthread_TSD *tsd)
80{
Brian Paule2b10e71999-12-17 11:13:54 +000081 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
Brian Paulc11371a1999-12-16 17:31:06 +000082 perror(INIT_TSD_ERROR);
83 exit(-1);
84 }
Brian Paula9601f12000-02-10 21:27:25 +000085 tsd->initMagic = INIT_MAGIC;
Brian Paulc11371a1999-12-16 17:31:06 +000086}
87
88
89void *
90_glthread_GetTSD(_glthread_TSD *tsd)
91{
Brian Paulb51b0a82001-03-07 05:06:11 +000092 if (tsd->initMagic != (int) INIT_MAGIC) {
Brian Paula9601f12000-02-10 21:27:25 +000093 _glthread_InitTSD(tsd);
94 }
Brian Paulc11371a1999-12-16 17:31:06 +000095 return pthread_getspecific(tsd->key);
96}
97
98
99void
Brian Paula9601f12000-02-10 21:27:25 +0000100_glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
Brian Paulc11371a1999-12-16 17:31:06 +0000101{
Brian Paulb51b0a82001-03-07 05:06:11 +0000102 if (tsd->initMagic != (int) INIT_MAGIC) {
Brian Paula9601f12000-02-10 21:27:25 +0000103 _glthread_InitTSD(tsd);
104 }
Brian Paulc11371a1999-12-16 17:31:06 +0000105 if (pthread_setspecific(tsd->key, ptr) != 0) {
106 perror(SET_TSD_ERROR);
107 exit(-1);
Brian Paule2b10e71999-12-17 11:13:54 +0000108 }
Brian Paulc11371a1999-12-16 17:31:06 +0000109}
110
111#endif /* PTHREADS */
112
113
114
115/*
Gareth Hughes22144ab2001-03-12 00:48:37 +0000116 * Solaris/Unix International Threads -- Use only if POSIX threads
Brian Paulc11371a1999-12-16 17:31:06 +0000117 * aren't available on your Unix platform. Solaris 2.[34] are examples
Gareth Hughes22144ab2001-03-12 00:48:37 +0000118 * of platforms where this is the case. Be sure to use -mt and/or
Brian Paulc11371a1999-12-16 17:31:06 +0000119 * -D_REENTRANT when compiling.
120 */
121#ifdef SOLARIS_THREADS
122#define USE_LOCK_FOR_KEY /* undef this to try a version without
123 lock for the global key... */
124
125unsigned long
126_glthread_GetID(void)
127{
128 abort(); /* XXX not implemented yet */
129 return (unsigned long) 0;
130}
131
132
133void
134_glthread_InitTSD(_glthread_TSD *tsd)
135{
136 if ((errno = mutex_init(&tsd->keylock, 0, NULL)) != 0 ||
137 (errno = thr_keycreate(&(tsd->key), free)) != 0) {
138 perror(INIT_TSD_ERROR);
139 exit(-1);
140 }
Brian Paula9601f12000-02-10 21:27:25 +0000141 tsd->initMagic = INIT_MAGIC;
Brian Paulc11371a1999-12-16 17:31:06 +0000142}
143
144
145void *
146_glthread_GetTSD(_glthread_TSD *tsd)
147{
148 void* ret;
Brian Paula9601f12000-02-10 21:27:25 +0000149 if (tsd->initMagic != INIT_MAGIC) {
150 _glthread_InitTSD(tsd);
151 }
Brian Paulc11371a1999-12-16 17:31:06 +0000152#ifdef USE_LOCK_FOR_KEY
153 mutex_lock(&tsd->keylock);
154 thr_getspecific(tsd->key, &ret);
Gareth Hughes22144ab2001-03-12 00:48:37 +0000155 mutex_unlock(&tsd->keylock);
Brian Paulc11371a1999-12-16 17:31:06 +0000156#else
157 if ((errno = thr_getspecific(tsd->key, &ret)) != 0) {
158 perror(GET_TSD_ERROR);
159 exit(-1);
Brian Paule2b10e71999-12-17 11:13:54 +0000160 }
Brian Paulc11371a1999-12-16 17:31:06 +0000161#endif
162 return ret;
163}
164
165
166void
Brian Paula9601f12000-02-10 21:27:25 +0000167_glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
Brian Paulc11371a1999-12-16 17:31:06 +0000168{
Brian Paula9601f12000-02-10 21:27:25 +0000169 if (tsd->initMagic != INIT_MAGIC) {
170 _glthread_InitTSD(tsd);
Brian Paulc11371a1999-12-16 17:31:06 +0000171 }
172 if ((errno = thr_setspecific(tsd->key, ptr)) != 0) {
173 perror(SET_TSD_ERROR);
174 exit(-1);
Brian Paule2b10e71999-12-17 11:13:54 +0000175 }
Brian Paulc11371a1999-12-16 17:31:06 +0000176}
177
178#undef USE_LOCK_FOR_KEY
179#endif /* SOLARIS_THREADS */
180
181
182
183/*
184 * Win32 Threads. The only available option for Windows 95/NT.
185 * Be sure that you compile using the Multithreaded runtime, otherwise
186 * bad things will happen.
Gareth Hughes22144ab2001-03-12 00:48:37 +0000187 */
Brian Paulfa937f62000-02-11 21:38:33 +0000188#ifdef WIN32_THREADS
Brian Paulc11371a1999-12-16 17:31:06 +0000189
190unsigned long
191_glthread_GetID(void)
192{
193 abort(); /* XXX not implemented yet */
194 return (unsigned long) 0;
195}
196
197
198void
199_glthread_InitTSD(_glthread_TSD *tsd)
200{
201 tsd->key = TlsAlloc();
202 if (tsd->key == 0xffffffff) {
203 /* Can Windows handle stderr messages for non-console
204 applications? Does Windows have perror? */
205 /* perror(SET_INIT_ERROR);*/
206 exit(-1);
207 }
Brian Paula9601f12000-02-10 21:27:25 +0000208 tsd->initMagic = INIT_MAGIC;
Brian Paulc11371a1999-12-16 17:31:06 +0000209}
210
211
212void *
213_glthread_GetTSD(_glthread_TSD *tsd)
214{
Brian Paula9601f12000-02-10 21:27:25 +0000215 if (tsd->initMagic != INIT_MAGIC) {
216 _glthread_InitTSD(tsd);
217 }
Brian Paulc11371a1999-12-16 17:31:06 +0000218 return TlsGetValue(tsd->key);
219}
220
221
222void
Brian Paula9601f12000-02-10 21:27:25 +0000223_glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
Brian Paulc11371a1999-12-16 17:31:06 +0000224{
225 /* the following code assumes that the _glthread_TSD has been initialized
226 to zero at creation */
Brian Paula9601f12000-02-10 21:27:25 +0000227 if (tsd->initMagic != INIT_MAGIC) {
228 _glthread_InitTSD(tsd);
Brian Paulc11371a1999-12-16 17:31:06 +0000229 }
230 if (TlsSetValue(tsd->key, ptr) == 0) {
231 /* Can Windows handle stderr messages for non-console
232 applications? Does Windows have perror? */
233 /* perror(SET_TSD_ERROR);*/
234 exit(-1);
Brian Paule2b10e71999-12-17 11:13:54 +0000235 }
Brian Paulc11371a1999-12-16 17:31:06 +0000236}
237
Brian Paulfa937f62000-02-11 21:38:33 +0000238#endif /* WIN32_THREADS */
Brian Paulc11371a1999-12-16 17:31:06 +0000239
Brian Paula9601f12000-02-10 21:27:25 +0000240
241
242/*
243 * XFree86 has its own thread wrapper, Xthreads.h
244 * We wrap it again for GL.
245 */
246#ifdef XTHREADS
247
248unsigned long
249_glthread_GetID(void)
250{
251 return (unsigned long) xthread_self();
252}
253
254
255void
256_glthread_InitTSD(_glthread_TSD *tsd)
257{
258 if (xthread_key_create(&tsd->key, NULL) != 0) {
259 perror(INIT_TSD_ERROR);
260 exit(-1);
261 }
262 tsd->initMagic = INIT_MAGIC;
263}
264
265
266void *
267_glthread_GetTSD(_glthread_TSD *tsd)
268{
269 void *ptr;
270 if (tsd->initMagic != INIT_MAGIC) {
271 _glthread_InitTSD(tsd);
272 }
273 xthread_get_specific(tsd->key, &ptr);
274 return ptr;
275}
276
277
278void
279_glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
280{
281 if (tsd->initMagic != INIT_MAGIC) {
282 _glthread_InitTSD(tsd);
283 }
284 xthread_set_specific(tsd->key, ptr);
285}
286
287#endif /* XTHREAD */
288
289
Brian Paula360ab22000-02-10 21:54:06 +0000290
Brian Paul1b37d6c2001-11-12 23:50:12 +0000291/*
292 * BeOS threads
293 */
294#ifdef BEOS_THREADS
295
296unsigned long
297_glthread_GetID(void)
298{
299 return (unsigned long) find_thread(NULL);
300}
301
302void
303_glthread_InitTSD(_glthread_TSD *tsd)
304{
305 tsd->key = tls_allocate();
306 tsd->initMagic = INIT_MAGIC;
307}
308
309void *
310_glthread_GetTSD(_glthread_TSD *tsd)
311{
312 if (tsd->initMagic != (int) INIT_MAGIC) {
313 _glthread_InitTSD(tsd);
314 }
315 return tls_get(tsd->key);
316}
317
318void
319_glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
320{
321 if (tsd->initMagic != (int) INIT_MAGIC) {
322 _glthread_InitTSD(tsd);
323 }
324 tls_set(tsd->key, ptr);
325}
326
327#endif /* BEOS_THREADS */
328
329
330
Brian Paula360ab22000-02-10 21:54:06 +0000331#else /* THREADS */
332
333
334/*
335 * no-op functions
336 */
337
338unsigned long
339_glthread_GetID(void)
340{
341 return 0;
342}
343
344
345void
346_glthread_InitTSD(_glthread_TSD *tsd)
347{
348 (void) tsd;
349}
350
351
352void *
353_glthread_GetTSD(_glthread_TSD *tsd)
354{
355 (void) tsd;
356 return NULL;
357}
358
359
360void
361_glthread_SetTSD(_glthread_TSD *tsd, void *ptr)
362{
363 (void) tsd;
364 (void) ptr;
365}
366
367
Brian Paulc11371a1999-12-16 17:31:06 +0000368#endif /* THREADS */