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