1 .\" Copyright (c) 2007 Julian Elischer (julian - freebsd org )
2 .\" 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, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .Nd kernel synchronization primitives
34 All sorts of stuff to go here.
39 kernel is written to run across multiple CPUs and as such requires
40 several different synchronization primitives to allow the developers
41 to safely access and manipulate the many data types required.
52 Shared-Exclusive locks
71 The primitives interact and have a number of rules regarding how
72 they can and can not be combined.
73 There are too many for the average
74 human mind and they keep changing.
75 (if you disagree, please write replacement text) :-)
77 Some of these primitives may be used at the low (interrupt) level and
80 There are strict ordering requirements and for some of the types this
86 Mutexes are the basic primitive.
87 You either hold it or you don't.
88 If you don't own it then you just spin, waiting for the holder (on
89 another CPU) to release it.
90 Hopefully they are doing something fast.
93 do anything that deschedules the thread while you
94 are holding a SPIN mutex.
96 Basically (regular) mutexes will deschedule the thread if the
97 mutex can not be acquired.
98 A non-spin mutex can be considered to be equivalent
99 to getting a write lock on an
101 (see below), and in fact non-spin mutexes and rw_locks may soon become the same thing.
102 As in spin mutexes, you either get it or you don't.
103 You may only call the
110 These will atomically drop the mutex and reacquire it
111 as part of waking up.
112 This is often however a
114 idea because it generally relies on you having
115 such a good knowledge of all the call graph above you
116 and what assumptions it is making that there are a lot
117 of ways to make hard-to-find mistakes.
118 For example you MUST re-test all the assumptions you made before,
119 all the way up the call graph to where you got the lock.
120 You can not just assume that mtx_sleep can be inserted anywhere.
121 If any caller above you has any mutex or
122 rwlock, your sleep, will cause a panic.
123 If the sleep only happens rarely it may be years before the
124 bad code path is found.
126 A variant of regular mutexes where the allocation of the mutex is handled
129 Reader/writer locks allow shared access to protected data by multiple threads,
130 or exclusive access by a single thread.
131 The threads with shared access are known as
133 since they should only read the protected data.
134 A thread with exclusive access is known as a
136 since it may modify protected data.
138 Although reader/writer locks look very similar to
140 (see below) locks, their usage pattern is different.
141 Reader/writer locks can be treated as mutexes (see above and
143 with shared/exclusive semantics.
144 More specifically, regular mutexes can be
145 considered to be equivalent to a write-lock on an
147 In the future this may in fact
148 become literally the fact.
151 can be locked while holding a regular mutex, but
154 be held while sleeping.
157 locks have priority propagation like mutexes, but priority
158 can be propagated only to an exclusive holder.
159 This limitation comes from the fact that shared owners
161 Another important property is that shared holders of
163 can recurse, but exclusive locks are not allowed to recurse.
164 This ability should not be used lightly and
166 Users of recursion in any locks should be prepared to
167 defend their decision against vigorous criticism.
169 Mostly reader locks are similar to
171 locks but optimized for very infrequent
175 locks implement full priority propagation by tracking shared owners
176 using a lock user supplied
180 Shared/exclusive locks are used to protect data that are read far more often
181 than they are written.
182 Mutexes are inherently more efficient than shared/exclusive locks, so
183 shared/exclusive locks should be used prudently.
184 The main reason for using an
186 is that a thread may hold a shared or exclusive lock on an
189 As a consequence of this however, an
191 lock may not be acquired while holding a mutex.
192 The reason for this is that, if one thread slept while holding an
194 lock while another thread blocked on the same
196 lock after acquiring a mutex, then the second thread would effectively
197 end up sleeping while holding a mutex, which is not allowed.
200 should be considered to be closely related to
202 In fact it could in some cases be
203 considered a conditional sleep.
205 Turnstiles are used to hold a queue of threads blocked on
207 Sleepable locks use condition variables to implement their queues.
208 Turnstiles differ from a sleep queue in that turnstile queue's
209 are assigned to a lock held by an owning thread.
210 Thus, when one thread is enqueued onto a turnstile, it can lend its
211 priority to the owning thread.
212 If this sounds confusing, we need to describe it better.
214 .Ss Condition variables
215 Condition variables are used in conjunction with mutexes to wait for
217 A thread must hold the mutex before calling the
220 When a thread waits on a condition, the mutex
221 is atomically released before the thread is blocked, then reacquired
222 before the function call returns.
224 Giant is a special instance of a sleep lock.
225 It has several special characteristics.
230 Drivers can request that Giant be locked around them, but this is
233 You can sleep while it has recursed, but other recursive locks cannot.
235 Giant must be locked first before other locks.
237 There are places in the kernel that drop Giant and pick it back up
239 Sleep locks will do this before sleeping.
240 Parts of the Network or VM code may do this as well, depending on the
242 This means that you cannot count on Giant keeping other code from
243 running if your code sleeps, even if you want it to.
254 handle event-based thread blocking.
255 If a thread must wait for an external event, it is put to sleep by
261 Threads may also wait using one of the locking primitive sleep routines
269 is an arbitrary address that uniquely identifies the event on which
270 the thread is being put to sleep.
271 All threads sleeping on a single
273 are woken up later by
275 often called from inside an interrupt routine, to indicate that the
276 resource the thread was blocking on is available now.
278 Several of the sleep functions including
281 and the locking primitive sleep routines specify an additional lock
283 The lock will be released before sleeping and reacquired
284 before the sleep routine returns.
289 flag, then the lock will not be reacquired before returning.
290 The lock is used to ensure that a condition can be checked atomically,
291 and that the current thread can be suspended without missing a
292 change to the condition, or an associated wakeup.
293 In addition, all of the sleep routines will fully drop the
297 while the thread is suspended and will reacquire the
299 mutex before the function returns.
301 .Ss lockmanager locks
305 page for more information.
306 I don't know what the downsides are but I'm sure someone will fill in this part.
308 .Ss Interaction table.
309 The following table shows what you can and can not do if you hold
310 one of the synchronization primitives discussed here:
311 (someone who knows what they are talking about should write this table)
312 .Bl -column ".Ic xxxxxxxxxxxxxxxxxxxx" ".Xr XXXXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXX" -offset indent
314 .Em "You have: You want:" Ta Spin_mtx Ta Slp_mtx Ta sx_lock Ta rw_lock Ta rm_lock Ta sleep
316 .It Ic SPIN mutex Ta \&ok-1 Ta \&no Ta \&no Ta \&no Ta \&no Ta \&no-3
317 .It Ic Sleep mutex Ta \&ok Ta \&ok-1 Ta \&no Ta \&ok Ta \&ok Ta \&no-3
318 .It Ic sx_lock Ta \&ok Ta \&ok Ta \&ok-2 Ta \&ok Ta \&ok Ta \&ok-4
319 .It Ic rw_lock Ta \&ok Ta \&ok Ta \&no Ta \&ok-2 Ta \&ok Ta \&no-3
320 .It Ic rm_lock Ta \&ok Ta \&ok Ta \&no Ta \&ok Ta \&ok-2 Ta \&no
324 Recursion is defined per lock.
325 Lock order is important.
328 readers can recurse though writers can not.
329 Lock order is important.
332 There are calls atomically release this primitive when going to sleep
333 and reacquire it on wakeup (e.g.
341 Though one can sleep holding an sx lock, one can also use
343 which atomically release this primitive when going to sleep and
344 reacquire it on wakeup.
345 .Ss Context mode table.
346 The next table shows what can be used in different contexts.
347 At this time this is a rather easy to remember table.
348 .Bl -column ".Ic Xxxxxxxxxxxxxxxxxxxx" ".Xr XXXXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXX" -offset indent
350 .Em "Context:" Ta Spin_mtx Ta Slp_mtx Ta sx_lock Ta rw_lock Ta rm_lock Ta sleep
352 .It interrupt: Ta \&ok Ta \&no Ta \&no Ta \&no Ta \&no Ta \&no
353 .It idle: Ta \&ok Ta \&no Ta \&no Ta \&no Ta \&no Ta \&no
365 .Xr LOCK_PROFILING 9 ,
369 functions appeared in