Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 6 Apr 2017 17:16:45 +0800
From:      Yubin Ruan <ablacktshirt@gmail.com>
To:        freebsd-hackers@freebsd.org
Subject:   Understanding the FreeBSD locking mechanism
Message-ID:  <e99b6366-7d30-a889-b7db-4a3b3133ff5e@gmail.com>

next in thread | raw e-mail | index | archive | help
Hi all freebsd hackers,
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?e99b6366-7d30-a889-b7db-4a3b3133ff5e>