os, rbug: remove PIPE_THREAD_HAVE_CONDVAR
[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
49 /* pipe_thread
50 */
51 typedef pthread_t pipe_thread;
52
53 #define PIPE_THREAD_ROUTINE( name, param ) \
54 void *name( void *param )
55
56 static INLINE pipe_thread pipe_thread_create( void *(* routine)( void *), void *param )
57 {
58 pipe_thread thread;
59 if (pthread_create( &thread, NULL, routine, param ))
60 return 0;
61 return thread;
62 }
63
64 static INLINE int pipe_thread_wait( pipe_thread thread )
65 {
66 return pthread_join( thread, NULL );
67 }
68
69 static INLINE int pipe_thread_destroy( pipe_thread thread )
70 {
71 return pthread_detach( thread );
72 }
73
74
75 /* pipe_mutex
76 */
77 typedef pthread_mutex_t pipe_mutex;
78
79 #define pipe_static_mutex(mutex) \
80 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
81
82 #define pipe_mutex_init(mutex) \
83 (void) pthread_mutex_init(&(mutex), NULL)
84
85 #define pipe_mutex_destroy(mutex) \
86 pthread_mutex_destroy(&(mutex))
87
88 #define pipe_mutex_lock(mutex) \
89 (void) pthread_mutex_lock(&(mutex))
90
91 #define pipe_mutex_unlock(mutex) \
92 (void) pthread_mutex_unlock(&(mutex))
93
94
95 /* pipe_condvar
96 */
97 typedef pthread_cond_t pipe_condvar;
98
99 #define pipe_static_condvar(mutex) \
100 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
101
102 #define pipe_condvar_init(cond) \
103 pthread_cond_init(&(cond), NULL)
104
105 #define pipe_condvar_destroy(cond) \
106 pthread_cond_destroy(&(cond))
107
108 #define pipe_condvar_wait(cond, mutex) \
109 pthread_cond_wait(&(cond), &(mutex))
110
111 #define pipe_condvar_signal(cond) \
112 pthread_cond_signal(&(cond))
113
114 #define pipe_condvar_broadcast(cond) \
115 pthread_cond_broadcast(&(cond))
116
117
118
119 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
120
121 #include <windows.h>
122
123 /* pipe_thread
124 */
125 typedef HANDLE pipe_thread;
126
127 #define PIPE_THREAD_ROUTINE( name, param ) \
128 void * WINAPI name( void *param )
129
130 static INLINE pipe_thread pipe_thread_create( void *(WINAPI * routine)( void *), void *param )
131 {
132 DWORD id;
133 return CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) routine, param, 0, &id );
134 }
135
136 static INLINE int pipe_thread_wait( pipe_thread thread )
137 {
138 if (WaitForSingleObject( thread, INFINITE ) == WAIT_OBJECT_0)
139 return 0;
140 return -1;
141 }
142
143 static INLINE int pipe_thread_destroy( pipe_thread thread )
144 {
145 if (CloseHandle( thread ))
146 return 0;
147 return -1;
148 }
149
150
151 /* pipe_mutex
152 */
153 typedef CRITICAL_SECTION pipe_mutex;
154
155 #define pipe_static_mutex(mutex) \
156 /*static*/ pipe_mutex mutex = {0,0,0,0,0,0}
157
158 #define pipe_mutex_init(mutex) \
159 InitializeCriticalSection(&mutex)
160
161 #define pipe_mutex_destroy(mutex) \
162 DeleteCriticalSection(&mutex)
163
164 #define pipe_mutex_lock(mutex) \
165 EnterCriticalSection(&mutex)
166
167 #define pipe_mutex_unlock(mutex) \
168 LeaveCriticalSection(&mutex)
169
170 /* TODO: Need a macro to declare "I don't care about WinXP compatibilty" */
171 #if 0 && defined (_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
172 /* CONDITION_VARIABLE is only available on newer versions of Windows
173 * (Server 2008/Vista or later).
174 * http://msdn.microsoft.com/en-us/library/ms682052(VS.85).aspx
175 *
176 * pipe_condvar
177 */
178 typedef CONDITION_VARIABLE pipe_condvar;
179
180 #define pipe_static_condvar(cond) \
181 /*static*/ pipe_condvar cond = CONDITION_VARIABLE_INIT
182
183 #define pipe_condvar_init(cond) \
184 InitializeConditionVariable(&(cond))
185
186 #define pipe_condvar_destroy(cond) \
187 (void) cond /* nothing to do */
188
189 #define pipe_condvar_wait(cond, mutex) \
190 SleepConditionVariableCS(&(cond), &(mutex), INFINITE)
191
192 #define pipe_condvar_signal(cond) \
193 WakeConditionVariable(&(cond))
194
195 #define pipe_condvar_broadcast(cond) \
196 WakeAllConditionVariable(&(cond))
197
198 #else /* need compatibility with pre-Vista Win32 */
199
200 /* pipe_condvar (XXX FIX THIS)
201 * See http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
202 * for potential pitfalls in implementation.
203 */
204 typedef DWORD pipe_condvar;
205
206 #define pipe_static_condvar(cond) \
207 /*static*/ pipe_condvar cond = 1
208
209 #define pipe_condvar_init(cond) \
210 (void) (cond = 1)
211
212 #define pipe_condvar_destroy(cond) \
213 (void) cond
214
215 /* Poor man's pthread_cond_wait():
216 Just release the mutex and sleep for one millisecond.
217 The caller's while() loop does all the work. */
218 #define pipe_condvar_wait(cond, mutex) \
219 do { pipe_mutex_unlock(mutex); \
220 Sleep(cond); \
221 pipe_mutex_lock(mutex); \
222 } while (0)
223
224 #define pipe_condvar_signal(cond) \
225 (void) cond
226
227 #define pipe_condvar_broadcast(cond) \
228 (void) cond
229
230 #endif /* pre-Vista win32 */
231
232 #else
233
234 #include "os/os_time.h"
235
236 /** Dummy definitions */
237
238 typedef unsigned pipe_thread;
239
240 #define PIPE_THREAD_ROUTINE( name, param ) \
241 void * name( void *param )
242
243 static INLINE pipe_thread pipe_thread_create( void *(* routine)( void *), void *param )
244 {
245 return 0;
246 }
247
248 static INLINE int pipe_thread_wait( pipe_thread thread )
249 {
250 return -1;
251 }
252
253 static INLINE int pipe_thread_destroy( pipe_thread thread )
254 {
255 return -1;
256 }
257
258 typedef unsigned pipe_mutex;
259
260 #define pipe_static_mutex(mutex) \
261 static pipe_mutex mutex = 0
262
263 #define pipe_mutex_init(mutex) \
264 (void) mutex
265
266 #define pipe_mutex_destroy(mutex) \
267 (void) mutex
268
269 #define pipe_mutex_lock(mutex) \
270 (void) mutex
271
272 #define pipe_mutex_unlock(mutex) \
273 (void) mutex
274
275 typedef int64_t pipe_condvar;
276
277 #define pipe_static_condvar(condvar) \
278 static pipe_condvar condvar = 1000
279
280 #define pipe_condvar_init(condvar) \
281 (void) (condvar = 1000)
282
283 #define pipe_condvar_destroy(condvar) \
284 (void) condvar
285
286 /* Poor man's pthread_cond_wait():
287 Just release the mutex and sleep for one millisecond.
288 The caller's while() loop does all the work. */
289 #define pipe_condvar_wait(condvar, mutex) \
290 do { pipe_mutex_unlock(mutex); \
291 os_time_sleep(condvar); \
292 pipe_mutex_lock(mutex); \
293 } while (0)
294
295 #define pipe_condvar_signal(condvar) \
296 (void) condvar
297
298 #define pipe_condvar_broadcast(condvar) \
299 (void) condvar
300
301
302 #endif /* PIPE_OS_? */
303
304
305 /*
306 * pipe_barrier
307 */
308
309 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_EMBEDDED)
310
311 typedef pthread_barrier_t pipe_barrier;
312
313 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
314 {
315 pthread_barrier_init(barrier, NULL, count);
316 }
317
318 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
319 {
320 pthread_barrier_destroy(barrier);
321 }
322
323 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
324 {
325 pthread_barrier_wait(barrier);
326 }
327
328
329 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
330
331 /* XXX FIX THIS */
332 typedef unsigned pipe_barrier;
333
334 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
335 {
336 /* XXX we could implement barriers with a mutex and condition var */
337 }
338
339 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
340 {
341 }
342
343 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
344 {
345 assert(0);
346 }
347
348
349 #else
350
351 typedef struct {
352 unsigned count;
353 unsigned waiters;
354 uint64_t sequence;
355 pipe_mutex mutex;
356 pipe_condvar condvar;
357 } pipe_barrier;
358
359 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
360 {
361 barrier->count = count;
362 barrier->waiters = 0;
363 barrier->sequence = 0;
364 pipe_mutex_init(barrier->mutex);
365 pipe_condvar_init(barrier->condvar);
366 }
367
368 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
369 {
370 assert(barrier->waiters == 0);
371 pipe_mutex_destroy(barrier->mutex);
372 pipe_condvar_destroy(barrier->condvar);
373 }
374
375 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
376 {
377 pipe_mutex_lock(barrier->mutex);
378
379 assert(barrier->waiters < barrier->count);
380 barrier->waiters++;
381
382 if (barrier->waiters < barrier->count) {
383 uint64_t sequence = barrier->sequence;
384
385 do {
386 pipe_condvar_wait(barrier->condvar, barrier->mutex);
387 } while (sequence == barrier->sequence);
388 } else {
389 barrier->waiters = 0;
390 barrier->sequence++;
391 pipe_condvar_broadcast(barrier->condvar);
392 }
393
394 pipe_mutex_unlock(barrier->mutex);
395 }
396
397
398 #endif
399
400
401 /*
402 * Semaphores
403 */
404
405 typedef struct
406 {
407 pipe_mutex mutex;
408 pipe_condvar cond;
409 int counter;
410 } pipe_semaphore;
411
412
413 static INLINE void
414 pipe_semaphore_init(pipe_semaphore *sema, int init_val)
415 {
416 pipe_mutex_init(sema->mutex);
417 pipe_condvar_init(sema->cond);
418 sema->counter = init_val;
419 }
420
421 static INLINE void
422 pipe_semaphore_destroy(pipe_semaphore *sema)
423 {
424 pipe_mutex_destroy(sema->mutex);
425 pipe_condvar_destroy(sema->cond);
426 }
427
428 /** Signal/increment semaphore counter */
429 static INLINE void
430 pipe_semaphore_signal(pipe_semaphore *sema)
431 {
432 pipe_mutex_lock(sema->mutex);
433 sema->counter++;
434 pipe_condvar_signal(sema->cond);
435 pipe_mutex_unlock(sema->mutex);
436 }
437
438 /** Wait for semaphore counter to be greater than zero */
439 static INLINE void
440 pipe_semaphore_wait(pipe_semaphore *sema)
441 {
442 pipe_mutex_lock(sema->mutex);
443 while (sema->counter <= 0) {
444 pipe_condvar_wait(sema->cond, sema->mutex);
445 }
446 sema->counter--;
447 pipe_mutex_unlock(sema->mutex);
448 }
449
450
451
452 /*
453 * Thread-specific data.
454 */
455
456 typedef struct {
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 pthread_key_t key;
459 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
460 DWORD key;
461 #endif
462 int initMagic;
463 } pipe_tsd;
464
465
466 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
467
468
469 static INLINE void
470 pipe_tsd_init(pipe_tsd *tsd)
471 {
472 #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)
473 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
474 perror("pthread_key_create(): failed to allocate key for thread specific data");
475 exit(-1);
476 }
477 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
478 assert(0);
479 #endif
480 tsd->initMagic = PIPE_TSD_INIT_MAGIC;
481 }
482
483 static INLINE void *
484 pipe_tsd_get(pipe_tsd *tsd)
485 {
486 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
487 pipe_tsd_init(tsd);
488 }
489 #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)
490 return pthread_getspecific(tsd->key);
491 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
492 assert(0);
493 return NULL;
494 #else
495 assert(0);
496 return NULL;
497 #endif
498 }
499
500 static INLINE void
501 pipe_tsd_set(pipe_tsd *tsd, void *value)
502 {
503 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
504 pipe_tsd_init(tsd);
505 }
506 #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)
507 if (pthread_setspecific(tsd->key, value) != 0) {
508 perror("pthread_set_specific() failed");
509 exit(-1);
510 }
511 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
512 assert(0);
513 #else
514 assert(0);
515 #endif
516 }
517
518
519
520 #endif /* OS_THREAD_H_ */