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 typedef pthread_t pipe_thread
;
49 #define PIPE_THREAD_ROUTINE( name, param ) \
50 void *name( void *param )
52 static INLINE pipe_thread
pipe_thread_create( void *(* routine
)( void *), void *param
)
55 if (pthread_create( &thread
, NULL
, routine
, param
))
60 static INLINE
int pipe_thread_wait( pipe_thread thread
)
62 return pthread_join( thread
, NULL
);
65 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
67 return pthread_detach( thread
);
70 typedef pthread_mutex_t pipe_mutex
;
71 typedef pthread_cond_t pipe_condvar
;
73 #define pipe_static_mutex(mutex) \
74 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
76 #define pipe_mutex_init(mutex) \
77 (void) pthread_mutex_init(&(mutex), NULL)
79 #define pipe_mutex_destroy(mutex) \
80 pthread_mutex_destroy(&(mutex))
82 #define pipe_mutex_lock(mutex) \
83 (void) pthread_mutex_lock(&(mutex))
85 #define pipe_mutex_unlock(mutex) \
86 (void) pthread_mutex_unlock(&(mutex))
88 #define pipe_static_condvar(mutex) \
89 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
91 #define pipe_condvar_init(cond) \
92 pthread_cond_init(&(cond), NULL)
94 #define pipe_condvar_destroy(cond) \
95 pthread_cond_destroy(&(cond))
97 #define pipe_condvar_wait(cond, mutex) \
98 pthread_cond_wait(&(cond), &(mutex))
100 #define pipe_condvar_signal(cond) \
101 pthread_cond_signal(&(cond))
103 #define pipe_condvar_broadcast(cond) \
104 pthread_cond_broadcast(&(cond))
107 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
111 typedef HANDLE pipe_thread
;
113 #define PIPE_THREAD_ROUTINE( name, param ) \
114 void * WINAPI name( void *param )
116 static INLINE pipe_thread
pipe_thread_create( void *(WINAPI
* routine
)( void *), void *param
)
119 return CreateThread( NULL
, 0, (LPTHREAD_START_ROUTINE
) routine
, param
, 0, &id
);
122 static INLINE
int pipe_thread_wait( pipe_thread thread
)
124 if (WaitForSingleObject( thread
, INFINITE
) == WAIT_OBJECT_0
)
129 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
131 if (CloseHandle( thread
))
136 typedef CRITICAL_SECTION pipe_mutex
;
138 #define pipe_static_mutex(mutex) \
139 /*static*/ pipe_mutex mutex = {0,0,0,0,0,0}
141 #define pipe_mutex_init(mutex) \
142 InitializeCriticalSection(&mutex)
144 #define pipe_mutex_destroy(mutex) \
145 DeleteCriticalSection(&mutex)
147 #define pipe_mutex_lock(mutex) \
148 EnterCriticalSection(&mutex)
150 #define pipe_mutex_unlock(mutex) \
151 LeaveCriticalSection(&mutex)
153 /* XXX: dummy definitions, make it compile */
155 typedef unsigned pipe_condvar
;
157 #define pipe_condvar_init(condvar) \
160 #define pipe_condvar_broadcast(condvar) \
165 /** Dummy definitions */
167 typedef unsigned pipe_thread
;
168 typedef unsigned pipe_mutex
;
169 typedef unsigned pipe_condvar
;
171 #define pipe_static_mutex(mutex) \
172 static pipe_mutex mutex = 0
174 #define pipe_mutex_init(mutex) \
177 #define pipe_mutex_destroy(mutex) \
180 #define pipe_mutex_lock(mutex) \
183 #define pipe_mutex_unlock(mutex) \
186 #define pipe_static_condvar(condvar) \
187 static unsigned condvar = 0
189 #define pipe_condvar_init(condvar) \
192 #define pipe_condvar_destroy(condvar) \
195 #define pipe_condvar_wait(condvar, mutex) \
198 #define pipe_condvar_signal(condvar) \
201 #define pipe_condvar_broadcast(condvar) \
205 #endif /* PIPE_OS_? */
210 * Thread-specific data.
214 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
216 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
223 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
227 pipe_tsd_init(pipe_tsd
*tsd
)
229 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
230 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
231 perror("pthread_key_create(): failed to allocate key for thread specific data");
234 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
237 tsd
->initMagic
= PIPE_TSD_INIT_MAGIC
;
241 pipe_tsd_get(pipe_tsd
*tsd
)
243 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
246 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
247 return pthread_getspecific(tsd
->key
);
248 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
258 pipe_tsd_set(pipe_tsd
*tsd
, void *value
)
260 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
263 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
264 if (pthread_setspecific(tsd
->key
, value
) != 0) {
265 perror("pthread_set_specific() failed");
268 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
277 #endif /* _P_THREAD2_H_ */