Mercurial > sdl-ios-xcode
annotate docs/html/sdluserevent.html @ 3069:caefe2344f65
Date: Thu, 27 Dec 2007 07:38:25 +0000
From: John Bartholomew
Subject: [SDL] SDL Semaphore implementation broken on Windows?
Hi,
Over the past couple of days, I've been battling with SDL, SDL_Mixer and SMPEG to try to find an audio hang bug. I believe I've found the problem, which I think is a race condition inside SDL's semaphore implementation (at least the Windows implementation). The semaphore code uses Windows' built in semaphore functions, but it also maintains a separate count value. This count value is updated with bare increment and decrement operations in SemPost and SemWaitTimeout - no locking primitives to protect them.
In tracking down the apparent audio bug, I found that at some point a semaphore's count value was being decremented to -1, which is clearly not a valid value for it to take.
I'm still not certain exactly what sequence of operations is occuring for this to happen, but I believe that overall it's a race condition between a thread calling SemPost (which increments the count) and the thread on the other end calling SemWait (which decrements it).
I will try to make a test case to verify this, but I'm not sure if I'll be able to (threading errors being difficult to reproduce even in the best circumstances).
However, assuming this is the cause of my problems, there is a very
simple fix:
Windows provides InterlockedIncrement() and InterlockedDecrement()
functions to perform increments and decrements which are guaranteed to be atomic. So the fix is in thread/win32/SDL_syssem.c: replace occurrences of --sem->count with InterlockedDecrement(&sem->count); and replace occurrences of ++sem->count with InterlockedIncrement(&sem->count);
This is using SDL v1.2.12, built with VC++ 2008 Express, running on a
Core 2 duo processor.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 17 Feb 2009 05:39:18 +0000 |
parents | 355632dca928 |
children |
rev | line source |
---|---|
0 | 1 <HTML |
2 ><HEAD | |
3 ><TITLE | |
4 >SDL_UserEvent</TITLE | |
5 ><META | |
6 NAME="GENERATOR" | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
7 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+ |
0 | 8 "><LINK |
9 REL="HOME" | |
10 TITLE="SDL Library Documentation" | |
11 HREF="index.html"><LINK | |
12 REL="UP" | |
13 TITLE="SDL Event Structures." | |
14 HREF="eventstructures.html"><LINK | |
15 REL="PREVIOUS" | |
16 TITLE="SDL_SysWMEvent" | |
17 HREF="sdlsyswmevent.html"><LINK | |
18 REL="NEXT" | |
19 TITLE="SDL_QuitEvent" | |
20 HREF="sdlquitevent.html"></HEAD | |
21 ><BODY | |
22 CLASS="REFENTRY" | |
23 BGCOLOR="#FFF8DC" | |
24 TEXT="#000000" | |
25 LINK="#0000ee" | |
26 VLINK="#551a8b" | |
27 ALINK="#ff0000" | |
28 ><DIV | |
29 CLASS="NAVHEADER" | |
30 ><TABLE | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
31 SUMMARY="Header navigation table" |
0 | 32 WIDTH="100%" |
33 BORDER="0" | |
34 CELLPADDING="0" | |
35 CELLSPACING="0" | |
36 ><TR | |
37 ><TH | |
38 COLSPAN="3" | |
39 ALIGN="center" | |
40 >SDL Library Documentation</TH | |
41 ></TR | |
42 ><TR | |
43 ><TD | |
44 WIDTH="10%" | |
45 ALIGN="left" | |
46 VALIGN="bottom" | |
47 ><A | |
48 HREF="sdlsyswmevent.html" | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
49 ACCESSKEY="P" |
0 | 50 >Prev</A |
51 ></TD | |
52 ><TD | |
53 WIDTH="80%" | |
54 ALIGN="center" | |
55 VALIGN="bottom" | |
56 ></TD | |
57 ><TD | |
58 WIDTH="10%" | |
59 ALIGN="right" | |
60 VALIGN="bottom" | |
61 ><A | |
62 HREF="sdlquitevent.html" | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
63 ACCESSKEY="N" |
0 | 64 >Next</A |
65 ></TD | |
66 ></TR | |
67 ></TABLE | |
68 ><HR | |
69 ALIGN="LEFT" | |
70 WIDTH="100%"></DIV | |
71 ><H1 | |
72 ><A | |
73 NAME="SDLUSEREVENT" | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
74 ></A |
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
75 >SDL_UserEvent</H1 |
0 | 76 ><DIV |
77 CLASS="REFNAMEDIV" | |
78 ><A | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
79 NAME="AEN4565" |
0 | 80 ></A |
81 ><H2 | |
82 >Name</H2 | |
83 >SDL_UserEvent -- A user-defined event type</DIV | |
84 ><DIV | |
85 CLASS="REFSECT1" | |
86 ><A | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
87 NAME="AEN4568" |
0 | 88 ></A |
89 ><H2 | |
90 >Structure Definition</H2 | |
91 ><PRE | |
92 CLASS="PROGRAMLISTING" | |
93 >typedef struct{ | |
94 Uint8 type; | |
95 int code; | |
96 void *data1; | |
97 void *data2; | |
98 } SDL_UserEvent;</PRE | |
99 ></DIV | |
100 ><DIV | |
101 CLASS="REFSECT1" | |
102 ><A | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
103 NAME="AEN4571" |
0 | 104 ></A |
105 ><H2 | |
106 >Structure Data</H2 | |
107 ><DIV | |
108 CLASS="INFORMALTABLE" | |
109 ><A | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
110 NAME="AEN4573" |
0 | 111 ></A |
112 ><P | |
113 ></P | |
114 ><TABLE | |
115 BORDER="0" | |
116 CLASS="CALSTABLE" | |
117 ><TBODY | |
118 ><TR | |
119 ><TD | |
120 ALIGN="LEFT" | |
121 VALIGN="TOP" | |
122 ><TT | |
123 CLASS="STRUCTFIELD" | |
124 ><I | |
125 >type</I | |
126 ></TT | |
127 ></TD | |
128 ><TD | |
129 ALIGN="LEFT" | |
130 VALIGN="TOP" | |
131 ><TT | |
132 CLASS="LITERAL" | |
133 >SDL_USEREVENT</TT | |
134 > through to <TT | |
135 CLASS="LITERAL" | |
136 >SDL_NUMEVENTS-1</TT | |
137 ></TD | |
138 ></TR | |
139 ><TR | |
140 ><TD | |
141 ALIGN="LEFT" | |
142 VALIGN="TOP" | |
143 ><TT | |
144 CLASS="STRUCTFIELD" | |
145 ><I | |
146 >code</I | |
147 ></TT | |
148 ></TD | |
149 ><TD | |
150 ALIGN="LEFT" | |
151 VALIGN="TOP" | |
152 >User defined event code</TD | |
153 ></TR | |
154 ><TR | |
155 ><TD | |
156 ALIGN="LEFT" | |
157 VALIGN="TOP" | |
158 ><TT | |
159 CLASS="STRUCTFIELD" | |
160 ><I | |
161 >data1</I | |
162 ></TT | |
163 ></TD | |
164 ><TD | |
165 ALIGN="LEFT" | |
166 VALIGN="TOP" | |
167 >User defined data pointer</TD | |
168 ></TR | |
169 ><TR | |
170 ><TD | |
171 ALIGN="LEFT" | |
172 VALIGN="TOP" | |
173 ><TT | |
174 CLASS="STRUCTFIELD" | |
175 ><I | |
176 >data2</I | |
177 ></TT | |
178 ></TD | |
179 ><TD | |
180 ALIGN="LEFT" | |
181 VALIGN="TOP" | |
182 >User defined data pointer</TD | |
183 ></TR | |
184 ></TBODY | |
185 ></TABLE | |
186 ><P | |
187 ></P | |
188 ></DIV | |
189 ></DIV | |
190 ><DIV | |
191 CLASS="REFSECT1" | |
192 ><A | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
193 NAME="AEN4594" |
0 | 194 ></A |
195 ><H2 | |
196 >Description</H2 | |
197 ><P | |
198 ><SPAN | |
199 CLASS="STRUCTNAME" | |
200 >SDL_UserEvent</SPAN | |
201 > is in the <TT | |
202 CLASS="STRUCTFIELD" | |
203 ><I | |
204 >user</I | |
205 ></TT | |
206 > member of the structure <A | |
207 HREF="sdlevent.html" | |
208 ><SPAN | |
209 CLASS="STRUCTNAME" | |
210 >SDL_Event</SPAN | |
211 ></A | |
212 >. This event is unique, it is never created by SDL but only by the user. The event can be pushed onto the event queue using <A | |
213 HREF="sdlpushevent.html" | |
214 ><TT | |
215 CLASS="FUNCTION" | |
216 >SDL_PushEvent</TT | |
217 ></A | |
218 >. The contents of the structure members or completely up to the programmer, the only requirement is that <TT | |
219 CLASS="STRUCTFIELD" | |
220 ><I | |
221 >type</I | |
222 ></TT | |
223 > is a value from <TT | |
224 CLASS="LITERAL" | |
225 >SDL_USEREVENT</TT | |
226 > to <TT | |
227 CLASS="LITERAL" | |
228 >SDL_NUMEVENTS-1</TT | |
229 > (inclusive).</P | |
230 ></DIV | |
231 ><DIV | |
232 CLASS="REFSECT1" | |
233 ><A | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
234 NAME="AEN4606" |
0 | 235 ></A |
236 ><H2 | |
237 >Examples</H2 | |
238 ><P | |
239 ><PRE | |
240 CLASS="PROGRAMLISTING" | |
241 >SDL_Event event; | |
242 | |
243 event.type = SDL_USEREVENT; | |
244 event.user.code = my_event_code; | |
245 event.user.data1 = significant_data; | |
246 event.user.data2 = 0; | |
247 SDL_PushEvent(&event);</PRE | |
248 ></P | |
249 ></DIV | |
250 ><DIV | |
251 CLASS="REFSECT1" | |
252 ><A | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
253 NAME="AEN4610" |
0 | 254 ></A |
255 ><H2 | |
256 >See Also</H2 | |
257 ><P | |
258 ><A | |
259 HREF="sdlevent.html" | |
260 ><SPAN | |
261 CLASS="STRUCTNAME" | |
262 >SDL_Event</SPAN | |
263 ></A | |
264 >, | |
265 <A | |
266 HREF="sdlpushevent.html" | |
267 ><TT | |
268 CLASS="FUNCTION" | |
269 >SDL_PushEvent</TT | |
270 ></A | |
271 ></P | |
272 ></DIV | |
273 ><DIV | |
274 CLASS="NAVFOOTER" | |
275 ><HR | |
276 ALIGN="LEFT" | |
277 WIDTH="100%"><TABLE | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
278 SUMMARY="Footer navigation table" |
0 | 279 WIDTH="100%" |
280 BORDER="0" | |
281 CELLPADDING="0" | |
282 CELLSPACING="0" | |
283 ><TR | |
284 ><TD | |
285 WIDTH="33%" | |
286 ALIGN="left" | |
287 VALIGN="top" | |
288 ><A | |
289 HREF="sdlsyswmevent.html" | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
290 ACCESSKEY="P" |
0 | 291 >Prev</A |
292 ></TD | |
293 ><TD | |
294 WIDTH="34%" | |
295 ALIGN="center" | |
296 VALIGN="top" | |
297 ><A | |
298 HREF="index.html" | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
299 ACCESSKEY="H" |
0 | 300 >Home</A |
301 ></TD | |
302 ><TD | |
303 WIDTH="33%" | |
304 ALIGN="right" | |
305 VALIGN="top" | |
306 ><A | |
307 HREF="sdlquitevent.html" | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
308 ACCESSKEY="N" |
0 | 309 >Next</A |
310 ></TD | |
311 ></TR | |
312 ><TR | |
313 ><TD | |
314 WIDTH="33%" | |
315 ALIGN="left" | |
316 VALIGN="top" | |
317 >SDL_SysWMEvent</TD | |
318 ><TD | |
319 WIDTH="34%" | |
320 ALIGN="center" | |
321 VALIGN="top" | |
322 ><A | |
323 HREF="eventstructures.html" | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
324 ACCESSKEY="U" |
0 | 325 >Up</A |
326 ></TD | |
327 ><TD | |
328 WIDTH="33%" | |
329 ALIGN="right" | |
330 VALIGN="top" | |
331 >SDL_QuitEvent</TD | |
332 ></TR | |
333 ></TABLE | |
334 ></DIV | |
335 ></BODY | |
336 ></HTML | |
337 > |