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
69 The primitives interact and have a number of rules regarding how
70 they can and can not be combined. There are too many for the average
71 human mind and they keep changing.
72 (if you disagree, please write replacement text) :-)
74 Some of these primitives may be used at the low (interrupt) level and
77 There are strict ordering requirements and for some of the types this
83 Mutexes are the basic primitive.
84 You either hold it or you don't.
85 If you don't own it then you just spin, waiting for the holder (on
86 another CPU) to release it.
87 Hopefully they are doing something fast.
88 You can not do anything that deschedules the thread while you
89 are holding a SPIN mutex.
91 Basically sleep (regular) mutexes will deschedule the thread if the
92 mutex can not be acquired.
93 As in spin mutexes, you either get it or you don't.
100 variant. These will atomically drop the mutex and reacquire it
101 as part of waking up.
103 A variant of SLEEP mutexes where the allocation of the mutex is handled
106 Shared/exclusive locks are used to protect data that are read far more often
107 than they are written.
108 Mutexes are inherently more efficient than shared/exclusive locks, so
109 shared/exclusive locks should be used prudently.
110 A thread may hold a shared or exclusive lock on an
115 lock may not be acquired while holding a mutex.
116 Otherwise, if one thread slept while holding an
118 lock while another thread blocked on the same
120 lock after acquiring a mutex, then the second thread would effectively
121 end up sleeping while holding a mutex, which is not allowed.
123 Reader/writer locks allow shared access to protected data by multiple threads,
124 or exclusive access by a single thread.
125 The threads with shared access are known as
127 since they only read the protected data.
128 A thread with exclusive access is known as a
130 since it can modify protected data.
132 Although reader/writer locks look very similar to
134 locks, their usage pattern is different.
135 Reader/writer locks can be treated as mutexes (see
137 with shared/exclusive semantics.
142 can be locked while holding a non-spin mutex, and an
144 cannot be held while sleeping.
147 locks have priority propagation like mutexes, but priority
148 can be propagated only to an exclusive holder.
149 This limitation comes from the fact that shared owners
151 Another important property is that shared holders of
154 but exclusive locks are not allowed to recurse.
156 Turnstiles are used to hold a queue of threads blocked on
158 Sleepable locks use condition variables to implement their queues.
159 Turnstiles differ from a sleep queue in that turnstile queue's
160 are assigned to a lock held by an owning thread.
161 Thus, when one thread is enqueued onto a turnstile, it can lend its
162 priority to the owning thread.
164 .Ss Condition variables
165 Condition variables are used in conjunction with mutexes to wait for
167 A thread must hold the mutex before calling the
170 When a thread waits on a condition, the mutex
171 is atomically released before the thread is blocked, then reacquired
172 before the function call returns.
174 Giant is a special instance of a sleep lock.
175 It has several special characteristics.
180 Drivers can request that Giant be locked around them, but this is
183 You can sleep while it has recursed, but other recursive locks cannot.
185 Giant must be locked first.
187 There are places in the kernel that drop Giant and pick it back up
189 Sleep locks will do this before sleeping.
190 Parts of the Network or VM code may do this as well, depending on the
192 This means that you cannot count on Giant keeping other code from
193 running if your code sleeps, even if you want it to.
204 handle event-based thread blocking.
205 If a thread must wait for an external event, it is put to sleep by
211 Threads may also wait using one of the locking primitive sleep routines
219 is an arbitrary address that uniquely identifies the event on which
220 the thread is being put to sleep.
221 All threads sleeping on a single
223 are woken up later by
225 often called from inside an interrupt routine, to indicate that the
226 resource the thread was blocking on is available now.
228 Several of the sleep functions including
231 and the locking primitive sleep routines specify an additional lock
233 The lock will be released before sleeping and reacquired
234 before the sleep routine returns.
239 flag, then the lock will not be reacquired before returning.
240 The lock is used to ensure that a condition can be checked atomically,
241 and that the current thread can be suspended without missing a
242 change to the condition, or an associated wakeup.
243 In addition, all of the sleep routines will fully drop the
247 while the thread is suspended and will reacquire the
249 mutex before the function returns.
251 .Ss lockmanager locks
252 Largely deprecated. See the
254 page for more information.
255 I don't know what the downsides are but I'm sure someone will fill in this part.
257 .Ss Interaction table.
258 The following table shows what you can and can not do if you hold
259 one of the synchronization primitives discussed here:
260 (someone who knows what they are talking about should write this table)
261 .Bl -column ".Ic xxxxxxxxxxxxxxxxxxxx" ".Xr XXXXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXX" -offset indent
263 .Em "You have: You want:" Ta Spin_mtx Ta Slp_mtx Ta sx_lock Ta rw_lock Ta sleep
265 .It Ic SPIN mutex Ta \&ok Ta \&no Ta \&no Ta \&no Ta \&no-3
266 .It Ic Sleep mutex Ta \&ok Ta \&ok-1 Ta \&no Ta \&ok Ta \&no-3
267 .It Ic sx_lock Ta \&ok Ta \&no Ta \&ok-2 Ta \&no Ta \&ok-4
268 .It Ic rw_lock Ta \&ok Ta \&ok Ta \&no Ta \&ok-2 Ta \&no-3
272 Recursion is defined per lock. Lock order is important.
275 readers can recurse though writers can not. Lock order is important.
278 There are calls atomically release this primitive when going to sleep
279 and reacquire it on wakeup (e.g.
287 Though one can sleep holding an sx lock, one can also use
289 which atomically release this primitive when going to sleep and
290 reacquire it on wakeup.
291 .Ss Context mode table.
292 The next table shows what can be used in different contexts.
293 At this time this is a rather easy to remember table.
294 .Bl -column ".Ic Xxxxxxxxxxxxxxxxxxxx" ".Xr XXXXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXX" -offset indent
296 .Em "Context:" Ta Spin_mtx Ta Slp_mtx Ta sx_lock Ta rw_lock Ta sleep
298 .It interrupt: Ta \&ok Ta \&no Ta \&no Ta \&no Ta \&no
299 .It idle: Ta \&ok Ta \&no Ta \&no Ta \&no Ta \&no
309 .Xr LOCK_PROFILING 9 ,
313 functions appeared in