On SDL_AtomicLock

Hello, everyone.

I have a question about implementation of spinlocks in SDL: why does
loop in SDL_AtomicLock contain SDL_Delay(0)? What are the use-cases,
when a thread would want to immediately yield control after the first
failure?

Best regards,
Oleg

Single CPU system yielding to other threads would be my first thought, but
unless that situation is detected, it probably isn’t intended.On Mon, Mar 28, 2011 at 8:22 AM, Oleg Andreev wrote:

Hello, everyone.

I have a question about implementation of spinlocks in SDL: why does loop
in SDL_AtomicLock contain SDL_Delay(0)? What are the use-cases, when a
thread would want to immediately yield control after the first failure?

Best regards,
Oleg


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I actually tried adding a few loops before yielding, and it reduced overall
performance of the atomic tests. You’re welcome to experiment with it a bit
and post your results.On Mon, Mar 28, 2011 at 6:22 AM, Oleg Andreev wrote:

Hello, everyone.

I have a question about implementation of spinlocks in SDL: why does loop
in SDL_AtomicLock contain SDL_Delay(0)? What are the use-cases, when a
thread would want to immediately yield control after the first failure?

Best regards,
Oleg


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


-Sam Lantinga, Founder and CEO, Galaxy Gameworks

You can’t generalize results like that. I can come up with test cases where
each performs better than the other. Spinning assumes the critical section
is extremely short (5-10 instructions) while immediate yielding means that
the critical section will be quite long.

In short, depending on HOW the locks are used, they can be better or worse
with spinning or yielding first. Mutexes already provide the performance of
spin+yield (usually they spin anyways), so true spinlocks (as in, spin only,
never yield) are generally rarely used unless the person REALLY knows what
they are doing.On Sat, Apr 2, 2011 at 2:13 PM, Sam Lantinga wrote:

I actually tried adding a few loops before yielding, and it reduced overall
performance of the atomic tests. You’re welcome to experiment with it a bit
and post your results.

On Mon, Mar 28, 2011 at 6:22 AM, Oleg Andreev wrote:

Hello, everyone.

I have a question about implementation of spinlocks in SDL: why does loop
in SDL_AtomicLock contain SDL_Delay(0)? What are the use-cases, when a
thread would want to immediately yield control after the first failure?

Best regards,
Oleg


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


-Sam Lantinga, Founder and CEO, Galaxy Gameworks


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org