Copyright (C) 2002 Chad David <firstname.lastname@example.org>. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice(s), this list of conditions and the following disclaimer as the first lines of this file unmodified other than the possible addition of one or more copyright notices. 2. Redistributions in binary for...
NAMElockinit lockdestroy lockmgr lockmgr_args lockmgr_args_rw lockmgr_disown lockmgr_printinfo lockmgr_recursed lockmgr_rw lockmgr_waiters lockstatus lockmgr_assert - lockmgr family of functions
SYNOPSISIn sys/types.h In sys/lock.h In sys/lockmgr.h Ft void Fn lockinit struct lock *lkp int prio const char *wmesg int timo int flags Ft void Fn lockdestroy struct lock *lkp Ft int Fn lockmgr struct lock *lkp u_int flags struct mtx *ilk Ft int Fn lockmgr_args struct lock *lkp u_int flags struct mtx *ilk const char *wmesg int prio int timo Ft int Fn lockmgr_args_rw struct lock *lkp u_int flags struct rwlock *ilk const char *wmesg int prio int timo Ft void Fn lockmgr_disown struct lock *lkp Ft void Fn lockmgr_printinfo const struct lock *lkp Ft int Fn lockmgr_recursed const struct lock *lkp Ft int Fn lockmgr_rw struct lock *lkp u_int flags struct rwlock *ilk Ft int Fn lockmgr_waiters const struct lock *lkp Ft int Fn lockstatus const struct lock *lkp
options INVARIANTS options INVARIANT_SUPPORT Ft void Fn lockmgr_assert const struct lock *lkp int what
DESCRIPTIONThe Fn lockinit function is used to initialize a lock. It must be called before any operation can be performed on a lock. Its arguments are:
- Fa lkp
- A pointer to the lock to initialize.
- Fa prio
- The priority passed to sleep(9).
- Fa wmesg
- The lock message. This is used for both debugging output and sleep(9).
- Fa timo
- The timeout value passed to sleep(9).
- Fa flags
The flags the lock is to be initialized with:
- Enable adaptive spinning for this lock if the kernel is compiled with the ADAPTIVE_LOCKMGRS option.
- Allow recursive exclusive locks.
- Disable lock profiling for this lock.
- Allow exclusive locks only.
- Instruct witness(4) to ignore this lock.
- witness(4) should log messages about duplicate locks being acquired.
- Disable ktr(4) logging for this lock.
- Use Fa timo during a sleep; otherwise, 0 is used.
The Fn 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 Fn lockmgr and Fn lockmgr_rw functions handle general locking functionality within the kernel, including support for shared and exclusive locks, and recursion. Fn lockmgr and Fn lockmgr_rw are also able to upgrade and downgrade locks.
Their arguments are:
- Fa lkp
- A pointer to the lock to manipulate.
- Fa flags
Flags indicating what action is to be taken.
- Acquire a shared lock. If an exclusive lock is currently held, EDEADLK will be returned.
- Acquire an exclusive lock. If an exclusive lock is already held, and LK_CANRECURSE is not set, the system will panic(9).
- Downgrade exclusive lock to a shared lock. Downgrading a shared lock is not permitted. If an exclusive lock has been recursed, the system will panic(9).
- Upgrade a shared lock to an exclusive lock. If this call fails, the shared lock is lost, even if the LK_NOWAIT flag is specified. During the upgrade, the shared lock could be temporarily dropped. Attempts to upgrade an exclusive lock will cause a panic(9).
- Try to upgrade a shared lock to an exclusive lock. The failure to upgrade does not result in the dropping of the shared lock ownership.
- Release the lock. Releasing a lock that is not held can cause a panic(9).
- 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 In sys/lockmgr.h . )
- Fail if operation has slept.
- Do not allow the call to sleep. This can be used to test the lock.
- Skip the witness(4) checks for this instance.
- Allow recursion on an exclusive lock. For every lock there must be a release.
- Unlock the interlock (which should be locked already).
postpones serving further shared requests for shared-locked lock if there is
exclusive waiter, to avoid exclusive lock starvation.
But, if the thread requesting the shared lock already owns a shared lockmgr
lock, the request is granted even in presence of the parallel exclusive lock
request, which is done to avoid deadlocks with recursive shared acquisition.
The LK_NODDLKTREAT flag can only be used by code which requests shared non-recursive lock. The flag allows exclusive requests to preempt the current shared request even if the current thread owns shared locks. This is safe since shared lock is guaranteed to not recurse, and is used when thread is known to held unrelated shared locks, to not cause unnecessary starvation. An example is vp locking in VFS lookup(9), when dvp is already locked.
- Fa ilk
- An interlock mutex for controlling group access to the lock. If LK_INTERLOCK is specified, Fn lockmgr and Fn lockmgr_rw assume Fa ilk is currently owned and not recursed, and will return it unlocked. See mtx_assert9.
The Fn lockmgr_args and Fn lockmgr_args_rw function work like Fn lockmgr and Fn lockmgr_rw but accepting a Fa wmesg , Fa timo and Fa 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 and LK_TIMO_DEFAULT
The Fn lockmgr_disown function switches the owner from the current thread to be LK_KERNPROC if the lock is already held.
The Fn lockmgr_printinfo function prints debugging information about the lock. It is used primarily by VOP_PRINT9 functions.
The Fn lockmgr_recursed function returns true if the lock is recursed, 0 otherwise.
The Fn lockmgr_waiters function returns true if the lock has waiters, 0 otherwise.
The Fn lockstatus function returns the status of the lock in relation to the current thread.
When compiled with options INVARIANTS and options INVARIANT_SUPPORT the Fn lockmgr_assert function tests Fa lkp for the assertions specified in Fa what , and panics if they are not met. One of the following assertions must be specified:
- Assert that the current thread has either a shared or an exclusive lock on the Vt lkp lock pointed to by the first argument.
- Assert that the current thread has a shared lock on the Vt lkp lock pointed to by the first argument.
- Assert that the current thread has an exclusive lock on the Vt lkp lock pointed to by the first argument.
- Assert that the current thread has no lock on the Vt 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:
- Assert that the current thread has a recursed lock on Fa lkp .
- Assert that the current thread does not have a recursed lock on Fa lkp .
RETURN VALUESThe Fn lockmgr and Fn lockmgr_rw functions return 0 on success and non-zero on failure.
The Fn lockstatus function returns:
- An exclusive lock is held by the current thread.
- An exclusive lock is held by someone other than the current thread.
- A shared lock is held.
- The lock is not held by anyone.
ERRORSFn lockmgr and Fn lockmgr_rw fail if:
- Bq Er EBUSY
- LK_FORCEUPGRADE was requested and another thread had already requested a lock upgrade.
- Bq Er EBUSY
- LK_NOWAIT was set, and a sleep would have been required, or LK_TRYUPGRADE operation was not able to upgrade the lock.
- Bq Er ENOLCK
- LK_SLEEPFAIL was set and Fn lockmgr or Fn lockmgr_rw did sleep.
- Bq Er EINTR
- PCATCH was set in the lock priority, and a signal was delivered during a sleep. Note the Er ERESTART error below.
- Bq Er ERESTART
- PCATCH was set in the lock priority, a signal was delivered during a sleep, and the system call is to be restarted.
- Bq Er EWOULDBLOCK
- a non-zero timeout was given, and the timeout expired.
LOCKSIf LK_INTERLOCK is passed in the Fa flags argument to Fn lockmgr or Fn lockmgr_rw , the Fa ilk must be held prior to calling Fn lockmgr or Fn lockmgr_rw , and will be returned unlocked.
Upgrade attempts that fail result in the loss of the lock that is currently held. Also, it is invalid to upgrade an exclusive lock, and a panic(9) will be the result of trying.