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
49 .Nd kernel shared/exclusive lock
55 .Fn sx_init "struct sx *sx" "const char *description"
57 .Fn sx_destroy "struct sx *sx"
59 .Fn sx_slock "struct sx *sx"
61 .Fn sx_xlock "struct sx *sx"
63 .Fn sx_try_slock "struct sx *sx"
65 .Fn sx_try_xlock "struct sx *sx"
67 .Fn sx_sunlock "struct sx *sx"
69 .Fn sx_xunlock "struct sx *sx"
71 .Fn sx_unlock "struct sx *sx"
73 .Fn sx_try_upgrade "struct sx *sx"
75 .Fn sx_downgrade "struct sx *sx"
77 .Fn sx_sleep "void *chan" "struct sx *sx" "int priority" "const char *wmesg" "int timo"
79 .Fn sx_xlocked "struct sx *sx"
81 .Cd "options INVARIANTS"
82 .Cd "options INVARIANT_SUPPORT"
84 .Fn sx_assert "struct sx *sx" "int what"
86 .Fn SX_SYSINIT "name" "struct sx *sx" "const char *description"
88 Shared/exclusive locks are used to protect data that are read far more often
89 than they are written.
90 Mutexes are inherently more efficient than shared/exclusive locks, so
91 shared/exclusive locks should be used prudently.
93 Shared/exclusive locks are created with
97 is a pointer to space for a
101 is a pointer to a null-terminated character string that describes the
102 shared/exclusive lock.
103 Shared/exclusive locks are destroyed with
105 Threads acquire and release a shared lock by calling
113 Threads acquire and release an exclusive lock by calling
121 A thread can attempt to upgrade a currently held shared lock to an exclusive
124 A thread that has an exclusive lock can downgrade it to a shared lock by
131 will return 0 if the shared/exclusive lock cannot be acquired immediately;
132 otherwise the shared/exclusive lock will be acquired and a non-zero value will
136 will return 0 if the shared lock cannot be upgraded to an exclusive lock
137 immediately; otherwise the exclusive lock will be acquired and a non-zero value
140 A thread can atomically release a shared/exclusive lock while waiting for an
143 For more details on the parameters to this function,
148 .Cd "options INVARIANTS"
150 .Cd "options INVARIANT_SUPPORT" ,
155 for the assertions specified in
157 and panics if they are not met.
158 The following assertions are supported:
159 .Bl -tag -width ".Dv SX_UNLOCKED"
161 Assert that the current thread has either a shared or an exclusive lock on the
163 lock pointed to by the first argument.
165 Assert that the current thread has a shared lock on the
170 Assert that the current thread has an exclusive lock on the
173 by the first argument.
175 Assert that the current thread has no lock on the
178 by the first argument.
182 will return non-zero if the current thread holds the exclusive lock;
183 otherwise, it will return zero.
185 For ease of programming,
187 is provided as a macro frontend to the respective functions,
191 Algorithms that are aware of what state the lock is in should use either
192 of the two specific functions for a minor performance benefit.
196 macro is used to generate a call to the
198 routine at system startup in order to initialize a given
201 The parameters are the same as
203 but with an additional argument,
205 that is used in generating unique variable names for the related
206 structures associated with the lock and the sysinit routine.
208 A thread may not hold both a shared lock and an exclusive lock on the same
210 attempting to do so will result in deadlock.
212 A thread may hold a shared or exclusive lock on an
217 lock may not be acquired while holding a mutex.
218 Otherwise, if one thread slept while holding an
220 lock while another thread blocked on the same
222 lock after acquiring a mutex, then the second thread would effectively
223 end up sleeping while holding a mutex, which is not allowed.
230 Currently there is no way to assert that a lock is not held.
231 This is not possible in the
232 .No non- Ns Dv WITNESS
233 case for asserting that this thread
234 does not hold a shared lock.
236 .No non- Ns Dv WITNESS
241 assertions merely check that some thread holds a shared lock.
242 They do not ensure that the current thread holds a shared lock.