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 typedef pthread_t pipe_thread
;
48 #define PIPE_THREAD_ROUTINE( name, param ) \
49 void *name( void *param )
51 static INLINE pipe_thread
pipe_thread_create( void *(* routine
)( void *), void *param
)
54 if (pthread_create( &thread
, NULL
, routine
, param
))
59 static INLINE
int pipe_thread_wait( pipe_thread thread
)
61 return pthread_join( thread
, NULL
);
64 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
66 return pthread_detach( thread
);
69 typedef pthread_mutex_t pipe_mutex
;
70 typedef pthread_cond_t pipe_condvar
;
72 #define pipe_static_mutex(mutex) \
73 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
75 #define pipe_mutex_init(mutex) \
76 (void) pthread_mutex_init(&(mutex), NULL)
78 #define pipe_mutex_destroy(mutex) \
79 pthread_mutex_destroy(&(mutex))
81 #define pipe_mutex_lock(mutex) \
82 (void) pthread_mutex_lock(&(mutex))
84 #define pipe_mutex_unlock(mutex) \
85 (void) pthread_mutex_unlock(&(mutex))
87 #define pipe_static_condvar(mutex) \
88 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
90 #define pipe_condvar_init(cond) \
91 pthread_cond_init(&(cond), NULL)
93 #define pipe_condvar_destroy(cond) \
94 pthread_cond_destroy(&(cond))
96 #define pipe_condvar_wait(cond, mutex) \
97 pthread_cond_wait(&(cond), &(mutex))
99 #define pipe_condvar_signal(cond) \
100 pthread_cond_signal(&(cond))
102 #define pipe_condvar_broadcast(cond) \
103 pthread_cond_broadcast(&(cond))
106 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
110 typedef HANDLE pipe_thread
;
112 #define PIPE_THREAD_ROUTINE( name, param ) \
113 void * WINAPI name( void *param )
115 static INLINE pipe_thread
pipe_thread_create( void *(WINAPI
* routine
)( void *), void *param
)
118 return CreateThread( NULL
, 0, (LPTHREAD_START_ROUTINE
) routine
, param
, 0, &id
);
121 static INLINE
int pipe_thread_wait( pipe_thread thread
)
123 if (WaitForSingleObject( thread
, INFINITE
) == WAIT_OBJECT_0
)
128 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
130 if (CloseHandle( thread
))
135 typedef CRITICAL_SECTION pipe_mutex
;
137 #define pipe_static_mutex(mutex) \
138 /*static*/ pipe_mutex mutex = {0,0,0,0,0,0}
140 #define pipe_mutex_init(mutex) \
141 InitializeCriticalSection(&mutex)
143 #define pipe_mutex_destroy(mutex) \
144 DeleteCriticalSection(&mutex)
146 #define pipe_mutex_lock(mutex) \
147 EnterCriticalSection(&mutex)
149 #define pipe_mutex_unlock(mutex) \
150 LeaveCriticalSection(&mutex)
152 /* XXX: dummy definitions, make it compile */
154 typedef unsigned pipe_condvar
;
156 #define pipe_condvar_init(condvar) \
159 #define pipe_condvar_broadcast(condvar) \
164 /** Dummy definitions */
166 typedef unsigned pipe_thread
;
167 typedef unsigned pipe_mutex
;
168 typedef unsigned pipe_condvar
;
170 #define pipe_static_mutex(mutex) \
171 static pipe_mutex mutex = 0
173 #define pipe_mutex_init(mutex) \
176 #define pipe_mutex_destroy(mutex) \
179 #define pipe_mutex_lock(mutex) \
182 #define pipe_mutex_unlock(mutex) \
185 #define pipe_static_condvar(condvar) \
186 static unsigned condvar = 0
188 #define pipe_condvar_init(condvar) \
191 #define pipe_condvar_destroy(condvar) \
194 #define pipe_condvar_wait(condvar, mutex) \
197 #define pipe_condvar_signal(condvar) \
200 #define pipe_condvar_broadcast(condvar) \
204 #endif /* PIPE_OS_? */
209 * Thread-specific data.
213 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
215 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
222 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
226 pipe_tsd_init(pipe_tsd
*tsd
)
228 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
229 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
230 perror("pthread_key_create(): failed to allocate key for thread specific data");
233 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
236 tsd
->initMagic
= PIPE_TSD_INIT_MAGIC
;
240 pipe_tsd_get(pipe_tsd
*tsd
)
242 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
245 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
246 return pthread_getspecific(tsd
->key
);
247 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
257 pipe_tsd_set(pipe_tsd
*tsd
, void *value
)
259 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
262 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
263 if (pthread_setspecific(tsd
->key
, value
) != 0) {
264 perror("pthread_set_specific() failed");
267 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
276 #endif /* _P_THREAD2_H_ */