xref: /freebsd/share/man/man9/lock.9 (revision 571dba6ec9f25ecf7582dc2192daf1ceea70065f)
1e5e600c9SMark Murray.\"
25385e8bfSChad David.\" Copyright (C) 2002 Chad David <davidc@acns.ab.ca>. All rights reserved.
3e5e600c9SMark Murray.\"
4e5e600c9SMark Murray.\" Redistribution and use in source and binary forms, with or without
5e5e600c9SMark Murray.\" modification, are permitted provided that the following conditions
6e5e600c9SMark Murray.\" are met:
7e5e600c9SMark Murray.\" 1. Redistributions of source code must retain the above copyright
85385e8bfSChad David.\"    notice(s), this list of conditions and the following disclaimer as
95385e8bfSChad David.\"    the first lines of this file unmodified other than the possible
105385e8bfSChad David.\"    addition of one or more copyright notices.
11e5e600c9SMark Murray.\" 2. Redistributions in binary form must reproduce the above copyright
125385e8bfSChad David.\"    notice(s), this list of conditions and the following disclaimer in the
13e5e600c9SMark Murray.\"    documentation and/or other materials provided with the distribution.
14e5e600c9SMark Murray.\"
155385e8bfSChad David.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
165385e8bfSChad David.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
175385e8bfSChad David.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
185385e8bfSChad David.\" DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
195385e8bfSChad David.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
205385e8bfSChad David.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
215385e8bfSChad David.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
225385e8bfSChad David.\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
235385e8bfSChad David.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
245385e8bfSChad David.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
255385e8bfSChad David.\" DAMAGE.
26e5e600c9SMark Murray.\"
27e5e600c9SMark Murray.\" $FreeBSD$
28e5e600c9SMark Murray.\"
295385e8bfSChad David.Dd July 9, 2001
30e5e600c9SMark Murray.Dt LOCK 9
313d45e180SRuslan Ermilov.Os
32e5e600c9SMark Murray.Sh NAME
33e5e600c9SMark Murray.Nm lockinit ,
345385e8bfSChad David.Nm lockdestroy ,
35e5e600c9SMark Murray.Nm lockcount ,
365385e8bfSChad David.Nm lockmgr ,
375385e8bfSChad David.Nm lockstatus ,
385385e8bfSChad David.Nm lockmgr_printinfo
395385e8bfSChad David.Nd "lockmgr family of functions"
40e5e600c9SMark Murray.Sh SYNOPSIS
4132eef9aeSRuslan Ermilov.In sys/types.h
425385e8bfSChad David.In sys/lockmgr.h
43e5e600c9SMark Murray.Ft void
44c4a7ba6eSChad David.Fn lockinit "struct lock *lkp" "int prio" "const char *wmesg" "int timo" "int flags"
455385e8bfSChad David.Ft void
465385e8bfSChad David.Fn lockdestroy "struct lock *lkp"
47e5e600c9SMark Murray.Ft int
48e5e600c9SMark Murray.Fn lockcount "struct lock *lkp"
49e5e600c9SMark Murray.Ft int
505385e8bfSChad David.Fn lockmgr "struct lock *lkp" "u_int flags" "struct mtx *interlkp" "struct thread *td"
515385e8bfSChad David.Ft int
52f16b3c0dSChad David.Fn lockstatus "struct lock *lkp" "struct thread *td"
535385e8bfSChad David.Ft void
545385e8bfSChad David.Fn lockmgr_printinfo "struct lock *lkp"
55e5e600c9SMark Murray.Sh DESCRIPTION
565385e8bfSChad DavidThe
57e5e600c9SMark Murray.Fn lockinit
585385e8bfSChad Davidfunction is used to initialize a lock.
59ffa23792SChad DavidIt must be called before any operation can be performed on a lock.
605385e8bfSChad DavidIts arguments are:
615385e8bfSChad David.Bl -tag -width ".Fa wmesg"
625385e8bfSChad David.It Fa lkp
635385e8bfSChad DavidA pointer to the lock to initialize.
645385e8bfSChad David.It Fa prio
655385e8bfSChad DavidThe priority passed to
665385e8bfSChad David.Xr msleep 9 .
675385e8bfSChad David.It Fa wmesg
685385e8bfSChad DavidThe lock message.
695385e8bfSChad DavidThis is used for both debugging output and
705385e8bfSChad David.Xr msleep 9 .
715385e8bfSChad David.It Fa timo
725385e8bfSChad DavidThe timeout value passed to
735385e8bfSChad David.Xr msleep 9 .
745385e8bfSChad David.It Fa flags
755385e8bfSChad DavidThe flags the lock is to be initialized with.
765385e8bfSChad David.Bl -tag -width ".Dv LG_CANRECURSE"
775385e8bfSChad David.It Dv LK_NOWAIT
785385e8bfSChad DavidDo not sleep while acquiring the lock.
795385e8bfSChad David.It Dv LK_SLEEPFAIL
805385e8bfSChad DavidFail after a sleep.
815385e8bfSChad David.It Dv LK_CANRECURSE
825385e8bfSChad DavidAllow recursive exclusive locks.
835385e8bfSChad David.It Dv LK_REENABLE
845385e8bfSChad DavidRe-enable the lock after a drain.
855385e8bfSChad David.It Dv LK_NOPAUSE
865385e8bfSChad DavidDisable the spinlock while acquiring the lock.
875385e8bfSChad David.It Dv LK_TIMELOCK
885385e8bfSChad DavidUse
895385e8bfSChad David.Fa timo
905385e8bfSChad Davidduring a sleep; otherwise, 0 is used.
915385e8bfSChad David.El
925385e8bfSChad David.El
93e5e600c9SMark Murray.Pp
945385e8bfSChad DavidThe
955385e8bfSChad David.Fn lockdestroy
965385e8bfSChad Davidfunction is used to destroy a lock, and while it is called in a number of
975385e8bfSChad Davidplaces in the kernel, it currently does nothing.
985385e8bfSChad David.Pp
995385e8bfSChad DavidThe
1005385e8bfSChad David.Fn lockcount
1015385e8bfSChad Davidfunction returns a count of the number of exclusive locks and shared locks
1025385e8bfSChad Davidheld against the lock
1035385e8bfSChad David.Fa lkp .
1045385e8bfSChad David.Pp
1055385e8bfSChad DavidThe
1065385e8bfSChad David.Fn lockmgr
1075385e8bfSChad Davidfunction handles general locking functionality within the kernel, including
1085385e8bfSChad Davidsupport for shared and exclusive locks, and recursion.
1095385e8bfSChad David.Fn lockmgr
1105385e8bfSChad Davidis also able to upgrade and downgrade locks.
1115385e8bfSChad David.Pp
1125385e8bfSChad DavidIts arguments are:
1135385e8bfSChad David.Bl -tag -width ".Fa interlkp"
1145385e8bfSChad David.It Fa lkp
1155385e8bfSChad DavidA pointer to the lock to manipulate.
1165385e8bfSChad David.It Fa flags
1175385e8bfSChad DavidFlags indicating what action is to be taken.
1188271a102SRuslan Ermilov.Bl -tag -width ".Dv LK_EXCLUPGRADE"
119e5e600c9SMark Murray.It Dv LK_SHARED
1205385e8bfSChad DavidAcquire a shared lock.
1215385e8bfSChad DavidIf an exclusive lock is currently held, it will be downgraded.
122e5e600c9SMark Murray.It Dv LK_EXCLUSIVE
1235385e8bfSChad DavidAcquire an exclusive lock.
1245385e8bfSChad DavidIf an exclusive lock is already held, and
1258271a102SRuslan Ermilov.Dv LK_CANRECURSE
1265385e8bfSChad Davidis not set, the system will
1275385e8bfSChad David.Xr panic 9 .
128e5e600c9SMark Murray.It Dv LK_DOWNGRADE
1295385e8bfSChad DavidDowngrade exclusive lock to a shared lock.
1305385e8bfSChad DavidDowngrading a shared lock is not permitted.
1315385e8bfSChad DavidIf an exclusive lock has been recursed, all references will be downgraded.
1325385e8bfSChad David.It Dv LK_EXCLUPGRADE
1335385e8bfSChad DavidUpgrade a shared lock to an exclusive lock.
1345385e8bfSChad DavidFails with
1355385e8bfSChad David.Er EBUSY
136ffa23792SChad Davidif there is someone ahead of you in line waiting for an upgrade.
1375385e8bfSChad DavidIf this call fails, the shared lock is lost.
138ffa23792SChad DavidAttempts to upgrade an exclusive lock will cause a
1395385e8bfSChad David.Xr panic 9 .
1405385e8bfSChad David.It Dv LK_UPGRADE
1415385e8bfSChad DavidUpgrade a shared lock to an exclusive lock.
1425385e8bfSChad DavidIf this call fails, the shared lock is lost.
143ffa23792SChad DavidAttempts to upgrade an exclusive lock will cause a
1445385e8bfSChad David.Xr panic 9 .
145e5e600c9SMark Murray.It Dv LK_RELEASE
1465385e8bfSChad DavidRelease the lock.
1475385e8bfSChad DavidReleasing a lock that is not held can cause a
1485385e8bfSChad David.Xr panic 9 .
149e5e600c9SMark Murray.It Dv LK_DRAIN
1505385e8bfSChad DavidWait for all activity on the lock to end, then mark it decommissioned.
1515385e8bfSChad DavidThis is used before freeing a lock that is part of a piece of memory that is
1525385e8bfSChad Davidabout to be freed.
1535385e8bfSChad David(As documented in
154fe08efe6SRuslan Ermilov.In sys/lockmgr.h . )
1555385e8bfSChad David.It Dv LK_SLEEPFAIL
1565385e8bfSChad DavidFail if operation has slept.
1575385e8bfSChad David.It Dv LK_NOWAIT
1585385e8bfSChad DavidDo not allow the call to sleep.
1595385e8bfSChad DavidThis can be used to test the lock.
1605385e8bfSChad David.It Dv LK_CANRECURSE
1615385e8bfSChad DavidAllow recursion on an exclusive lock.
1625385e8bfSChad DavidFor every lock there must be a release.
1635385e8bfSChad David.It Dv LK_INTERLOCK
1645385e8bfSChad DavidUnlock the interlock (which should be locked already).
165e5e600c9SMark Murray.El
1665385e8bfSChad David.It Fa interlkp
1675385e8bfSChad DavidAn interlock mutex for controlling group access to the lock.
1685385e8bfSChad DavidIf
1695385e8bfSChad David.Dv LK_INTERLOCK
1705385e8bfSChad Davidis specified,
1715385e8bfSChad David.Fn lockmgr
1725385e8bfSChad Davidassumes
1735385e8bfSChad David.Fa interlkp
1745385e8bfSChad Davidis currently owned and not recursed, and will return it unlocked.
1755385e8bfSChad DavidSee
1765385e8bfSChad David.Xr mtx_assert 9 .
1775385e8bfSChad David.It Fa td
1785385e8bfSChad DavidA thread responsible for this call.
1795385e8bfSChad David.Dv NULL
1805385e8bfSChad Davidbecomes
1815385e8bfSChad David.Dv LK_KERNPROC .
1825385e8bfSChad David.El
1835385e8bfSChad David.Pp
1845385e8bfSChad DavidThe
1855385e8bfSChad David.Fn lockstatus
1865385e8bfSChad Davidfunction returns the status of the lock in relation to the
1875385e8bfSChad David.Vt thread
1885385e8bfSChad Davidpassed to it.
1895385e8bfSChad DavidNote that if
1905385e8bfSChad David.Fa td
1915385e8bfSChad Davidis
1925385e8bfSChad David.Dv NULL
1935385e8bfSChad Davidand an exclusive lock is held,
1945385e8bfSChad David.Dv LK_EXCLUSIVE
1955385e8bfSChad Davidwill be returned.
1965385e8bfSChad David.Pp
1975385e8bfSChad DavidThe
1985385e8bfSChad David.Fn lockmgr_printinfo
1995385e8bfSChad Davidfunction prints debugging information about the lock.
2005385e8bfSChad DavidIt is used primarily by
2015385e8bfSChad David.Xr VOP_PRINT 9
2025385e8bfSChad Davidfunctions.
203e5e600c9SMark Murray.Sh RETURN VALUES
2045385e8bfSChad DavidThe
2055385e8bfSChad David.Fn lockcount
2065385e8bfSChad Davidfunction returns an integer greater than or equal to zero.
2075385e8bfSChad David.Pp
2085385e8bfSChad DavidThe
2095385e8bfSChad David.Fn lockmgr
2105385e8bfSChad Davidfunction returns 0 on success and non-zero on failure.
2115385e8bfSChad David.Pp
2125385e8bfSChad DavidThe
2135385e8bfSChad David.Fn lockstatus
2145385e8bfSChad Davidfunction returns:
2155385e8bfSChad David.Bl -tag -width ".Dv LK_EXCLUSIVE"
2165385e8bfSChad David.It Dv LK_EXCLUSIVE
2175385e8bfSChad DavidAn exclusive lock is held by the thread
2185385e8bfSChad David.Fa td .
2195385e8bfSChad David.It Dv LK_EXCLOTHER
2205385e8bfSChad DavidAn exclusive lock is held by someone other than the thread
2215385e8bfSChad David.Fa td .
2225385e8bfSChad David.It Dv LK_SHARED
2235385e8bfSChad DavidA shared lock is held.
2245385e8bfSChad David.It Li 0
2255385e8bfSChad DavidThe lock is not held by anyone.
2265385e8bfSChad David.El
2275385e8bfSChad David.Sh ERRORS
2285385e8bfSChad David.Fn lockmgr
2295385e8bfSChad Davidfails if:
2305385e8bfSChad David.Bl -tag -width Er
2315385e8bfSChad David.It Bq Er EBUSY
2328271a102SRuslan Ermilov.Dv LK_FORCEUPGRADE
2335385e8bfSChad Davidwas requested and another thread had already requested a lock upgrade.
2345385e8bfSChad David.It Bq Er EBUSY
2355385e8bfSChad David.Dv LK_NOWAIT
2365385e8bfSChad Davidwas set, and a sleep would have been required.
2375385e8bfSChad David.It Bq Er ENOLCK
2388271a102SRuslan Ermilov.Dv LK_SLEEPFAIL
2395385e8bfSChad Davidwas set and
2405385e8bfSChad David.Fn lockmgr
2415385e8bfSChad Daviddid sleep.
2425385e8bfSChad David.It Bq Er EINTR
2438271a102SRuslan Ermilov.Dv PCATCH
2445385e8bfSChad Davidwas set in the lock priority, and a signal was delivered during a sleep.
2455385e8bfSChad DavidNote the
246e5e600c9SMark Murray.Er ERESTART
2475385e8bfSChad Daviderror below.
2485385e8bfSChad David.It Bq Er ERESTART
2495385e8bfSChad David.Dv PCATCH
2505385e8bfSChad Davidwas set in the lock priority, a signal was delivered during a sleep,
2515385e8bfSChad Davidand the system call is to be restarted.
2525385e8bfSChad David.It Bq Er EWOULDBLOCK
2535385e8bfSChad Davida non-zero timeout was given, and the timeout expired.
2545385e8bfSChad David.El
2555385e8bfSChad David.Sh LOCKS
2565385e8bfSChad DavidIf
2575385e8bfSChad David.Dv LK_INTERLOCK
2585385e8bfSChad Davidis passed in the
2595385e8bfSChad David.Fa flags
2605385e8bfSChad Davidargument to
2615385e8bfSChad David.Fn lockmgr ,
2625385e8bfSChad Davidthe
2635385e8bfSChad David.Fa interlkp
2645385e8bfSChad Davidmust be held prior to calling
2655385e8bfSChad David.Fn lockmgr ,
2665385e8bfSChad Davidand will be returned unlocked.
2675385e8bfSChad David.Pp
2685385e8bfSChad DavidUpgrade attempts that fail result in the loss of the lock that
2695385e8bfSChad Davidis currently held.
2705385e8bfSChad DavidAlso, it is invalid to upgrade an
2715385e8bfSChad Davidexclusive lock, and a
2725385e8bfSChad David.Xr panic 9
2735385e8bfSChad Davidwill be the result of trying.
2745385e8bfSChad David.Sh SEE ALSO
2755385e8bfSChad David.Xr msleep 9 ,
2765385e8bfSChad David.Xr mtx_assert 9 ,
2775385e8bfSChad David.Xr panic 9 ,
2785385e8bfSChad David.Xr VOP_PRINT 9
2795385e8bfSChad David.Sh AUTHORS
280571dba6eSHiten PandyaThis manual page was written by
2815385e8bfSChad David.An Chad David Aq davidc@acns.ab.ca .
282