xref: /freebsd/share/man/man9/sx.9 (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
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.Dd November 11, 2017
28.Dt SX 9
29.Os
30.Sh NAME
31.Nm sx ,
32.Nm sx_init ,
33.Nm sx_init_flags ,
34.Nm sx_destroy ,
35.Nm sx_slock ,
36.Nm sx_xlock ,
37.Nm sx_slock_sig ,
38.Nm sx_xlock_sig ,
39.Nm sx_try_slock ,
40.Nm sx_try_xlock ,
41.Nm sx_sunlock ,
42.Nm sx_xunlock ,
43.Nm sx_unlock ,
44.Nm sx_try_upgrade ,
45.Nm sx_downgrade ,
46.Nm sx_sleep ,
47.Nm sx_xholder ,
48.Nm sx_xlocked ,
49.Nm sx_assert ,
50.Nm SX_SYSINIT ,
51.Nm SX_SYSINIT_FLAGS
52.Nd kernel shared/exclusive lock
53.Sh SYNOPSIS
54.In sys/param.h
55.In sys/lock.h
56.In sys/sx.h
57.Ft void
58.Fn sx_init "struct sx *sx" "const char *description"
59.Ft void
60.Fn sx_init_flags "struct sx *sx" "const char *description" "int opts"
61.Ft void
62.Fn sx_destroy "struct sx *sx"
63.Ft void
64.Fn sx_slock "struct sx *sx"
65.Ft void
66.Fn sx_xlock "struct sx *sx"
67.Ft int
68.Fn sx_slock_sig "struct sx *sx"
69.Ft int
70.Fn sx_xlock_sig "struct sx *sx"
71.Ft int
72.Fn sx_try_slock "struct sx *sx"
73.Ft int
74.Fn sx_try_xlock "struct sx *sx"
75.Ft void
76.Fn sx_sunlock "struct sx *sx"
77.Ft void
78.Fn sx_xunlock "struct sx *sx"
79.Ft void
80.Fn sx_unlock "struct sx *sx"
81.Ft int
82.Fn sx_try_upgrade "struct sx *sx"
83.Ft void
84.Fn sx_downgrade "struct sx *sx"
85.Ft int
86.Fn sx_sleep "void *chan" "struct sx *sx" "int priority" "const char *wmesg" "int timo"
87.Ft "struct thread *"
88.Fn sx_xholder "struct sx *sx"
89.Ft int
90.Fn sx_xlocked "const struct sx *sx"
91.Pp
92.Cd "options INVARIANTS"
93.Cd "options INVARIANT_SUPPORT"
94.Ft void
95.Fn sx_assert "const struct sx *sx" "int what"
96.In sys/kernel.h
97.Fn SX_SYSINIT "name" "struct sx *sx" "const char *desc"
98.Fn SX_SYSINIT_FLAGS "name" "struct sx *sx" "const char *desc" "int flags"
99.Sh DESCRIPTION
100Shared/exclusive locks are used to protect data that are read far more often
101than they are written.
102Shared/exclusive locks do not implement priority propagation like mutexes and
103reader/writer locks to prevent priority inversions, so
104shared/exclusive locks should be used prudently.
105.Pp
106Shared/exclusive locks are created with either
107.Fn sx_init
108or
109.Fn sx_init_flags
110where
111.Fa sx
112is a pointer to space for a
113.Vt struct sx ,
114and
115.Fa description
116is a pointer to a null-terminated character string that describes the
117shared/exclusive lock.
118The
119.Fa opts
120argument to
121.Fn sx_init_flags
122specifies a set of optional flags to alter the behavior of
123.Fa sx .
124It contains one or more of the following flags:
125.Bl -tag -width SX_NOWITNESS
126.It Dv SX_DUPOK
127Witness should not log messages about duplicate locks being acquired.
128.It Dv SX_NOWITNESS
129Instruct
130.Xr witness 4
131to ignore this lock.
132.It Dv SX_NOPROFILE
133Do not profile this lock.
134.It Dv SX_RECURSE
135Allow threads to recursively acquire exclusive locks for
136.Fa sx .
137.It Dv SX_QUIET
138Do not log any operations for this lock via
139.Xr ktr 4 .
140.It Dv SX_NEW
141If the kernel has been compiled with
142.Cd "options INVARIANTS" ,
143.Fn sx_init
144will assert that the
145.Fa sx
146has not been initialized multiple times without intervening calls to
147.Fn sx_destroy
148unless this option is specified.
149.El
150.Pp
151Shared/exclusive locks are destroyed with
152.Fn sx_destroy .
153The lock
154.Fa sx
155must not be locked by any thread when it is destroyed.
156.Pp
157Threads acquire and release a shared lock by calling
158.Fn sx_slock ,
159.Fn sx_slock_sig
160or
161.Fn sx_try_slock
162and
163.Fn sx_sunlock
164or
165.Fn sx_unlock .
166Threads acquire and release an exclusive lock by calling
167.Fn sx_xlock ,
168.Fn sx_xlock_sig
169or
170.Fn sx_try_xlock
171and
172.Fn sx_xunlock
173or
174.Fn sx_unlock .
175A thread can attempt to upgrade a currently held shared lock to an exclusive
176lock by calling
177.Fn sx_try_upgrade .
178A thread that has an exclusive lock can downgrade it to a shared lock by
179calling
180.Fn sx_downgrade .
181.Pp
182.Fn sx_try_slock
183and
184.Fn sx_try_xlock
185will return 0 if the shared/exclusive lock cannot be acquired immediately;
186otherwise the shared/exclusive lock will be acquired and a non-zero value will
187be returned.
188.Pp
189.Fn sx_try_upgrade
190will return 0 if the shared lock cannot be upgraded to an exclusive lock
191immediately; otherwise the exclusive lock will be acquired and a non-zero value
192will be returned.
193.Pp
194.Fn sx_slock_sig
195and
196.Fn sx_xlock_sig
197do the same as their normal versions but performing an interruptible sleep.
198They return a non-zero value if the sleep has been interrupted by a signal
199or an interrupt, otherwise 0.
200.Pp
201A thread can atomically release a shared/exclusive lock while waiting for an
202event by calling
203.Fn sx_sleep .
204For more details on the parameters to this function,
205see
206.Xr sleep 9 .
207.Pp
208When compiled with
209.Cd "options INVARIANTS"
210and
211.Cd "options INVARIANT_SUPPORT" ,
212the
213.Fn sx_assert
214function tests
215.Fa sx
216for the assertions specified in
217.Fa what ,
218and panics if they are not met.
219One of the following assertions must be specified:
220.Bl -tag -width ".Dv SA_UNLOCKED"
221.It Dv SA_LOCKED
222Assert that the current thread has either a shared or an exclusive lock on the
223.Vt sx
224lock pointed to by the first argument.
225.It Dv SA_SLOCKED
226Assert that the current thread has a shared lock on the
227.Vt sx
228lock pointed to by
229the first argument.
230.It Dv SA_XLOCKED
231Assert that the current thread has an exclusive lock on the
232.Vt sx
233lock pointed to
234by the first argument.
235.It Dv SA_UNLOCKED
236Assert that the current thread has no lock on the
237.Vt sx
238lock pointed to
239by the first argument.
240.El
241.Pp
242In addition, one of the following optional assertions may be included with
243either an
244.Dv SA_LOCKED ,
245.Dv SA_SLOCKED ,
246or
247.Dv SA_XLOCKED
248assertion:
249.Bl -tag -width ".Dv SA_NOTRECURSED"
250.It Dv SA_RECURSED
251Assert that the current thread has a recursed lock on
252.Fa sx .
253.It Dv SA_NOTRECURSED
254Assert that the current thread does not have a recursed lock on
255.Fa sx .
256.El
257.Pp
258.Fn sx_xholder
259will return a pointer to the thread which currently holds an exclusive lock on
260.Fa sx .
261If no thread holds an exclusive lock on
262.Fa sx ,
263then
264.Dv NULL
265is returned instead.
266.Pp
267.Fn sx_xlocked
268will return non-zero if the current thread holds the exclusive lock;
269otherwise, it will return zero.
270.Pp
271For ease of programming,
272.Fn sx_unlock
273is provided as a macro frontend to the respective functions,
274.Fn sx_sunlock
275and
276.Fn sx_xunlock .
277Algorithms that are aware of what state the lock is in should use either
278of the two specific functions for a minor performance benefit.
279.Pp
280The
281.Fn SX_SYSINIT
282macro is used to generate a call to the
283.Fn sx_sysinit
284routine at system startup in order to initialize a given
285.Fa sx
286lock.
287The parameters are the same as
288.Fn sx_init
289but with an additional argument,
290.Fa name ,
291that is used in generating unique variable names for the related
292structures associated with the lock and the sysinit routine.
293The
294.Fn SX_SYSINIT_FLAGS
295macro can similarly be used to initialize a given
296.Fa sx
297lock using
298.Fn sx_init_flags .
299.Pp
300A thread may not hold both a shared lock and an exclusive lock on the same
301lock simultaneously;
302attempting to do so will result in deadlock.
303.Sh CONTEXT
304A thread may hold a shared or exclusive lock on an
305.Nm
306lock while sleeping.
307As a result, an
308.Nm
309lock may not be acquired while holding a mutex.
310Otherwise, if one thread slept while holding an
311.Nm
312lock while another thread blocked on the same
313.Nm
314lock after acquiring a mutex, then the second thread would effectively
315end up sleeping while holding a mutex, which is not allowed.
316.Sh SEE ALSO
317.Xr lock 9 ,
318.Xr locking 9 ,
319.Xr mutex 9 ,
320.Xr panic 9 ,
321.Xr rwlock 9 ,
322.Xr sema 9
323.Sh BUGS
324A kernel without
325.Dv WITNESS
326cannot assert whether the current thread does or does not hold a shared lock.
327.Dv SA_LOCKED
328and
329.Dv SA_SLOCKED
330can only assert that
331.Em any
332thread holds a shared lock.
333They cannot ensure that the current thread holds a shared lock.
334Further,
335.Dv SA_UNLOCKED
336can only assert that the current thread does not hold an exclusive lock.
337