git: kernel - Integrate the counter & 1 trick into the spinlock API

Matthew Dillon dillon at
Tue Mar 3 19:39:35 PST 2020

commit d033fb3299d77f66e925d39d1ad27828b3614651
Author: Matthew Dillon <dillon at>
Date:   Tue Mar 3 10:21:33 2020 -0800

    kernel - Integrate the counter & 1 trick into the spinlock API
    * The counter trick allows read accessors to sample a data structure
      without any further locks or ref-counts, as long as the data structure
      is free-safe.
      It does not necessarily protect pointers within the data structure so e.g.
      ref'ing some sub-structure via a data structure pointer is not safe
      on its own unless the sub-structure is able to provide some sort of
      additional guarantee.
    * Our struct spinlock has always been 8 bytes, but only uses 4 bytes for
      the lock.  Ipmlement the new API using the second field.
      Accessor side:
      Modifer side:
    * On the acessor side if spin_update_start() detects a change in-progress
      it will obtain a shared spin-lock, else remains unlocked.
      spin_update_end() tells the caller whether it must retry the operation,
      i.e. if a change occurred between start and end.  This can only happen
      if spin_update_start() remained unlocked.
      If the start did a shared lock then no changes are assumed to have occurred
      and spin_update_end() will release the shared spinlock and return 0.
    * On the modifier side, spin_lock_update() obtains an exclusive spinlock
      and increments the update counter, making it odd ((spin->update & 1) != 0).
      spin_unlock_update() increments the counter again, making it even but
      different, and releases the exclusive spinlock.

Summary of changes:
 sys/kern/kern_spinlock.c | 36 +++++++++++-----------
 sys/sys/spinlock.h       |  4 +--
 sys/sys/spinlock2.h      | 80 ++++++++++++++++++++++++++++++++++++++----------
 3 files changed, 84 insertions(+), 36 deletions(-)

DragonFly BSD source repository

More information about the Commits mailing list