.\" .\" Copyright (C) 2002 Chad David . 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 form must reproduce the above copyright .\" notice(s), this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY .\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE .\" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES .\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER .\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH .\" DAMAGE. .\" .\" $FreeBSD: src/share/man/man9/lock.9,v 1.11 2003/09/08 19:57:21 ru Exp $ .\" .Dd January 25, 2024 .Dt LOCK 9 .Os .Sh NAME .Nm lockinit , .Nm lockmgr , .Nm lockmgr_try , .Nm lockstatus , .Nm lockmgr_printinfo , .Nm lockowned .Nd "lockmgr family of functions" .Sh SYNOPSIS .In sys/types.h .In sys/lock.h .Ft void .Fn lockinit "struct lock *lkp" "const char *wmesg" "int timo" "int flags" .Ft void .Fn lockuninit "struct lock *lkp" .Ft int .Fn lockmgr "struct lock *lkp" "u_int flags" .Ft boolean_t .Fn lockmgr_try "struct lock *lkp" "u_int flags" .Ft int .Fn lockstatus "struct lock *lkp" "struct thread *td" .Ft void .Fn lockmgr_printinfo "struct lock *lkp" .Ft int .Fn lockowned "struct lock *lkp" .Sh DESCRIPTION The .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: .Bl -tag -width ".Fa wmesg" .It Fa lkp A pointer to the lock to initialize. .It Fa wmesg The lock message. This is used for both debugging output and .Xr tsleep 9 . .It Fa timo The timeout value passed to .Xr tsleep 9 . .It Fa flags The flags the lock is to be initialized with. .Bl -tag -width ".Dv LK_CANRECURSE" .It Dv LK_NOWAIT Do not sleep while acquiring the lock. .It Dv LK_SLEEPFAIL Fail after a sleep. .It Dv LK_CANRECURSE Allow recursive exclusive locks. .It Dv LK_TIMELOCK Use .Fa timo during a sleep; otherwise, 0 is used. .El .El .Pp The .Fn lockuninit function destroys a lock that was previously initialized using .Fn lockinit . .Pp The .Fn lockmgr function handles general locking functionality within the kernel, including support for shared and exclusive locks, and recursion. .Fn lockmgr is also able to upgrade and downgrade locks. .Pp Its arguments are: .Bl -tag -width ".Fa flags" .It Fa lkp A pointer to the lock to manipulate. .It Fa flags Flags indicating what action is to be taken. .Bl -tag -width ".Dv LK_EXCLUPGRADE" .It Dv LK_SHARED Acquire a shared lock. If an exclusive lock is currently held, it will be downgraded. .It Dv LK_EXCLUSIVE Acquire an exclusive lock. If an exclusive lock is already held, and .Dv LK_CANRECURSE is not set, the system will .Xr panic 9 . .It Dv LK_DOWNGRADE 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. .It Dv LK_EXCLUPGRADE Upgrade a shared lock to an exclusive lock. Fails with .Er 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 .Xr 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. .It Dv LK_UPGRADE 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 .Xr 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. .It Dv LK_RELEASE Release the lock. Releasing a lock that is not held can cause a .Xr panic 9 . .It Dv LK_SLEEPFAIL Fail if operation has slept. .It Dv LK_NOWAIT Do not allow the call to sleep. This can be used to test the lock. .It Dv LK_CANRECURSE Allow recursion on an exclusive lock. For every lock there must be a release. .El .El .Pp The .Fn lockmgr_try function is similar to .Fn lockmgr but it will not sleep and returns .Dv TRUE if the lock was successfully obtained and .Dv FALSE if it was not. .Pp The .Fn lockstatus function returns the status of the lock in relation to the .Vt thread passed to it. Note that if .Fa td is .Dv NULL and an exclusive lock is held, .Dv LK_EXCLUSIVE will be returned. .Pp The .Fn lockmgr_printinfo function prints debugging information about the lock. It is used primarily by .Xr VOP_PRINT 9 functions. .Pp The .Fn lockowned function is used to determine whether the calling thread owns a lock. .Sh RETURN VALUES The .Fn lockmgr function returns 0 on success and non-zero on failure. .Pp The .Fn lockstatus function returns: .Bl -tag -width ".Dv LK_EXCLUSIVE" -offset indent .It Dv LK_EXCLUSIVE An exclusive lock is held by the thread .Fa td . .It Dv LK_EXCLOTHER An exclusive lock is held by someone other than the thread .Fa td . .It Dv LK_SHARED A shared lock is held. .It Li 0 The lock is not held by anyone. .El .Pp The .Fn lockowned function returns a non-zero value if the caller owns the lock shared or exclusive. .Sh FILES .Bl -tag -width /sys/kern/kern_lock.c -compact .It Pa /sys/kern/kern_lock.c the lock manager implementation .It Pa /sys/sys/lock.h data structures and function prototypes for the lock manager .El .Sh ERRORS .Fn lockmgr fails if: .Bl -tag -width Er .It Bq Er EBUSY .Dv LK_NOWAIT was set, and a sleep would have been required. .It Bq Er ENOLCK .Dv LK_SLEEPFAIL was set and .Fn lockmgr did sleep. .It Bq Er EINTR .Dv PCATCH was set in the lock priority, and a signal was delivered during a sleep. Note the .Er ERESTART error below. .It Bq Er ERESTART .Dv PCATCH was set in the lock priority, a signal was delivered during a sleep, and the system call is to be restarted. .It Bq Er EWOULDBLOCK a non-zero timeout was given, and the timeout expired. .El .Sh LOCKS 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 .Xr panic 9 will be the result of trying. .Sh SEE ALSO .Xr locking 9 , .Xr panic 9 , .Xr tsleep 9 , .Xr VOP_PRINT 9 .Sh HISTORY The lock manager appeared in .Dx 1.0 . .Pp The lock manager API first appeared in .Bx 4.4 lite2 . .Sh AUTHORS This man page was written by .An Chad David Aq Mt davidc@acns.ab.ca .