gallium: s/PIPE_OS_WINDOWS/PIPE_SUBSYSTEM_WINDOWS_USER/ in p_thread.
[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_SUBSYSTEM_WINDOWS_USER)
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
123 #define pipe_static_mutex(mutex) \
124 static pipe_mutex mutex = 0
125
126 #define pipe_mutex_init(mutex) \
127 (void) mutex
128
129 #define pipe_mutex_destroy(mutex) \
130 (void) mutex
131
132 #define pipe_mutex_lock(mutex) \
133 (void) mutex
134
135 #define pipe_mutex_unlock(mutex) \
136 (void) mutex
137
138 #define pipe_static_condvar(condvar) \
139 static unsigned condvar = 0
140
141 #define pipe_condvar_init(condvar) \
142 (void) condvar
143
144 #define pipe_condvar_destroy(condvar) \
145 (void) condvar
146
147 #define pipe_condvar_wait(condvar, mutex) \
148 (void) condvar
149
150 #define pipe_condvar_signal(condvar) \
151 (void) condvar
152
153 #define pipe_condvar_broadcast(condvar) \
154 (void) condvar
155
156
157 #endif /* PIPE_OS_? */
158
159
160
161 /*
162 * Thread-specific data.
163 */
164
165 typedef struct {
166 #if defined(PIPE_OS_LINUX)
167 pthread_key_t key;
168 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
169 DWORD key;
170 #endif
171 int initMagic;
172 } pipe_tsd;
173
174
175 #define PIPE_TSD_INIT_MAGIC 0xff8adc98
176
177
178 static INLINE void
179 pipe_tsd_init(pipe_tsd *tsd)
180 {
181 #if defined(PIPE_OS_LINUX)
182 if (pthread_key_create(&tsd->key, NULL/*free*/) != 0) {
183 perror("pthread_key_create(): failed to allocate key for thread specific data");
184 exit(-1);
185 }
186 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
187 assert(0);
188 #endif
189 tsd->initMagic = PIPE_TSD_INIT_MAGIC;
190 }
191
192 static INLINE void *
193 pipe_tsd_get(pipe_tsd *tsd)
194 {
195 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
196 pipe_tsd_init(tsd);
197 }
198 #if defined(PIPE_OS_LINUX)
199 return pthread_getspecific(tsd->key);
200 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
201 assert(0);
202 return NULL;
203 #else
204 assert(0);
205 return NULL;
206 #endif
207 }
208
209 static INLINE void
210 pipe_tsd_set(pipe_tsd *tsd, void *value)
211 {
212 if (tsd->initMagic != (int) PIPE_TSD_INIT_MAGIC) {
213 pipe_tsd_init(tsd);
214 }
215 #if defined(PIPE_OS_LINUX)
216 if (pthread_setspecific(tsd->key, value) != 0) {
217 perror("pthread_set_specific() failed");
218 exit(-1);
219 }
220 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
221 assert(0);
222 #else
223 assert(0);
224 #endif
225 }
226
227
228
229 #endif /* _P_THREAD2_H_ */