• Sam Lantinga's avatar
    Date: Thu, 27 Dec 2007 07:38:25 +0000 · 605f1db1
    Sam Lantinga authored
    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.
    
    --HG--
    extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%403535
    605f1db1
Name
Last commit
Last update
VisualC Loading commit data...
VisualCE Loading commit data...
XCodeiPhoneOS Loading commit data...
Xcode Loading commit data...
build-scripts Loading commit data...
docs Loading commit data...
include Loading commit data...
src Loading commit data...
test Loading commit data...
.indent.pro Loading commit data...
BUGS Loading commit data...
Borland.html Loading commit data...
Borland.zip Loading commit data...
COPYING Loading commit data...
CREDITS Loading commit data...
INSTALL Loading commit data...
Makefile.dc Loading commit data...
Makefile.ds Loading commit data...
Makefile.in Loading commit data...
Makefile.minimal Loading commit data...
NOTES Loading commit data...
README Loading commit data...
README-SDL.txt Loading commit data...
README.BeOS Loading commit data...
README.CVS Loading commit data...
README.DC Loading commit data...
README.DirectFB Loading commit data...
README.MacOSX Loading commit data...
README.MiNT Loading commit data...
README.NanoX Loading commit data...
README.OS2 Loading commit data...
README.Porting Loading commit data...
README.QNX Loading commit data...
README.RISCOS Loading commit data...
README.SVN Loading commit data...
README.Watcom Loading commit data...
README.WinCE Loading commit data...
README.iphoneos Loading commit data...
README.wscons Loading commit data...
SDL.qpg.in Loading commit data...
SDL.spec.in Loading commit data...
TODO Loading commit data...
UNDER_CONSTRUCTION.txt Loading commit data...
VisualC.html Loading commit data...
Watcom-OS2.zip Loading commit data...
Watcom-Win32.zip Loading commit data...
WhatsNew Loading commit data...
acinclude.m4 Loading commit data...
autogen.sh Loading commit data...
configure.in Loading commit data...
docs.html Loading commit data...
sdl-config.in Loading commit data...
sdl.m4 Loading commit data...
sdl.pc.in Loading commit data...