gallium: Add dummy defines of pipe_condvar for Windows to make it compile.
[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
45 typedef pthread_t pipe_thread;
46 typedef pthread_mutex_t pipe_mutex;
47 typedef pthread_cond_t pipe_condvar;
48
49 #define pipe_static_mutex(mutex) \
50 static pipe_mutex mutex = PTHREAD_MUTEX_INITIALIZER
51
52 #define pipe_mutex_init(mutex) \
53 pthread_mutex_init(&(mutex), NULL)
54
55 #define pipe_mutex_destroy(mutex) \
56 pthread_mutex_destroy(&(mutex))
57
58 #define pipe_mutex_lock(mutex) \
59 (void) pthread_mutex_lock(&(mutex))
60
61 #define pipe_mutex_unlock(mutex) \
62 (void) pthread_mutex_unlock(&(mutex))
63
64 #define pipe_static_condvar(mutex) \
65 static pipe_condvar mutex = PTHREAD_COND_INITIALIZER
66
67 #define pipe_condvar_init(cond) \
68 pthread_cond_init(&(cond), NULL)
69
70 #define pipe_condvar_destroy(cond) \
71 pthread_cond_destroy(&(cond))
72
73 #define pipe_condvar_wait(cond, mutex) \
74 pthread_cond_wait(&(cond), &(mutex))
75
76 #define pipe_condvar_signal(cond) \
77 pthread_cond_signal(&(cond))
78
79 #define pipe_condvar_broadcast(cond) \
80 pthread_cond_broadcast(&(cond))
81
82
83 #elif defined(PIPE_OS_WINDOWS)
84
85 #include <windows.h>
86
87 typedef HANDLE pipe_thread;
88 typedef CRITICAL_SECTION pipe_mutex;
89
90 #define pipe_static_mutex(name) \
91 /*static*/ pipe_mutex name = {0,0,0,0,0,0}
92
93 #define pipe_mutex_init(name) \
94 InitializeCriticalSection(&name)
95
96 #define pipe_mutex_destroy(name) \
97 DeleteCriticalSection(&name)
98
99 #define pipe_mutex_lock(name) \
100 EnterCriticalSection(&name)
101
102 #define pipe_mutex_unlock(name) \
103 LeaveCriticalSection(&name)
104
105 /* XXX: dummy definitions, make it compile */
106
107 typedef unsigned pipe_condvar;
108
109 #define pipe_condvar_init(condvar) \
110 (void) condvar
111
112 #define pipe_condvar_broadcast(condvar) \
113 (void) condvar
114
115 #else
116
117 /** Dummy definitions */
118
119 typedef unsigned pipe_thread;
120 typedef unsigned pipe_mutex;
121 typedef unsigned pipe_condvar;
122 typedef unsigned pipe_tsd;
123
124 #define pipe_static_mutex(mutex) \
125 static pipe_mutex mutex = 0
126
127 #define pipe_mutex_init(mutex) \
128 (void) mutex
129
130 #define pipe_mutex_destroy(mutex) \
131 (void) mutex
132
133 #define pipe_mutex_lock(mutex) \
134 (void) mutex
135
136 #define pipe_mutex_unlock(mutex) \
137 (void) mutex
138
139 #define pipe_static_condvar(condvar) \
140 static _glthread_Cond condvar = 0
141
142 #define pipe_condvar_init(condvar) \
143 (void) condvar
144
145 #define pipe_condvar_destroy(condvar) \
146 (void) condvar
147
148 #define pipe_condvar_wait(condvar, mutex) \
149 (void) condvar
150
151 #define pipe_condvar_signal(condvar) \
152 (void) condvar
153
154 #define pipe_condvar_broadcast(condvar) \
155 (void) condvar
156
157
158 #endif /* PIPE_OS_? */
159
160
161
162 /*
163 * Thread-specific data.
164 */
165
166 typedef struct {
167 #if defined(PIPE_OS_LINUX)
168 pthread_key_t key;
169 #elif defined(PIPE_OS_WINDOWS)
170 DWORD key;
171 #endif
172 int initMagic;
173 } pipe_tsd;
174
175
176 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
177
178
179 static INLINE void
180 pipe_tsd_init(pipe_tsd *tsd)
181 {
182 #if defined(PIPE_OS_LINUX)
183 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
184 perror("pthread_key_create(): failed to allocate key for thread specific data");
185 exit(-1);
186 }
187 #elif defined(PIPE_OS_WINDOWS)
188 assert(0);
189 #endif
190 tsd->initMagic = PIPE_TSD_INIT_MAGIC;
191 }
192
193 static INLINE void *
194 pipe_tsd_get(pipe_tsd *tsd)
195 {
196 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
197 pipe_tsd_init(tsd);
198 }
199 #if defined(PIPE_OS_LINUX)
200 return pthread_getspecific(tsd->key);
201 #elif defined(PIPE_OS_WINDOWS)
202 assert(0);
203 return NULL;
204 #else
205 assert(0);
206 return NULL;
207 #endif
208 }
209
210 static INLINE void
211 pipe_tsd_set(pipe_tsd *tsd, void *value)
212 {
213 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
214 pipe_tsd_init(tsd);
215 }
216 #if defined(PIPE_OS_LINUX)
217 if (pthread_setspecific(tsd->key, value) != 0) {
218 perror("pthread_set_specific() failed");
219 exit(-1);
220 }
221 #elif defined(PIPE_OS_WINDOWS)
222 assert(0);
223 #else
224 assert(0);
225 #endif
226 }
227
228
229
230 #endif /* _P_THREAD2_H_ */