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 **************************************************************************/
28 * Thread, mutex, condition var and thread-specific data functions.
36 #include "pipe/p_compiler.h"
39 #if defined(PIPE_OS_LINUX)
41 #include <pthread.h> /* POSIX threads headers */
42 #include <stdio.h> /* for perror() */
44 typedef pthread_t pipe_thread
;
46 #define PIPE_THREAD_ROUTINE( name, param ) \
47 void *name( void *param )
49 static INLINE pipe_thread
pipe_thread_create( void *(* routine
)( void *), void *param
)
52 if (pthread_create( &thread
, NULL
, routine
, param
))
57 static INLINE
int pipe_thread_wait( pipe_thread thread
)
59 return pthread_join( thread
, NULL
);
62 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
64 return pthread_detach( thread
);
67 typedef pthread_mutex_t pipe_mutex
;
68 typedef pthread_cond_t pipe_condvar
;
70 #define pipe_static_mutex(mutex) \
71 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
73 #define pipe_mutex_init(mutex) \
74 pthread_mutex_init(&(mutex), NULL)
76 #define pipe_mutex_destroy(mutex) \
77 pthread_mutex_destroy(&(mutex))
79 #define pipe_mutex_lock(mutex) \
80 (void) pthread_mutex_lock(&(mutex))
82 #define pipe_mutex_unlock(mutex) \
83 (void) pthread_mutex_unlock(&(mutex))
85 #define pipe_static_condvar(mutex) \
86 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
88 #define pipe_condvar_init(cond) \
89 pthread_cond_init(&(cond), NULL)
91 #define pipe_condvar_destroy(cond) \
92 pthread_cond_destroy(&(cond))
94 #define pipe_condvar_wait(cond, mutex) \
95 pthread_cond_wait(&(cond), &(mutex))
97 #define pipe_condvar_signal(cond) \
98 pthread_cond_signal(&(cond))
100 #define pipe_condvar_broadcast(cond) \
101 pthread_cond_broadcast(&(cond))
104 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
108 typedef HANDLE pipe_thread
;
110 #define PIPE_THREAD_ROUTINE( name, param ) \
111 void * WINAPI name( void *param )
113 static INLINE pipe_thread
pipe_thread_create( void *(WINAPI
* routine
)( void *), void *param
)
116 return CreateThread( NULL
, 0, (LPTHREAD_START_ROUTINE
) routine
, param
, 0, &id
);
119 static INLINE
int pipe_thread_wait( pipe_thread thread
)
121 if (WaitForSingleObject( thread
, INFINITE
) == WAIT_OBJECT_0
)
126 static INLINE
int pipe_thread_destroy( pipe_thread thread
)
128 if (CloseHandle( thread
))
133 typedef CRITICAL_SECTION pipe_mutex
;
135 #define pipe_static_mutex(name) \
136 /*static*/ pipe_mutex name = {0,0,0,0,0,0}
138 #define pipe_mutex_init(name) \
139 InitializeCriticalSection(&name)
141 #define pipe_mutex_destroy(name) \
142 DeleteCriticalSection(&name)
144 #define pipe_mutex_lock(name) \
145 EnterCriticalSection(&name)
147 #define pipe_mutex_unlock(name) \
148 LeaveCriticalSection(&name)
150 /* XXX: dummy definitions, make it compile */
152 typedef unsigned pipe_condvar
;
154 #define pipe_condvar_init(condvar) \
157 #define pipe_condvar_broadcast(condvar) \
162 /** Dummy definitions */
164 typedef unsigned pipe_thread
;
165 typedef unsigned pipe_mutex
;
166 typedef unsigned pipe_condvar
;
168 #define pipe_static_mutex(mutex) \
169 static pipe_mutex mutex = 0
171 #define pipe_mutex_init(mutex) \
174 #define pipe_mutex_destroy(mutex) \
177 #define pipe_mutex_lock(mutex) \
180 #define pipe_mutex_unlock(mutex) \
183 #define pipe_static_condvar(condvar) \
184 static unsigned condvar = 0
186 #define pipe_condvar_init(condvar) \
189 #define pipe_condvar_destroy(condvar) \
192 #define pipe_condvar_wait(condvar, mutex) \
195 #define pipe_condvar_signal(condvar) \
198 #define pipe_condvar_broadcast(condvar) \
202 #endif /* PIPE_OS_? */
207 * Thread-specific data.
211 #if defined(PIPE_OS_LINUX)
213 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
220 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
224 pipe_tsd_init(pipe_tsd
*tsd
)
226 #if defined(PIPE_OS_LINUX)
227 if (pthread_key_create(&tsd
->key
, NULL
/*free*/) != 0) {
228 perror("pthread_key_create(): failed to allocate key for thread specific data");
231 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
234 tsd
->initMagic
= PIPE_TSD_INIT_MAGIC
;
238 pipe_tsd_get(pipe_tsd
*tsd
)
240 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
243 #if defined(PIPE_OS_LINUX)
244 return pthread_getspecific(tsd
->key
);
245 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
255 pipe_tsd_set(pipe_tsd
*tsd
, void *value
)
257 if (tsd
->initMagic
!= (int) PIPE_TSD_INIT_MAGIC
) {
260 #if defined(PIPE_OS_LINUX)
261 if (pthread_setspecific(tsd
->key
, value
) != 0) {
262 perror("pthread_set_specific() failed");
265 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
274 #endif /* _P_THREAD2_H_ */