[SDL] SDL Semaphore implementation broken on Windows?

Brian brian.ripoff at gmail.com
Fri Dec 28 09:29:32 PST 2007


Atomic operations aren't enough on multi core systems with multiple
caches, I thought. Something to do with memory barriers, which I doubt
a compiler would mess with for a simple increment/decrement operation.
I don't pretend to be a multi processor guru though, but from what
little I have read I thought such unprotected updates were a bad idea.
Of course I could be wrong, and I apologise if so...

On Dec 28, 2007 3:22 AM, Sam Lantinga <slouken at devolution.com> wrote:
> > 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?



More information about the SDL mailing list