gallium/util: replace pipe_thread_setname() with u_thread_setname()
[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 #include "util/u_thread.h"
43
44
45 static inline int pipe_thread_is_self( thrd_t thread )
46 {
47 #if defined(HAVE_PTHREAD)
48 # if defined(__GNU_LIBRARY__) && defined(__GLIBC__) && defined(__GLIBC_MINOR__) && \
49 (__GLIBC__ >= 3 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 12))
50 return pthread_equal(pthread_self(), thread);
51 # endif
52 #endif
53 return 0;
54 }
55
56 #define pipe_mutex_assert_locked(mutex) \
57 __pipe_mutex_assert_locked(&(mutex))
58
59 static inline void
60 __pipe_mutex_assert_locked(mtx_t *mutex)
61 {
62 #ifdef DEBUG
63 /* NOTE: this would not work for recursive mutexes, but
64 * mtx_t doesn't support those
65 */
66 int ret = mtx_trylock(mutex);
67 assert(ret == thrd_busy);
68 if (ret == thrd_success)
69 mtx_unlock(mutex);
70 #endif
71 }
72
73
74 /*
75 * pipe_barrier
76 */
77
78 #if (defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HURD)) && !defined(PIPE_OS_ANDROID)
79
80 typedef pthread_barrier_t pipe_barrier;
81
82 static inline void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
83 {
84 pthread_barrier_init(barrier, NULL, count);
85 }
86
87 static inline void pipe_barrier_destroy(pipe_barrier *barrier)
88 {
89 pthread_barrier_destroy(barrier);
90 }
91
92 static inline void pipe_barrier_wait(pipe_barrier *barrier)
93 {
94 pthread_barrier_wait(barrier);
95 }
96
97
98 #else /* If the OS doesn't have its own, implement barriers using a mutex and a condvar */
99
100 typedef struct {
101 unsigned count;
102 unsigned waiters;
103 uint64_t sequence;
104 mtx_t mutex;
105 cnd_t condvar;
106 } pipe_barrier;
107
108 static inline void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
109 {
110 barrier->count = count;
111 barrier->waiters = 0;
112 barrier->sequence = 0;
113 (void) mtx_init(&barrier->mutex, mtx_plain);
114 cnd_init(&barrier->condvar);
115 }
116
117 static inline void pipe_barrier_destroy(pipe_barrier *barrier)
118 {
119 assert(barrier->waiters == 0);
120 mtx_destroy(&barrier->mutex);
121 cnd_destroy(&barrier->condvar);
122 }
123
124 static inline void pipe_barrier_wait(pipe_barrier *barrier)
125 {
126 mtx_lock(&barrier->mutex);
127
128 assert(barrier->waiters < barrier->count);
129 barrier->waiters++;
130
131 if (barrier->waiters < barrier->count) {
132 uint64_t sequence = barrier->sequence;
133
134 do {
135 cnd_wait(&barrier->condvar, &barrier->mutex);
136 } while (sequence == barrier->sequence);
137 } else {
138 barrier->waiters = 0;
139 barrier->sequence++;
140 cnd_broadcast(&barrier->condvar);
141 }
142
143 mtx_unlock(&barrier->mutex);
144 }
145
146
147 #endif
148
149
150 /*
151 * Semaphores
152 */
153
154 typedef struct
155 {
156 mtx_t mutex;
157 cnd_t cond;
158 int counter;
159 } pipe_semaphore;
160
161
162 static inline void
163 pipe_semaphore_init(pipe_semaphore *sema, int init_val)
164 {
165 (void) mtx_init(&sema->mutex, mtx_plain);
166 cnd_init(&sema->cond);
167 sema->counter = init_val;
168 }
169
170 static inline void
171 pipe_semaphore_destroy(pipe_semaphore *sema)
172 {
173 mtx_destroy(&sema->mutex);
174 cnd_destroy(&sema->cond);
175 }
176
177 /** Signal/increment semaphore counter */
178 static inline void
179 pipe_semaphore_signal(pipe_semaphore *sema)
180 {
181 mtx_lock(&sema->mutex);
182 sema->counter++;
183 cnd_signal(&sema->cond);
184 mtx_unlock(&sema->mutex);
185 }
186
187 /** Wait for semaphore counter to be greater than zero */
188 static inline void
189 pipe_semaphore_wait(pipe_semaphore *sema)
190 {
191 mtx_lock(&sema->mutex);
192 while (sema->counter <= 0) {
193 cnd_wait(&sema->cond, &sema->mutex);
194 }
195 sema->counter--;
196 mtx_unlock(&sema->mutex);
197 }
198
199
200
201 /*
202 * Thread-specific data.
203 */
204
205 typedef struct {
206 tss_t key;
207 int initMagic;
208 } pipe_tsd;
209
210
211 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
212
213
214 static inline void
215 pipe_tsd_init(pipe_tsd *tsd)
216 {
217 if (tss_create(&tsd->key, NULL/*free*/) != 0) {
218 exit(-1);
219 }
220 tsd->initMagic = PIPE_TSD_INIT_MAGIC;
221 }
222
223 static inline void *
224 pipe_tsd_get(pipe_tsd *tsd)
225 {
226 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
227 pipe_tsd_init(tsd);
228 }
229 return tss_get(tsd->key);
230 }
231
232 static inline void
233 pipe_tsd_set(pipe_tsd *tsd, void *value)
234 {
235 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
236 pipe_tsd_init(tsd);
237 }
238 if (tss_set(tsd->key, value) != 0) {
239 exit(-1);
240 }
241 }
242
243
244
245 /*
246 * Thread statistics.
247 */
248
249 /* Return the time of the current thread's CPU time clock. */
250 static inline int64_t
251 pipe_current_thread_get_time_nano(void)
252 {
253 #if defined(HAVE_PTHREAD)
254 return u_thread_get_time_nano(pthread_self());
255 #else
256 return 0;
257 #endif
258 }
259
260 #endif /* OS_THREAD_H_ */