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$ 28.\" 29.Dd January 9, 2008 30.Dt LOCK 9 31.Os 32.Sh NAME 33.Nm lockinit , 34.Nm lockdestroy , 35.Nm lockcount , 36.Nm lockmgr , 37.Nm lockstatus , 38.Nm lockmgr_disown , 39.Nm lockmgr_printinfo 40.Nd "lockmgr family of functions" 41.Sh SYNOPSIS 42.In sys/types.h 43.In sys/lockmgr.h 44.Ft void 45.Fn lockinit "struct lock *lkp" "int prio" "const char *wmesg" "int timo" "int flags" 46.Ft void 47.Fn lockdestroy "struct lock *lkp" 48.Ft int 49.Fn lockcount "struct lock *lkp" 50.Ft int 51.Fn lockmgr "struct lock *lkp" "u_int flags" "struct mtx *interlkp" "struct thread *td" 52.Ft int 53.Fn lockstatus "struct lock *lkp" "struct thread *td" 54.Ft void 55.Fn lockmgr_disown "struct lock *lkp" 56.Ft void 57.Fn lockmgr_printinfo "struct lock *lkp" 58.Sh DESCRIPTION 59The 60.Fn lockinit 61function is used to initialize a lock. 62It must be called before any operation can be performed on a lock. 63Its arguments are: 64.Bl -tag -width ".Fa wmesg" 65.It Fa lkp 66A pointer to the lock to initialize. 67.It Fa prio 68The priority passed to 69.Xr sleep 9 . 70.It Fa wmesg 71The lock message. 72This is used for both debugging output and 73.Xr sleep 9 . 74.It Fa timo 75The timeout value passed to 76.Xr sleep 9 . 77.It Fa flags 78The flags the lock is to be initialized with. 79.Bl -tag -width ".Dv LG_CANRECURSE" 80.It Dv LK_NOWAIT 81Do not sleep while acquiring the lock. 82.It Dv LK_SLEEPFAIL 83Fail after a sleep. 84.It Dv LK_CANRECURSE 85Allow recursive exclusive locks. 86.It Dv LK_NOSHARE 87Allow exclusive locks only. 88.It Dv LK_TIMELOCK 89Use 90.Fa timo 91during a sleep; otherwise, 0 is used. 92.El 93.El 94.Pp 95The 96.Fn lockdestroy 97function is used to destroy a lock, and while it is called in a number of 98places in the kernel, it currently does nothing. 99.Pp 100The 101.Fn lockcount 102function returns a count of the number of exclusive locks and shared locks 103held against the lock 104.Fa lkp . 105.Pp 106The 107.Fn lockmgr 108function handles general locking functionality within the kernel, including 109support for shared and exclusive locks, and recursion. 110.Fn lockmgr 111is also able to upgrade and downgrade locks. 112.Pp 113Its arguments are: 114.Bl -tag -width ".Fa interlkp" 115.It Fa lkp 116A pointer to the lock to manipulate. 117.It Fa flags 118Flags indicating what action is to be taken. 119.Bl -tag -width ".Dv LK_CANRECURSE" 120.It Dv LK_SHARED 121Acquire a shared lock. 122If an exclusive lock is currently held, it will be downgraded. 123.It Dv LK_EXCLUSIVE 124Acquire an exclusive lock. 125If an exclusive lock is already held, and 126.Dv LK_CANRECURSE 127is not set, the system will 128.Xr panic 9 . 129.It Dv LK_DOWNGRADE 130Downgrade exclusive lock to a shared lock. 131Downgrading a shared lock is not permitted. 132If an exclusive lock has been recursed, all references will be downgraded. 133.It Dv LK_UPGRADE 134Upgrade a shared lock to an exclusive lock. 135If this call fails, the shared lock is lost. 136During the upgrade, the shared lock could 137be temporarily dropped. 138Attempts to upgrade an exclusive lock will cause a 139.Xr panic 9 . 140.It Dv LK_RELEASE 141Release the lock. 142Releasing a lock that is not held can cause a 143.Xr panic 9 . 144.It Dv LK_DRAIN 145Wait for all activity on the lock to end, then mark it decommissioned. 146This is used before freeing a lock that is part of a piece of memory that is 147about to be freed. 148(As documented in 149.In sys/lockmgr.h . ) 150.It Dv LK_SLEEPFAIL 151Fail if operation has slept. 152.It Dv LK_NOWAIT 153Do not allow the call to sleep. 154This can be used to test the lock. 155.It Dv LK_CANRECURSE 156Allow recursion on an exclusive lock. 157For every lock there must be a release. 158.It Dv LK_INTERLOCK 159Unlock the interlock (which should be locked already). 160.El 161.It Fa interlkp 162An interlock mutex for controlling group access to the lock. 163If 164.Dv LK_INTERLOCK 165is specified, 166.Fn lockmgr 167assumes 168.Fa interlkp 169is currently owned and not recursed, and will return it unlocked. 170See 171.Xr mtx_assert 9 . 172.It Fa td 173A thread responsible for this call. 174.Dv NULL 175becomes 176.Dv LK_KERNPROC . 177.El 178.Pp 179The 180.Fn lockstatus 181function returns the status of the lock in relation to the 182.Vt thread 183passed to it. 184Note that if 185.Fa td 186is 187.Dv NULL 188and an exclusive lock is held, 189.Dv LK_EXCLUSIVE 190will be returned. 191.Pp 192The 193.Fn lockmgr_disown 194function switches the owner from curthread to be 195.Dv LK_KERNPROC, 196if the lock is already held. 197.Pp 198The 199.Fn lockmgr_printinfo 200function prints debugging information about the lock. 201It is used primarily by 202.Xr VOP_PRINT 9 203functions. 204.Sh RETURN VALUES 205The 206.Fn lockcount 207function returns an integer greater than or equal to zero. 208.Pp 209The 210.Fn lockmgr 211function returns 0 on success and non-zero on failure. 212.Pp 213The 214.Fn lockstatus 215function returns: 216.Bl -tag -width ".Dv LK_EXCLUSIVE" 217.It Dv LK_EXCLUSIVE 218An exclusive lock is held by the thread 219.Fa td . 220.It Dv LK_EXCLOTHER 221An exclusive lock is held by someone other than the thread 222.Fa td . 223.It Dv LK_SHARED 224A shared lock is held. 225.It Li 0 226The lock is not held by anyone. 227.El 228.Sh ERRORS 229.Fn lockmgr 230fails if: 231.Bl -tag -width Er 232.It Bq Er EBUSY 233.Dv LK_FORCEUPGRADE 234was requested and another thread had already requested a lock upgrade. 235.It Bq Er EBUSY 236.Dv LK_NOWAIT 237was set, and a sleep would have been required. 238.It Bq Er ENOLCK 239.Dv LK_SLEEPFAIL 240was set and 241.Fn lockmgr 242did sleep. 243.It Bq Er EINTR 244.Dv PCATCH 245was set in the lock priority, and a signal was delivered during a sleep. 246Note the 247.Er ERESTART 248error below. 249.It Bq Er ERESTART 250.Dv PCATCH 251was set in the lock priority, a signal was delivered during a sleep, 252and the system call is to be restarted. 253.It Bq Er EWOULDBLOCK 254a non-zero timeout was given, and the timeout expired. 255.El 256.Sh LOCKS 257If 258.Dv LK_INTERLOCK 259is passed in the 260.Fa flags 261argument to 262.Fn lockmgr , 263the 264.Fa interlkp 265must be held prior to calling 266.Fn lockmgr , 267and will be returned unlocked. 268.Pp 269Upgrade attempts that fail result in the loss of the lock that 270is currently held. 271Also, it is invalid to upgrade an 272exclusive lock, and a 273.Xr panic 9 274will be the result of trying. 275.Sh SEE ALSO 276.Xr condvar 9 , 277.Xr locking 9 , 278.Xr mutex 9 , 279.Xr rwlock 9 , 280.Xr sleep 9 , 281.Xr sx 9 , 282.Xr mtx_assert 9 , 283.Xr panic 9 , 284.Xr VOP_PRINT 9 285.Sh AUTHORS 286This manual page was written by 287.An Chad David Aq davidc@acns.ab.ca . 288