i965: fix bugs in projective texture coordinates
[mesa.git] / src / gallium / include / pipe / p_thread.h
1 /**************************************************************************
2 *
3 * Copyright 1999-2006 Brian Paul
4 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * 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 /**
28 * @file
29 *
30 * Thread, mutex, condition var and thread-specific data functions.
31 */
32
33
34 #ifndef _P_THREAD2_H_
35 #define _P_THREAD2_H_
36
37
38 #include "pipe/p_compiler.h"
39 #include "util/u_debug.h" /* for assert */
40
41
42 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
43
44 #include <pthread.h> /* POSIX threads headers */
45 #include <stdio.h> /* for perror() */
46
47 typedef pthread_t pipe_thread;
48
49 #define PIPE_THREAD_ROUTINE( name, param ) \
50 void *name( void *param )
51
52 static INLINE pipe_thread pipe_thread_create( void *(* routine)( void *), void *param )
53 {
54 pipe_thread thread;
55 if (pthread_create( &thread, NULL, routine, param ))
56 return 0;
57 return thread;
58 }
59
60 static INLINE int pipe_thread_wait( pipe_thread thread )
61 {
62 return pthread_join( thread, NULL );
63 }
64
65 static INLINE int pipe_thread_destroy( pipe_thread thread )
66 {
67 return pthread_detach( thread );
68 }
69
70 typedef pthread_mutex_t pipe_mutex;
71 typedef pthread_cond_t pipe_condvar;
72
73 #define pipe_static_mutex(mutex) \
74 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
75
76 #define pipe_mutex_init(mutex) \
77 (void) pthread_mutex_init(&(mutex), NULL)
78
79 #define pipe_mutex_destroy(mutex) \
80 pthread_mutex_destroy(&(mutex))
81
82 #define pipe_mutex_lock(mutex) \
83 (void) pthread_mutex_lock(&(mutex))
84
85 #define pipe_mutex_unlock(mutex) \
86 (void) pthread_mutex_unlock(&(mutex))
87
88 #define pipe_static_condvar(mutex) \
89 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
90
91 #define pipe_condvar_init(cond) \
92 pthread_cond_init(&(cond), NULL)
93
94 #define pipe_condvar_destroy(cond) \
95 pthread_cond_destroy(&(cond))
96
97 #define pipe_condvar_wait(cond, mutex) \
98 pthread_cond_wait(&(cond), &(mutex))
99
100 #define pipe_condvar_signal(cond) \
101 pthread_cond_signal(&(cond))
102
103 #define pipe_condvar_broadcast(cond) \
104 pthread_cond_broadcast(&(cond))
105
106
107 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
108
109 #include <windows.h>
110
111 typedef HANDLE pipe_thread;
112
113 #define PIPE_THREAD_ROUTINE( name, param ) \
114 void * WINAPI name( void *param )
115
116 static INLINE pipe_thread pipe_thread_create( void *(WINAPI * routine)( void *), void *param )
117 {
118 DWORD id;
119 return CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) routine, param, 0, &id );
120 }
121
122 static INLINE int pipe_thread_wait( pipe_thread thread )
123 {
124 if (WaitForSingleObject( thread, INFINITE ) == WAIT_OBJECT_0)
125 return 0;
126 return -1;
127 }
128
129 static INLINE int pipe_thread_destroy( pipe_thread thread )
130 {
131 if (CloseHandle( thread ))
132 return 0;
133 return -1;
134 }
135
136 typedef CRITICAL_SECTION pipe_mutex;
137
138 #define pipe_static_mutex(mutex) \
139 /*static*/ pipe_mutex mutex = {0,0,0,0,0,0}
140
141 #define pipe_mutex_init(mutex) \
142 InitializeCriticalSection(&mutex)
143
144 #define pipe_mutex_destroy(mutex) \
145 DeleteCriticalSection(&mutex)
146
147 #define pipe_mutex_lock(mutex) \
148 EnterCriticalSection(&mutex)
149
150 #define pipe_mutex_unlock(mutex) \
151 LeaveCriticalSection(&mutex)
152
153 /* XXX: dummy definitions, make it compile */
154
155 typedef unsigned pipe_condvar;
156
157 #define pipe_condvar_init(condvar) \
158 (void) condvar
159
160 #define pipe_condvar_broadcast(condvar) \
161 (void) condvar
162
163 #else
164
165 /** Dummy definitions */
166
167 typedef unsigned pipe_thread;
168 typedef unsigned pipe_mutex;
169 typedef unsigned pipe_condvar;
170
171 #define pipe_static_mutex(mutex) \
172 static pipe_mutex mutex = 0
173
174 #define pipe_mutex_init(mutex) \
175 (void) mutex
176
177 #define pipe_mutex_destroy(mutex) \
178 (void) mutex
179
180 #define pipe_mutex_lock(mutex) \
181 (void) mutex
182
183 #define pipe_mutex_unlock(mutex) \
184 (void) mutex
185
186 #define pipe_static_condvar(condvar) \
187 static unsigned condvar = 0
188
189 #define pipe_condvar_init(condvar) \
190 (void) condvar
191
192 #define pipe_condvar_destroy(condvar) \
193 (void) condvar
194
195 #define pipe_condvar_wait(condvar, mutex) \
196 (void) condvar
197
198 #define pipe_condvar_signal(condvar) \
199 (void) condvar
200
201 #define pipe_condvar_broadcast(condvar) \
202 (void) condvar
203
204
205 #endif /* PIPE_OS_? */
206
207
208
209 /*
210 * Thread-specific data.
211 */
212
213 typedef struct {
214 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
215 pthread_key_t key;
216 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
217 DWORD key;
218 #endif
219 int initMagic;
220 } pipe_tsd;
221
222
223 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
224
225
226 static INLINE void
227 pipe_tsd_init(pipe_tsd *tsd)
228 {
229 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
230 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
231 perror("pthread_key_create(): failed to allocate key for thread specific data");
232 exit(-1);
233 }
234 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
235 assert(0);
236 #endif
237 tsd->initMagic = PIPE_TSD_INIT_MAGIC;
238 }
239
240 static INLINE void *
241 pipe_tsd_get(pipe_tsd *tsd)
242 {
243 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
244 pipe_tsd_init(tsd);
245 }
246 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
247 return pthread_getspecific(tsd->key);
248 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
249 assert(0);
250 return NULL;
251 #else
252 assert(0);
253 return NULL;
254 #endif
255 }
256
257 static INLINE void
258 pipe_tsd_set(pipe_tsd *tsd, void *value)
259 {
260 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
261 pipe_tsd_init(tsd);
262 }
263 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
264 if (pthread_setspecific(tsd->key, value) != 0) {
265 perror("pthread_set_specific() failed");
266 exit(-1);
267 }
268 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
269 assert(0);
270 #else
271 assert(0);
272 #endif
273 }
274
275
276
277 #endif /* _P_THREAD2_H_ */