Understanding the FreeBSD locking mechanism

Ed Schouten ed at nuxi.nl
Thu Apr 6 09:31:54 UTC 2017


Hi Yubin,

2017-04-06 11:16 GMT+02:00 Yubin Ruan <ablacktshirt at gmail.com>:
> 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?

Locking a spinlock is done through macro mtx_lock_spin(), which
expands to __mtx_lock_spin() in sys/sys/mutex.h. That macro first
calls into the function you looked at, spinlock_enter(), to disable
interrupts. It then calls into the _mtx_obtain_lock_fetch() to do the
test-and-set operation you were looking for.

Best regards,
-- 
Ed Schouten <ed at nuxi.nl>
Nuxi, 's-Hertogenbosch, the Netherlands
KvK-nr.: 62051717


More information about the freebsd-hackers mailing list