revert 213 commits (to 56092) from the last month. 10 still need work to resolve...
[AROS.git] / workbench / libs / mesa / src / gallium / auxiliary / os / os_thread.h
blobc115e77f0f884fda90359e448c70d07680369eaa
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
24 **************************************************************************/
27 /**
28 * @file
30 * Thread, mutex, condition variable, barrier, semaphore and
31 * thread-specific data functions.
35 #ifndef OS_THREAD_H_
36 #define OS_THREAD_H_
39 #include "pipe/p_compiler.h"
40 #include "util/u_debug.h" /* for assert */
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_CYGWIN)
45 #include <pthread.h> /* POSIX threads headers */
46 #include <stdio.h> /* for perror() */
49 /* pipe_thread
51 typedef pthread_t pipe_thread;
53 #define PIPE_THREAD_ROUTINE( name, param ) \
54 void *name( void *param )
56 static INLINE pipe_thread pipe_thread_create( void *(* routine)( void *), void *param )
58 pipe_thread thread;
59 sigset_t saved_set, new_set;
60 int ret;
62 sigfillset(&new_set);
63 pthread_sigmask(SIG_SETMASK, &new_set, &saved_set);
64 ret = pthread_create( &thread, NULL, routine, param );
65 pthread_sigmask(SIG_SETMASK, &saved_set, NULL);
66 if (ret)
67 return 0;
68 return thread;
71 static INLINE int pipe_thread_wait( pipe_thread thread )
73 return pthread_join( thread, NULL );
76 static INLINE int pipe_thread_destroy( pipe_thread thread )
78 return pthread_detach( thread );
82 /* pipe_mutex
84 typedef pthread_mutex_t pipe_mutex;
86 #define pipe_static_mutex(mutex) \
87 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
89 #define pipe_mutex_init(mutex) \
90 (void) pthread_mutex_init(&(mutex), NULL)
92 #define pipe_mutex_destroy(mutex) \
93 pthread_mutex_destroy(&(mutex))
95 #define pipe_mutex_lock(mutex) \
96 (void) pthread_mutex_lock(&(mutex))
98 #define pipe_mutex_unlock(mutex) \
99 (void) pthread_mutex_unlock(&(mutex))
102 /* pipe_condvar
104 typedef pthread_cond_t pipe_condvar;
106 #define pipe_static_condvar(mutex) \
107 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
109 #define pipe_condvar_init(cond) \
110 pthread_cond_init(&(cond), NULL)
112 #define pipe_condvar_destroy(cond) \
113 pthread_cond_destroy(&(cond))
115 #define pipe_condvar_wait(cond, mutex) \
116 pthread_cond_wait(&(cond), &(mutex))
118 #define pipe_condvar_signal(cond) \
119 pthread_cond_signal(&(cond))
121 #define pipe_condvar_broadcast(cond) \
122 pthread_cond_broadcast(&(cond))
126 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
128 #include <windows.h>
130 /* pipe_thread
132 typedef HANDLE pipe_thread;
134 #define PIPE_THREAD_ROUTINE( name, param ) \
135 void * WINAPI name( void *param )
137 static INLINE pipe_thread pipe_thread_create( void *(WINAPI * routine)( void *), void *param )
139 DWORD id;
140 return CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) routine, param, 0, &id );
143 static INLINE int pipe_thread_wait( pipe_thread thread )
145 if (WaitForSingleObject( thread, INFINITE ) == WAIT_OBJECT_0)
146 return 0;
147 return -1;
150 static INLINE int pipe_thread_destroy( pipe_thread thread )
152 if (CloseHandle( thread ))
153 return 0;
154 return -1;
158 /* pipe_mutex
160 typedef CRITICAL_SECTION pipe_mutex;
162 /* http://locklessinc.com/articles/pthreads_on_windows/ */
163 #define pipe_static_mutex(mutex) \
164 static pipe_mutex mutex = {(PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0}
166 #define pipe_mutex_init(mutex) \
167 InitializeCriticalSection(&mutex)
169 #define pipe_mutex_destroy(mutex) \
170 DeleteCriticalSection(&mutex)
172 #define pipe_mutex_lock(mutex) \
173 EnterCriticalSection(&mutex)
175 #define pipe_mutex_unlock(mutex) \
176 LeaveCriticalSection(&mutex)
178 /* TODO: Need a macro to declare "I don't care about WinXP compatibilty" */
179 #if 0 && defined (_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
180 /* CONDITION_VARIABLE is only available on newer versions of Windows
181 * (Server 2008/Vista or later).
182 * http://msdn.microsoft.com/en-us/library/ms682052(VS.85).aspx
184 * pipe_condvar
186 typedef CONDITION_VARIABLE pipe_condvar;
188 #define pipe_static_condvar(cond) \
189 /*static*/ pipe_condvar cond = CONDITION_VARIABLE_INIT
191 #define pipe_condvar_init(cond) \
192 InitializeConditionVariable(&(cond))
194 #define pipe_condvar_destroy(cond) \
195 (void) cond /* nothing to do */
197 #define pipe_condvar_wait(cond, mutex) \
198 SleepConditionVariableCS(&(cond), &(mutex), INFINITE)
200 #define pipe_condvar_signal(cond) \
201 WakeConditionVariable(&(cond))
203 #define pipe_condvar_broadcast(cond) \
204 WakeAllConditionVariable(&(cond))
206 #else /* need compatibility with pre-Vista Win32 */
208 /* pipe_condvar (XXX FIX THIS)
209 * See http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
210 * for potential pitfalls in implementation.
212 typedef DWORD pipe_condvar;
214 #define pipe_static_condvar(cond) \
215 /*static*/ pipe_condvar cond = 1
217 #define pipe_condvar_init(cond) \
218 (void) (cond = 1)
220 #define pipe_condvar_destroy(cond) \
221 (void) cond
223 /* Poor man's pthread_cond_wait():
224 Just release the mutex and sleep for one millisecond.
225 The caller's while() loop does all the work. */
226 #define pipe_condvar_wait(cond, mutex) \
227 do { pipe_mutex_unlock(mutex); \
228 Sleep(cond); \
229 pipe_mutex_lock(mutex); \
230 } while (0)
232 #define pipe_condvar_signal(cond) \
233 (void) cond
235 #define pipe_condvar_broadcast(cond) \
236 (void) cond
238 #endif /* pre-Vista win32 */
240 #elif defined(PIPE_OS_AROS)
242 #include <proto/exec.h>
244 #include "os/os_time.h"
246 typedef struct SignalSemaphore pipe_mutex;
248 #include <aros/symbolsets.h> /* For ADD2INIT */
250 /* Declare variable, declare init function, add to auto init. Ugly but works. */
251 #define pipe_static_mutex(mutex) \
252 static pipe_mutex mutex; \
253 static void init##mutex() \
255 pipe_mutex_init(mutex); \
257 ADD2INIT(init##mutex, 5);
259 #define pipe_mutex_init(mutex) \
260 InitSemaphore(&mutex)
262 #define pipe_mutex_destroy(mutex) \
263 (void) mutex
265 #define pipe_mutex_lock(mutex) \
266 ObtainSemaphore(&mutex)
268 #define pipe_mutex_unlock(mutex) \
269 ReleaseSemaphore(&mutex)
271 typedef int64_t pipe_condvar;
273 #define pipe_static_condvar(condvar) \
274 static pipe_condvar condvar = 1000
276 #define pipe_condvar_init(condvar) \
277 (void) (condvar = 1000)
279 #define pipe_condvar_destroy(condvar) \
280 (void) condvar
282 /* Poor man's pthread_cond_wait():
283 Just release the mutex and sleep for one millisecond.
284 The caller's while() loop does all the work. */
285 #define pipe_condvar_wait(condvar, mutex) \
286 do { pipe_mutex_unlock(mutex); \
287 os_time_sleep(condvar); \
288 pipe_mutex_lock(mutex); \
289 } while (0)
291 #define pipe_condvar_signal(condvar) \
292 (void) condvar
294 #define pipe_condvar_broadcast(condvar) \
295 (void) condvar
297 #else
299 #include "os/os_time.h"
301 /** Dummy definitions */
303 typedef unsigned pipe_thread;
305 #define PIPE_THREAD_ROUTINE( name, param ) \
306 void * name( void *param )
308 static INLINE pipe_thread pipe_thread_create( void *(* routine)( void *), void *param )
310 return 0;
313 static INLINE int pipe_thread_wait( pipe_thread thread )
315 return -1;
318 static INLINE int pipe_thread_destroy( pipe_thread thread )
320 return -1;
323 typedef unsigned pipe_mutex;
325 #define pipe_static_mutex(mutex) \
326 static pipe_mutex mutex = 0
328 #define pipe_mutex_init(mutex) \
329 (void) mutex
331 #define pipe_mutex_destroy(mutex) \
332 (void) mutex
334 #define pipe_mutex_lock(mutex) \
335 (void) mutex
337 #define pipe_mutex_unlock(mutex) \
338 (void) mutex
340 typedef int64_t pipe_condvar;
342 #define pipe_static_condvar(condvar) \
343 static pipe_condvar condvar = 1000
345 #define pipe_condvar_init(condvar) \
346 (void) (condvar = 1000)
348 #define pipe_condvar_destroy(condvar) \
349 (void) condvar
351 /* Poor man's pthread_cond_wait():
352 Just release the mutex and sleep for one millisecond.
353 The caller's while() loop does all the work. */
354 #define pipe_condvar_wait(condvar, mutex) \
355 do { pipe_mutex_unlock(mutex); \
356 os_time_sleep(condvar); \
357 pipe_mutex_lock(mutex); \
358 } while (0)
360 #define pipe_condvar_signal(condvar) \
361 (void) condvar
363 #define pipe_condvar_broadcast(condvar) \
364 (void) condvar
367 #endif /* PIPE_OS_? */
371 * pipe_barrier
374 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HAIKU)
376 typedef pthread_barrier_t pipe_barrier;
378 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
380 pthread_barrier_init(barrier, NULL, count);
383 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
385 pthread_barrier_destroy(barrier);
388 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
390 pthread_barrier_wait(barrier);
394 #else /* If the OS doesn't have its own, implement barriers using a mutex and a condvar */
396 typedef struct {
397 unsigned count;
398 unsigned waiters;
399 uint64_t sequence;
400 pipe_mutex mutex;
401 pipe_condvar condvar;
402 } pipe_barrier;
404 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
406 barrier->count = count;
407 barrier->waiters = 0;
408 barrier->sequence = 0;
409 pipe_mutex_init(barrier->mutex);
410 pipe_condvar_init(barrier->condvar);
413 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
415 assert(barrier->waiters == 0);
416 pipe_mutex_destroy(barrier->mutex);
417 pipe_condvar_destroy(barrier->condvar);
420 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
422 pipe_mutex_lock(barrier->mutex);
424 assert(barrier->waiters < barrier->count);
425 barrier->waiters++;
427 if (barrier->waiters < barrier->count) {
428 uint64_t sequence = barrier->sequence;
430 do {
431 pipe_condvar_wait(barrier->condvar, barrier->mutex);
432 } while (sequence == barrier->sequence);
433 } else {
434 barrier->waiters = 0;
435 barrier->sequence++;
436 pipe_condvar_broadcast(barrier->condvar);
439 pipe_mutex_unlock(barrier->mutex);
443 #endif
447 * Semaphores
450 typedef struct
452 pipe_mutex mutex;
453 pipe_condvar cond;
454 int counter;
455 } pipe_semaphore;
458 static INLINE void
459 pipe_semaphore_init(pipe_semaphore *sema, int init_val)
461 pipe_mutex_init(sema->mutex);
462 pipe_condvar_init(sema->cond);
463 sema->counter = init_val;
466 static INLINE void
467 pipe_semaphore_destroy(pipe_semaphore *sema)
469 pipe_mutex_destroy(sema->mutex);
470 pipe_condvar_destroy(sema->cond);
473 /** Signal/increment semaphore counter */
474 static INLINE void
475 pipe_semaphore_signal(pipe_semaphore *sema)
477 pipe_mutex_lock(sema->mutex);
478 sema->counter++;
479 pipe_condvar_signal(sema->cond);
480 pipe_mutex_unlock(sema->mutex);
483 /** Wait for semaphore counter to be greater than zero */
484 static INLINE void
485 pipe_semaphore_wait(pipe_semaphore *sema)
487 pipe_mutex_lock(sema->mutex);
488 while (sema->counter <= 0) {
489 pipe_condvar_wait(sema->cond, sema->mutex);
491 sema->counter--;
492 pipe_mutex_unlock(sema->mutex);
498 * Thread-specific data.
501 typedef struct {
502 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_CYGWIN)
503 pthread_key_t key;
504 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
505 DWORD key;
506 #endif
507 int initMagic;
508 } pipe_tsd;
511 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
514 static INLINE void
515 pipe_tsd_init(pipe_tsd *tsd)
517 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_CYGWIN)
518 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
519 perror("pthread_key_create(): failed to allocate key for thread specific data");
520 exit(-1);
522 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
523 assert(0);
524 #endif
525 tsd->initMagic = PIPE_TSD_INIT_MAGIC;
528 static INLINE void *
529 pipe_tsd_get(pipe_tsd *tsd)
531 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
532 pipe_tsd_init(tsd);
534 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_CYGWIN)
535 return pthread_getspecific(tsd->key);
536 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
537 assert(0);
538 return NULL;
539 #else
540 assert(0);
541 return NULL;
542 #endif
545 static INLINE void
546 pipe_tsd_set(pipe_tsd *tsd, void *value)
548 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
549 pipe_tsd_init(tsd);
551 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_CYGWIN)
552 if (pthread_setspecific(tsd->key, value) != 0) {
553 perror("pthread_set_specific() failed");
554 exit(-1);
556 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
557 assert(0);
558 #else
559 assert(0);
560 #endif
565 #endif /* OS_THREAD_H_ */