LOCK(9) BSD Kernel Developer's Manual LOCK(9)NAME
lockinit, lockdestroy, lockmgr, lockmgr_args, lockmgr_args_rw,
lockmgr_disown, lockmgr_printinfo, lockmgr_recursed, lockmgr_rw,
lockmgr_waiters, lockstatus, lockmgr_assert — lockmgr family of functions
lockinit(struct lock *lkp, int prio, const char *wmesg, int timo,
lockdestroy(struct lock *lkp);
lockmgr(struct lock *lkp, u_int flags, struct mtx *ilk);
lockmgr_args(struct lock *lkp, u_int flags, struct mtx *ilk,
const char *wmesg, int prio, int timo);
lockmgr_args_rw(struct lock *lkp, u_int flags, struct rwlock *ilk,
const char *wmesg, int prio, int timo);
lockmgr_disown(struct lock *lkp);
lockmgr_printinfo(struct lock *lkp);
lockmgr_recursed(struct lock *lkp);
lockmgr_rw(struct lock *lkp, u_int flags, struct rwlock *ilk);
lockmgr_waiters(struct lock *lkp);
lockstatus(struct lock *lkp);
lockmgr_assert(struct lock *lkp, int what);
The lockinit() function is used to initialize a lock. It must be called
before any operation can be performed on a lock. Its arguments are:
lkp A pointer to the lock to initialize.
prio The priority passed to sleep(9).
wmesg The lock message. This is used for both debugging output and
timo The timeout value passed to sleep(9).
flags The flags the lock is to be initialized with:
LK_ADAPTIVE Enable adaptive spinning for this lock if the ker‐
nel is compiled with the ADAPTIVE_LOCKMGRS option.
LK_CANRECURSE Allow recursive exclusive locks.
LK_NOPROFILE Disable lock profiling for this lock.
LK_NOSHARE Allow exclusive locks only.
LK_NOWITNESS Instruct witness(4) to ignore this lock.
LK_NODUP witness(4) should log messages about duplicate
locks being acquired.
LK_QUIET Disable ktr(4) logging for this lock.
LK_TIMELOCK Use timo during a sleep; otherwise, 0 is used.
The lockdestroy() function is used to destroy a lock, and while it is
called in a number of places in the kernel, it currently does nothing.
The lockmgr() and lockmgr_rw() functions handle general locking function‐
ality within the kernel, including support for shared and exclusive
locks, and recursion. lockmgr() and lockmgr_rw() are also able to
upgrade and downgrade locks.
Their arguments are:
lkp A pointer to the lock to manipulate.
flags Flags indicating what action is to be taken.
LK_SHARED Acquire a shared lock. If an exclusive lock is
currently held, it will be downgraded.
LK_EXCLUSIVE Acquire an exclusive lock. If an exclusive lock is
already held, and LK_CANRECURSE is not set, the
system will panic(9).
LK_DOWNGRADE Downgrade exclusive lock to a shared lock. Down‐
grading a shared lock is not permitted. If an
exclusive lock has been recursed, all references
will be downgraded.
LK_UPGRADE Upgrade a shared lock to an exclusive lock. If
this call fails, the shared lock is lost. During
the upgrade, the shared lock could be temporarily
dropped. Attempts to upgrade an exclusive lock
will cause a panic(9).
LK_RELEASE Release the lock. Releasing a lock that is not
held can cause a panic(9).
LK_DRAIN Wait for all activity on the lock to end, then mark
it decommissioned. This is used before freeing a
lock that is part of a piece of memory that is
about to be freed. (As documented in
LK_SLEEPFAIL Fail if operation has slept.
LK_NOWAIT Do not allow the call to sleep. This can be used
to test the lock.
LK_NOWITNESS Skip the witness(4) checks for this instance.
LK_CANRECURSE Allow recursion on an exclusive lock. For every
lock there must be a release.
LK_INTERLOCK Unlock the interlock (which should be locked
ilk An interlock mutex for controlling group access to the lock. If
LK_INTERLOCK is specified, lockmgr() and lockmgr_rw() assume ilk
is currently owned and not recursed, and will return it unlocked.
The lockmgr_args() and lockmgr_args_rw() function work like lockmgr() and
lockmgr_rw() but accepting a wmesg, timo and prio on a per-instance
basis. The specified values will override the default ones, but this can
still be used passing, respectively, LK_WMESG_DEFAULT, LK_PRIO_DEFAULT
The lockmgr_disown() function switches the owner from the current thread
to be LK_KERNPROC, if the lock is already held.
The lockmgr_printinfo() function prints debugging information about the
lock. It is used primarily by VOP_PRINT(9) functions.
The lockmgr_recursed() function returns true if the lock is recursed, 0
The lockmgr_waiters() function returns true if the lock has waiters, 0
The lockstatus() function returns the status of the lock in relation to
the current thread.
When compiled with options INVARIANTS and options INVARIANT_SUPPORT, the
lockmgr_assert() function tests lkp for the assertions specified in what,
and panics if they are not met. One of the following assertions must be
KA_LOCKED Assert that the current thread has either a shared or an
exclusive lock on the lkp lock pointed to by the first argu‐
KA_SLOCKED Assert that the current thread has a shared lock on the lkp
lock pointed to by the first argument.
KA_XLOCKED Assert that the current thread has an exclusive lock on the
lkp lock pointed to by the first argument.
KA_UNLOCKED Assert that the current thread has no lock on the lkp lock
pointed to by the first argument.
In addition, one of the following optional assertions can be used with
either an KA_LOCKED, KA_SLOCKED, or KA_XLOCKED assertion:
KA_RECURSED Assert that the current thread has a recursed lock on
KA_NOTRECURSED Assert that the current thread does not have a recursed
lock on lkp.
The lockmgr() and lockmgr_rw() functions return 0 on success and non-zero
The lockstatus() function returns:
LK_EXCLUSIVE An exclusive lock is held by the current thread.
LK_EXCLOTHER An exclusive lock is held by someone other than the current
LK_SHARED A shared lock is held.
0 The lock is not held by anyone.
ERRORSlockmgr() and lockmgr_rw() fail if:
[EBUSY] LK_FORCEUPGRADE was requested and another thread had
already requested a lock upgrade.
[EBUSY] LK_NOWAIT was set, and a sleep would have been
[ENOLCK] LK_SLEEPFAIL was set and lockmgr() or lockmgr_rw() did
[EINTR] PCATCH was set in the lock priority, and a signal was
delivered during a sleep. Note the ERESTART error
[ERESTART] PCATCH was set in the lock priority, a signal was
delivered during a sleep, and the system call is to be
[EWOULDBLOCK] a non-zero timeout was given, and the timeout expired.
If LK_INTERLOCK is passed in the flags argument to lockmgr() or
lockmgr_rw(), the ilk must be held prior to calling lockmgr() or
lockmgr_rw(), and will be returned unlocked.
Upgrade attempts that fail result in the loss of the lock that is cur‐
rently held. Also, it is invalid to upgrade an exclusive lock, and a
panic(9) will be the result of trying.
SEE ALSOcondvar(9), locking(9), mutex(9), rwlock(9), sleep(9), sx(9),
mtx_assert(9), panic(9), VOP_PRINT(9)AUTHORS
This manual page was written by Chad David ⟨firstname.lastname@example.org⟩.
BSD June 16, 2009 BSD