[SDL] SDL Semaphore implementation broken on Windows?

William Petiot william at exoide.com
Fri Dec 28 15:11:49 PST 2007

Hi, here are my 2 cents,

x86 ASM  INC and DEC are only atomic on mono-x86, if they are used on 
aligned memory.

An alternative (very x86 specific) would be "__asm LOCK INC DWORD PTR 
provided that the memory is aligned on 32 bits boundary.

Anyway, one has to use the InterlockedXXXXX functions under Windows if 
he wants to provide guaranteed atomic operation on any system running 
Windows, ( XBox 360 included ) without assembly langage and cpu-specific 


Sam Lantinga a écrit :
>> 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);
> I'm pretty sure the 32-bit ++ and -- operators are atomic on x86 architectures.
> Are you sure this fixes the problem?
> Thanks!
> 	-Sam Lantinga, Lead Software Engineer, Blizzard Entertainment
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20071229/57e75c62/attachment-0008.htm>

More information about the SDL mailing list