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