1.\" 2.\" Copyright (C) 2002 Chad David <davidc@acns.ab.ca>. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice(s), this list of conditions and the following disclaimer as 9.\" the first lines of this file unmodified other than the possible 10.\" addition of one or more copyright notices. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice(s), this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 16.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18.\" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 19.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22.\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 25.\" DAMAGE. 26.\" 27.\" $FreeBSD: src/share/man/man9/lock.9,v 1.11 2003/09/08 19:57:21 ru Exp $ 28.\" 29.Dd January 19, 2014 30.Dt LOCK 9 31.Os 32.Sh NAME 33.Nm lockinit , 34.Nm lockcount , 35.Nm lockmgr , 36.Nm lockstatus , 37.Nm lockmgr_printinfo , 38.Nm lockowned 39.Nd "lockmgr family of functions" 40.Sh SYNOPSIS 41.In sys/types.h 42.In sys/lock.h 43.Ft void 44.Fn lockinit "struct lock *lkp" "const char *wmesg" "int timo" "int flags" 45.Ft void 46.Fn lockuninit "struct lock *lkp" 47.Ft int 48.Fn lockcount "struct lock *lkp" 49.Ft int 50.Fn lockcountnb "struct lock *lkp" 51.Ft int 52.Fn lockmgr "struct lock *lkp" "u_int flags" 53.Ft int 54.Fn lockstatus "struct lock *lkp" "struct thread *td" 55.Ft void 56.Fn lockmgr_printinfo "struct lock *lkp" 57.Ft int 58.Fn lockowned "struct lock *lkp" 59.Sh DESCRIPTION 60The 61.Fn lockinit 62function is used to initialize a lock. 63It must be called before any operation can be performed on a lock. 64Its arguments are: 65.Bl -tag -width ".Fa wmesg" 66.It Fa lkp 67A pointer to the lock to initialize. 68.It Fa wmesg 69The lock message. 70This is used for both debugging output and 71.Xr tsleep 9 . 72.It Fa timo 73The timeout value passed to 74.Xr tsleep 9 . 75.It Fa flags 76The flags the lock is to be initialized with. 77.Bl -tag -width ".Dv LK_CANRECURSE" 78.It Dv LK_NOWAIT 79Do not sleep while acquiring the lock. 80.It Dv LK_SLEEPFAIL 81Fail after a sleep. 82.It Dv LK_CANRECURSE 83Allow recursive exclusive locks. 84.It Dv LK_TIMELOCK 85Use 86.Fa timo 87during a sleep; otherwise, 0 is used. 88.El 89.El 90.Pp 91The 92.Fn lockuninit 93function destroys a lock that was previously initialized using 94.Fn lockinit . 95.Pp 96The 97.Fn lockcount 98function returns a count of the number of exclusive locks and shared locks 99held against the lock 100.Fa lkp . 101.Pp 102The 103.Fn lockcountnb 104function is a non-blocking counter-part of 105.Fn lockcount . 106which, can be safely used in assertion statements e.g. a 107.Xr KASSERT 9 . 108.Pp 109The 110.Fn lockmgr 111function handles general locking functionality within the kernel, including 112support for shared and exclusive locks, and recursion. 113.Fn lockmgr 114is also able to upgrade and downgrade locks. 115.Pp 116Its arguments are: 117.Bl -tag -width ".Fa flags" 118.It Fa lkp 119A pointer to the lock to manipulate. 120.It Fa flags 121Flags indicating what action is to be taken. 122.Bl -tag -width ".Dv LK_EXCLUPGRADE" 123.It Dv LK_SHARED 124Acquire a shared lock. 125If an exclusive lock is currently held, it will be downgraded. 126.It Dv LK_EXCLUSIVE 127Acquire an exclusive lock. 128If an exclusive lock is already held, and 129.Dv LK_CANRECURSE 130is not set, the system will 131.Xr panic 9 . 132.It Dv LK_DOWNGRADE 133Downgrade exclusive lock to a shared lock. 134Downgrading a shared lock is not permitted. 135If an exclusive lock has been recursed, all references will be downgraded. 136.It Dv LK_EXCLUPGRADE 137Upgrade a shared lock to an exclusive lock. 138Fails with 139.Er EBUSY 140if there is someone ahead of you in line waiting for an upgrade. 141If this call fails for any reason, the shared lock is lost. 142Attempts to upgrade an exclusive lock not already 143owned by the caller will cause a 144.Xr panic 9 , 145but otherwise will always succeed. 146NOTE! When this operation succeeds, it guarantees that no other 147exclusive lock was able to acquire the lock ahead of you, but 148as indicated above, if it fails your current shared lock is lost. 149.It Dv LK_UPGRADE 150Upgrade a shared lock to an exclusive lock. 151If this call fails for any reason, the shared lock is lost. 152Attempts to upgrade an exclusive lock not already 153owned by the caller will cause a 154.Xr panic 9 , 155but otherwise will always succeed. 156WARNING! This operation can block with the current lock temporarily 157released, and other exclusive or shared lock holders can inject before 158the lock is acquired on your behalf. 159.It Dv LK_RELEASE 160Release the lock. 161Releasing a lock that is not held can cause a 162.Xr panic 9 . 163.It Dv LK_SLEEPFAIL 164Fail if operation has slept. 165.It Dv LK_NOWAIT 166Do not allow the call to sleep. 167This can be used to test the lock. 168.It Dv LK_CANRECURSE 169Allow recursion on an exclusive lock. 170For every lock there must be a release. 171.El 172.El 173.Pp 174The 175.Fn lockstatus 176function returns the status of the lock in relation to the 177.Vt thread 178passed to it. 179Note that if 180.Fa td 181is 182.Dv NULL 183and an exclusive lock is held, 184.Dv LK_EXCLUSIVE 185will be returned. 186.Pp 187The 188.Fn lockmgr_printinfo 189function prints debugging information about the lock. 190It is used primarily by 191.Xr VOP_PRINT 9 192functions. 193.Pp 194The 195.Fn lockowned 196function is used to determine whether the calling thread owns a lock. 197.Sh RETURN VALUES 198The 199.Fn lockcount 200function returns an integer greater than or equal to zero. 201.Pp 202The 203.Fn lockmgr 204function returns 0 on success and non-zero on failure. 205.Pp 206The 207.Fn lockstatus 208function returns: 209.Bl -tag -width ".Dv LK_EXCLUSIVE" 210.It Dv LK_EXCLUSIVE 211An exclusive lock is held by the thread 212.Fa td . 213.It Dv LK_EXCLOTHER 214An exclusive lock is held by someone other than the thread 215.Fa td . 216.It Dv LK_SHARED 217A shared lock is held. 218.It Li 0 219The lock is not held by anyone. 220.El 221.Pp 222The 223.Fn lockowned 224function returns a non-zero return value if the caller owns the lock shared or exclusive. 225.Sh FILES 226The lock manager itself is implemented within the file 227.Pa /sys/kern/kern_lock.c . 228Data structures and function prototypes for the lock manager are in 229.Pa /sys/sys/lock.h . 230.Sh ERRORS 231.Fn lockmgr 232fails if: 233.Bl -tag -width Er 234.It Bq Er EBUSY 235.Dv LK_NOWAIT 236was set, and a sleep would have been required. 237.It Bq Er ENOLCK 238.Dv LK_SLEEPFAIL 239was set and 240.Fn lockmgr 241did sleep. 242.It Bq Er EINTR 243.Dv PCATCH 244was set in the lock priority, and a signal was delivered during a sleep. 245Note the 246.Er ERESTART 247error below. 248.It Bq Er ERESTART 249.Dv PCATCH 250was set in the lock priority, a signal was delivered during a sleep, 251and the system call is to be restarted. 252.It Bq Er EWOULDBLOCK 253a non-zero timeout was given, and the timeout expired. 254.El 255.Sh LOCKS 256Upgrade attempts that fail result in the loss of the lock that 257is currently held. 258Also, it is invalid to upgrade an 259exclusive lock, and a 260.Xr panic 9 261will be the result of trying. 262.Sh SEE ALSO 263.Xr locking 9 , 264.Xr panic 9 , 265.Xr tsleep 9 , 266.Xr VOP_PRINT 9 267.Sh HISTORY 268The lock manager appeared in 269.Dx 1.0 . 270.Pp 271The lock manager API first appeared in 272.Bx 4.4 lite2 . 273.Sh AUTHORS 274This man page was written by 275.An Chad David Aq Mt davidc@acns.ab.ca . 276