Merge remote branch 'origin/master' 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
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 #else /* If the OS doesn't have its own, implement barriers using a mutex and a condvar */
330
331 typedef struct {
332 unsigned count;
333 unsigned waiters;
334 uint64_t sequence;
335 pipe_mutex mutex;
336 pipe_condvar condvar;
337 } pipe_barrier;
338
339 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
340 {
341 barrier->count = count;
342 barrier->waiters = 0;
343 barrier->sequence = 0;
344 pipe_mutex_init(barrier->mutex);
345 pipe_condvar_init(barrier->condvar);
346 }
347
348 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
349 {
350 assert(barrier->waiters == 0);
351 pipe_mutex_destroy(barrier->mutex);
352 pipe_condvar_destroy(barrier->condvar);
353 }
354
355 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
356 {
357 pipe_mutex_lock(barrier->mutex);
358
359 assert(barrier->waiters < barrier->count);
360 barrier->waiters++;
361
362 if (barrier->waiters < barrier->count) {
363 uint64_t sequence = barrier->sequence;
364
365 do {
366 pipe_condvar_wait(barrier->condvar, barrier->mutex);
367 } while (sequence == barrier->sequence);
368 } else {
369 barrier->waiters = 0;
370 barrier->sequence++;
371 pipe_condvar_broadcast(barrier->condvar);
372 }
373
374 pipe_mutex_unlock(barrier->mutex);
375 }
376
377
378 #endif
379
380
381 /*
382 * Semaphores
383 */
384
385 typedef struct
386 {
387 pipe_mutex mutex;
388 pipe_condvar cond;
389 int counter;
390 } pipe_semaphore;
391
392
393 static INLINE void
394 pipe_semaphore_init(pipe_semaphore *sema, int init_val)
395 {
396 pipe_mutex_init(sema->mutex);
397 pipe_condvar_init(sema->cond);
398 sema->counter = init_val;
399 }
400
401 static INLINE void
402 pipe_semaphore_destroy(pipe_semaphore *sema)
403 {
404 pipe_mutex_destroy(sema->mutex);
405 pipe_condvar_destroy(sema->cond);
406 }
407
408 /** Signal/increment semaphore counter */
409 static INLINE void
410 pipe_semaphore_signal(pipe_semaphore *sema)
411 {
412 pipe_mutex_lock(sema->mutex);
413 sema->counter++;
414 pipe_condvar_signal(sema->cond);
415 pipe_mutex_unlock(sema->mutex);
416 }
417
418 /** Wait for semaphore counter to be greater than zero */
419 static INLINE void
420 pipe_semaphore_wait(pipe_semaphore *sema)
421 {
422 pipe_mutex_lock(sema->mutex);
423 while (sema->counter <= 0) {
424 pipe_condvar_wait(sema->cond, sema->mutex);
425 }
426 sema->counter--;
427 pipe_mutex_unlock(sema->mutex);
428 }
429
430
431
432 /*
433 * Thread-specific data.
434 */
435
436 typedef struct {
437 #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)
438 pthread_key_t key;
439 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
440 DWORD key;
441 #endif
442 int initMagic;
443 } pipe_tsd;
444
445
446 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
447
448
449 static INLINE void
450 pipe_tsd_init(pipe_tsd *tsd)
451 {
452 #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)
453 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
454 perror("pthread_key_create(): failed to allocate key for thread specific data");
455 exit(-1);
456 }
457 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
458 assert(0);
459 #endif
460 tsd->initMagic = PIPE_TSD_INIT_MAGIC;
461 }
462
463 static INLINE void *
464 pipe_tsd_get(pipe_tsd *tsd)
465 {
466 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
467 pipe_tsd_init(tsd);
468 }
469 #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)
470 return pthread_getspecific(tsd->key);
471 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
472 assert(0);
473 return NULL;
474 #else
475 assert(0);
476 return NULL;
477 #endif
478 }
479
480 static INLINE void
481 pipe_tsd_set(pipe_tsd *tsd, void *value)
482 {
483 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
484 pipe_tsd_init(tsd);
485 }
486 #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)
487 if (pthread_setspecific(tsd->key, value) != 0) {
488 perror("pthread_set_specific() failed");
489 exit(-1);
490 }
491 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
492 assert(0);
493 #else
494 assert(0);
495 #endif
496 }
497
498
499
500 #endif /* OS_THREAD_H_ */