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