An idea for spin_lock_contested()

Venkatesh Srinivas vsrinivas at dragonflybsd.org
Mon Oct 31 12:45:58 PDT 2011


Hi,

Currently spin_lock_quick() bumps td->td_critcount on entry, disabling
preemption throughout an entire spinlocked cycle. This makes sense --
the preempting thread might try to take a spinlock the preempted
thread held, which would lead to deadlock. It might be nice to allow
preemption while waiting for a spinlock, however.

Something like:
        --gd->gd_curthread->td_critcount;
        for (;;) {
                if (spin->counta == 1)
                        continue;

                ++gd->gd_curthread->td_critcount;
                cpu_ccfence();
                if (atomic_swap_int(&spin->counta, 1) == 0)
                        break;
                --gd->gd_curthread->td_critcount;

                if((++i & 0x7F) == 0x7F) {
                        ++spin->countb;
                        if (spin_indefinite_check(spin, &info))
                                break;
                }
        }

for spin_lock_contested() would allow preemption while waiting for a spinlock.

Preemption would still not happen if you held another spinlock, as
that lock bumped td_critcount for its entire held section, so
deadlocks against the preempted thread wouldn't happen.

Thoughts?

--vs;





More information about the Kernel mailing list