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