man.bsd.lv manual page server

Manual Page Search Parameters

LOCK(9) Kernel Developer's Manual LOCK(9)

lockinit, lockmgr, lockmgr_try, lockstatus, lockmgr_printinfo, lockownedlockmgr family of functions

#include <sys/types.h>
#include <sys/lock.h>

void
lockinit(struct lock *lkp, const char *wmesg, int timo, int flags);

void
lockuninit(struct lock *lkp);

int
lockmgr(struct lock *lkp, u_int flags);

boolean_t
lockmgr_try(struct lock *lkp, u_int flags);

int
lockstatus(struct lock *lkp, struct thread *td);

void
lockmgr_printinfo(struct lock *lkp);

int
lockowned(struct lock *lkp);

The () 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.
wmesg
The lock message. This is used for both debugging output and tsleep(9).
timo
The timeout value passed to tsleep(9).
flags
The flags the lock is to be initialized with.
Do not sleep while acquiring the lock.
Fail after a sleep.
Allow recursive exclusive locks.
Use timo during a sleep; otherwise, 0 is used.

The () function destroys a lock that was previously initialized using lockinit().

The () function handles general locking functionality within the kernel, including support for shared and exclusive locks, and recursion. lockmgr() is also able to upgrade and downgrade locks.

Its arguments are:

lkp
A pointer to the lock to manipulate.
flags
Flags indicating what action is to be taken.
Acquire a shared lock. If an exclusive lock is currently held, it will be downgraded.
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, all references will be downgraded.
Upgrade a shared lock to an exclusive lock. Fails with EBUSY if there is someone ahead of you in line waiting for an upgrade. If this call fails for any reason, the shared lock is lost. Attempts to upgrade an exclusive lock not already owned by the caller will cause a panic(9), but otherwise will always succeed. NOTE! When this operation succeeds, it guarantees that no other exclusive lock was able to acquire the lock ahead of you, but as indicated above, if it fails your current shared lock is lost.
Upgrade a shared lock to an exclusive lock. If this call fails for any reason, the shared lock is lost. Attempts to upgrade an exclusive lock not already owned by the caller will cause a panic(9), but otherwise will always succeed. WARNING! This operation can block with the current lock temporarily released, and other exclusive or shared lock holders can inject before the lock is acquired on your behalf.
Release the lock. Releasing a lock that is not held can cause a panic(9).
Fail if operation has slept.
Do not allow the call to sleep. This can be used to test the lock.
Allow recursion on an exclusive lock. For every lock there must be a release.

The () function is similar to lockmgr() but it will not sleep and returns TRUE if the lock was successfully obtained and FALSE if it was not.

The () function returns the status of the lock in relation to the thread passed to it. Note that if td is NULL and an exclusive lock is held, LK_EXCLUSIVE will be returned.

The () function prints debugging information about the lock. It is used primarily by VOP_PRINT(9) functions.

The () function is used to determine whether the calling thread owns a lock.

The lockmgr() function returns 0 on success and non-zero on failure.

The lockstatus() function returns:

An exclusive lock is held by the thread td.
An exclusive lock is held by someone other than the thread td.
A shared lock is held.
The lock is not held by anyone.

The lockowned() function returns a non-zero return value if the caller owns the lock shared or exclusive.

The lock manager itself is implemented within the file /sys/kern/kern_lock.c. Data structures and function prototypes for the lock manager are in /sys/sys/lock.h.

lockmgr() fails if:

[]
was set, and a sleep would have been required.
[]
was set and lockmgr() did sleep.
[]
was set in the lock priority, and a signal was delivered during a sleep. Note the error below.
[]
was set in the lock priority, a signal was delivered during a sleep, and the system call is to be restarted.
[]
a non-zero timeout was given, and the timeout expired.

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.

locking(9), panic(9), tsleep(9), VOP_PRINT(9)

The lock manager appeared in DragonFly 1.0.

The lock manager API first appeared in 4.4BSD-Lite2.

This man page was written by Chad David <davidc@acns.ab.ca>.

July 21, 2018 DragonFly-5.6.1