gallium/util: replace pipe_mutex_destroy() with mtx_destroy()
[mesa.git] / src / gallium / auxiliary / os / os_thread.h
1 /**************************************************************************
2 *
3 * Copyright 1999-2006 Brian Paul
4 * Copyright 2008 VMware, Inc.
5 * All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
24 *
25 **************************************************************************/
26
27
28 /**
29 * @file
30 *
31 * Thread, mutex, condition variable, barrier, semaphore and
32 * thread-specific data functions.
33 */
34
35
36 #ifndef OS_THREAD_H_
37 #define OS_THREAD_H_
38
39
40 #include "pipe/p_compiler.h"
41 #include "util/u_debug.h" /* for assert */
42
43 #include "c11/threads.h"
44
45 #ifdef HAVE_PTHREAD
46 #include <signal.h>
47 #endif
48
49
50 /* pipe_thread
51 */
52 typedef thrd_t pipe_thread;
53
54 #define PIPE_THREAD_ROUTINE( name, param ) \
55 int name( void *param )
56
57 static inline pipe_thread pipe_thread_create( PIPE_THREAD_ROUTINE((*routine), ), void *param )
58 {
59 pipe_thread thread;
60 #ifdef HAVE_PTHREAD
61 sigset_t saved_set, new_set;
62 int ret;
63
64 sigfillset(&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);
68 #else
69 int ret;
70 ret = thrd_create( &thread, routine, param );
71 #endif
72 if (ret)
73 return 0;
74
75 return thread;
76 }
77
78 static inline int pipe_thread_wait( pipe_thread thread )
79 {
80 return thrd_join( thread, NULL );
81 }
82
83 static inline int pipe_thread_destroy( pipe_thread thread )
84 {
85 return thrd_detach( thread );
86 }
87
88 static inline void pipe_thread_setname( const char *name )
89 {
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);
94 # endif
95 #endif
96 (void)name;
97 }
98
99
100 static inline int pipe_thread_is_self( pipe_thread thread )
101 {
102 #if defined(HAVE_PTHREAD)
103 # if defined(__GNU_LIBRARY__) && defined(__GLIBC__) && defined(__GLIBC_MINOR__) && \
104 (__GLIBC__ >= 3 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 12))
105 return pthread_equal(pthread_self(), thread);
106 # endif
107 #endif
108 return 0;
109 }
110
111 #define pipe_mutex_lock(mutex) \
112 (void) mtx_lock(&(mutex))
113
114 #define pipe_mutex_unlock(mutex) \
115 (void) mtx_unlock(&(mutex))
116
117 #define pipe_mutex_assert_locked(mutex) \
118 __pipe_mutex_assert_locked(&(mutex))
119
120 static inline void
121 __pipe_mutex_assert_locked(mtx_t *mutex)
122 {
123 #ifdef DEBUG
124 /* NOTE: this would not work for recursive mutexes, but
125 * mtx_t doesn't support those
126 */
127 int ret = mtx_trylock(mutex);
128 assert(ret == thrd_busy);
129 if (ret == thrd_success)
130 mtx_unlock(mutex);
131 #endif
132 }
133
134 /* pipe_condvar
135 */
136 typedef cnd_t pipe_condvar;
137
138
139 /*
140 * pipe_barrier
141 */
142
143 #if (defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HURD)) && !defined(PIPE_OS_ANDROID)
144
145 typedef pthread_barrier_t pipe_barrier;
146
147 static inline void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
148 {
149 pthread_barrier_init(barrier, NULL, count);
150 }
151
152 static inline void pipe_barrier_destroy(pipe_barrier *barrier)
153 {
154 pthread_barrier_destroy(barrier);
155 }
156
157 static inline void pipe_barrier_wait(pipe_barrier *barrier)
158 {
159 pthread_barrier_wait(barrier);
160 }
161
162
163 #else /* If the OS doesn't have its own, implement barriers using a mutex and a condvar */
164
165 typedef struct {
166 unsigned count;
167 unsigned waiters;
168 uint64_t sequence;
169 mtx_t mutex;
170 pipe_condvar condvar;
171 } pipe_barrier;
172
173 static inline void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
174 {
175 barrier->count = count;
176 barrier->waiters = 0;
177 barrier->sequence = 0;
178 (void) mtx_init(&barrier->mutex, mtx_plain);
179 cnd_init(&barrier->condvar);
180 }
181
182 static inline void pipe_barrier_destroy(pipe_barrier *barrier)
183 {
184 assert(barrier->waiters == 0);
185 mtx_destroy(&barrier->mutex);
186 cnd_destroy(&barrier->condvar);
187 }
188
189 static inline void pipe_barrier_wait(pipe_barrier *barrier)
190 {
191 pipe_mutex_lock(barrier->mutex);
192
193 assert(barrier->waiters < barrier->count);
194 barrier->waiters++;
195
196 if (barrier->waiters < barrier->count) {
197 uint64_t sequence = barrier->sequence;
198
199 do {
200 cnd_wait(&barrier->condvar, &barrier->mutex);
201 } while (sequence == barrier->sequence);
202 } else {
203 barrier->waiters = 0;
204 barrier->sequence++;
205 cnd_broadcast(&barrier->condvar);
206 }
207
208 pipe_mutex_unlock(barrier->mutex);
209 }
210
211
212 #endif
213
214
215 /*
216 * Semaphores
217 */
218
219 typedef struct
220 {
221 mtx_t mutex;
222 pipe_condvar cond;
223 int counter;
224 } pipe_semaphore;
225
226
227 static inline void
228 pipe_semaphore_init(pipe_semaphore *sema, int init_val)
229 {
230 (void) mtx_init(&sema->mutex, mtx_plain);
231 cnd_init(&sema->cond);
232 sema->counter = init_val;
233 }
234
235 static inline void
236 pipe_semaphore_destroy(pipe_semaphore *sema)
237 {
238 mtx_destroy(&sema->mutex);
239 cnd_destroy(&sema->cond);
240 }
241
242 /** Signal/increment semaphore counter */
243 static inline void
244 pipe_semaphore_signal(pipe_semaphore *sema)
245 {
246 pipe_mutex_lock(sema->mutex);
247 sema->counter++;
248 cnd_signal(&sema->cond);
249 pipe_mutex_unlock(sema->mutex);
250 }
251
252 /** Wait for semaphore counter to be greater than zero */
253 static inline void
254 pipe_semaphore_wait(pipe_semaphore *sema)
255 {
256 pipe_mutex_lock(sema->mutex);
257 while (sema->counter <= 0) {
258 cnd_wait(&sema->cond, &sema->mutex);
259 }
260 sema->counter--;
261 pipe_mutex_unlock(sema->mutex);
262 }
263
264
265
266 /*
267 * Thread-specific data.
268 */
269
270 typedef struct {
271 tss_t key;
272 int initMagic;
273 } pipe_tsd;
274
275
276 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
277
278
279 static inline void
280 pipe_tsd_init(pipe_tsd *tsd)
281 {
282 if (tss_create(&tsd->key, NULL/*free*/) != 0) {
283 exit(-1);
284 }
285 tsd->initMagic = PIPE_TSD_INIT_MAGIC;
286 }
287
288 static inline void *
289 pipe_tsd_get(pipe_tsd *tsd)
290 {
291 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
292 pipe_tsd_init(tsd);
293 }
294 return tss_get(tsd->key);
295 }
296
297 static inline void
298 pipe_tsd_set(pipe_tsd *tsd, void *value)
299 {
300 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
301 pipe_tsd_init(tsd);
302 }
303 if (tss_set(tsd->key, value) != 0) {
304 exit(-1);
305 }
306 }
307
308
309
310 /*
311 * Thread statistics.
312 */
313
314 /* Return the time of a thread's CPU time clock. */
315 static inline int64_t
316 pipe_thread_get_time_nano(pipe_thread thread)
317 {
318 #if defined(PIPE_OS_LINUX) && defined(HAVE_PTHREAD)
319 struct timespec ts;
320 clockid_t cid;
321
322 pthread_getcpuclockid(thread, &cid);
323 clock_gettime(cid, &ts);
324 return (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
325 #else
326 return 0;
327 #endif
328 }
329
330 /* Return the time of the current thread's CPU time clock. */
331 static inline int64_t
332 pipe_current_thread_get_time_nano(void)
333 {
334 #if defined(HAVE_PTHREAD)
335 return pipe_thread_get_time_nano(pthread_self());
336 #else
337 return 0;
338 #endif
339 }
340
341 #endif /* OS_THREAD_H_ */