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