1 /**************************************************************************
3 * Copyright 1999-2006 Brian Paul
4 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 **************************************************************************/
30 * Thread, mutex, condition variable, barrier, semaphore and
31 * thread-specific data functions.
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() */
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 )
59 sigset_t saved_set, 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);
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 );
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))
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)
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 )
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)
150 static INLINE int pipe_thread_destroy( pipe_thread thread )
152 if (CloseHandle( thread ))
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
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) \
220 #define pipe_condvar_destroy(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); \
229 pipe_mutex_lock(mutex); \
232 #define pipe_condvar_signal(cond) \
235 #define pipe_condvar_broadcast(cond) \
238 #endif /* pre-Vista win32 */
242 #include "os/os_time.h"
244 /** Dummy definitions */
246 typedef unsigned pipe_thread;
248 #define PIPE_THREAD_ROUTINE( name, param ) \
249 void * name( void *param )
251 static INLINE pipe_thread pipe_thread_create( void *(* routine)( void *), void *param )
256 static INLINE int pipe_thread_wait( pipe_thread thread )
261 static INLINE int pipe_thread_destroy( pipe_thread thread )
266 typedef unsigned pipe_mutex;
268 #define pipe_static_mutex(mutex) \
269 static pipe_mutex mutex = 0
271 #define pipe_mutex_init(mutex) \
274 #define pipe_mutex_destroy(mutex) \
277 #define pipe_mutex_lock(mutex) \
280 #define pipe_mutex_unlock(mutex) \
283 typedef int64_t pipe_condvar;
285 #define pipe_static_condvar(condvar) \
286 static pipe_condvar condvar = 1000
288 #define pipe_condvar_init(condvar) \
289 (void) (condvar = 1000)
291 #define pipe_condvar_destroy(condvar) \
294 /* Poor man's pthread_cond_wait():
295 Just release the mutex and sleep for one millisecond.
296 The caller's while() loop does all the work. */
297 #define pipe_condvar_wait(condvar, mutex) \
298 do { pipe_mutex_unlock(mutex); \
299 os_time_sleep(condvar); \
300 pipe_mutex_lock(mutex); \
303 #define pipe_condvar_signal(condvar) \
306 #define pipe_condvar_broadcast(condvar) \
310 #endif /* PIPE_OS_? */
317 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HAIKU)
319 typedef pthread_barrier_t pipe_barrier;
321 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
323 pthread_barrier_init(barrier, NULL, count);
326 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
328 pthread_barrier_destroy(barrier);
331 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
333 pthread_barrier_wait(barrier);
337 #else /* If the OS doesn't have its own, implement barriers using a mutex and a condvar */
344 pipe_condvar condvar;
347 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
349 barrier->count = count;
350 barrier->waiters = 0;
351 barrier->sequence = 0;
352 pipe_mutex_init(barrier->mutex);
353 pipe_condvar_init(barrier->condvar);
356 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
358 assert(barrier->waiters == 0);
359 pipe_mutex_destroy(barrier->mutex);
360 pipe_condvar_destroy(barrier->condvar);
363 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
365 pipe_mutex_lock(barrier->mutex);
367 assert(barrier->waiters < barrier->count);
370 if (barrier->waiters < barrier->count) {
371 uint64_t sequence = barrier->sequence;
374 pipe_condvar_wait(barrier->condvar, barrier->mutex);
375 } while (sequence == barrier->sequence);
377 barrier->waiters = 0;
379 pipe_condvar_broadcast(barrier->condvar);
382 pipe_mutex_unlock(barrier->mutex);
402 pipe_semaphore_init(pipe_semaphore *sema, int init_val)
404 pipe_mutex_init(sema->mutex);
405 pipe_condvar_init(sema->cond);
406 sema->counter = init_val;
410 pipe_semaphore_destroy(pipe_semaphore *sema)
412 pipe_mutex_destroy(sema->mutex);
413 pipe_condvar_destroy(sema->cond);
416 /** Signal/increment semaphore counter */
418 pipe_semaphore_signal(pipe_semaphore *sema)
420 pipe_mutex_lock(sema->mutex);
422 pipe_condvar_signal(sema->cond);
423 pipe_mutex_unlock(sema->mutex);
426 /** Wait for semaphore counter to be greater than zero */
428 pipe_semaphore_wait(pipe_semaphore *sema)
430 pipe_mutex_lock(sema->mutex);
431 while (sema->counter <= 0) {
432 pipe_condvar_wait(sema->cond, sema->mutex);
435 pipe_mutex_unlock(sema->mutex);
441 * Thread-specific data.
445 #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)
447 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
454 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
458 pipe_tsd_init(pipe_tsd *tsd)
460 #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)
461 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
462 perror("pthread_key_create(): failed to allocate key for thread specific data");
465 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
468 tsd->initMagic = PIPE_TSD_INIT_MAGIC;
472 pipe_tsd_get(pipe_tsd *tsd)
474 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
477 #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)
478 return pthread_getspecific(tsd->key);
479 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
489 pipe_tsd_set(pipe_tsd *tsd, void *value)
491 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
494 #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)
495 if (pthread_setspecific(tsd->key, value) != 0) {
496 perror("pthread_set_specific() failed");
499 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
508 #endif /* OS_THREAD_H_ */