i965g: Fix after context transfers
[mesa/mesa-lb.git] / src / gallium / auxiliary / os / os_thread.h
bloba04df4106f2b2278a4864904ba836a039efc7c55
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
24 **************************************************************************/
27 /**
28 * @file
30 * Thread, mutex, condition variable, barrier, semaphore and
31 * thread-specific data functions.
35 #ifndef OS_THREAD_H_
36 #define OS_THREAD_H_
39 #include "pipe/p_compiler.h"
40 #include "util/u_debug.h" /* for assert */
43 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU)
45 #include <pthread.h> /* POSIX threads headers */
46 #include <stdio.h> /* for perror() */
48 #define PIPE_THREAD_HAVE_CONDVAR
50 /* pipe_thread
52 typedef pthread_t pipe_thread;
54 #define PIPE_THREAD_ROUTINE( name, param ) \
55 void *name( void *param )
57 static INLINE pipe_thread pipe_thread_create( void *(* routine)( void *), void *param )
59 pipe_thread thread;
60 if (pthread_create( &thread, NULL, routine, param ))
61 return 0;
62 return thread;
65 static INLINE int pipe_thread_wait( pipe_thread thread )
67 return pthread_join( thread, NULL );
70 static INLINE int pipe_thread_destroy( pipe_thread thread )
72 return pthread_detach( thread );
76 /* pipe_mutex
78 typedef pthread_mutex_t pipe_mutex;
80 #define pipe_static_mutex(mutex) \
81 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
83 #define pipe_mutex_init(mutex) \
84 (void) pthread_mutex_init(&(mutex), NULL)
86 #define pipe_mutex_destroy(mutex) \
87 pthread_mutex_destroy(&(mutex))
89 #define pipe_mutex_lock(mutex) \
90 (void) pthread_mutex_lock(&(mutex))
92 #define pipe_mutex_unlock(mutex) \
93 (void) pthread_mutex_unlock(&(mutex))
96 /* pipe_condvar
98 typedef pthread_cond_t pipe_condvar;
100 #define pipe_static_condvar(mutex) \
101 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
103 #define pipe_condvar_init(cond) \
104 pthread_cond_init(&(cond), NULL)
106 #define pipe_condvar_destroy(cond) \
107 pthread_cond_destroy(&(cond))
109 #define pipe_condvar_wait(cond, mutex) \
110 pthread_cond_wait(&(cond), &(mutex))
112 #define pipe_condvar_signal(cond) \
113 pthread_cond_signal(&(cond))
115 #define pipe_condvar_broadcast(cond) \
116 pthread_cond_broadcast(&(cond))
120 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
122 #include <windows.h>
124 /* pipe_thread
126 typedef HANDLE pipe_thread;
128 #define PIPE_THREAD_ROUTINE( name, param ) \
129 void * WINAPI name( void *param )
131 static INLINE pipe_thread pipe_thread_create( void *(WINAPI * routine)( void *), void *param )
133 DWORD id;
134 return CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) routine, param, 0, &id );
137 static INLINE int pipe_thread_wait( pipe_thread thread )
139 if (WaitForSingleObject( thread, INFINITE ) == WAIT_OBJECT_0)
140 return 0;
141 return -1;
144 static INLINE int pipe_thread_destroy( pipe_thread thread )
146 if (CloseHandle( thread ))
147 return 0;
148 return -1;
152 /* pipe_mutex
154 typedef CRITICAL_SECTION pipe_mutex;
156 #define pipe_static_mutex(mutex) \
157 /*static*/ pipe_mutex mutex = {0,0,0,0,0,0}
159 #define pipe_mutex_init(mutex) \
160 InitializeCriticalSection(&mutex)
162 #define pipe_mutex_destroy(mutex) \
163 DeleteCriticalSection(&mutex)
165 #define pipe_mutex_lock(mutex) \
166 EnterCriticalSection(&mutex)
168 #define pipe_mutex_unlock(mutex) \
169 LeaveCriticalSection(&mutex)
172 /* pipe_condvar (XXX FIX THIS)
174 typedef unsigned pipe_condvar;
176 #define pipe_condvar_init(cond) \
177 (void) cond
179 #define pipe_condvar_destroy(cond) \
180 (void) cond
182 #define pipe_condvar_wait(cond, mutex) \
183 (void) cond; (void) mutex
185 #define pipe_condvar_signal(cond) \
186 (void) cond
188 #define pipe_condvar_broadcast(cond) \
189 (void) cond
192 #else
194 /** Dummy definitions */
196 typedef unsigned pipe_thread;
198 #define PIPE_THREAD_ROUTINE( name, param ) \
199 void * name( void *param )
201 static INLINE pipe_thread pipe_thread_create( void *(* routine)( void *), void *param )
203 return 0;
206 static INLINE int pipe_thread_wait( pipe_thread thread )
208 return -1;
211 static INLINE int pipe_thread_destroy( pipe_thread thread )
213 return -1;
216 typedef unsigned pipe_mutex;
217 typedef unsigned pipe_condvar;
219 #define pipe_static_mutex(mutex) \
220 static pipe_mutex mutex = 0
222 #define pipe_mutex_init(mutex) \
223 (void) mutex
225 #define pipe_mutex_destroy(mutex) \
226 (void) mutex
228 #define pipe_mutex_lock(mutex) \
229 (void) mutex
231 #define pipe_mutex_unlock(mutex) \
232 (void) mutex
234 #define pipe_static_condvar(condvar) \
235 static unsigned condvar = 0
237 #define pipe_condvar_init(condvar) \
238 (void) condvar
240 #define pipe_condvar_destroy(condvar) \
241 (void) condvar
243 #define pipe_condvar_wait(condvar, mutex) \
244 (void) condvar
246 #define pipe_condvar_signal(condvar) \
247 (void) condvar
249 #define pipe_condvar_broadcast(condvar) \
250 (void) condvar
253 #endif /* PIPE_OS_? */
257 * pipe_barrier
260 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HAIKU)
262 typedef pthread_barrier_t pipe_barrier;
264 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
266 pthread_barrier_init(barrier, NULL, count);
269 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
271 pthread_barrier_destroy(barrier);
274 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
276 pthread_barrier_wait(barrier);
280 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
282 /* XXX FIX THIS */
283 typedef unsigned pipe_barrier;
285 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
287 /* XXX we could implement barriers with a mutex and condition var */
290 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
294 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
296 assert(0);
300 #else
302 typedef unsigned pipe_barrier;
304 static INLINE void pipe_barrier_init(pipe_barrier *barrier, unsigned count)
306 /* XXX we could implement barriers with a mutex and condition var */
307 assert(0);
310 static INLINE void pipe_barrier_destroy(pipe_barrier *barrier)
312 assert(0);
315 static INLINE void pipe_barrier_wait(pipe_barrier *barrier)
317 assert(0);
321 #endif
325 * Semaphores
328 typedef struct
330 pipe_mutex mutex;
331 pipe_condvar cond;
332 int counter;
333 } pipe_semaphore;
336 static INLINE void
337 pipe_semaphore_init(pipe_semaphore *sema, int init_val)
339 pipe_mutex_init(sema->mutex);
340 pipe_condvar_init(sema->cond);
341 sema->counter = init_val;
344 static INLINE void
345 pipe_semaphore_destroy(pipe_semaphore *sema)
347 pipe_mutex_destroy(sema->mutex);
348 pipe_condvar_destroy(sema->cond);
351 /** Signal/increment semaphore counter */
352 static INLINE void
353 pipe_semaphore_signal(pipe_semaphore *sema)
355 pipe_mutex_lock(sema->mutex);
356 sema->counter++;
357 pipe_condvar_signal(sema->cond);
358 pipe_mutex_unlock(sema->mutex);
361 /** Wait for semaphore counter to be greater than zero */
362 static INLINE void
363 pipe_semaphore_wait(pipe_semaphore *sema)
365 pipe_mutex_lock(sema->mutex);
366 while (sema->counter <= 0) {
367 pipe_condvar_wait(sema->cond, sema->mutex);
369 sema->counter--;
370 pipe_mutex_unlock(sema->mutex);
376 * Thread-specific data.
379 typedef struct {
380 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU)
381 pthread_key_t key;
382 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
383 DWORD key;
384 #endif
385 int initMagic;
386 } pipe_tsd;
389 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
392 static INLINE void
393 pipe_tsd_init(pipe_tsd *tsd)
395 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU)
396 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
397 perror("pthread_key_create(): failed to allocate key for thread specific data");
398 exit(-1);
400 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
401 assert(0);
402 #endif
403 tsd->initMagic = PIPE_TSD_INIT_MAGIC;
406 static INLINE void *
407 pipe_tsd_get(pipe_tsd *tsd)
409 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
410 pipe_tsd_init(tsd);
412 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU)
413 return pthread_getspecific(tsd->key);
414 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
415 assert(0);
416 return NULL;
417 #else
418 assert(0);
419 return NULL;
420 #endif
423 static INLINE void
424 pipe_tsd_set(pipe_tsd *tsd, void *value)
426 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
427 pipe_tsd_init(tsd);
429 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_APPLE) || defined(PIPE_OS_HAIKU)
430 if (pthread_setspecific(tsd->key, value) != 0) {
431 perror("pthread_set_specific() failed");
432 exit(-1);
434 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
435 assert(0);
436 #else
437 assert(0);
438 #endif
443 #endif /* OS_THREAD_H_ */