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 */
43 #include "c11/threads.h"
52 typedef thrd_t pipe_thread
;
54 #define PIPE_THREAD_ROUTINE( name, param ) \
55 int name( void *param )
57 static inline pipe_thread
pipe_thread_create( PIPE_THREAD_ROUTINE((*routine
), ), void *param
)
61 sigset_t saved_set
, new_set
;
65 pthread_sigmask(SIG_SETMASK
, &new_set
, &saved_set
);
66 ret
= thrd_create( &thread
, routine
, param
);
67 pthread_sigmask(SIG_SETMASK
, &saved_set
, NULL
);
70 ret
= thrd_create( &thread
, routine
, param
);
78 static inline int pipe_thread_wait( pipe_thread thread
)
80 return thrd_join( thread
, NULL
);
83 static inline int pipe_thread_destroy( pipe_thread thread
)
85 return thrd_detach( thread
);
88 static inline void pipe_thread_setname( const char *name
)
90 #if defined(HAVE_PTHREAD)
91 # if defined(__GNU_LIBRARY__) && defined(__GLIBC__) && defined(__GLIBC_MINOR__) && \
92 (__GLIBC__ >= 3 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 12))
93 pthread_setname_np(pthread_self(), name
);
102 typedef mtx_t pipe_mutex
;
104 #define pipe_static_mutex(mutex) \
105 static pipe_mutex mutex = _MTX_INITIALIZER_NP
107 #define pipe_mutex_init(mutex) \
108 (void) mtx_init(&(mutex), mtx_plain)
110 #define pipe_mutex_destroy(mutex) \
111 mtx_destroy(&(mutex))
113 #define pipe_mutex_lock(mutex) \
114 (void) mtx_lock(&(mutex))
116 #define pipe_mutex_unlock(mutex) \
117 (void) mtx_unlock(&(mutex))
119 #define pipe_mutex_assert_locked(mutex) \
120 __pipe_mutex_assert_locked(&(mutex))
123 __pipe_mutex_assert_locked(pipe_mutex
*mutex
)
126 /* NOTE: this would not work for recursive mutexes, but
127 * pipe_mutex doesn't support those
129 int ret
= mtx_trylock(mutex
);
130 assert(ret
== thrd_busy
);
131 if (ret
== thrd_success
)
138 typedef cnd_t pipe_condvar
;
140 #define pipe_condvar_init(cond) \
143 #define pipe_condvar_destroy(cond) \
146 #define pipe_condvar_wait(cond, mutex) \
147 cnd_wait(&(cond), &(mutex))
149 #define pipe_condvar_signal(cond) \
152 #define pipe_condvar_broadcast(cond) \
153 cnd_broadcast(&(cond))
160 #if (defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HURD)) && !defined(PIPE_OS_ANDROID)
162 typedef pthread_barrier_t pipe_barrier
;
164 static inline void pipe_barrier_init(pipe_barrier
*barrier
, unsigned count
)
166 pthread_barrier_init(barrier
, NULL
, count
);
169 static inline void pipe_barrier_destroy(pipe_barrier
*barrier
)
171 pthread_barrier_destroy(barrier
);
174 static inline void pipe_barrier_wait(pipe_barrier
*barrier
)
176 pthread_barrier_wait(barrier
);
180 #else /* If the OS doesn't have its own, implement barriers using a mutex and a condvar */
187 pipe_condvar condvar
;
190 static inline void pipe_barrier_init(pipe_barrier
*barrier
, unsigned count
)
192 barrier
->count
= count
;
193 barrier
->waiters
= 0;
194 barrier
->sequence
= 0;
195 pipe_mutex_init(barrier
->mutex
);
196 pipe_condvar_init(barrier
->condvar
);
199 static inline void pipe_barrier_destroy(pipe_barrier
*barrier
)
201 assert(barrier
->waiters
== 0);
202 pipe_mutex_destroy(barrier
->mutex
);
203 pipe_condvar_destroy(barrier
->condvar
);
206 static inline void pipe_barrier_wait(pipe_barrier
*barrier
)
208 pipe_mutex_lock(barrier
->mutex
);
210 assert(barrier
->waiters
< barrier
->count
);
213 if (barrier
->waiters
< barrier
->count
) {
214 uint64_t sequence
= barrier
->sequence
;
217 pipe_condvar_wait(barrier
->condvar
, barrier
->mutex
);
218 } while (sequence
== barrier
->sequence
);
220 barrier
->waiters
= 0;
222 pipe_condvar_broadcast(barrier
->condvar
);
225 pipe_mutex_unlock(barrier
->mutex
);
245 pipe_semaphore_init(pipe_semaphore
*sema
, int init_val
)
247 pipe_mutex_init(sema
->mutex
);
248 pipe_condvar_init(sema
->cond
);
249 sema
->counter
= init_val
;
253 pipe_semaphore_destroy(pipe_semaphore
*sema
)
255 pipe_mutex_destroy(sema
->mutex
);
256 pipe_condvar_destroy(sema
->cond
);
259 /** Signal/increment semaphore counter */
261 pipe_semaphore_signal(pipe_semaphore
*sema
)
263 pipe_mutex_lock(sema
->mutex
);
265 pipe_condvar_signal(sema
->cond
);
266 pipe_mutex_unlock(sema
->mutex
);
269 /** Wait for semaphore counter to be greater than zero */
271 pipe_semaphore_wait(pipe_semaphore
*sema
)
273 pipe_mutex_lock(sema
->mutex
);
274 while (sema
->counter
<= 0) {
275 pipe_condvar_wait(sema
->cond
, sema
->mutex
);
278 pipe_mutex_unlock(sema
->mutex
);
284 * Thread-specific data.
293 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
297 pipe_tsd_init(pipe_tsd
*tsd
)
299 if (tss_create(&tsd
->key
, NULL
/*free*/) != 0) {
302 tsd
->initMagic
= PIPE_TSD_INIT_MAGIC
;
306 pipe_tsd_get(pipe_tsd
*tsd
)
308 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
311 return tss_get(tsd
->key
);
315 pipe_tsd_set(pipe_tsd
*tsd
, void *value
)
317 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
320 if (tss_set(tsd
->key
, value
) != 0) {
327 #endif /* OS_THREAD_H_ */