Commit c11f6934 authored by Dylan Yudaken's avatar Dylan Yudaken

Do not cas on slowpath of SpinLock unnecessarily

This is a well known technique to reduce inter-CPU bus traffic while waiting for the lock by reducing the number of writes.
parent 0dba7841
Pipeline #29974 failed with stages
in 443 minutes and 15 seconds
......@@ -24,13 +24,35 @@
#if defined(THREADED_RTS)
#if defined(PROF_SPIN)
static ATTR_ALWAYS_INLINE StgWord try_acquire_spin_slow_path(SpinLock * p)
{
StgWord r;
r = cas((StgVolatilePtr)&(p->lock), 1, 0);
if (r == 0) RELAXED_ADD(&p->spin, 1);
return r;
}
#else /* !PROF_SPIN */
static ATTR_ALWAYS_INLINE StgWord try_acquire_spin_slow_path(SpinLock * p)
{
StgWord r;
r = RELAXED_LOAD(&p->lock);
if (r != 0) {
r = cas((StgVolatilePtr)&(p->lock), 1, 0);
}
return r;
}
#endif
void acquire_spin_lock_slow_path(SpinLock * p)
{
do {
for (uint32_t i = 0; i < SPIN_COUNT; i++) {
StgWord32 r = cas((StgVolatilePtr)&(p->lock), 1, 0);
if (r != 0) return;
IF_PROF_SPIN(RELAXED_ADD(&p->spin, 1));
if (try_acquire_spin_slow_path(p) != 0) return;
busy_wait_nop();
}
IF_PROF_SPIN(RELAXED_ADD(&p->yield, 1));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment