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