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 */
42 #include "util/u_thread.h"
45 #define pipe_mutex_assert_locked(mutex) \
46 __pipe_mutex_assert_locked(&(mutex))
49 __pipe_mutex_assert_locked(mtx_t
*mutex
)
52 /* NOTE: this would not work for recursive mutexes, but
53 * mtx_t doesn't support those
55 int ret
= mtx_trylock(mutex
);
56 assert(ret
== thrd_busy
);
57 if (ret
== thrd_success
)
78 pipe_semaphore_init(pipe_semaphore
*sema
, int init_val
)
80 (void) mtx_init(&sema
->mutex
, mtx_plain
);
81 cnd_init(&sema
->cond
);
82 sema
->counter
= init_val
;
86 pipe_semaphore_destroy(pipe_semaphore
*sema
)
88 mtx_destroy(&sema
->mutex
);
89 cnd_destroy(&sema
->cond
);
92 /** Signal/increment semaphore counter */
94 pipe_semaphore_signal(pipe_semaphore
*sema
)
96 mtx_lock(&sema
->mutex
);
98 cnd_signal(&sema
->cond
);
99 mtx_unlock(&sema
->mutex
);
102 /** Wait for semaphore counter to be greater than zero */
104 pipe_semaphore_wait(pipe_semaphore
*sema
)
106 mtx_lock(&sema
->mutex
);
107 while (sema
->counter
<= 0) {
108 cnd_wait(&sema
->cond
, &sema
->mutex
);
111 mtx_unlock(&sema
->mutex
);
117 * Thread-specific data.
126 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
130 pipe_tsd_init(pipe_tsd
*tsd
)
132 if (tss_create(&tsd
->key
, NULL
/*free*/) != 0) {
135 tsd
->initMagic
= PIPE_TSD_INIT_MAGIC
;
139 pipe_tsd_get(pipe_tsd
*tsd
)
141 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
144 return tss_get(tsd
->key
);
148 pipe_tsd_set(pipe_tsd
*tsd
, void *value
)
150 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
153 if (tss_set(tsd
->key
, value
) != 0) {
164 /* Return the time of the current thread's CPU time clock. */
165 static inline int64_t
166 pipe_current_thread_get_time_nano(void)
168 #if defined(HAVE_PTHREAD)
169 return u_thread_get_time_nano(pthread_self());
175 #endif /* OS_THREAD_H_ */