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 */
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) \
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) \
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); \
291 #define pipe_condvar_signal(condvar) \
294 #define pipe_condvar_broadcast(condvar) \
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
)
313 static INLINE
int pipe_thread_wait( pipe_thread thread
)
318 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
323 typedef unsigned pipe_mutex
;
325 #define pipe_static_mutex(mutex) \
326 static pipe_mutex mutex = 0
328 #define pipe_mutex_init(mutex) \
331 #define pipe_mutex_destroy(mutex) \
334 #define pipe_mutex_lock(mutex) \
337 #define pipe_mutex_unlock(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) \
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); \
360 #define pipe_condvar_signal(condvar) \
363 #define pipe_condvar_broadcast(condvar) \
367 #endif /* PIPE_OS_? */
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 */
401 pipe_condvar condvar
;
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
);
427 if (barrier
->waiters
< barrier
->count
) {
428 uint64_t sequence
= barrier
->sequence
;
431 pipe_condvar_wait(barrier
->condvar
, barrier
->mutex
);
432 } while (sequence
== barrier
->sequence
);
434 barrier
->waiters
= 0;
436 pipe_condvar_broadcast(barrier
->condvar
);
439 pipe_mutex_unlock(barrier
->mutex
);
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
;
467 pipe_semaphore_destroy(pipe_semaphore
*sema
)
469 pipe_mutex_destroy(sema
->mutex
);
470 pipe_condvar_destroy(sema
->cond
);
473 /** Signal/increment semaphore counter */
475 pipe_semaphore_signal(pipe_semaphore
*sema
)
477 pipe_mutex_lock(sema
->mutex
);
479 pipe_condvar_signal(sema
->cond
);
480 pipe_mutex_unlock(sema
->mutex
);
483 /** Wait for semaphore counter to be greater than zero */
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
);
492 pipe_mutex_unlock(sema
->mutex
);
498 * Thread-specific data.
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)
504 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
511 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
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");
522 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
525 tsd
->initMagic
= PIPE_TSD_INIT_MAGIC
;
529 pipe_tsd_get(pipe_tsd
*tsd
)
531 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
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)
546 pipe_tsd_set(pipe_tsd
*tsd
, void *value
)
548 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
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");
556 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
565 #endif /* OS_THREAD_H_ */