Understanding the FreeBSD locking mechanism
Yubin Ruan
ablacktshirt at gmail.com
Sun Apr 2 08:30:38 UTC 2017
Hi,
I am reading the FreeBSD source code related to its locking mechanism. I
have done some researches but still cannot understand some codes.
Let's take `spinlock' for example. I know there are different kinds of
mutex in FreeBSD: spin mutex and other kinds of mutex. I try to locate
the source of spin mutex but what I find all look very weird to me. For
example, the `spinlock_enter()`
1816 void
1817 spinlock_enter(void)
1818 {
1819 struct thread *td;
1820 register_t flags;
1821
1822 td = curthread;
1823 if (td->td_md.md_spinlock_count == 0) {
1824 flags = intr_disable();
1825 td->td_md.md_spinlock_count = 1;
1826 td->td_md.md_saved_flags = flags;
1827 } else
1828 td->td_md.md_spinlock_count++;
1829 critical_enter();
1830 }
Does this function provides the ordinary "spinlock" functionality? There
is no special "test-and-set" instruction, and neither any extra locking
to protect internal data structure manipulation. Isn't this subjected to
race condition?
I also checked the `mtx_lock()`, but neither can't find a seemingly
correct implementation.
Do I miss anything? Which is the real implementation of the spin lock in
FreeBSD?
Thanks
Yubin Ruan
More information about the freebsd-questions
mailing list