os: Implement pipe_condvar on Windows Vista and later
[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 /* TODO: Need a macro to declare "I don't care about WinXP compatibilty" */
172 #if 0 && defined (_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
173 /* CONDITION_VARIABLE is only available on newer versions of Windows
174 * (Server 2008/Vista or later).
175 * http://msdn.microsoft.com/en-us/library/ms682052(VS.85).aspx
176 *
177 * pipe_condvar
178 */
179 typedef CONDITION_VARIABLE pipe_condvar;
180
181 #define pipe_static_condvar(cond) \
182 /*static*/ pipe_condvar cond = CONDITION_VARIABLE_INIT
183
184 #define pipe_condvar_init(cond) \
185 InitializeConditionVariable(&(cond))
186
187 #define pipe_condvar_destroy(cond) \
188 (void) cond /* nothing to do */
189
190 #define pipe_condvar_wait(cond, mutex) \
191 SleepConditionVariableCS(&(cond), &(mutex), INFINITE)
192
193 #define pipe_condvar_signal(cond) \
194 WakeConditionVariable(&(cond))
195
196 #define pipe_condvar_broadcast(cond) \
197 WakeAllConditionVariable(&(cond))
198
199 #else /* need compatibility with pre-Vista Win32 */
200
201 /* pipe_condvar (XXX FIX THIS)
202 * See http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
203 * for potential pitfalls in implementation.
204 */
205 typedef DWORD pipe_condvar;
206
207 #define pipe_static_condvar(cond) \
208 /*static*/ pipe_condvar cond = 1
209
210 #define pipe_condvar_init(cond) \
211 (void) (cond = 1)
212
213 #define pipe_condvar_destroy(cond) \
214 (void) cond
215
216 /* Poor man's pthread_cond_wait():
217 Just release the mutex and sleep for one millisecond.
218 The caller's while() loop does all the work. */
219 #define pipe_condvar_wait(cond, mutex) \
220 do { pipe_mutex_unlock(mutex); \
221 Sleep(cond); \
222 pipe_mutex_lock(mutex); \
223 } while (0)
224
225 #define pipe_condvar_signal(cond) \
226 (void) cond
227
228 #define pipe_condvar_broadcast(cond) \
229 (void) cond
230
231 #endif /* pre-Vista win32 */
232
233 #else
234
235 #include "os/os_time.h"
236
237 /** Dummy definitions */
238
239 typedef unsigned pipe_thread;
240
241 #define PIPE_THREAD_ROUTINE( name, param ) \
242 void * name( void *param )
243
244 static INLINE pipe_thread pipe_thread_create( void *(* routine)( void *), void *param )
245 {
246 return 0;
247 }
248
249 static INLINE int pipe_thread_wait( pipe_thread thread )
250 {
251 return -1;
252 }
253
254 static INLINE int pipe_thread_destroy( pipe_thread thread )
255 {
256 return -1;
257 }
258
259 typedef unsigned pipe_mutex;
260
261 #define pipe_static_mutex(mutex) \
262 static pipe_mutex mutex = 0
263
264 #define pipe_mutex_init(mutex) \
265 (void) mutex
266
267 #define pipe_mutex_destroy(mutex) \
268 (void) mutex
269
270 #define pipe_mutex_lock(mutex) \
271 (void) mutex
272
273 #define pipe_mutex_unlock(mutex) \
274 (void) mutex
275
276 typedef int64_t pipe_condvar;
277
278 #define pipe_static_condvar(condvar) \
279 static pipe_condvar condvar = 1000
280
281 #define pipe_condvar_init(condvar) \
282 (void) (condvar = 1000)
283
284 #define pipe_condvar_destroy(condvar) \
285 (void) condvar
286
287 /* Poor man's pthread_cond_wait():
288 Just release the mutex and sleep for one millisecond.
289 The caller's while() loop does all the work. */
290 #define pipe_condvar_wait(condvar, mutex) \
291 do { pipe_mutex_unlock(mutex); \
292 os_time_sleep(condvar); \
293 pipe_mutex_lock(mutex); \
294 } while (0)
295
296 #define pipe_condvar_signal(condvar) \
297 (void) condvar
298
299 #define pipe_condvar_broadcast(condvar) \
300 (void) condvar
301
302
303 #endif /* PIPE_OS_? */
304
305
306 /*
307 * pipe_barrier
308 */
309
310 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_EMBEDDED)
311
312 typedef pthread_barrier_t pipe_barrier;
313
314 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
315 {
316 pthread_barrier_init(barrier, NULL, count);
317 }
318
319 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
320 {
321 pthread_barrier_destroy(barrier);
322 }
323
324 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
325 {
326 pthread_barrier_wait(barrier);
327 }
328
329
330 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
331
332 /* XXX FIX THIS */
333 typedef unsigned pipe_barrier;
334
335 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
336 {
337 /* XXX we could implement barriers with a mutex and condition var */
338 }
339
340 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
341 {
342 }
343
344 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
345 {
346 assert(0);
347 }
348
349
350 #else
351
352 typedef struct {
353 unsigned count;
354 unsigned waiters;
355 uint64_t sequence;
356 pipe_mutex mutex;
357 pipe_condvar condvar;
358 } pipe_barrier;
359
360 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
361 {
362 barrier->count = count;
363 barrier->waiters = 0;
364 barrier->sequence = 0;
365 pipe_mutex_init(barrier->mutex);
366 pipe_condvar_init(barrier->condvar);
367 }
368
369 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
370 {
371 assert(barrier->waiters == 0);
372 pipe_mutex_destroy(barrier->mutex);
373 pipe_condvar_destroy(barrier->condvar);
374 }
375
376 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
377 {
378 pipe_mutex_lock(barrier->mutex);
379
380 assert(barrier->waiters < barrier->count);
381 barrier->waiters++;
382
383 if (barrier->waiters < barrier->count) {
384 uint64_t sequence = barrier->sequence;
385
386 do {
387 pipe_condvar_wait(barrier->condvar, barrier->mutex);
388 } while (sequence == barrier->sequence);
389 } else {
390 barrier->waiters = 0;
391 barrier->sequence++;
392 pipe_condvar_broadcast(barrier->condvar);
393 }
394
395 pipe_mutex_unlock(barrier->mutex);
396 }
397
398
399 #endif
400
401
402 /*
403 * Semaphores
404 */
405
406 typedef struct
407 {
408 pipe_mutex mutex;
409 pipe_condvar cond;
410 int counter;
411 } pipe_semaphore;
412
413
414 static INLINE void
415 pipe_semaphore_init(pipe_semaphore *sema, int init_val)
416 {
417 pipe_mutex_init(sema->mutex);
418 pipe_condvar_init(sema->cond);
419 sema->counter = init_val;
420 }
421
422 static INLINE void
423 pipe_semaphore_destroy(pipe_semaphore *sema)
424 {
425 pipe_mutex_destroy(sema->mutex);
426 pipe_condvar_destroy(sema->cond);
427 }
428
429 /** Signal/increment semaphore counter */
430 static INLINE void
431 pipe_semaphore_signal(pipe_semaphore *sema)
432 {
433 pipe_mutex_lock(sema->mutex);
434 sema->counter++;
435 pipe_condvar_signal(sema->cond);
436 pipe_mutex_unlock(sema->mutex);
437 }
438
439 /** Wait for semaphore counter to be greater than zero */
440 static INLINE void
441 pipe_semaphore_wait(pipe_semaphore *sema)
442 {
443 pipe_mutex_lock(sema->mutex);
444 while (sema->counter <= 0) {
445 pipe_condvar_wait(sema->cond, sema->mutex);
446 }
447 sema->counter--;
448 pipe_mutex_unlock(sema->mutex);
449 }
450
451
452
453 /*
454 * Thread-specific data.
455 */
456
457 typedef struct {
458 #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)
459 pthread_key_t key;
460 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
461 DWORD key;
462 #endif
463 int initMagic;
464 } pipe_tsd;
465
466
467 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
468
469
470 static INLINE void
471 pipe_tsd_init(pipe_tsd *tsd)
472 {
473 #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)
474 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
475 perror("pthread_key_create(): failed to allocate key for thread specific data");
476 exit(-1);
477 }
478 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
479 assert(0);
480 #endif
481 tsd->initMagic = PIPE_TSD_INIT_MAGIC;
482 }
483
484 static INLINE void *
485 pipe_tsd_get(pipe_tsd *tsd)
486 {
487 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
488 pipe_tsd_init(tsd);
489 }
490 #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)
491 return pthread_getspecific(tsd->key);
492 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
493 assert(0);
494 return NULL;
495 #else
496 assert(0);
497 return NULL;
498 #endif
499 }
500
501 static INLINE void
502 pipe_tsd_set(pipe_tsd *tsd, void *value)
503 {
504 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
505 pipe_tsd_init(tsd);
506 }
507 #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)
508 if (pthread_setspecific(tsd->key, value) != 0) {
509 perror("pthread_set_specific() failed");
510 exit(-1);
511 }
512 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
513 assert(0);
514 #else
515 assert(0);
516 #endif
517 }
518
519
520
521 #endif /* OS_THREAD_H_ */