Merge branch 'glsl2-head' into glsl2
[mesa.git] / src / gallium / auxiliary / os / os_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 variable, barrier, semaphore and
31 * thread-specific data functions.
32 */
33
34
35 #ifndef OS_THREAD_H_
36 #define OS_THREAD_H_
37
38
39 #include "pipe/p_compiler.h"
40 #include "util/u_debug.h" /* for assert */
41
42
43 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_EMBEDDED) || defined(PIPE_OS_CYGWIN)
44
45 #include <pthread.h> /* POSIX threads headers */
46 #include <stdio.h> /* for perror() */
47
48 #define PIPE_THREAD_HAVE_CONDVAR
49
50 /* pipe_thread
51 */
52 typedef pthread_t pipe_thread;
53
54 #define PIPE_THREAD_ROUTINE( name, param ) \
55 void *name( void *param )
56
57 static INLINE pipe_thread pipe_thread_create( void *(* routine)( void *), void *param )
58 {
59 pipe_thread thread;
60 if (pthread_create( &thread, NULL, routine, param ))
61 return 0;
62 return thread;
63 }
64
65 static INLINE int pipe_thread_wait( pipe_thread thread )
66 {
67 return pthread_join( thread, NULL );
68 }
69
70 static INLINE int pipe_thread_destroy( pipe_thread thread )
71 {
72 return pthread_detach( thread );
73 }
74
75
76 /* pipe_mutex
77 */
78 typedef pthread_mutex_t pipe_mutex;
79
80 #define pipe_static_mutex(mutex) \
81 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
82
83 #define pipe_mutex_init(mutex) \
84 (void) pthread_mutex_init(&(mutex), NULL)
85
86 #define pipe_mutex_destroy(mutex) \
87 pthread_mutex_destroy(&(mutex))
88
89 #define pipe_mutex_lock(mutex) \
90 (void) pthread_mutex_lock(&(mutex))
91
92 #define pipe_mutex_unlock(mutex) \
93 (void) pthread_mutex_unlock(&(mutex))
94
95
96 /* pipe_condvar
97 */
98 typedef pthread_cond_t pipe_condvar;
99
100 #define pipe_static_condvar(mutex) \
101 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
102
103 #define pipe_condvar_init(cond) \
104 pthread_cond_init(&(cond), NULL)
105
106 #define pipe_condvar_destroy(cond) \
107 pthread_cond_destroy(&(cond))
108
109 #define pipe_condvar_wait(cond, mutex) \
110 pthread_cond_wait(&(cond), &(mutex))
111
112 #define pipe_condvar_signal(cond) \
113 pthread_cond_signal(&(cond))
114
115 #define pipe_condvar_broadcast(cond) \
116 pthread_cond_broadcast(&(cond))
117
118
119
120 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
121
122 #include <windows.h>
123
124 /* pipe_thread
125 */
126 typedef HANDLE pipe_thread;
127
128 #define PIPE_THREAD_ROUTINE( name, param ) \
129 void * WINAPI name( void *param )
130
131 static INLINE pipe_thread pipe_thread_create( void *(WINAPI * routine)( void *), void *param )
132 {
133 DWORD id;
134 return CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) routine, param, 0, &id );
135 }
136
137 static INLINE int pipe_thread_wait( pipe_thread thread )
138 {
139 if (WaitForSingleObject( thread, INFINITE ) == WAIT_OBJECT_0)
140 return 0;
141 return -1;
142 }
143
144 static INLINE int pipe_thread_destroy( pipe_thread thread )
145 {
146 if (CloseHandle( thread ))
147 return 0;
148 return -1;
149 }
150
151
152 /* pipe_mutex
153 */
154 typedef CRITICAL_SECTION pipe_mutex;
155
156 #define pipe_static_mutex(mutex) \
157 /*static*/ pipe_mutex mutex = {0,0,0,0,0,0}
158
159 #define pipe_mutex_init(mutex) \
160 InitializeCriticalSection(&mutex)
161
162 #define pipe_mutex_destroy(mutex) \
163 DeleteCriticalSection(&mutex)
164
165 #define pipe_mutex_lock(mutex) \
166 EnterCriticalSection(&mutex)
167
168 #define pipe_mutex_unlock(mutex) \
169 LeaveCriticalSection(&mutex)
170
171
172 /* pipe_condvar (XXX FIX THIS)
173 */
174 typedef unsigned pipe_condvar;
175
176 #define pipe_condvar_init(cond) \
177 (void) cond
178
179 #define pipe_condvar_destroy(cond) \
180 (void) cond
181
182 #define pipe_condvar_wait(cond, mutex) \
183 (void) cond; (void) mutex
184
185 #define pipe_condvar_signal(cond) \
186 (void) cond
187
188 #define pipe_condvar_broadcast(cond) \
189 (void) cond
190
191
192 #else
193
194 /** Dummy definitions */
195
196 typedef unsigned pipe_thread;
197
198 #define PIPE_THREAD_ROUTINE( name, param ) \
199 void * name( void *param )
200
201 static INLINE pipe_thread pipe_thread_create( void *(* routine)( void *), void *param )
202 {
203 return 0;
204 }
205
206 static INLINE int pipe_thread_wait( pipe_thread thread )
207 {
208 return -1;
209 }
210
211 static INLINE int pipe_thread_destroy( pipe_thread thread )
212 {
213 return -1;
214 }
215
216 typedef unsigned pipe_mutex;
217 typedef unsigned pipe_condvar;
218
219 #define pipe_static_mutex(mutex) \
220 static pipe_mutex mutex = 0
221
222 #define pipe_mutex_init(mutex) \
223 (void) mutex
224
225 #define pipe_mutex_destroy(mutex) \
226 (void) mutex
227
228 #define pipe_mutex_lock(mutex) \
229 (void) mutex
230
231 #define pipe_mutex_unlock(mutex) \
232 (void) mutex
233
234 #define pipe_static_condvar(condvar) \
235 static unsigned condvar = 0
236
237 #define pipe_condvar_init(condvar) \
238 (void) condvar
239
240 #define pipe_condvar_destroy(condvar) \
241 (void) condvar
242
243 #define pipe_condvar_wait(condvar, mutex) \
244 (void) condvar
245
246 #define pipe_condvar_signal(condvar) \
247 (void) condvar
248
249 #define pipe_condvar_broadcast(condvar) \
250 (void) condvar
251
252
253 #endif /* PIPE_OS_? */
254
255
256 /*
257 * pipe_barrier
258 */
259
260 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_EMBEDDED)
261
262 typedef pthread_barrier_t pipe_barrier;
263
264 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
265 {
266 pthread_barrier_init(barrier, NULL, count);
267 }
268
269 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
270 {
271 pthread_barrier_destroy(barrier);
272 }
273
274 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
275 {
276 pthread_barrier_wait(barrier);
277 }
278
279
280 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
281
282 /* XXX FIX THIS */
283 typedef unsigned pipe_barrier;
284
285 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
286 {
287 /* XXX we could implement barriers with a mutex and condition var */
288 }
289
290 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
291 {
292 }
293
294 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
295 {
296 assert(0);
297 }
298
299
300 #else
301
302 typedef struct {
303 unsigned count;
304 unsigned waiters;
305 uint64_t sequence;
306 pipe_mutex mutex;
307 pipe_condvar condvar;
308 } pipe_barrier;
309
310 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
311 {
312 barrier->count = count;
313 barrier->waiters = 0;
314 barrier->sequence = 0;
315 pipe_mutex_init(barrier->mutex);
316 pipe_condvar_init(barrier->condvar);
317 }
318
319 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
320 {
321 assert(barrier->waiters == 0);
322 pipe_mutex_destroy(barrier->mutex);
323 pipe_condvar_destroy(barrier->condvar);
324 }
325
326 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
327 {
328 pipe_mutex_lock(barrier->mutex);
329
330 assert(barrier->waiters < barrier->count);
331 barrier->waiters++;
332
333 if (barrier->waiters < barrier->count) {
334 uint64_t sequence = barrier->sequence;
335
336 do {
337 pipe_condvar_wait(barrier->condvar, barrier->mutex);
338 } while (sequence == barrier->sequence);
339 } else {
340 barrier->waiters = 0;
341 barrier->sequence++;
342 pipe_condvar_broadcast(barrier->condvar);
343 }
344
345 pipe_mutex_unlock(barrier->mutex);
346 }
347
348
349 #endif
350
351
352 /*
353 * Semaphores
354 */
355
356 typedef struct
357 {
358 pipe_mutex mutex;
359 pipe_condvar cond;
360 int counter;
361 } pipe_semaphore;
362
363
364 static INLINE void
365 pipe_semaphore_init(pipe_semaphore *sema, int init_val)
366 {
367 pipe_mutex_init(sema->mutex);
368 pipe_condvar_init(sema->cond);
369 sema->counter = init_val;
370 }
371
372 static INLINE void
373 pipe_semaphore_destroy(pipe_semaphore *sema)
374 {
375 pipe_mutex_destroy(sema->mutex);
376 pipe_condvar_destroy(sema->cond);
377 }
378
379 /** Signal/increment semaphore counter */
380 static INLINE void
381 pipe_semaphore_signal(pipe_semaphore *sema)
382 {
383 pipe_mutex_lock(sema->mutex);
384 sema->counter++;
385 pipe_condvar_signal(sema->cond);
386 pipe_mutex_unlock(sema->mutex);
387 }
388
389 /** Wait for semaphore counter to be greater than zero */
390 static INLINE void
391 pipe_semaphore_wait(pipe_semaphore *sema)
392 {
393 pipe_mutex_lock(sema->mutex);
394 while (sema->counter <= 0) {
395 pipe_condvar_wait(sema->cond, sema->mutex);
396 }
397 sema->counter--;
398 pipe_mutex_unlock(sema->mutex);
399 }
400
401
402
403 /*
404 * Thread-specific data.
405 */
406
407 typedef struct {
408 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_EMBEDDED) || defined(PIPE_OS_CYGWIN)
409 pthread_key_t key;
410 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
411 DWORD key;
412 #endif
413 int initMagic;
414 } pipe_tsd;
415
416
417 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
418
419
420 static INLINE void
421 pipe_tsd_init(pipe_tsd *tsd)
422 {
423 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_EMBEDDED) || defined(PIPE_OS_CYGWIN)
424 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
425 perror("pthread_key_create(): failed to allocate key for thread specific data");
426 exit(-1);
427 }
428 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
429 assert(0);
430 #endif
431 tsd->initMagic = PIPE_TSD_INIT_MAGIC;
432 }
433
434 static INLINE void *
435 pipe_tsd_get(pipe_tsd *tsd)
436 {
437 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
438 pipe_tsd_init(tsd);
439 }
440 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_EMBEDDED) || defined(PIPE_OS_CYGWIN)
441 return pthread_getspecific(tsd->key);
442 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
443 assert(0);
444 return NULL;
445 #else
446 assert(0);
447 return NULL;
448 #endif
449 }
450
451 static INLINE void
452 pipe_tsd_set(pipe_tsd *tsd, void *value)
453 {
454 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
455 pipe_tsd_init(tsd);
456 }
457 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_EMBEDDED) || defined(PIPE_OS_CYGWIN)
458 if (pthread_setspecific(tsd->key, value) != 0) {
459 perror("pthread_set_specific() failed");
460 exit(-1);
461 }
462 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
463 assert(0);
464 #else
465 assert(0);
466 #endif
467 }
468
469
470
471 #endif /* OS_THREAD_H_ */