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 #define pipe_static_mutex(mutex) \
156 /*static*/ pipe_mutex mutex = {0,0,0,0,0,0}
158 #define pipe_mutex_init(mutex) \
159 InitializeCriticalSection(&mutex)
161 #define pipe_mutex_destroy(mutex) \
162 DeleteCriticalSection(&mutex)
164 #define pipe_mutex_lock(mutex) \
165 EnterCriticalSection(&mutex)
167 #define pipe_mutex_unlock(mutex) \
168 LeaveCriticalSection(&mutex)
170 /* TODO: Need a macro to declare "I don't care about WinXP compatibilty" */
171 #if 0 && defined (_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
172 /* CONDITION_VARIABLE is only available on newer versions of Windows
173 * (Server 2008/Vista or later).
174 * http://msdn.microsoft.com/en-us/library/ms682052(VS.85).aspx
178 typedef CONDITION_VARIABLE pipe_condvar
;
180 #define pipe_static_condvar(cond) \
181 /*static*/ pipe_condvar cond = CONDITION_VARIABLE_INIT
183 #define pipe_condvar_init(cond) \
184 InitializeConditionVariable(&(cond))
186 #define pipe_condvar_destroy(cond) \
187 (void) cond /* nothing to do */
189 #define pipe_condvar_wait(cond, mutex) \
190 SleepConditionVariableCS(&(cond), &(mutex), INFINITE)
192 #define pipe_condvar_signal(cond) \
193 WakeConditionVariable(&(cond))
195 #define pipe_condvar_broadcast(cond) \
196 WakeAllConditionVariable(&(cond))
198 #else /* need compatibility with pre-Vista Win32 */
200 /* pipe_condvar (XXX FIX THIS)
201 * See http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
202 * for potential pitfalls in implementation.
204 typedef DWORD pipe_condvar
;
206 #define pipe_static_condvar(cond) \
207 /*static*/ pipe_condvar cond = 1
209 #define pipe_condvar_init(cond) \
212 #define pipe_condvar_destroy(cond) \
215 /* Poor man's pthread_cond_wait():
216 Just release the mutex and sleep for one millisecond.
217 The caller's while() loop does all the work. */
218 #define pipe_condvar_wait(cond, mutex) \
219 do { pipe_mutex_unlock(mutex); \
221 pipe_mutex_lock(mutex); \
224 #define pipe_condvar_signal(cond) \
227 #define pipe_condvar_broadcast(cond) \
230 #endif /* pre-Vista win32 */
234 #include "os/os_time.h"
236 /** Dummy definitions */
238 typedef unsigned pipe_thread
;
240 #define PIPE_THREAD_ROUTINE( name, param ) \
241 void * name( void *param )
243 static INLINE pipe_thread
pipe_thread_create( void *(* routine
)( void *), void *param
)
248 static INLINE
int pipe_thread_wait( pipe_thread thread
)
253 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
258 typedef unsigned pipe_mutex
;
260 #define pipe_static_mutex(mutex) \
261 static pipe_mutex mutex = 0
263 #define pipe_mutex_init(mutex) \
266 #define pipe_mutex_destroy(mutex) \
269 #define pipe_mutex_lock(mutex) \
272 #define pipe_mutex_unlock(mutex) \
275 typedef int64_t pipe_condvar
;
277 #define pipe_static_condvar(condvar) \
278 static pipe_condvar condvar = 1000
280 #define pipe_condvar_init(condvar) \
281 (void) (condvar = 1000)
283 #define pipe_condvar_destroy(condvar) \
286 /* Poor man's pthread_cond_wait():
287 Just release the mutex and sleep for one millisecond.
288 The caller's while() loop does all the work. */
289 #define pipe_condvar_wait(condvar, mutex) \
290 do { pipe_mutex_unlock(mutex); \
291 os_time_sleep(condvar); \
292 pipe_mutex_lock(mutex); \
295 #define pipe_condvar_signal(condvar) \
298 #define pipe_condvar_broadcast(condvar) \
302 #endif /* PIPE_OS_? */
309 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HAIKU) || defined(PIPE_OS_EMBEDDED)
311 typedef pthread_barrier_t pipe_barrier
;
313 static INLINE
void pipe_barrier_init(pipe_barrier
*barrier
, unsigned count
)
315 pthread_barrier_init(barrier
, NULL
, count
);
318 static INLINE
void pipe_barrier_destroy(pipe_barrier
*barrier
)
320 pthread_barrier_destroy(barrier
);
323 static INLINE
void pipe_barrier_wait(pipe_barrier
*barrier
)
325 pthread_barrier_wait(barrier
);
329 #else /* If the OS doesn't have its own, implement barriers using a mutex and a condvar */
336 pipe_condvar condvar
;
339 static INLINE
void pipe_barrier_init(pipe_barrier
*barrier
, unsigned count
)
341 barrier
->count
= count
;
342 barrier
->waiters
= 0;
343 barrier
->sequence
= 0;
344 pipe_mutex_init(barrier
->mutex
);
345 pipe_condvar_init(barrier
->condvar
);
348 static INLINE
void pipe_barrier_destroy(pipe_barrier
*barrier
)
350 assert(barrier
->waiters
== 0);
351 pipe_mutex_destroy(barrier
->mutex
);
352 pipe_condvar_destroy(barrier
->condvar
);
355 static INLINE
void pipe_barrier_wait(pipe_barrier
*barrier
)
357 pipe_mutex_lock(barrier
->mutex
);
359 assert(barrier
->waiters
< barrier
->count
);
362 if (barrier
->waiters
< barrier
->count
) {
363 uint64_t sequence
= barrier
->sequence
;
366 pipe_condvar_wait(barrier
->condvar
, barrier
->mutex
);
367 } while (sequence
== barrier
->sequence
);
369 barrier
->waiters
= 0;
371 pipe_condvar_broadcast(barrier
->condvar
);
374 pipe_mutex_unlock(barrier
->mutex
);
394 pipe_semaphore_init(pipe_semaphore
*sema
, int init_val
)
396 pipe_mutex_init(sema
->mutex
);
397 pipe_condvar_init(sema
->cond
);
398 sema
->counter
= init_val
;
402 pipe_semaphore_destroy(pipe_semaphore
*sema
)
404 pipe_mutex_destroy(sema
->mutex
);
405 pipe_condvar_destroy(sema
->cond
);
408 /** Signal/increment semaphore counter */
410 pipe_semaphore_signal(pipe_semaphore
*sema
)
412 pipe_mutex_lock(sema
->mutex
);
414 pipe_condvar_signal(sema
->cond
);
415 pipe_mutex_unlock(sema
->mutex
);
418 /** Wait for semaphore counter to be greater than zero */
420 pipe_semaphore_wait(pipe_semaphore
*sema
)
422 pipe_mutex_lock(sema
->mutex
);
423 while (sema
->counter
<= 0) {
424 pipe_condvar_wait(sema
->cond
, sema
->mutex
);
427 pipe_mutex_unlock(sema
->mutex
);
433 * Thread-specific data.
437 #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)
439 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
446 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
450 pipe_tsd_init(pipe_tsd
*tsd
)
452 #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)
453 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
454 perror("pthread_key_create(): failed to allocate key for thread specific data");
457 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
460 tsd
->initMagic
= PIPE_TSD_INIT_MAGIC
;
464 pipe_tsd_get(pipe_tsd
*tsd
)
466 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
469 #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)
470 return pthread_getspecific(tsd
->key
);
471 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
481 pipe_tsd_set(pipe_tsd
*tsd
, void *value
)
483 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
486 #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)
487 if (pthread_setspecific(tsd
->key
, value
) != 0) {
488 perror("pthread_set_specific() failed");
491 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
500 #endif /* OS_THREAD_H_ */