xref: /freebsd/share/man/man9/sx.9 (revision 52ec752989b2e6d4e9a59a8ff25d8ff596d85e62)
1.\"
2.\" Copyright (C) 2001 Jason Evans <jasone@FreeBSD.org>.  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 August 14, 2001
30.Dt SX 9
31.Os
32.Sh NAME
33.Nm sx ,
34.Nm sx_init ,
35.Nm sx_destroy ,
36.Nm sx_slock ,
37.Nm sx_xlock ,
38.Nm sx_try_slock ,
39.Nm sx_try_xlock ,
40.Nm sx_sunlock ,
41.Nm sx_xunlock ,
42.Nm sx_try_upgrade ,
43.Nm sx_downgrade ,
44.Nm sx_assert ,
45.Nm SX_SYSINIT
46.Nd kernel shared/exclusive lock
47.Sh SYNOPSIS
48.In sys/param.h
49.In sys/lock.h
50.In sys/sx.h
51.Ft void
52.Fn sx_init "struct sx *sx" "const char *description"
53.Ft void
54.Fn sx_destroy "struct sx *sx"
55.Ft void
56.Fn sx_slock "struct sx *sx"
57.Ft void
58.Fn sx_xlock "struct sx *sx"
59.Ft int
60.Fn sx_try_slock "struct sx *sx"
61.Ft int
62.Fn sx_try_xlock "struct sx *sx"
63.Ft void
64.Fn sx_sunlock "struct sx *sx"
65.Ft void
66.Fn sx_xunlock "struct sx *sx"
67.Ft int
68.Fn sx_try_upgrade "struct sx *sx"
69.Ft void
70.Fn sx_downgrade "struct sx *sx"
71.Pp
72.Cd "options INVARIANTS"
73.Cd "options INVARIANT_SUPPORT"
74.Ft void
75.Fn sx_assert "struct sx *sx" "int what"
76.In sys/kernel.h
77.Fn SX_SYSINIT "name" "struct sx *sx" "const char *description"
78.Sh DESCRIPTION
79Shared/exclusive locks are used to protect data that are read far more often
80than they are written.
81Mutexes are inherently more efficient than shared/exclusive locks, so
82shared/exclusive locks should be used prudently.
83.Pp
84Shared/exclusive locks are created with
85.Fn sx_init ,
86where
87.Fa sx
88is a pointer to space for a
89.Vt struct sx ,
90and
91.Fa description
92is a pointer to a null-terminated character string that describes the
93shared/exclusive lock.
94Shared/exclusive locks are destroyed with
95.Fn sx_destroy .
96Threads acquire and release a shared lock by calling
97.Fn sx_slock
98or
99.Fn sx_try_slock
100and
101.Fn sx_sunlock .
102Threads acquire and release an exclusive lock by calling
103.Fn sx_xlock
104or
105.Fn sx_try_xlock
106and
107.Fn sx_xunlock .
108A thread can attempt to upgrade a currently owned shared lock to an exclusive
109lock by calling
110.Fn sx_try_upgrade .
111A thread that owns an exclusive lock can downgrade it to a shared lock by
112calling
113.Fn sx_downgrade .
114.Pp
115.Fn sx_try_slock
116and
117.Fn sx_try_xlock
118will return 0 if the shared/exclusive lock cannot be acquired immediately;
119otherwise the shared/exclusive lock will be acquired and a non-zero value will
120be returned.
121.Pp
122.Fn sx_try_upgrade
123will return 0 if the shared lock cannot be upgraded to an exclusive lock
124immediately; otherwise the exclusive lock will be acquired and a non-zero value
125will be returned.
126.Pp
127When compiled with
128.Cd "options INVARIANTS"
129and
130.Cd "options INVARIANT_SUPPORT" ,
131the
132.Fn sx_assert
133function tests
134.Ar sx
135for the assertions specified in
136.Ar what
137and panics if they are not met.
138The following assertions are supported:
139.Bl -tag -width ".Dv SX_XLOCKED"
140.It Dv SX_LOCKED
141Assert that the current thread has either a shared or an exclusive lock on the
142.Vt sx
143lock pointed to by the first argument.
144.It Dv SX_SLOCKED
145Assert that the current thread has a shared lock on the
146.Vt sx
147lock pointed to by
148the first argument.
149.It Dv SX_XLOCKED
150Assert that the current thread has an exclusive lock on the
151.Vt sx
152lock pointed to
153by the first argument.
154.El
155.Pp
156The
157.Fn SX_SYSINIT
158macro is used to generate a call to the
159.Fn sx_sysinit
160routine at system startup in order to initialize a given
161.Fa sx
162lock.
163The parameters are the same as
164.Fn sx_init
165but with an additional argument,
166.Fa name ,
167that is used in generating unique variable names for the related
168structures associated with the lock and the sysinit routine.
169.Pp
170A thread may not own a shared lock and an exclusive lock simultaneously;
171attempting to do so will result in deadlock.
172.Sh CONTEXT
173It is allowed to own a shared lock or an exclusive lock while sleeping.
174.Sh SEE ALSO
175.Xr condvar 9 ,
176.Xr mtx_pool 9 ,
177.Xr mutex 9 ,
178.Xr panic 9 ,
179.Xr sema 9
180.Sh BUGS
181Currently there is no way to assert that a lock is not held.
182This is not possible in the
183.No non- Ns Dv WITNESS
184case for asserting that this thread
185does not hold a shared lock.
186In the
187.No non- Ns Dv WITNESS
188case, the
189.Dv SX_LOCKED
190and
191.Dv SX_SLOCKED
192assertions merely check that some thread holds a shared lock.
193They do not ensure that the current thread holds a shared lock.
194