1 /**************************************************************************
3 * Copyright 1999-2006 Brian Paul
4 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 **************************************************************************/
30 * Thread, mutex, condition var and thread-specific data functions.
38 #include "pipe/p_compiler.h"
41 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
43 #include <pthread.h> /* POSIX threads headers */
44 #include <stdio.h> /* for perror() */
46 #define PIPE_THREAD_HAVE_CONDVAR
48 typedef pthread_t pipe_thread
;
50 #define PIPE_THREAD_ROUTINE( name, param ) \
51 void *name( void *param )
53 static INLINE pipe_thread
pipe_thread_create( void *(* routine
)( void *), void *param
)
56 if (pthread_create( &thread
, NULL
, routine
, param
))
61 static INLINE
int pipe_thread_wait( pipe_thread thread
)
63 return pthread_join( thread
, NULL
);
66 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
68 return pthread_detach( thread
);
71 typedef pthread_mutex_t pipe_mutex
;
72 typedef pthread_cond_t pipe_condvar
;
74 #define pipe_static_mutex(mutex) \
75 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
77 #define pipe_mutex_init(mutex) \
78 (void) pthread_mutex_init(&(mutex), NULL)
80 #define pipe_mutex_destroy(mutex) \
81 pthread_mutex_destroy(&(mutex))
83 #define pipe_mutex_lock(mutex) \
84 (void) pthread_mutex_lock(&(mutex))
86 #define pipe_mutex_unlock(mutex) \
87 (void) pthread_mutex_unlock(&(mutex))
89 #define pipe_static_condvar(mutex) \
90 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
92 #define pipe_condvar_init(cond) \
93 pthread_cond_init(&(cond), NULL)
95 #define pipe_condvar_destroy(cond) \
96 pthread_cond_destroy(&(cond))
98 #define pipe_condvar_wait(cond, mutex) \
99 pthread_cond_wait(&(cond), &(mutex))
101 #define pipe_condvar_signal(cond) \
102 pthread_cond_signal(&(cond))
104 #define pipe_condvar_broadcast(cond) \
105 pthread_cond_broadcast(&(cond))
108 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
112 typedef HANDLE pipe_thread
;
114 #define PIPE_THREAD_ROUTINE( name, param ) \
115 void * WINAPI name( void *param )
117 static INLINE pipe_thread
pipe_thread_create( void *(WINAPI
* routine
)( void *), void *param
)
120 return CreateThread( NULL
, 0, (LPTHREAD_START_ROUTINE
) routine
, param
, 0, &id
);
123 static INLINE
int pipe_thread_wait( pipe_thread thread
)
125 if (WaitForSingleObject( thread
, INFINITE
) == WAIT_OBJECT_0
)
130 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
132 if (CloseHandle( thread
))
137 typedef CRITICAL_SECTION pipe_mutex
;
139 #define pipe_static_mutex(mutex) \
140 /*static*/ pipe_mutex mutex = {0,0,0,0,0,0}
142 #define pipe_mutex_init(mutex) \
143 InitializeCriticalSection(&mutex)
145 #define pipe_mutex_destroy(mutex) \
146 DeleteCriticalSection(&mutex)
148 #define pipe_mutex_lock(mutex) \
149 EnterCriticalSection(&mutex)
151 #define pipe_mutex_unlock(mutex) \
152 LeaveCriticalSection(&mutex)
154 /* XXX: dummy definitions, make it compile */
156 typedef unsigned pipe_condvar
;
158 #define pipe_condvar_init(condvar) \
161 #define pipe_condvar_broadcast(condvar) \
166 /** Dummy definitions */
168 typedef unsigned pipe_thread
;
169 typedef unsigned pipe_mutex
;
170 typedef unsigned pipe_condvar
;
172 #define pipe_static_mutex(mutex) \
173 static pipe_mutex mutex = 0
175 #define pipe_mutex_init(mutex) \
178 #define pipe_mutex_destroy(mutex) \
181 #define pipe_mutex_lock(mutex) \
184 #define pipe_mutex_unlock(mutex) \
187 #define pipe_static_condvar(condvar) \
188 static unsigned condvar = 0
190 #define pipe_condvar_init(condvar) \
193 #define pipe_condvar_destroy(condvar) \
196 #define pipe_condvar_wait(condvar, mutex) \
199 #define pipe_condvar_signal(condvar) \
202 #define pipe_condvar_broadcast(condvar) \
206 #endif /* PIPE_OS_? */
211 * Thread-specific data.
215 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
217 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
224 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
228 pipe_tsd_init(pipe_tsd
*tsd
)
230 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
231 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
232 perror("pthread_key_create(): failed to allocate key for thread specific data");
235 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
238 tsd
->initMagic
= PIPE_TSD_INIT_MAGIC
;
242 pipe_tsd_get(pipe_tsd
*tsd
)
244 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
247 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
248 return pthread_getspecific(tsd
->key
);
249 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
259 pipe_tsd_set(pipe_tsd
*tsd
, void *value
)
261 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
264 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
265 if (pthread_setspecific(tsd
->key
, value
) != 0) {
266 perror("pthread_set_specific() failed");
269 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
278 #endif /* _P_THREAD2_H_ */