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