xref: /freebsd/share/man/man9/lock.9 (revision 8271a1025c27baa73529ee2a29d0302421fdb71c)
1e5e600c9SMark Murray.\" Copyright (c) 2000
2e5e600c9SMark Murray.\"	The Regents of the University of California.  All rights reserved.
3e5e600c9SMark Murray.\"
4e5e600c9SMark Murray.\" All rights reserved.
5e5e600c9SMark Murray.\"
6e5e600c9SMark Murray.\" Redistribution and use in source and binary forms, with or without
7e5e600c9SMark Murray.\" modification, are permitted provided that the following conditions
8e5e600c9SMark Murray.\" are met:
9e5e600c9SMark Murray.\" 1. Redistributions of source code must retain the above copyright
10e5e600c9SMark Murray.\"    notice, this list of conditions and the following disclaimer.
11e5e600c9SMark Murray.\" 2. Redistributions in binary form must reproduce the above copyright
12e5e600c9SMark Murray.\"    notice, this list of conditions and the following disclaimer in the
13e5e600c9SMark Murray.\"    documentation and/or other materials provided with the distribution.
14e5e600c9SMark Murray.\"
15e5e600c9SMark Murray.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
16e5e600c9SMark Murray.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17e5e600c9SMark Murray.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18e5e600c9SMark Murray.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
19e5e600c9SMark Murray.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20e5e600c9SMark Murray.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21e5e600c9SMark Murray.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22e5e600c9SMark Murray.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23e5e600c9SMark Murray.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24e5e600c9SMark Murray.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25e5e600c9SMark Murray.\"
26e5e600c9SMark Murray.\" $FreeBSD$
27e5e600c9SMark Murray.\"
28ea3d1f09SRuslan Ermilov.Dd October 24, 2000
29e5e600c9SMark Murray.Dt LOCK 9
303d45e180SRuslan Ermilov.Os
31e5e600c9SMark Murray.Sh NAME
32e5e600c9SMark Murray.Nm lockinit ,
33e5e600c9SMark Murray.Nm lockmgr ,
34e5e600c9SMark Murray.Nm lockcount ,
35e5e600c9SMark Murray.Nm lockstatus
36f16b3c0dSChad David.Nd kernel thread locking
37e5e600c9SMark Murray.Sh SYNOPSIS
3832eef9aeSRuslan Ermilov.In sys/types.h
3932eef9aeSRuslan Ermilov.In sys/lock.h
40e5e600c9SMark Murray.Ft void
41e5e600c9SMark Murray.Fn lockinit "struct lock *lkp" "int prio" "char *wmesg" "int timo" "int flags"
42e5e600c9SMark Murray.Ft int
43f16b3c0dSChad David.Fn lockmgr "struct lock *lkp" "u_int flags" "struct mtx *interlkp" "struct thread *td"
44e5e600c9SMark Murray.Ft int
45e5e600c9SMark Murray.Fn lockcount "struct lock *lkp"
46e5e600c9SMark Murray.Ft int
47f16b3c0dSChad David.Fn lockstatus "struct lock *lkp" "struct thread *td"
48e5e600c9SMark Murray.Sh DESCRIPTION
49e5e600c9SMark MurrayThe function
50e5e600c9SMark Murray.Fn lockinit
51e5e600c9SMark Murrayis used to initialise a lock.
52e5e600c9SMark MurrayIt is required if locks are used.
53e5e600c9SMark MurrayThe function
54e5e600c9SMark Murray.Fn lockmgr
55e5e600c9SMark Murrayis used to manage locks.
56e5e600c9SMark MurrayThe function
57e5e600c9SMark Murray.Fn lockcount
58e5e600c9SMark Murrayreturns the number of shared lock holders on a lock.
59e5e600c9SMark MurrayThe function
60e5e600c9SMark Murray.Fn lockstatus
61e5e600c9SMark Murraydetermines the status of a lock.
62e5e600c9SMark Murray.Pp
63e5e600c9SMark MurrayThe following values are defined:
648271a102SRuslan Ermilov.Bl -tag -width ".Dv LK_EXCLUPGRADE"
65e5e600c9SMark Murray.It Dv LK_SHARED
66e5e600c9SMark Murrayget one of many possible shared locks.
67f16b3c0dSChad DavidIf a thread holding an exclusive lock requests a shared lock,
68e5e600c9SMark Murraythe exclusive lock(s) will be downgraded to shared locks.
69e5e600c9SMark Murray.It Dv LK_EXCLUSIVE
70e5e600c9SMark Murraystop further shared locks, when they are cleared,
71e5e600c9SMark Murraygrant a pending upgrade if it exists, then grant an exclusive
72e5e600c9SMark Murraylock.
73e5e600c9SMark MurrayOnly one exclusive lock may exist at a time, except that
74f16b3c0dSChad Davida thread holding an exclusive lock may get additional exclusive
758271a102SRuslan Ermilovlocks if it explicitly sets the
768271a102SRuslan Ermilov.Dv LK_CANRECURSE
778271a102SRuslan Ermilovflag in the lock request, or if the
788271a102SRuslan Ermilov.Dv LK_CANRECURSE
798271a102SRuslan Ermilovflag was set when the lock was initialized.
80e5e600c9SMark Murray.It Dv LK_UPGRADE
81f16b3c0dSChad Davidthe thread must hold a shared lock that it wants to
82e5e600c9SMark Murrayhave upgraded to an exclusive lock.
83f16b3c0dSChad DavidOther threads may get exclusive access to the resource between
84e5e600c9SMark Murraythe time that the upgrade is requested and the time that it is
85e5e600c9SMark Murraygranted.
86e5e600c9SMark Murray.It Dv LK_EXCLUPGRADE
87f16b3c0dSChad Davidthe thread must hold a shared lock that it wants to
88e5e600c9SMark Murrayhave upgraded to an exclusive lock.
89f16b3c0dSChad DavidIf the request succeeds, no other thread will have gotten
90e5e600c9SMark Murrayexclusive access to the resource between the time that the upgrade
91e5e600c9SMark Murrayis requested and the time that it is granted.
92f16b3c0dSChad DavidHowever, if another thread has already requested an upgrade,
93e5e600c9SMark Murraythe request will fail.
94e5e600c9SMark Murray.It Dv LK_DOWNGRADE
95f16b3c0dSChad Davidthe thread must hold an exclusive lock that it wants
96e5e600c9SMark Murrayto have downgraded to a shared lock.
97f16b3c0dSChad DavidIf the thread holds multiple (recursive) exclusive locks,
98e5e600c9SMark Murraythey will all be downgraded to shared locks.
99e5e600c9SMark Murray.It Dv LK_RELEASE
100e5e600c9SMark Murrayrelease one instance of a lock.
101e5e600c9SMark Murray.It Dv LK_DRAIN
102e5e600c9SMark Murraywait for all activity on the lock to end, then mark it
103e5e600c9SMark Murraydecommissioned.
104e5e600c9SMark MurrayThis feature is used before freeing a lock that is part of a
105e5e600c9SMark Murraypiece of memory that is about to be freed.
106e5e600c9SMark Murray.It Dv LK_EXCLOTHER
1078271a102SRuslan Ermilovreturn for
1088271a102SRuslan Ermilov.Fn lockstatus .
109f16b3c0dSChad DavidUsed when another thread holds the lock exclusively.
110e5e600c9SMark Murray.El
111e5e600c9SMark Murray.Sh RETURN VALUES
112e5e600c9SMark MurraySuccessfully obtained locks return 0.
113e5e600c9SMark MurrayLocks will always succeed unless one of the following is true:
1148271a102SRuslan Ermilov.Dv LK_FORCEUPGRADE
1158271a102SRuslan Ermilovis requested and some other thread has already
116e5e600c9SMark Murrayrequested a lock upgrade; returns
117e5e600c9SMark Murray.Er EBUSY .
1188271a102SRuslan Ermilov.Dv LK_WAIT
1198271a102SRuslan Ermilovis set and a sleep would be required; returns
120e5e600c9SMark Murray.Er EBUSY .
1218271a102SRuslan Ermilov.Dv LK_SLEEPFAIL
1228271a102SRuslan Ermilovis set and a sleep was done; returns
123e5e600c9SMark Murray.Er ENOLCK .
1248271a102SRuslan Ermilov.Dv PCATCH
1258271a102SRuslan Ermilovis set in lock priority and a signal arrives; returns
126e5e600c9SMark Murrayeither
127e5e600c9SMark Murray.Er EINTR
128e5e600c9SMark Murrayor
129e5e600c9SMark Murray.Er ERESTART
130e5e600c9SMark Murrayif system calls is to be restarted.
131e5e600c9SMark MurrayNon-null lock timeout and timeout expires; returns
132e5e600c9SMark Murray.Er EWOULDBLOCK .
133e5e600c9SMark MurrayA failed lock attempt always returns a non-zero error value.
134e5e600c9SMark MurrayNo lock is held after an error return (in particular, a failed
1358271a102SRuslan Ermilov.Dv LK_UPGRADE
1368271a102SRuslan Ermilovor
1378271a102SRuslan Ermilov.Dv LK_FORCEUPGRADE
1388271a102SRuslan Ermilovwill have released its shared
139e5e600c9SMark Murrayaccess lock).
140