llvmpipe: Temporary workaround to prevent segfault on array textures.
[mesa.git] / src / mapi / u_thread.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /*
28 * Thread support for gl dispatch.
29 *
30 * Initial version by John Stone (j.stone@acm.org) (johns@cs.umr.edu)
31 * and Christoph Poliwoda (poliwoda@volumegraphics.com)
32 * Revised by Keith Whitwell
33 * Adapted for new gl dispatcher by Brian Paul
34 * Modified for use in mapi by Chia-I Wu
35 */
36
37 /*
38 * If this file is accidentally included by a non-threaded build,
39 * it should not cause the build to fail, or otherwise cause problems.
40 * In general, it should only be included when needed however.
41 */
42
43 #ifndef _U_THREAD_H_
44 #define _U_THREAD_H_
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include "u_compiler.h"
49
50 #if defined(HAVE_PTHREAD)
51 #include <pthread.h> /* POSIX threads headers */
52 #endif
53 #ifdef _WIN32
54 #include <windows.h>
55 #endif
56
57 #if defined(HAVE_PTHREAD) || defined(_WIN32)
58 #ifndef THREADS
59 #define THREADS
60 #endif
61 #endif
62
63 /*
64 * Error messages
65 */
66 #define INIT_TSD_ERROR "_glthread_: failed to allocate key for thread specific data"
67 #define GET_TSD_ERROR "_glthread_: failed to get thread specific data"
68 #define SET_TSD_ERROR "_glthread_: thread failed to set thread specific data"
69
70
71 /*
72 * Magic number to determine if a TSD object has been initialized.
73 * Kind of a hack but there doesn't appear to be a better cross-platform
74 * solution.
75 */
76 #define INIT_MAGIC 0xff8adc98
77
78 #ifdef __cplusplus
79 extern "C" {
80 #endif
81
82
83 /*
84 * POSIX threads. This should be your choice in the Unix world
85 * whenever possible. When building with POSIX threads, be sure
86 * to enable any compiler flags which will cause the MT-safe
87 * libc (if one exists) to be used when linking, as well as any
88 * header macros for MT-safe errno, etc. For Solaris, this is the -mt
89 * compiler flag. On Solaris with gcc, use -D_REENTRANT to enable
90 * proper compiling for MT-safe libc etc.
91 */
92 #if defined(HAVE_PTHREAD)
93
94 struct u_tsd {
95 pthread_key_t key;
96 unsigned initMagic;
97 };
98
99 typedef pthread_mutex_t u_mutex;
100
101 #define u_mutex_declare_static(name) \
102 static u_mutex name = PTHREAD_MUTEX_INITIALIZER
103
104 #define u_mutex_init(name) pthread_mutex_init(&(name), NULL)
105 #define u_mutex_destroy(name) pthread_mutex_destroy(&(name))
106 #define u_mutex_lock(name) (void) pthread_mutex_lock(&(name))
107 #define u_mutex_unlock(name) (void) pthread_mutex_unlock(&(name))
108
109 static INLINE unsigned long
110 u_thread_self(void)
111 {
112 return (unsigned long) pthread_self();
113 }
114
115
116 static INLINE void
117 u_tsd_init(struct u_tsd *tsd)
118 {
119 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
120 perror(INIT_TSD_ERROR);
121 exit(-1);
122 }
123 tsd->initMagic = INIT_MAGIC;
124 }
125
126
127 static INLINE void *
128 u_tsd_get(struct u_tsd *tsd)
129 {
130 if (tsd->initMagic != INIT_MAGIC) {
131 u_tsd_init(tsd);
132 }
133 return pthread_getspecific(tsd->key);
134 }
135
136
137 static INLINE void
138 u_tsd_set(struct u_tsd *tsd, void *ptr)
139 {
140 if (tsd->initMagic != INIT_MAGIC) {
141 u_tsd_init(tsd);
142 }
143 if (pthread_setspecific(tsd->key, ptr) != 0) {
144 perror(SET_TSD_ERROR);
145 exit(-1);
146 }
147 }
148
149 #endif /* HAVE_PTHREAD */
150
151
152 /*
153 * Windows threads. Should work with Windows NT and 95.
154 * IMPORTANT: Link with multithreaded runtime library when THREADS are
155 * used!
156 */
157 #ifdef _WIN32
158
159 struct u_tsd {
160 DWORD key;
161 unsigned initMagic;
162 };
163
164 typedef CRITICAL_SECTION u_mutex;
165
166 /* http://locklessinc.com/articles/pthreads_on_windows/ */
167 #define u_mutex_declare_static(name) \
168 static u_mutex name = {(PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0}
169
170 #define u_mutex_init(name) InitializeCriticalSection(&name)
171 #define u_mutex_destroy(name) DeleteCriticalSection(&name)
172 #define u_mutex_lock(name) EnterCriticalSection(&name)
173 #define u_mutex_unlock(name) LeaveCriticalSection(&name)
174
175 static INLINE unsigned long
176 u_thread_self(void)
177 {
178 return GetCurrentThreadId();
179 }
180
181
182 static INLINE void
183 u_tsd_init(struct u_tsd *tsd)
184 {
185 tsd->key = TlsAlloc();
186 if (tsd->key == TLS_OUT_OF_INDEXES) {
187 perror(INIT_TSD_ERROR);
188 exit(-1);
189 }
190 tsd->initMagic = INIT_MAGIC;
191 }
192
193
194 static INLINE void
195 u_tsd_destroy(struct u_tsd *tsd)
196 {
197 if (tsd->initMagic != INIT_MAGIC) {
198 return;
199 }
200 TlsFree(tsd->key);
201 tsd->initMagic = 0x0;
202 }
203
204
205 static INLINE void *
206 u_tsd_get(struct u_tsd *tsd)
207 {
208 if (tsd->initMagic != INIT_MAGIC) {
209 u_tsd_init(tsd);
210 }
211 return TlsGetValue(tsd->key);
212 }
213
214
215 static INLINE void
216 u_tsd_set(struct u_tsd *tsd, void *ptr)
217 {
218 /* the following code assumes that the struct u_tsd has been initialized
219 to zero at creation */
220 if (tsd->initMagic != INIT_MAGIC) {
221 u_tsd_init(tsd);
222 }
223 if (TlsSetValue(tsd->key, ptr) == 0) {
224 perror(SET_TSD_ERROR);
225 exit(-1);
226 }
227 }
228
229 #endif /* _WIN32 */
230
231
232 /*
233 * THREADS not defined
234 */
235 #ifndef THREADS
236
237 struct u_tsd {
238 unsigned initMagic;
239 };
240
241 typedef unsigned u_mutex;
242
243 #define u_mutex_declare_static(name) static u_mutex name = 0
244 #define u_mutex_init(name) (void) name
245 #define u_mutex_destroy(name) (void) name
246 #define u_mutex_lock(name) (void) name
247 #define u_mutex_unlock(name) (void) name
248
249 /*
250 * no-op functions
251 */
252
253 static INLINE unsigned long
254 u_thread_self(void)
255 {
256 return 0;
257 }
258
259
260 static INLINE void
261 u_tsd_init(struct u_tsd *tsd)
262 {
263 (void) tsd;
264 }
265
266
267 static INLINE void *
268 u_tsd_get(struct u_tsd *tsd)
269 {
270 (void) tsd;
271 return NULL;
272 }
273
274
275 static INLINE void
276 u_tsd_set(struct u_tsd *tsd, void *ptr)
277 {
278 (void) tsd;
279 (void) ptr;
280 }
281 #endif /* THREADS */
282
283
284 #ifdef __cplusplus
285 }
286 #endif
287
288 #endif /* _U_THREAD_H_ */