Merge remote branch 'origin/lp-binning'
[mesa.git] / src / gallium / auxiliary / os / os_thread.h
1 /**************************************************************************
2 *
3 * Copyright 1999-2006 Brian Paul
4 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 **************************************************************************/
25
26
27 /**
28 * @file
29 *
30 * Thread, mutex, condition variable, barrier, semaphore and
31 * thread-specific data functions.
32 */
33
34
35 #ifndef OS_THREAD_H_
36 #define OS_THREAD_H_
37
38
39 #include "pipe/p_compiler.h"
40 #include "util/u_debug.h" /* for assert */
41
42
43 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU)
44
45 #include <pthread.h> /* POSIX threads headers */
46 #include <stdio.h> /* for perror() */
47
48 #define PIPE_THREAD_HAVE_CONDVAR
49
50 /* pipe_thread
51 */
52 typedef pthread_t pipe_thread;
53
54 #define PIPE_THREAD_ROUTINE( name, param ) \
55 void *name( void *param )
56
57 static INLINE pipe_thread pipe_thread_create( void *(* routine)( void *), void *param )
58 {
59 pipe_thread thread;
60 if (pthread_create( &thread, NULL, routine, param ))
61 return 0;
62 return thread;
63 }
64
65 static INLINE int pipe_thread_wait( pipe_thread thread )
66 {
67 return pthread_join( thread, NULL );
68 }
69
70 static INLINE int pipe_thread_destroy( pipe_thread thread )
71 {
72 return pthread_detach( thread );
73 }
74
75
76 /* pipe_mutex
77 */
78 typedef pthread_mutex_t pipe_mutex;
79
80 #define pipe_static_mutex(mutex) \
81 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
82
83 #define pipe_mutex_init(mutex) \
84 (void) pthread_mutex_init(&(mutex), NULL)
85
86 #define pipe_mutex_destroy(mutex) \
87 pthread_mutex_destroy(&(mutex))
88
89 #define pipe_mutex_lock(mutex) \
90 (void) pthread_mutex_lock(&(mutex))
91
92 #define pipe_mutex_unlock(mutex) \
93 (void) pthread_mutex_unlock(&(mutex))
94
95
96 /* pipe_condvar
97 */
98 typedef pthread_cond_t pipe_condvar;
99
100 #define pipe_static_condvar(mutex) \
101 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
102
103 #define pipe_condvar_init(cond) \
104 pthread_cond_init(&(cond), NULL)
105
106 #define pipe_condvar_destroy(cond) \
107 pthread_cond_destroy(&(cond))
108
109 #define pipe_condvar_wait(cond, mutex) \
110 pthread_cond_wait(&(cond), &(mutex))
111
112 #define pipe_condvar_signal(cond) \
113 pthread_cond_signal(&(cond))
114
115 #define pipe_condvar_broadcast(cond) \
116 pthread_cond_broadcast(&(cond))
117
118
119 /* pipe_barrier
120 */
121 typedef pthread_barrier_t pipe_barrier;
122
123 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
124 {
125 pthread_barrier_init(barrier, NULL, count);
126 }
127
128 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
129 {
130 pthread_barrier_destroy(barrier);
131 }
132
133 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
134 {
135 pthread_barrier_wait(barrier);
136 }
137
138
139 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
140
141 #include <windows.h>
142
143 /* pipe_thread
144 */
145 typedef HANDLE pipe_thread;
146
147 #define PIPE_THREAD_ROUTINE( name, param ) \
148 void * WINAPI name( void *param )
149
150 static INLINE pipe_thread pipe_thread_create( void *(WINAPI * routine)( void *), void *param )
151 {
152 DWORD id;
153 return CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) routine, param, 0, &id );
154 }
155
156 static INLINE int pipe_thread_wait( pipe_thread thread )
157 {
158 if (WaitForSingleObject( thread, INFINITE ) == WAIT_OBJECT_0)
159 return 0;
160 return -1;
161 }
162
163 static INLINE int pipe_thread_destroy( pipe_thread thread )
164 {
165 if (CloseHandle( thread ))
166 return 0;
167 return -1;
168 }
169
170
171 /* pipe_mutex
172 */
173 typedef CRITICAL_SECTION pipe_mutex;
174
175 #define pipe_static_mutex(mutex) \
176 /*static*/ pipe_mutex mutex = {0,0,0,0,0,0}
177
178 #define pipe_mutex_init(mutex) \
179 InitializeCriticalSection(&mutex)
180
181 #define pipe_mutex_destroy(mutex) \
182 DeleteCriticalSection(&mutex)
183
184 #define pipe_mutex_lock(mutex) \
185 EnterCriticalSection(&mutex)
186
187 #define pipe_mutex_unlock(mutex) \
188 LeaveCriticalSection(&mutex)
189
190
191 /* pipe_condvar (XXX FIX THIS)
192 */
193 typedef unsigned pipe_condvar;
194
195 #define pipe_condvar_init(cond) \
196 (void) cond
197
198 #define pipe_condvar_destroy(cond) \
199 (void) cond
200
201 #define pipe_condvar_wait(cond, mutex) \
202 (void) cond; (void) mutex
203
204 #define pipe_condvar_signal(cond) \
205 (void) cond
206
207 #define pipe_condvar_broadcast(cond) \
208 (void) cond
209
210
211 /* pipe_barrier (XXX FIX THIS)
212 */
213 typedef unsigned pipe_barrier;
214
215 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
216 {
217 /* XXX we could implement barriers with a mutex and condition var */
218 assert(0);
219 }
220
221 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
222 {
223 assert(0);
224 }
225
226 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
227 {
228 assert(0);
229 }
230
231
232
233 #else
234
235 /** Dummy definitions */
236
237 typedef unsigned pipe_thread;
238
239 #define PIPE_THREAD_ROUTINE( name, param ) \
240 void * name( void *param )
241
242 static INLINE pipe_thread pipe_thread_create( void *(* routine)( void *), void *param )
243 {
244 return 0;
245 }
246
247 static INLINE int pipe_thread_wait( pipe_thread thread )
248 {
249 return -1;
250 }
251
252 static INLINE int pipe_thread_destroy( pipe_thread thread )
253 {
254 return -1;
255 }
256
257 typedef unsigned pipe_mutex;
258 typedef unsigned pipe_condvar;
259 typedef unsigned pipe_barrier;
260
261 #define pipe_static_mutex(mutex) \
262 static pipe_mutex mutex = 0
263
264 #define pipe_mutex_init(mutex) \
265 (void) mutex
266
267 #define pipe_mutex_destroy(mutex) \
268 (void) mutex
269
270 #define pipe_mutex_lock(mutex) \
271 (void) mutex
272
273 #define pipe_mutex_unlock(mutex) \
274 (void) mutex
275
276 #define pipe_static_condvar(condvar) \
277 static unsigned condvar = 0
278
279 #define pipe_condvar_init(condvar) \
280 (void) condvar
281
282 #define pipe_condvar_destroy(condvar) \
283 (void) condvar
284
285 #define pipe_condvar_wait(condvar, mutex) \
286 (void) condvar
287
288 #define pipe_condvar_signal(condvar) \
289 (void) condvar
290
291 #define pipe_condvar_broadcast(condvar) \
292 (void) condvar
293
294
295 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
296 {
297 /* XXX we could implement barriers with a mutex and condition var */
298 assert(0);
299 }
300
301 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
302 {
303 assert(0);
304 }
305
306 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
307 {
308 assert(0);
309 }
310
311
312
313 #endif /* PIPE_OS_? */
314
315
316 /*
317 * Semaphores
318 */
319
320 typedef struct
321 {
322 pipe_mutex mutex;
323 pipe_condvar cond;
324 int counter;
325 } pipe_semaphore;
326
327
328 static INLINE void
329 pipe_semaphore_init(pipe_semaphore *sema, int init_val)
330 {
331 pipe_mutex_init(sema->mutex);
332 pipe_condvar_init(sema->cond);
333 sema->counter = init_val;
334 }
335
336 static INLINE void
337 pipe_semaphore_destroy(pipe_semaphore *sema)
338 {
339 pipe_mutex_destroy(sema->mutex);
340 pipe_condvar_destroy(sema->cond);
341 }
342
343 /** Signal/increment semaphore counter */
344 static INLINE void
345 pipe_semaphore_signal(pipe_semaphore *sema)
346 {
347 pipe_mutex_lock(sema->mutex);
348 sema->counter++;
349 pipe_condvar_signal(sema->cond);
350 pipe_mutex_unlock(sema->mutex);
351 }
352
353 /** Wait for semaphore counter to be greater than zero */
354 static INLINE void
355 pipe_semaphore_wait(pipe_semaphore *sema)
356 {
357 pipe_mutex_lock(sema->mutex);
358 while (sema->counter <= 0) {
359 pipe_condvar_wait(sema->cond, sema->mutex);
360 }
361 sema->counter--;
362 pipe_mutex_unlock(sema->mutex);
363 }
364
365
366
367 /*
368 * Thread-specific data.
369 */
370
371 typedef struct {
372 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU)
373 pthread_key_t key;
374 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
375 DWORD key;
376 #endif
377 int initMagic;
378 } pipe_tsd;
379
380
381 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
382
383
384 static INLINE void
385 pipe_tsd_init(pipe_tsd *tsd)
386 {
387 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU)
388 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
389 perror("pthread_key_create(): failed to allocate key for thread specific data");
390 exit(-1);
391 }
392 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
393 assert(0);
394 #endif
395 tsd->initMagic = PIPE_TSD_INIT_MAGIC;
396 }
397
398 static INLINE void *
399 pipe_tsd_get(pipe_tsd *tsd)
400 {
401 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
402 pipe_tsd_init(tsd);
403 }
404 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU)
405 return pthread_getspecific(tsd->key);
406 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
407 assert(0);
408 return NULL;
409 #else
410 assert(0);
411 return NULL;
412 #endif
413 }
414
415 static INLINE void
416 pipe_tsd_set(pipe_tsd *tsd, void *value)
417 {
418 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
419 pipe_tsd_init(tsd);
420 }
421 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU)
422 if (pthread_setspecific(tsd->key, value) != 0) {
423 perror("pthread_set_specific() failed");
424 exit(-1);
425 }
426 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
427 assert(0);
428 #else
429 assert(0);
430 #endif
431 }
432
433
434
435 #endif /* OS_THREAD_H_ */