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_EMBEDDED) || 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 if (pthread_create( &thread
, NULL
, routine
, param
))
64 static INLINE
int pipe_thread_wait( pipe_thread thread
)
66 return pthread_join( thread
, NULL
);
69 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
71 return pthread_detach( thread
);
77 typedef pthread_mutex_t pipe_mutex
;
79 #define pipe_static_mutex(mutex) \
80 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
82 #define pipe_mutex_init(mutex) \
83 (void) pthread_mutex_init(&(mutex), NULL)
85 #define pipe_mutex_destroy(mutex) \
86 pthread_mutex_destroy(&(mutex))
88 #define pipe_mutex_lock(mutex) \
89 (void) pthread_mutex_lock(&(mutex))
91 #define pipe_mutex_unlock(mutex) \
92 (void) pthread_mutex_unlock(&(mutex))
97 typedef pthread_cond_t pipe_condvar
;
99 #define pipe_static_condvar(mutex) \
100 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
102 #define pipe_condvar_init(cond) \
103 pthread_cond_init(&(cond), NULL)
105 #define pipe_condvar_destroy(cond) \
106 pthread_cond_destroy(&(cond))
108 #define pipe_condvar_wait(cond, mutex) \
109 pthread_cond_wait(&(cond), &(mutex))
111 #define pipe_condvar_signal(cond) \
112 pthread_cond_signal(&(cond))
114 #define pipe_condvar_broadcast(cond) \
115 pthread_cond_broadcast(&(cond))
119 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
125 typedef HANDLE pipe_thread
;
127 #define PIPE_THREAD_ROUTINE( name, param ) \
128 void * WINAPI name( void *param )
130 static INLINE pipe_thread
pipe_thread_create( void *(WINAPI
* routine
)( void *), void *param
)
133 return CreateThread( NULL
, 0, (LPTHREAD_START_ROUTINE
) routine
, param
, 0, &id
);
136 static INLINE
int pipe_thread_wait( pipe_thread thread
)
138 if (WaitForSingleObject( thread
, INFINITE
) == WAIT_OBJECT_0
)
143 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
145 if (CloseHandle( thread
))
153 typedef CRITICAL_SECTION pipe_mutex
;
155 /* http://locklessinc.com/articles/pthreads_on_windows/ */
156 #define pipe_static_mutex(mutex) \
157 static pipe_mutex mutex = {(PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0}
159 #define pipe_mutex_init(mutex) \
160 InitializeCriticalSection(&mutex)
162 #define pipe_mutex_destroy(mutex) \
163 DeleteCriticalSection(&mutex)
165 #define pipe_mutex_lock(mutex) \
166 EnterCriticalSection(&mutex)
168 #define pipe_mutex_unlock(mutex) \
169 LeaveCriticalSection(&mutex)
171 /* TODO: Need a macro to declare "I don't care about WinXP compatibilty" */
172 #if 0 && defined (_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
173 /* CONDITION_VARIABLE is only available on newer versions of Windows
174 * (Server 2008/Vista or later).
175 * http://msdn.microsoft.com/en-us/library/ms682052(VS.85).aspx
179 typedef CONDITION_VARIABLE pipe_condvar
;
181 #define pipe_static_condvar(cond) \
182 /*static*/ pipe_condvar cond = CONDITION_VARIABLE_INIT
184 #define pipe_condvar_init(cond) \
185 InitializeConditionVariable(&(cond))
187 #define pipe_condvar_destroy(cond) \
188 (void) cond /* nothing to do */
190 #define pipe_condvar_wait(cond, mutex) \
191 SleepConditionVariableCS(&(cond), &(mutex), INFINITE)
193 #define pipe_condvar_signal(cond) \
194 WakeConditionVariable(&(cond))
196 #define pipe_condvar_broadcast(cond) \
197 WakeAllConditionVariable(&(cond))
199 #else /* need compatibility with pre-Vista Win32 */
201 /* pipe_condvar (XXX FIX THIS)
202 * See http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
203 * for potential pitfalls in implementation.
205 typedef DWORD pipe_condvar
;
207 #define pipe_static_condvar(cond) \
208 /*static*/ pipe_condvar cond = 1
210 #define pipe_condvar_init(cond) \
213 #define pipe_condvar_destroy(cond) \
216 /* Poor man's pthread_cond_wait():
217 Just release the mutex and sleep for one millisecond.
218 The caller's while() loop does all the work. */
219 #define pipe_condvar_wait(cond, mutex) \
220 do { pipe_mutex_unlock(mutex); \
222 pipe_mutex_lock(mutex); \
225 #define pipe_condvar_signal(cond) \
228 #define pipe_condvar_broadcast(cond) \
231 #endif /* pre-Vista win32 */
235 #include "os/os_time.h"
237 /** Dummy definitions */
239 typedef unsigned pipe_thread
;
241 #define PIPE_THREAD_ROUTINE( name, param ) \
242 void * name( void *param )
244 static INLINE pipe_thread
pipe_thread_create( void *(* routine
)( void *), void *param
)
249 static INLINE
int pipe_thread_wait( pipe_thread thread
)
254 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
259 typedef unsigned pipe_mutex
;
261 #define pipe_static_mutex(mutex) \
262 static pipe_mutex mutex = 0
264 #define pipe_mutex_init(mutex) \
267 #define pipe_mutex_destroy(mutex) \
270 #define pipe_mutex_lock(mutex) \
273 #define pipe_mutex_unlock(mutex) \
276 typedef int64_t pipe_condvar
;
278 #define pipe_static_condvar(condvar) \
279 static pipe_condvar condvar = 1000
281 #define pipe_condvar_init(condvar) \
282 (void) (condvar = 1000)
284 #define pipe_condvar_destroy(condvar) \
287 /* Poor man's pthread_cond_wait():
288 Just release the mutex and sleep for one millisecond.
289 The caller's while() loop does all the work. */
290 #define pipe_condvar_wait(condvar, mutex) \
291 do { pipe_mutex_unlock(mutex); \
292 os_time_sleep(condvar); \
293 pipe_mutex_lock(mutex); \
296 #define pipe_condvar_signal(condvar) \
299 #define pipe_condvar_broadcast(condvar) \
303 #endif /* PIPE_OS_? */
310 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_EMBEDDED)
312 typedef pthread_barrier_t pipe_barrier
;
314 static INLINE
void pipe_barrier_init(pipe_barrier
*barrier
, unsigned count
)
316 pthread_barrier_init(barrier
, NULL
, count
);
319 static INLINE
void pipe_barrier_destroy(pipe_barrier
*barrier
)
321 pthread_barrier_destroy(barrier
);
324 static INLINE
void pipe_barrier_wait(pipe_barrier
*barrier
)
326 pthread_barrier_wait(barrier
);
330 #else /* If the OS doesn't have its own, implement barriers using a mutex and a condvar */
337 pipe_condvar condvar
;
340 static INLINE
void pipe_barrier_init(pipe_barrier
*barrier
, unsigned count
)
342 barrier
->count
= count
;
343 barrier
->waiters
= 0;
344 barrier
->sequence
= 0;
345 pipe_mutex_init(barrier
->mutex
);
346 pipe_condvar_init(barrier
->condvar
);
349 static INLINE
void pipe_barrier_destroy(pipe_barrier
*barrier
)
351 assert(barrier
->waiters
== 0);
352 pipe_mutex_destroy(barrier
->mutex
);
353 pipe_condvar_destroy(barrier
->condvar
);
356 static INLINE
void pipe_barrier_wait(pipe_barrier
*barrier
)
358 pipe_mutex_lock(barrier
->mutex
);
360 assert(barrier
->waiters
< barrier
->count
);
363 if (barrier
->waiters
< barrier
->count
) {
364 uint64_t sequence
= barrier
->sequence
;
367 pipe_condvar_wait(barrier
->condvar
, barrier
->mutex
);
368 } while (sequence
== barrier
->sequence
);
370 barrier
->waiters
= 0;
372 pipe_condvar_broadcast(barrier
->condvar
);
375 pipe_mutex_unlock(barrier
->mutex
);
395 pipe_semaphore_init(pipe_semaphore
*sema
, int init_val
)
397 pipe_mutex_init(sema
->mutex
);
398 pipe_condvar_init(sema
->cond
);
399 sema
->counter
= init_val
;
403 pipe_semaphore_destroy(pipe_semaphore
*sema
)
405 pipe_mutex_destroy(sema
->mutex
);
406 pipe_condvar_destroy(sema
->cond
);
409 /** Signal/increment semaphore counter */
411 pipe_semaphore_signal(pipe_semaphore
*sema
)
413 pipe_mutex_lock(sema
->mutex
);
415 pipe_condvar_signal(sema
->cond
);
416 pipe_mutex_unlock(sema
->mutex
);
419 /** Wait for semaphore counter to be greater than zero */
421 pipe_semaphore_wait(pipe_semaphore
*sema
)
423 pipe_mutex_lock(sema
->mutex
);
424 while (sema
->counter
<= 0) {
425 pipe_condvar_wait(sema
->cond
, sema
->mutex
);
428 pipe_mutex_unlock(sema
->mutex
);
434 * Thread-specific data.
438 #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)
440 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
447 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
451 pipe_tsd_init(pipe_tsd
*tsd
)
453 #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)
454 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
455 perror("pthread_key_create(): failed to allocate key for thread specific data");
458 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
461 tsd
->initMagic
= PIPE_TSD_INIT_MAGIC
;
465 pipe_tsd_get(pipe_tsd
*tsd
)
467 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
470 #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)
471 return pthread_getspecific(tsd
->key
);
472 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
482 pipe_tsd_set(pipe_tsd
*tsd
, void *value
)
484 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
487 #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)
488 if (pthread_setspecific(tsd
->key
, value
) != 0) {
489 perror("pthread_set_specific() failed");
492 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
501 #endif /* OS_THREAD_H_ */