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