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)
45 #include <pthread.h> /* POSIX threads headers */
46 #include <stdio.h> /* for perror() */
48 #define PIPE_THREAD_HAVE_CONDVAR
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 if (pthread_create( &thread
, NULL
, routine
, param
))
65 static INLINE
int pipe_thread_wait( pipe_thread thread
)
67 return pthread_join( thread
, NULL
);
70 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
72 return pthread_detach( thread
);
78 typedef pthread_mutex_t pipe_mutex
;
80 #define pipe_static_mutex(mutex) \
81 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
83 #define pipe_mutex_init(mutex) \
84 (void) pthread_mutex_init(&(mutex), NULL)
86 #define pipe_mutex_destroy(mutex) \
87 pthread_mutex_destroy(&(mutex))
89 #define pipe_mutex_lock(mutex) \
90 (void) pthread_mutex_lock(&(mutex))
92 #define pipe_mutex_unlock(mutex) \
93 (void) pthread_mutex_unlock(&(mutex))
98 typedef pthread_cond_t pipe_condvar
;
100 #define pipe_static_condvar(mutex) \
101 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
103 #define pipe_condvar_init(cond) \
104 pthread_cond_init(&(cond), NULL)
106 #define pipe_condvar_destroy(cond) \
107 pthread_cond_destroy(&(cond))
109 #define pipe_condvar_wait(cond, mutex) \
110 pthread_cond_wait(&(cond), &(mutex))
112 #define pipe_condvar_signal(cond) \
113 pthread_cond_signal(&(cond))
115 #define pipe_condvar_broadcast(cond) \
116 pthread_cond_broadcast(&(cond))
121 typedef pthread_barrier_t pipe_barrier
;
123 static INLINE
void pipe_barrier_init(pipe_barrier
*barrier
, unsigned count
)
125 pthread_barrier_init(barrier
, NULL
, count
);
128 static INLINE
void pipe_barrier_destroy(pipe_barrier
*barrier
)
130 pthread_barrier_destroy(barrier
);
133 static INLINE
void pipe_barrier_wait(pipe_barrier
*barrier
)
135 pthread_barrier_wait(barrier
);
139 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
145 typedef HANDLE pipe_thread
;
147 #define PIPE_THREAD_ROUTINE( name, param ) \
148 void * WINAPI name( void *param )
150 static INLINE pipe_thread
pipe_thread_create( void *(WINAPI
* routine
)( void *), void *param
)
153 return CreateThread( NULL
, 0, (LPTHREAD_START_ROUTINE
) routine
, param
, 0, &id
);
156 static INLINE
int pipe_thread_wait( pipe_thread thread
)
158 if (WaitForSingleObject( thread
, INFINITE
) == WAIT_OBJECT_0
)
163 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
165 if (CloseHandle( thread
))
173 typedef CRITICAL_SECTION pipe_mutex
;
175 #define pipe_static_mutex(mutex) \
176 /*static*/ pipe_mutex mutex = {0,0,0,0,0,0}
178 #define pipe_mutex_init(mutex) \
179 InitializeCriticalSection(&mutex)
181 #define pipe_mutex_destroy(mutex) \
182 DeleteCriticalSection(&mutex)
184 #define pipe_mutex_lock(mutex) \
185 EnterCriticalSection(&mutex)
187 #define pipe_mutex_unlock(mutex) \
188 LeaveCriticalSection(&mutex)
191 /* pipe_condvar (XXX FIX THIS)
193 typedef unsigned pipe_condvar
;
195 #define pipe_condvar_init(cond) \
198 #define pipe_condvar_destroy(cond) \
201 #define pipe_condvar_wait(cond, mutex) \
202 (void) cond; (void) mutex
204 #define pipe_condvar_signal(cond) \
207 #define pipe_condvar_broadcast(cond) \
211 /* pipe_barrier (XXX FIX THIS)
213 typedef unsigned pipe_barrier
;
215 static INLINE
void pipe_barrier_init(pipe_barrier
*barrier
, unsigned count
)
217 /* XXX we could implement barriers with a mutex and condition var */
220 static INLINE
void pipe_barrier_destroy(pipe_barrier
*barrier
)
224 static INLINE
void pipe_barrier_wait(pipe_barrier
*barrier
)
233 /** Dummy definitions */
235 typedef unsigned pipe_thread
;
237 #define PIPE_THREAD_ROUTINE( name, param ) \
238 void * name( void *param )
240 static INLINE pipe_thread
pipe_thread_create( void *(* routine
)( void *), void *param
)
245 static INLINE
int pipe_thread_wait( pipe_thread thread
)
250 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
255 typedef unsigned pipe_mutex
;
256 typedef unsigned pipe_condvar
;
257 typedef unsigned pipe_barrier
;
259 #define pipe_static_mutex(mutex) \
260 static pipe_mutex mutex = 0
262 #define pipe_mutex_init(mutex) \
265 #define pipe_mutex_destroy(mutex) \
268 #define pipe_mutex_lock(mutex) \
271 #define pipe_mutex_unlock(mutex) \
274 #define pipe_static_condvar(condvar) \
275 static unsigned condvar = 0
277 #define pipe_condvar_init(condvar) \
280 #define pipe_condvar_destroy(condvar) \
283 #define pipe_condvar_wait(condvar, mutex) \
286 #define pipe_condvar_signal(condvar) \
289 #define pipe_condvar_broadcast(condvar) \
293 static INLINE
void pipe_barrier_init(pipe_barrier
*barrier
, unsigned count
)
295 /* XXX we could implement barriers with a mutex and condition var */
299 static INLINE
void pipe_barrier_destroy(pipe_barrier
*barrier
)
304 static INLINE
void pipe_barrier_wait(pipe_barrier
*barrier
)
311 #endif /* PIPE_OS_? */
327 pipe_semaphore_init(pipe_semaphore
*sema
, int init_val
)
329 pipe_mutex_init(sema
->mutex
);
330 pipe_condvar_init(sema
->cond
);
331 sema
->counter
= init_val
;
335 pipe_semaphore_destroy(pipe_semaphore
*sema
)
337 pipe_mutex_destroy(sema
->mutex
);
338 pipe_condvar_destroy(sema
->cond
);
341 /** Signal/increment semaphore counter */
343 pipe_semaphore_signal(pipe_semaphore
*sema
)
345 pipe_mutex_lock(sema
->mutex
);
347 pipe_condvar_signal(sema
->cond
);
348 pipe_mutex_unlock(sema
->mutex
);
351 /** Wait for semaphore counter to be greater than zero */
353 pipe_semaphore_wait(pipe_semaphore
*sema
)
355 pipe_mutex_lock(sema
->mutex
);
356 while (sema
->counter
<= 0) {
357 pipe_condvar_wait(sema
->cond
, sema
->mutex
);
360 pipe_mutex_unlock(sema
->mutex
);
366 * Thread-specific data.
370 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU)
372 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
379 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
383 pipe_tsd_init(pipe_tsd
*tsd
)
385 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU)
386 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
387 perror("pthread_key_create(): failed to allocate key for thread specific data");
390 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
393 tsd
->initMagic
= PIPE_TSD_INIT_MAGIC
;
397 pipe_tsd_get(pipe_tsd
*tsd
)
399 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
402 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU)
403 return pthread_getspecific(tsd
->key
);
404 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
414 pipe_tsd_set(pipe_tsd
*tsd
, void *value
)
416 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
419 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU)
420 if (pthread_setspecific(tsd
->key
, value
) != 0) {
421 perror("pthread_set_specific() failed");
424 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
433 #endif /* OS_THREAD_H_ */