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"
39 #include "util/u_debug.h" /* for assert */
42 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
44 #include <pthread.h> /* POSIX threads headers */
45 #include <stdio.h> /* for perror() */
47 #define PIPE_THREAD_HAVE_CONDVAR
49 typedef pthread_t pipe_thread
;
51 #define PIPE_THREAD_ROUTINE( name, param ) \
52 void *name( void *param )
54 static INLINE pipe_thread
pipe_thread_create( void *(* routine
)( void *), void *param
)
57 if (pthread_create( &thread
, NULL
, routine
, param
))
62 static INLINE
int pipe_thread_wait( pipe_thread thread
)
64 return pthread_join( thread
, NULL
);
67 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
69 return pthread_detach( thread
);
72 typedef pthread_mutex_t pipe_mutex
;
73 typedef pthread_cond_t pipe_condvar
;
75 #define pipe_static_mutex(mutex) \
76 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
78 #define pipe_mutex_init(mutex) \
79 (void) pthread_mutex_init(&(mutex), NULL)
81 #define pipe_mutex_destroy(mutex) \
82 pthread_mutex_destroy(&(mutex))
84 #define pipe_mutex_lock(mutex) \
85 (void) pthread_mutex_lock(&(mutex))
87 #define pipe_mutex_unlock(mutex) \
88 (void) pthread_mutex_unlock(&(mutex))
90 #define pipe_static_condvar(mutex) \
91 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
93 #define pipe_condvar_init(cond) \
94 pthread_cond_init(&(cond), NULL)
96 #define pipe_condvar_destroy(cond) \
97 pthread_cond_destroy(&(cond))
99 #define pipe_condvar_wait(cond, mutex) \
100 pthread_cond_wait(&(cond), &(mutex))
102 #define pipe_condvar_signal(cond) \
103 pthread_cond_signal(&(cond))
105 #define pipe_condvar_broadcast(cond) \
106 pthread_cond_broadcast(&(cond))
109 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
113 typedef HANDLE pipe_thread
;
115 #define PIPE_THREAD_ROUTINE( name, param ) \
116 void * WINAPI name( void *param )
118 static INLINE pipe_thread
pipe_thread_create( void *(WINAPI
* routine
)( void *), void *param
)
121 return CreateThread( NULL
, 0, (LPTHREAD_START_ROUTINE
) routine
, param
, 0, &id
);
124 static INLINE
int pipe_thread_wait( pipe_thread thread
)
126 if (WaitForSingleObject( thread
, INFINITE
) == WAIT_OBJECT_0
)
131 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
133 if (CloseHandle( thread
))
138 typedef CRITICAL_SECTION pipe_mutex
;
140 #define pipe_static_mutex(mutex) \
141 /*static*/ pipe_mutex mutex = {0,0,0,0,0,0}
143 #define pipe_mutex_init(mutex) \
144 InitializeCriticalSection(&mutex)
146 #define pipe_mutex_destroy(mutex) \
147 DeleteCriticalSection(&mutex)
149 #define pipe_mutex_lock(mutex) \
150 EnterCriticalSection(&mutex)
152 #define pipe_mutex_unlock(mutex) \
153 LeaveCriticalSection(&mutex)
155 /* XXX: dummy definitions, make it compile */
157 typedef unsigned pipe_condvar
;
159 #define pipe_condvar_init(condvar) \
162 #define pipe_condvar_broadcast(condvar) \
167 /** Dummy definitions */
169 typedef unsigned pipe_thread
;
170 typedef unsigned pipe_mutex
;
171 typedef unsigned pipe_condvar
;
173 #define pipe_static_mutex(mutex) \
174 static pipe_mutex mutex = 0
176 #define pipe_mutex_init(mutex) \
179 #define pipe_mutex_destroy(mutex) \
182 #define pipe_mutex_lock(mutex) \
185 #define pipe_mutex_unlock(mutex) \
188 #define pipe_static_condvar(condvar) \
189 static unsigned condvar = 0
191 #define pipe_condvar_init(condvar) \
194 #define pipe_condvar_destroy(condvar) \
197 #define pipe_condvar_wait(condvar, mutex) \
200 #define pipe_condvar_signal(condvar) \
203 #define pipe_condvar_broadcast(condvar) \
207 #endif /* PIPE_OS_? */
212 * Thread-specific data.
216 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
218 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
225 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
229 pipe_tsd_init(pipe_tsd
*tsd
)
231 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
232 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
233 perror("pthread_key_create(): failed to allocate key for thread specific data");
236 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
239 tsd
->initMagic
= PIPE_TSD_INIT_MAGIC
;
243 pipe_tsd_get(pipe_tsd
*tsd
)
245 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
248 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
249 return pthread_getspecific(tsd
->key
);
250 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
260 pipe_tsd_set(pipe_tsd
*tsd
, void *value
)
262 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
265 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
266 if (pthread_setspecific(tsd
->key
, value
) != 0) {
267 perror("pthread_set_specific() failed");
270 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
279 #endif /* _P_THREAD2_H_ */