2 .\" Copyright (C) 2001 Jason Evans <jasone@FreeBSD.org>. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
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.
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
48 .Nd kernel shared/exclusive lock
54 .Fn sx_init "struct sx *sx" "const char *description"
56 .Fn sx_destroy "struct sx *sx"
58 .Fn sx_slock "struct sx *sx"
60 .Fn sx_xlock "struct sx *sx"
62 .Fn sx_try_slock "struct sx *sx"
64 .Fn sx_try_xlock "struct sx *sx"
66 .Fn sx_sunlock "struct sx *sx"
68 .Fn sx_xunlock "struct sx *sx"
70 .Fn sx_try_upgrade "struct sx *sx"
72 .Fn sx_downgrade "struct sx *sx"
74 .Fn sx_assert "struct sx *sx" "int what"
76 .Fn sx_xlocked "struct sx *sx"
78 .Ss Nm Ss utility macros
79 .Fn sx_unlock "struct sx *sx"
80 .Fn SX_SYSINIT "name" "struct sx *sx" "const char *description"
83 .Cd "options INVARIANTS"
84 .Cd "options INVARIANT_SUPPORT"
86 Shared/exclusive locks are used to protect data that are read far more often
87 than they are written.
88 Mutexes are inherently more efficient than shared/exclusive locks, so
89 shared/exclusive locks should be used prudently.
91 Shared/exclusive locks are created with
95 is a pointer to space for a
99 is a pointer to a null-terminated character string that describes the
100 shared/exclusive lock.
101 Shared/exclusive locks are destroyed with
103 Threads acquire and release a shared lock by calling
111 Threads acquire and release an exclusive lock by calling
119 A thread can attempt to upgrade a currently held shared lock to an exclusive
122 A thread that has an exclusive lock can downgrade it to a shared lock by
129 will return 0 if the shared/exclusive lock cannot be acquired immediately;
130 otherwise the shared/exclusive lock will be acquired and a non-zero value will
134 will return 0 if the shared lock cannot be upgraded to an exclusive lock
135 immediately; otherwise the exclusive lock will be acquired and a non-zero value
139 .Cd "options INVARIANTS"
141 .Cd "options INVARIANT_SUPPORT" ,
146 for the assertions specified in
148 and panics if they are not met.
149 The following assertions are supported:
150 .Bl -tag -width ".Dv SX_UNLOCKED"
152 Assert that the current thread has either a shared or an exclusive lock on the
154 lock pointed to by the first argument.
156 Assert that the current thread has a shared lock on the
161 Assert that the current thread has an exclusive lock on the
164 by the first argument.
166 Assert that the current thread has no lock on the
169 by the first argument.
173 will return non-zero if the current process holds the exclusive lock;
174 otherwise, it will return zero.
176 For ease of programming,
178 is provided as a macro frontend to the respective functions,
182 Algorithms that are aware of what state the lock is in should use either
183 of the two specific functions for a minor performance benefit.
187 macro is used to generate a call to the
189 routine at system startup in order to initialize a given
192 The parameters are the same as
194 but with an additional argument,
196 that is used in generating unique variable names for the related
197 structures associated with the lock and the sysinit routine.
199 A thread may not hold both a shared lock and an exclusive lock on the same
201 attempting to do so will result in deadlock.
203 A thread may hold a shared or exclusive lock on an
208 lock may not be acquired while holding a mutex.
209 Otherwise, if one thread slept while holding an
211 lock while another thread blocked on the same
213 lock after acquiring a mutex, then the second thread would effectively
214 end up sleeping while holding a mutex, which is not allowed.
221 Currently there is no way to assert that a lock is not held.
222 This is not possible in the
223 .No non- Ns Dv WITNESS
224 case for asserting that this thread
225 does not hold a shared lock.
227 .No non- Ns Dv WITNESS
232 assertions merely check that some thread holds a shared lock.
233 They do not ensure that the current thread holds a shared lock.