don't free thread data in pthread_key_create()
[mesa.git] / src / mesa / glapi / glthread.c
1 /* $Id: glthread.c,v 1.2 1999/12/17 11:13:54 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.3
6 *
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 */
26
27
28 /*
29 * Thread support for gl dispatch.
30 *
31 * Initial version by John Stone (j.stone@acm.org) (johns@cs.umr.edu)
32 * and Christoph Poliwoda (poliwoda@volumegraphics.com)
33 *
34 * Revised by Keith Whitwell
35 * Adapted for new gl dispatcher by Brian Paul
36 */
37
38
39 #ifdef PC_ALL
40 #include "all.h"
41 #else
42 #include "glheader.h"
43 #endif
44
45
46
47 /*
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..
50 */
51 #ifdef THREADS
52 #include <errno.h>
53 #include "glthread.h"
54
55
56 /*
57 * Error messages
58 */
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"
62
63
64 /*
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.
67 */
68 #define INITFUNC_CALLED_MAGIC 0xff8adc98
69
70
71
72 /*
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.
78 */
79 #ifdef PTHREADS
80
81 unsigned long
82 _glthread_GetID(void)
83 {
84 return (unsigned long) pthread_self();
85 }
86
87
88 void
89 _glthread_InitTSD(_glthread_TSD *tsd)
90 {
91 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
92 perror(INIT_TSD_ERROR);
93 exit(-1);
94 }
95 }
96
97
98 void *
99 _glthread_GetTSD(_glthread_TSD *tsd)
100 {
101 return pthread_getspecific(tsd->key);
102 }
103
104
105 void
106 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr, void (*initfunc)(void))
107 {
108 pthread_once(&tsd->once, initfunc);
109 if (pthread_setspecific(tsd->key, ptr) != 0) {
110 perror(SET_TSD_ERROR);
111 exit(-1);
112 }
113 }
114
115 #endif /* PTHREADS */
116
117
118
119 /*
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.
124 */
125 #ifdef SOLARIS_THREADS
126 #define USE_LOCK_FOR_KEY /* undef this to try a version without
127 lock for the global key... */
128
129 unsigned long
130 _glthread_GetID(void)
131 {
132 abort(); /* XXX not implemented yet */
133 return (unsigned long) 0;
134 }
135
136
137 void
138 _glthread_InitTSD(_glthread_TSD *tsd)
139 {
140 if ((errno = mutex_init(&tsd->keylock, 0, NULL)) != 0 ||
141 (errno = thr_keycreate(&(tsd->key), free)) != 0) {
142 perror(INIT_TSD_ERROR);
143 exit(-1);
144 }
145 }
146
147
148 void *
149 _glthread_GetTSD(_glthread_TSD *tsd)
150 {
151 void* ret;
152 #ifdef USE_LOCK_FOR_KEY
153 mutex_lock(&tsd->keylock);
154 thr_getspecific(tsd->key, &ret);
155 mutex_unlock(&tsd->keylock);
156 #else
157 if ((errno = thr_getspecific(tsd->key, &ret)) != 0) {
158 perror(GET_TSD_ERROR);
159 exit(-1);
160 }
161 #endif
162 return ret;
163 }
164
165
166 void
167 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr, void (*initfunc)(void))
168 {
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) {
173 initfunc();
174 tsd->initfuncCalled = INITFUNC_CALLED_MAGIC;
175 }
176 if ((errno = thr_setspecific(tsd->key, ptr)) != 0) {
177 perror(SET_TSD_ERROR);
178 exit(-1);
179 }
180 }
181
182 #undef USE_LOCK_FOR_KEY
183 #endif /* SOLARIS_THREADS */
184
185
186
187 /*
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.
191 */
192 #ifdef WIN32
193
194 unsigned long
195 _glthread_GetID(void)
196 {
197 abort(); /* XXX not implemented yet */
198 return (unsigned long) 0;
199 }
200
201
202 void
203 _glthread_InitTSD(_glthread_TSD *tsd)
204 {
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);*/
210 exit(-1);
211 }
212 }
213
214
215 void *
216 _glthread_GetTSD(_glthread_TSD *tsd)
217 {
218 return TlsGetValue(tsd->key);
219 }
220
221
222 void
223 _glthread_SetTSD(_glthread_TSD *tsd, void *ptr, void (*initfunc)(void))
224 {
225 /* the following code assumes that the _glthread_TSD has been initialized
226 to zero at creation */
227 if (tsd->initfuncCalled != INITFUNC_CALLED_MAGIC) {
228 initfunc();
229 tsd->initfuncCalled = INITFUNC_CALLED_MAGIC;
230 }
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);*/
235 exit(-1);
236 }
237 }
238
239 #endif /* WIN32 */
240
241 #endif /* THREADS */
242
243