1 /**************************************************************************
3 * Copyright 1999-2006 Brian Paul
4 * Copyright 2008 VMware, Inc.
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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
25 **************************************************************************/
31 * Thread, mutex, condition variable, barrier, semaphore and
32 * thread-specific data functions.
40 #include "pipe/p_compiler.h"
41 #include "util/u_debug.h" /* for assert */
44 #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) || defined(PIPE_OS_HURD)
46 #include <pthread.h> /* POSIX threads headers */
47 #include <stdio.h> /* for perror() */
53 typedef pthread_t pipe_thread
;
55 #define PIPE_THREAD_ROUTINE( name, param ) \
56 void *name( void *param )
58 static INLINE pipe_thread
pipe_thread_create( void *(* routine
)( void *), void *param
)
61 sigset_t saved_set
, new_set
;
65 pthread_sigmask(SIG_SETMASK
, &new_set
, &saved_set
);
66 ret
= pthread_create( &thread
, NULL
, routine
, param
);
67 pthread_sigmask(SIG_SETMASK
, &saved_set
, NULL
);
73 static INLINE
int pipe_thread_wait( pipe_thread thread
)
75 return pthread_join( thread
, NULL
);
78 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
80 return pthread_detach( thread
);
86 typedef pthread_mutex_t pipe_mutex
;
88 #define pipe_static_mutex(mutex) \
89 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
91 #define pipe_mutex_init(mutex) \
92 (void) pthread_mutex_init(&(mutex), NULL)
94 #define pipe_mutex_destroy(mutex) \
95 pthread_mutex_destroy(&(mutex))
97 #define pipe_mutex_lock(mutex) \
98 (void) pthread_mutex_lock(&(mutex))
100 #define pipe_mutex_unlock(mutex) \
101 (void) pthread_mutex_unlock(&(mutex))
106 typedef pthread_cond_t pipe_condvar
;
108 #define pipe_static_condvar(mutex) \
109 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
111 #define pipe_condvar_init(cond) \
112 pthread_cond_init(&(cond), NULL)
114 #define pipe_condvar_destroy(cond) \
115 pthread_cond_destroy(&(cond))
117 #define pipe_condvar_wait(cond, mutex) \
118 pthread_cond_wait(&(cond), &(mutex))
120 #define pipe_condvar_signal(cond) \
121 pthread_cond_signal(&(cond))
123 #define pipe_condvar_broadcast(cond) \
124 pthread_cond_broadcast(&(cond))
128 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
134 typedef HANDLE pipe_thread
;
136 #define PIPE_THREAD_ROUTINE( name, param ) \
137 void * WINAPI name( void *param )
139 static INLINE pipe_thread
pipe_thread_create( void *(WINAPI
* routine
)( void *), void *param
)
142 return CreateThread( NULL
, 0, (LPTHREAD_START_ROUTINE
) routine
, param
, 0, &id
);
145 static INLINE
int pipe_thread_wait( pipe_thread thread
)
147 if (WaitForSingleObject( thread
, INFINITE
) == WAIT_OBJECT_0
)
152 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
154 if (CloseHandle( thread
))
162 typedef CRITICAL_SECTION pipe_mutex
;
164 /* http://locklessinc.com/articles/pthreads_on_windows/ */
165 #define pipe_static_mutex(mutex) \
166 static pipe_mutex mutex = {(PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0}
168 #define pipe_mutex_init(mutex) \
169 InitializeCriticalSection(&mutex)
171 #define pipe_mutex_destroy(mutex) \
172 DeleteCriticalSection(&mutex)
174 #define pipe_mutex_lock(mutex) \
175 EnterCriticalSection(&mutex)
177 #define pipe_mutex_unlock(mutex) \
178 LeaveCriticalSection(&mutex)
180 /* TODO: Need a macro to declare "I don't care about WinXP compatibilty" */
181 #if 0 && defined (_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
182 /* CONDITION_VARIABLE is only available on newer versions of Windows
183 * (Server 2008/Vista or later).
184 * http://msdn.microsoft.com/en-us/library/ms682052(VS.85).aspx
188 typedef CONDITION_VARIABLE pipe_condvar
;
190 #define pipe_static_condvar(cond) \
191 /*static*/ pipe_condvar cond = CONDITION_VARIABLE_INIT
193 #define pipe_condvar_init(cond) \
194 InitializeConditionVariable(&(cond))
196 #define pipe_condvar_destroy(cond) \
197 (void) cond /* nothing to do */
199 #define pipe_condvar_wait(cond, mutex) \
200 SleepConditionVariableCS(&(cond), &(mutex), INFINITE)
202 #define pipe_condvar_signal(cond) \
203 WakeConditionVariable(&(cond))
205 #define pipe_condvar_broadcast(cond) \
206 WakeAllConditionVariable(&(cond))
208 #else /* need compatibility with pre-Vista Win32 */
210 /* pipe_condvar (XXX FIX THIS)
211 * See http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
212 * for potential pitfalls in implementation.
214 typedef DWORD pipe_condvar
;
216 #define pipe_static_condvar(cond) \
217 /*static*/ pipe_condvar cond = 1
219 #define pipe_condvar_init(cond) \
222 #define pipe_condvar_destroy(cond) \
225 /* Poor man's pthread_cond_wait():
226 Just release the mutex and sleep for one millisecond.
227 The caller's while() loop does all the work. */
228 #define pipe_condvar_wait(cond, mutex) \
229 do { pipe_mutex_unlock(mutex); \
231 pipe_mutex_lock(mutex); \
234 #define pipe_condvar_signal(cond) \
237 #define pipe_condvar_broadcast(cond) \
240 #endif /* pre-Vista win32 */
244 #include "os/os_time.h"
246 /** Dummy definitions */
248 typedef unsigned pipe_thread
;
250 #define PIPE_THREAD_ROUTINE( name, param ) \
251 void * name( void *param )
253 static INLINE pipe_thread
pipe_thread_create( void *(* routine
)( void *), void *param
)
258 static INLINE
int pipe_thread_wait( pipe_thread thread
)
263 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
268 typedef unsigned pipe_mutex
;
270 #define pipe_static_mutex(mutex) \
271 static pipe_mutex mutex = 0
273 #define pipe_mutex_init(mutex) \
276 #define pipe_mutex_destroy(mutex) \
279 #define pipe_mutex_lock(mutex) \
282 #define pipe_mutex_unlock(mutex) \
285 typedef int64_t pipe_condvar
;
287 #define pipe_static_condvar(condvar) \
288 static pipe_condvar condvar = 1000
290 #define pipe_condvar_init(condvar) \
291 (void) (condvar = 1000)
293 #define pipe_condvar_destroy(condvar) \
296 /* Poor man's pthread_cond_wait():
297 Just release the mutex and sleep for one millisecond.
298 The caller's while() loop does all the work. */
299 #define pipe_condvar_wait(condvar, mutex) \
300 do { pipe_mutex_unlock(mutex); \
301 os_time_sleep(condvar); \
302 pipe_mutex_lock(mutex); \
305 #define pipe_condvar_signal(condvar) \
308 #define pipe_condvar_broadcast(condvar) \
312 #endif /* PIPE_OS_? */
319 #if (defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HURD)) && !defined(PIPE_OS_ANDROID)
321 typedef pthread_barrier_t pipe_barrier
;
323 static INLINE
void pipe_barrier_init(pipe_barrier
*barrier
, unsigned count
)
325 pthread_barrier_init(barrier
, NULL
, count
);
328 static INLINE
void pipe_barrier_destroy(pipe_barrier
*barrier
)
330 pthread_barrier_destroy(barrier
);
333 static INLINE
void pipe_barrier_wait(pipe_barrier
*barrier
)
335 pthread_barrier_wait(barrier
);
339 #else /* If the OS doesn't have its own, implement barriers using a mutex and a condvar */
346 pipe_condvar condvar
;
349 static INLINE
void pipe_barrier_init(pipe_barrier
*barrier
, unsigned count
)
351 barrier
->count
= count
;
352 barrier
->waiters
= 0;
353 barrier
->sequence
= 0;
354 pipe_mutex_init(barrier
->mutex
);
355 pipe_condvar_init(barrier
->condvar
);
358 static INLINE
void pipe_barrier_destroy(pipe_barrier
*barrier
)
360 assert(barrier
->waiters
== 0);
361 pipe_mutex_destroy(barrier
->mutex
);
362 pipe_condvar_destroy(barrier
->condvar
);
365 static INLINE
void pipe_barrier_wait(pipe_barrier
*barrier
)
367 pipe_mutex_lock(barrier
->mutex
);
369 assert(barrier
->waiters
< barrier
->count
);
372 if (barrier
->waiters
< barrier
->count
) {
373 uint64_t sequence
= barrier
->sequence
;
376 pipe_condvar_wait(barrier
->condvar
, barrier
->mutex
);
377 } while (sequence
== barrier
->sequence
);
379 barrier
->waiters
= 0;
381 pipe_condvar_broadcast(barrier
->condvar
);
384 pipe_mutex_unlock(barrier
->mutex
);
404 pipe_semaphore_init(pipe_semaphore
*sema
, int init_val
)
406 pipe_mutex_init(sema
->mutex
);
407 pipe_condvar_init(sema
->cond
);
408 sema
->counter
= init_val
;
412 pipe_semaphore_destroy(pipe_semaphore
*sema
)
414 pipe_mutex_destroy(sema
->mutex
);
415 pipe_condvar_destroy(sema
->cond
);
418 /** Signal/increment semaphore counter */
420 pipe_semaphore_signal(pipe_semaphore
*sema
)
422 pipe_mutex_lock(sema
->mutex
);
424 pipe_condvar_signal(sema
->cond
);
425 pipe_mutex_unlock(sema
->mutex
);
428 /** Wait for semaphore counter to be greater than zero */
430 pipe_semaphore_wait(pipe_semaphore
*sema
)
432 pipe_mutex_lock(sema
->mutex
);
433 while (sema
->counter
<= 0) {
434 pipe_condvar_wait(sema
->cond
, sema
->mutex
);
437 pipe_mutex_unlock(sema
->mutex
);
443 * Thread-specific data.
447 #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) || defined(PIPE_OS_HURD)
449 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
456 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
460 pipe_tsd_init(pipe_tsd
*tsd
)
462 #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) || defined(PIPE_OS_HURD)
463 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
464 perror("pthread_key_create(): failed to allocate key for thread specific data");
467 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
470 tsd
->initMagic
= PIPE_TSD_INIT_MAGIC
;
474 pipe_tsd_get(pipe_tsd
*tsd
)
476 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
479 #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) || defined(PIPE_OS_HURD)
480 return pthread_getspecific(tsd
->key
);
481 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
491 pipe_tsd_set(pipe_tsd
*tsd
, void *value
)
493 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
496 #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) || defined(PIPE_OS_HURD)
497 if (pthread_setspecific(tsd
->key
, value
) != 0) {
498 perror("pthread_set_specific() failed");
501 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
510 #endif /* OS_THREAD_H_ */