1 /* $Id: glthread.c,v 1.2 1999/12/17 11:13:54 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
9 * 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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * 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.
29 * Thread support for gl dispatch.
31 * Initial version by John Stone (j.stone@acm.org) (johns@cs.umr.edu)
32 * and Christoph Poliwoda (poliwoda@volumegraphics.com)
34 * Revised by Keith Whitwell
35 * Adapted for new gl dispatcher by Brian Paul
48 * This file should still compile even when THREADS is not defined.
49 * This is to make things easier to deal with on the makefile scene..
59 #define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data"
60 #define GET_TSD_ERROR "_glthread_: failed to get thread specific data"
61 #define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data"
65 * magic number for win32 and solaris threads equivalents of pthread_once
66 * This could probably be done better, but we haven't figured out how yet.
68 #define INITFUNC_CALLED_MAGIC 0xff8adc98
73 * POSIX Threads -- The best way to go if your platform supports them.
74 * Solaris >= 2.5 have POSIX threads, IRIX >= 6.4 reportedly
75 * has them, and many of the free Unixes now have them.
76 * Be sure to use appropriate -mt or -D_REENTRANT type
77 * compile flags when building.
84 return (unsigned long) pthread_self();
89 _glthread_InitTSD(_glthread_TSD
*tsd
)
91 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
92 perror(INIT_TSD_ERROR
);
99 _glthread_GetTSD(_glthread_TSD
*tsd
)
101 return pthread_getspecific(tsd
->key
);
106 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
, void (*initfunc
)(void))
108 pthread_once(&tsd
->once
, initfunc
);
109 if (pthread_setspecific(tsd
->key
, ptr
) != 0) {
110 perror(SET_TSD_ERROR
);
115 #endif /* PTHREADS */
120 * Solaris/Unix International Threads -- Use only if POSIX threads
121 * aren't available on your Unix platform. Solaris 2.[34] are examples
122 * of platforms where this is the case. Be sure to use -mt and/or
123 * -D_REENTRANT when compiling.
125 #ifdef SOLARIS_THREADS
126 #define USE_LOCK_FOR_KEY /* undef this to try a version without
127 lock for the global key... */
130 _glthread_GetID(void)
132 abort(); /* XXX not implemented yet */
133 return (unsigned long) 0;
138 _glthread_InitTSD(_glthread_TSD
*tsd
)
140 if ((errno
= mutex_init(&tsd
->keylock
, 0, NULL
)) != 0 ||
141 (errno
= thr_keycreate(&(tsd
->key
), free
)) != 0) {
142 perror(INIT_TSD_ERROR
);
149 _glthread_GetTSD(_glthread_TSD
*tsd
)
152 #ifdef USE_LOCK_FOR_KEY
153 mutex_lock(&tsd
->keylock
);
154 thr_getspecific(tsd
->key
, &ret
);
155 mutex_unlock(&tsd
->keylock
);
157 if ((errno
= thr_getspecific(tsd
->key
, &ret
)) != 0) {
158 perror(GET_TSD_ERROR
);
167 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
, void (*initfunc
)(void))
169 /* the following code assumes that the _glthread_TSD has been initialized
170 to zero at creation */
171 fprintf(stderr
, "initfuncCalled = %d\n", tsd
->initfuncCalled
);
172 if (tsd
->initfuncCalled
!= INITFUNC_CALLED_MAGIC
) {
174 tsd
->initfuncCalled
= INITFUNC_CALLED_MAGIC
;
176 if ((errno
= thr_setspecific(tsd
->key
, ptr
)) != 0) {
177 perror(SET_TSD_ERROR
);
182 #undef USE_LOCK_FOR_KEY
183 #endif /* SOLARIS_THREADS */
188 * Win32 Threads. The only available option for Windows 95/NT.
189 * Be sure that you compile using the Multithreaded runtime, otherwise
190 * bad things will happen.
195 _glthread_GetID(void)
197 abort(); /* XXX not implemented yet */
198 return (unsigned long) 0;
203 _glthread_InitTSD(_glthread_TSD
*tsd
)
205 tsd
->key
= TlsAlloc();
206 if (tsd
->key
== 0xffffffff) {
207 /* Can Windows handle stderr messages for non-console
208 applications? Does Windows have perror? */
209 /* perror(SET_INIT_ERROR);*/
216 _glthread_GetTSD(_glthread_TSD
*tsd
)
218 return TlsGetValue(tsd
->key
);
223 _glthread_SetTSD(_glthread_TSD
*tsd
, void *ptr
, void (*initfunc
)(void))
225 /* the following code assumes that the _glthread_TSD has been initialized
226 to zero at creation */
227 if (tsd
->initfuncCalled
!= INITFUNC_CALLED_MAGIC
) {
229 tsd
->initfuncCalled
= INITFUNC_CALLED_MAGIC
;
231 if (TlsSetValue(tsd
->key
, ptr
) == 0) {
232 /* Can Windows handle stderr messages for non-console
233 applications? Does Windows have perror? */
234 /* perror(SET_TSD_ERROR);*/