Merge branch 'mesa_7_5_branch'
[mesa.git] / src / gallium / include / pipe / p_thread.h
1 /**************************************************************************
2 *
3 * Copyright 1999-2006 Brian Paul
4 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 *
24 **************************************************************************/
25
26
27 /**
28 * @file
29 *
30 * Thread, mutex, condition var and thread-specific data functions.
31 */
32
33
34 #ifndef _P_THREAD2_H_
35 #define _P_THREAD2_H_
36
37
38 #include "pipe/p_compiler.h"
39 #include "util/u_debug.h" /* for assert */
40
41
42 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
43
44 #include <pthread.h> /* POSIX threads headers */
45 #include <stdio.h> /* for perror() */
46
47 #define PIPE_THREAD_HAVE_CONDVAR
48
49 typedef pthread_t pipe_thread;
50
51 #define PIPE_THREAD_ROUTINE( name, param ) \
52 void *name( void *param )
53
54 static INLINE pipe_thread pipe_thread_create( void *(* routine)( void *), void *param )
55 {
56 pipe_thread thread;
57 if (pthread_create( &thread, NULL, routine, param ))
58 return 0;
59 return thread;
60 }
61
62 static INLINE int pipe_thread_wait( pipe_thread thread )
63 {
64 return pthread_join( thread, NULL );
65 }
66
67 static INLINE int pipe_thread_destroy( pipe_thread thread )
68 {
69 return pthread_detach( thread );
70 }
71
72 typedef pthread_mutex_t pipe_mutex;
73 typedef pthread_cond_t pipe_condvar;
74
75 #define pipe_static_mutex(mutex) \
76 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
77
78 #define pipe_mutex_init(mutex) \
79 (void) pthread_mutex_init(&(mutex), NULL)
80
81 #define pipe_mutex_destroy(mutex) \
82 pthread_mutex_destroy(&(mutex))
83
84 #define pipe_mutex_lock(mutex) \
85 (void) pthread_mutex_lock(&(mutex))
86
87 #define pipe_mutex_unlock(mutex) \
88 (void) pthread_mutex_unlock(&(mutex))
89
90 #define pipe_static_condvar(mutex) \
91 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
92
93 #define pipe_condvar_init(cond) \
94 pthread_cond_init(&(cond), NULL)
95
96 #define pipe_condvar_destroy(cond) \
97 pthread_cond_destroy(&(cond))
98
99 #define pipe_condvar_wait(cond, mutex) \
100 pthread_cond_wait(&(cond), &(mutex))
101
102 #define pipe_condvar_signal(cond) \
103 pthread_cond_signal(&(cond))
104
105 #define pipe_condvar_broadcast(cond) \
106 pthread_cond_broadcast(&(cond))
107
108
109 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
110
111 #include <windows.h>
112
113 typedef HANDLE pipe_thread;
114
115 #define PIPE_THREAD_ROUTINE( name, param ) \
116 void * WINAPI name( void *param )
117
118 static INLINE pipe_thread pipe_thread_create( void *(WINAPI * routine)( void *), void *param )
119 {
120 DWORD id;
121 return CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) routine, param, 0, &id );
122 }
123
124 static INLINE int pipe_thread_wait( pipe_thread thread )
125 {
126 if (WaitForSingleObject( thread, INFINITE ) == WAIT_OBJECT_0)
127 return 0;
128 return -1;
129 }
130
131 static INLINE int pipe_thread_destroy( pipe_thread thread )
132 {
133 if (CloseHandle( thread ))
134 return 0;
135 return -1;
136 }
137
138 typedef CRITICAL_SECTION pipe_mutex;
139
140 #define pipe_static_mutex(mutex) \
141 /*static*/ pipe_mutex mutex = {0,0,0,0,0,0}
142
143 #define pipe_mutex_init(mutex) \
144 InitializeCriticalSection(&mutex)
145
146 #define pipe_mutex_destroy(mutex) \
147 DeleteCriticalSection(&mutex)
148
149 #define pipe_mutex_lock(mutex) \
150 EnterCriticalSection(&mutex)
151
152 #define pipe_mutex_unlock(mutex) \
153 LeaveCriticalSection(&mutex)
154
155 /* XXX: dummy definitions, make it compile */
156
157 typedef unsigned pipe_condvar;
158
159 #define pipe_condvar_init(condvar) \
160 (void) condvar
161
162 #define pipe_condvar_broadcast(condvar) \
163 (void) condvar
164
165 #else
166
167 /** Dummy definitions */
168
169 typedef unsigned pipe_thread;
170 typedef unsigned pipe_mutex;
171 typedef unsigned pipe_condvar;
172
173 #define pipe_static_mutex(mutex) \
174 static pipe_mutex mutex = 0
175
176 #define pipe_mutex_init(mutex) \
177 (void) mutex
178
179 #define pipe_mutex_destroy(mutex) \
180 (void) mutex
181
182 #define pipe_mutex_lock(mutex) \
183 (void) mutex
184
185 #define pipe_mutex_unlock(mutex) \
186 (void) mutex
187
188 #define pipe_static_condvar(condvar) \
189 static unsigned condvar = 0
190
191 #define pipe_condvar_init(condvar) \
192 (void) condvar
193
194 #define pipe_condvar_destroy(condvar) \
195 (void) condvar
196
197 #define pipe_condvar_wait(condvar, mutex) \
198 (void) condvar
199
200 #define pipe_condvar_signal(condvar) \
201 (void) condvar
202
203 #define pipe_condvar_broadcast(condvar) \
204 (void) condvar
205
206
207 #endif /* PIPE_OS_? */
208
209
210
211 /*
212 * Thread-specific data.
213 */
214
215 typedef struct {
216 #if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD) || defined(PIPE_OS_SOLARIS)
217 pthread_key_t key;
218 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
219 DWORD key;
220 #endif
221 int initMagic;
222 } pipe_tsd;
223
224
225 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
226
227
228 static INLINE void
229 pipe_tsd_init(pipe_tsd *tsd)
230 {
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");
234 exit(-1);
235 }
236 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
237 assert(0);
238 #endif
239 tsd->initMagic = PIPE_TSD_INIT_MAGIC;
240 }
241
242 static INLINE void *
243 pipe_tsd_get(pipe_tsd *tsd)
244 {
245 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
246 pipe_tsd_init(tsd);
247 }
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)
251 assert(0);
252 return NULL;
253 #else
254 assert(0);
255 return NULL;
256 #endif
257 }
258
259 static INLINE void
260 pipe_tsd_set(pipe_tsd *tsd, void *value)
261 {
262 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
263 pipe_tsd_init(tsd);
264 }
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");
268 exit(-1);
269 }
270 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
271 assert(0);
272 #else
273 assert(0);
274 #endif
275 }
276
277
278
279 #endif /* _P_THREAD2_H_ */