Mercurial > sdl-ios-xcode
annotate docs/html/sdlsemwaittimeout.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_SemWaitTimeout</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="Multi-threaded Programming" | |
14 HREF="thread.html"><LINK | |
15 REL="PREVIOUS" | |
16 TITLE="SDL_SemTryWait" | |
17 HREF="sdlsemtrywait.html"><LINK | |
18 REL="NEXT" | |
19 TITLE="SDL_SemPost" | |
20 HREF="sdlsempost.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="sdlsemtrywait.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="sdlsempost.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="SDLSEMWAITTIMEOUT" | |
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_SemWaitTimeout</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="AEN8120" |
0 | 80 ></A |
81 ><H2 | |
82 >Name</H2 | |
83 >SDL_SemWaitTimeout -- Lock a semaphore, but only wait up to a specified maximum time.</DIV | |
84 ><DIV | |
85 CLASS="REFSYNOPSISDIV" | |
86 ><A | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
87 NAME="AEN8123" |
0 | 88 ></A |
89 ><H2 | |
90 >Synopsis</H2 | |
91 ><DIV | |
92 CLASS="FUNCSYNOPSIS" | |
93 ><A | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
94 NAME="AEN8124" |
0 | 95 ></A |
96 ><P | |
97 ></P | |
98 ><PRE | |
99 CLASS="FUNCSYNOPSISINFO" | |
100 >#include "SDL.h" | |
101 #include "SDL_thread.h"</PRE | |
102 ><P | |
103 ><CODE | |
104 ><CODE | |
105 CLASS="FUNCDEF" | |
106 >int <B | |
107 CLASS="FSFUNC" | |
108 >SDL_SemWaitTimeout</B | |
109 ></CODE | |
110 >(SDL_sem *sem, Uint32 timeout);</CODE | |
111 ></P | |
112 ><P | |
113 ></P | |
114 ></DIV | |
115 ></DIV | |
116 ><DIV | |
117 CLASS="REFSECT1" | |
118 ><A | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
119 NAME="AEN8130" |
0 | 120 ></A |
121 ><H2 | |
122 >Description</H2 | |
123 ><P | |
124 ><TT | |
125 CLASS="FUNCTION" | |
126 >SDL_SemWaitTimeout()</TT | |
127 > is a varient of | |
128 <A | |
129 HREF="sdlsemwait.html" | |
130 >SDL_SemWait</A | |
131 > | |
132 with a maximum timeout value. | |
133 If the value of the semaphore pointed to by <TT | |
134 CLASS="PARAMETER" | |
135 ><I | |
136 >sem</I | |
137 ></TT | |
138 > is | |
139 positive (greater than zero) it will atomically decrement the semaphore value | |
140 and return 0, otherwise it will wait up to <TT | |
141 CLASS="PARAMETER" | |
142 ><I | |
143 >timeout</I | |
144 ></TT | |
145 > | |
146 milliseconds trying to lock the semaphore. This function is to be avoided if | |
147 possible since on some platforms it is implemented by polling the semaphore | |
148 every millisecond in a busy loop.</P | |
149 ><P | |
150 >After <TT | |
151 CLASS="FUNCTION" | |
152 >SDL_SemWaitTimeout()</TT | |
153 > is successful, the semaphore | |
154 can be released and its count atomically incremented by a successful call to | |
155 <A | |
156 HREF="sdlsempost.html" | |
157 >SDL_SemPost</A | |
158 >.</P | |
159 ></DIV | |
160 ><DIV | |
161 CLASS="REFSECT1" | |
162 ><A | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
163 NAME="AEN8140" |
0 | 164 ></A |
165 ><H2 | |
166 >Return Value</H2 | |
167 ><P | |
168 >Returns <SPAN | |
169 CLASS="RETURNVALUE" | |
170 >0</SPAN | |
171 > if the semaphore was successfully locked or | |
172 either <SPAN | |
173 CLASS="RETURNVALUE" | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
174 >SDL_MUTEX_TIMEDOUT</SPAN |
0 | 175 > or <SPAN |
176 CLASS="RETURNVALUE" | |
177 >-1</SPAN | |
178 > | |
179 if the timeout period was exceeded or there was an error, respectivly.</P | |
180 ><P | |
181 >If the semaphore was not successfully locked, the semaphore will be unchanged.</P | |
182 ></DIV | |
183 ><DIV | |
184 CLASS="REFSECT1" | |
185 ><A | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
186 NAME="AEN8147" |
0 | 187 ></A |
188 ><H2 | |
189 >Examples</H2 | |
190 ><P | |
191 ><PRE | |
192 CLASS="PROGRAMLISTING" | |
193 >res = SDL_SemWaitTimeout(my_sem, WAIT_TIMEOUT_MILLISEC); | |
194 | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
195 if (res == SDL_MUTEX_TIMEDOUT) { |
0 | 196 return TRY_AGAIN; |
197 } | |
198 if (res == -1) { | |
199 return WAIT_ERROR; | |
200 } | |
201 | |
202 ... | |
203 | |
204 SDL_SemPost(my_sem);</PRE | |
205 ></P | |
206 ></DIV | |
207 ><DIV | |
208 CLASS="REFSECT1" | |
209 ><A | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
210 NAME="AEN8151" |
0 | 211 ></A |
212 ><H2 | |
213 >See Also</H2 | |
214 ><P | |
215 ><A | |
216 HREF="sdlcreatesemaphore.html" | |
217 ><TT | |
218 CLASS="FUNCTION" | |
219 >SDL_CreateSemaphore</TT | |
220 ></A | |
221 >, | |
222 <A | |
223 HREF="sdldestroysemaphore.html" | |
224 ><TT | |
225 CLASS="FUNCTION" | |
226 >SDL_DestroySemaphore</TT | |
227 ></A | |
228 >, | |
229 <A | |
230 HREF="sdlsemwait.html" | |
231 ><TT | |
232 CLASS="FUNCTION" | |
233 >SDL_SemWait</TT | |
234 ></A | |
235 >, | |
236 <A | |
237 HREF="sdlsemtrywait.html" | |
238 ><TT | |
239 CLASS="FUNCTION" | |
240 >SDL_SemTryWait</TT | |
241 ></A | |
242 >, | |
243 <A | |
244 HREF="sdlsempost.html" | |
245 ><TT | |
246 CLASS="FUNCTION" | |
247 >SDL_SemPost</TT | |
248 ></A | |
249 >, | |
250 <A | |
251 HREF="sdlsemvalue.html" | |
252 ><TT | |
253 CLASS="FUNCTION" | |
254 >SDL_SemValue</TT | |
255 ></A | |
256 ></P | |
257 ></DIV | |
258 ><DIV | |
259 CLASS="NAVFOOTER" | |
260 ><HR | |
261 ALIGN="LEFT" | |
262 WIDTH="100%"><TABLE | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
263 SUMMARY="Footer navigation table" |
0 | 264 WIDTH="100%" |
265 BORDER="0" | |
266 CELLPADDING="0" | |
267 CELLSPACING="0" | |
268 ><TR | |
269 ><TD | |
270 WIDTH="33%" | |
271 ALIGN="left" | |
272 VALIGN="top" | |
273 ><A | |
274 HREF="sdlsemtrywait.html" | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
275 ACCESSKEY="P" |
0 | 276 >Prev</A |
277 ></TD | |
278 ><TD | |
279 WIDTH="34%" | |
280 ALIGN="center" | |
281 VALIGN="top" | |
282 ><A | |
283 HREF="index.html" | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
284 ACCESSKEY="H" |
0 | 285 >Home</A |
286 ></TD | |
287 ><TD | |
288 WIDTH="33%" | |
289 ALIGN="right" | |
290 VALIGN="top" | |
291 ><A | |
292 HREF="sdlsempost.html" | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
293 ACCESSKEY="N" |
0 | 294 >Next</A |
295 ></TD | |
296 ></TR | |
297 ><TR | |
298 ><TD | |
299 WIDTH="33%" | |
300 ALIGN="left" | |
301 VALIGN="top" | |
302 >SDL_SemTryWait</TD | |
303 ><TD | |
304 WIDTH="34%" | |
305 ALIGN="center" | |
306 VALIGN="top" | |
307 ><A | |
308 HREF="thread.html" | |
803
355632dca928
Updated SDL HTML documentation
Sam Lantinga <slouken@libsdl.org>
parents:
181
diff
changeset
|
309 ACCESSKEY="U" |
0 | 310 >Up</A |
311 ></TD | |
312 ><TD | |
313 WIDTH="33%" | |
314 ALIGN="right" | |
315 VALIGN="top" | |
316 >SDL_SemPost</TD | |
317 ></TR | |
318 ></TABLE | |
319 ></DIV | |
320 ></BODY | |
321 ></HTML | |
322 > |