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 * THE AUTHORS 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() */
52 typedef pthread_t pipe_thread
;
54 #define PIPE_THREAD_ROUTINE( name, param ) \
55 void *name( void *param )
57 static INLINE pipe_thread
pipe_thread_create( void *(* routine
)( void *), void *param
)
60 sigset_t saved_set
, new_set
;
64 pthread_sigmask(SIG_SETMASK
, &new_set
, &saved_set
);
65 ret
= pthread_create( &thread
, NULL
, routine
, param
);
66 pthread_sigmask(SIG_SETMASK
, &saved_set
, NULL
);
72 static INLINE
int pipe_thread_wait( pipe_thread thread
)
74 return pthread_join( thread
, NULL
);
77 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
79 return pthread_detach( thread
);
85 typedef pthread_mutex_t pipe_mutex
;
87 #define pipe_static_mutex(mutex) \
88 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
90 #define pipe_mutex_init(mutex) \
91 (void) pthread_mutex_init(&(mutex), NULL)
93 #define pipe_mutex_destroy(mutex) \
94 pthread_mutex_destroy(&(mutex))
96 #define pipe_mutex_lock(mutex) \
97 (void) pthread_mutex_lock(&(mutex))
99 #define pipe_mutex_unlock(mutex) \
100 (void) pthread_mutex_unlock(&(mutex))
105 typedef pthread_cond_t pipe_condvar
;
107 #define pipe_static_condvar(mutex) \
108 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
110 #define pipe_condvar_init(cond) \
111 pthread_cond_init(&(cond), NULL)
113 #define pipe_condvar_destroy(cond) \
114 pthread_cond_destroy(&(cond))
116 #define pipe_condvar_wait(cond, mutex) \
117 pthread_cond_wait(&(cond), &(mutex))
119 #define pipe_condvar_signal(cond) \
120 pthread_cond_signal(&(cond))
122 #define pipe_condvar_broadcast(cond) \
123 pthread_cond_broadcast(&(cond))
127 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
133 typedef HANDLE pipe_thread
;
135 #define PIPE_THREAD_ROUTINE( name, param ) \
136 void * WINAPI name( void *param )
138 static INLINE pipe_thread
pipe_thread_create( void *(WINAPI
* routine
)( void *), void *param
)
141 return CreateThread( NULL
, 0, (LPTHREAD_START_ROUTINE
) routine
, param
, 0, &id
);
144 static INLINE
int pipe_thread_wait( pipe_thread thread
)
146 if (WaitForSingleObject( thread
, INFINITE
) == WAIT_OBJECT_0
)
151 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
153 if (CloseHandle( thread
))
161 typedef CRITICAL_SECTION pipe_mutex
;
163 /* http://locklessinc.com/articles/pthreads_on_windows/ */
164 #define pipe_static_mutex(mutex) \
165 static pipe_mutex mutex = {(PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0}
167 #define pipe_mutex_init(mutex) \
168 InitializeCriticalSection(&mutex)
170 #define pipe_mutex_destroy(mutex) \
171 DeleteCriticalSection(&mutex)
173 #define pipe_mutex_lock(mutex) \
174 EnterCriticalSection(&mutex)
176 #define pipe_mutex_unlock(mutex) \
177 LeaveCriticalSection(&mutex)
179 /* TODO: Need a macro to declare "I don't care about WinXP compatibilty" */
180 #if 0 && defined (_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
181 /* CONDITION_VARIABLE is only available on newer versions of Windows
182 * (Server 2008/Vista or later).
183 * http://msdn.microsoft.com/en-us/library/ms682052(VS.85).aspx
187 typedef CONDITION_VARIABLE pipe_condvar
;
189 #define pipe_static_condvar(cond) \
190 /*static*/ pipe_condvar cond = CONDITION_VARIABLE_INIT
192 #define pipe_condvar_init(cond) \
193 InitializeConditionVariable(&(cond))
195 #define pipe_condvar_destroy(cond) \
196 (void) cond /* nothing to do */
198 #define pipe_condvar_wait(cond, mutex) \
199 SleepConditionVariableCS(&(cond), &(mutex), INFINITE)
201 #define pipe_condvar_signal(cond) \
202 WakeConditionVariable(&(cond))
204 #define pipe_condvar_broadcast(cond) \
205 WakeAllConditionVariable(&(cond))
207 #else /* need compatibility with pre-Vista Win32 */
209 /* pipe_condvar (XXX FIX THIS)
210 * See http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
211 * for potential pitfalls in implementation.
213 typedef DWORD pipe_condvar
;
215 #define pipe_static_condvar(cond) \
216 /*static*/ pipe_condvar cond = 1
218 #define pipe_condvar_init(cond) \
221 #define pipe_condvar_destroy(cond) \
224 /* Poor man's pthread_cond_wait():
225 Just release the mutex and sleep for one millisecond.
226 The caller's while() loop does all the work. */
227 #define pipe_condvar_wait(cond, mutex) \
228 do { pipe_mutex_unlock(mutex); \
230 pipe_mutex_lock(mutex); \
233 #define pipe_condvar_signal(cond) \
236 #define pipe_condvar_broadcast(cond) \
239 #endif /* pre-Vista win32 */
243 #include "os/os_time.h"
245 /** Dummy definitions */
247 typedef unsigned pipe_thread
;
249 #define PIPE_THREAD_ROUTINE( name, param ) \
250 void * name( void *param )
252 static INLINE pipe_thread
pipe_thread_create( void *(* routine
)( void *), void *param
)
257 static INLINE
int pipe_thread_wait( pipe_thread thread
)
262 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
267 typedef unsigned pipe_mutex
;
269 #define pipe_static_mutex(mutex) \
270 static pipe_mutex mutex = 0
272 #define pipe_mutex_init(mutex) \
275 #define pipe_mutex_destroy(mutex) \
278 #define pipe_mutex_lock(mutex) \
281 #define pipe_mutex_unlock(mutex) \
284 typedef int64_t pipe_condvar
;
286 #define pipe_static_condvar(condvar) \
287 static pipe_condvar condvar = 1000
289 #define pipe_condvar_init(condvar) \
290 (void) (condvar = 1000)
292 #define pipe_condvar_destroy(condvar) \
295 /* Poor man's pthread_cond_wait():
296 Just release the mutex and sleep for one millisecond.
297 The caller's while() loop does all the work. */
298 #define pipe_condvar_wait(condvar, mutex) \
299 do { pipe_mutex_unlock(mutex); \
300 os_time_sleep(condvar); \
301 pipe_mutex_lock(mutex); \
304 #define pipe_condvar_signal(condvar) \
307 #define pipe_condvar_broadcast(condvar) \
311 #endif /* PIPE_OS_? */
318 #if (defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)) && !defined(PIPE_OS_ANDROID)
320 typedef pthread_barrier_t pipe_barrier
;
322 static INLINE
void pipe_barrier_init(pipe_barrier
*barrier
, unsigned count
)
324 pthread_barrier_init(barrier
, NULL
, count
);
327 static INLINE
void pipe_barrier_destroy(pipe_barrier
*barrier
)
329 pthread_barrier_destroy(barrier
);
332 static INLINE
void pipe_barrier_wait(pipe_barrier
*barrier
)
334 pthread_barrier_wait(barrier
);
338 #else /* If the OS doesn't have its own, implement barriers using a mutex and a condvar */
345 pipe_condvar condvar
;
348 static INLINE
void pipe_barrier_init(pipe_barrier
*barrier
, unsigned count
)
350 barrier
->count
= count
;
351 barrier
->waiters
= 0;
352 barrier
->sequence
= 0;
353 pipe_mutex_init(barrier
->mutex
);
354 pipe_condvar_init(barrier
->condvar
);
357 static INLINE
void pipe_barrier_destroy(pipe_barrier
*barrier
)
359 assert(barrier
->waiters
== 0);
360 pipe_mutex_destroy(barrier
->mutex
);
361 pipe_condvar_destroy(barrier
->condvar
);
364 static INLINE
void pipe_barrier_wait(pipe_barrier
*barrier
)
366 pipe_mutex_lock(barrier
->mutex
);
368 assert(barrier
->waiters
< barrier
->count
);
371 if (barrier
->waiters
< barrier
->count
) {
372 uint64_t sequence
= barrier
->sequence
;
375 pipe_condvar_wait(barrier
->condvar
, barrier
->mutex
);
376 } while (sequence
== barrier
->sequence
);
378 barrier
->waiters
= 0;
380 pipe_condvar_broadcast(barrier
->condvar
);
383 pipe_mutex_unlock(barrier
->mutex
);
403 pipe_semaphore_init(pipe_semaphore
*sema
, int init_val
)
405 pipe_mutex_init(sema
->mutex
);
406 pipe_condvar_init(sema
->cond
);
407 sema
->counter
= init_val
;
411 pipe_semaphore_destroy(pipe_semaphore
*sema
)
413 pipe_mutex_destroy(sema
->mutex
);
414 pipe_condvar_destroy(sema
->cond
);
417 /** Signal/increment semaphore counter */
419 pipe_semaphore_signal(pipe_semaphore
*sema
)
421 pipe_mutex_lock(sema
->mutex
);
423 pipe_condvar_signal(sema
->cond
);
424 pipe_mutex_unlock(sema
->mutex
);
427 /** Wait for semaphore counter to be greater than zero */
429 pipe_semaphore_wait(pipe_semaphore
*sema
)
431 pipe_mutex_lock(sema
->mutex
);
432 while (sema
->counter
<= 0) {
433 pipe_condvar_wait(sema
->cond
, sema
->mutex
);
436 pipe_mutex_unlock(sema
->mutex
);
442 * Thread-specific data.
446 #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)
448 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
455 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
459 pipe_tsd_init(pipe_tsd
*tsd
)
461 #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)
462 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
463 perror("pthread_key_create(): failed to allocate key for thread specific data");
466 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
469 tsd
->initMagic
= PIPE_TSD_INIT_MAGIC
;
473 pipe_tsd_get(pipe_tsd
*tsd
)
475 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
478 #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)
479 return pthread_getspecific(tsd
->key
);
480 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
490 pipe_tsd_set(pipe_tsd
*tsd
, void *value
)
492 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
495 #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)
496 if (pthread_setspecific(tsd
->key
, value
) != 0) {
497 perror("pthread_set_specific() failed");
500 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
509 #endif /* OS_THREAD_H_ */