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 sychronization primatives to allow the developers
41 to safely access and manipulate the many data types required.
52 Shared-Exclusive locks
69 The primnatives interact and have a number of rules regarding how
70 they can and can not be combined. Ther eare too many for the average humanmind and they
71 keep changing. (if you disagree, please write replacement text) :-)
73 Some of these primatives may be used at the low (interrupt) level and some may not.
75 There are strict ordering requirements and for some of the types this
81 Mutexes are the basic primative.
82 You either hold it or you don't.
83 If you don't own it then you just spin, waiting for the holder (on another CPU)
84 to release it. Hopefully they are doing something fast. You can not do anythign that
85 deschedules the thread while you are holding a SPIN mutex.
87 Basically sleep (regular) mutexes will deschedule the thread if
88 the mutex can not be acquired. As in spin mutexes, you either get it or you don't.
95 variant. These will atomically drop the mutex and reacquire it
98 A variant of SLEEP mutexes where the allocation of the mutex is handled more by the system.
100 Shared/exclusive locks are used to protect data that are read far more often
101 than they are written.
102 Mutexes are inherently more efficient than shared/exclusive locks, so
103 shared/exclusive locks should be used prudently.
104 A thread may hold a shared or exclusive lock on an
109 lock may not be acquired while holding a mutex.
110 Otherwise, if one thread slept while holding an
112 lock while another thread blocked on the same
114 lock after acquiring a mutex, then the second thread would effectively
115 end up sleeping while holding a mutex, which is not allowed.
117 Reader/writer locks allow shared access to protected data by multiple threads,
118 or exclusive access by a single thread.
119 The threads with shared access are known as
121 since they only read the protected data.
122 A thread with exclusive access is known as a
124 since it can modify protected data.
126 Although reader/writer locks look very similar to
128 locks, their usage pattern is different.
129 Reader/writer locks can be treated as mutexes (see
131 with shared/exclusive semantics.
136 can be locked while holding a non-spin mutex, and an
138 cannot be held while sleeping.
141 locks have priority propagation like mutexes, but priority
142 can be propagated only to an exclusive holder.
143 This limitation comes from the fact that shared owners
145 Another important property is that shared holders of
148 but exclusive locks are not allowed to recurse.
150 Turnstiles are used to hold a queue of threads blocked on
151 non-sleepable locks. Sleepable locks use condition variables to
152 implement their queues. Turnstiles differ from a sleep queue in that
153 turnstile queue's are assigned to a lock held by an owning thread. Thus,
154 when one thread is enqueued onto a turnstile, it can lend its priority
155 to the owning thread.
157 .Ss Condition variables
158 Condition variables are used in conjunction with mutexes to wait for conditions
160 A thread must hold the mutex before calling the
163 When a thread waits on a condition, the mutex
164 is atomically released before the thread is blocked, then reacquired
165 before the function call returns.
167 Giant is a special instance of a sleep lock.
168 it has several special characteristics.
178 handle event-based thread blocking.
179 If a thread must wait for an
180 external event, it is put to sleep by
186 Threads may also wait using one of the locking primitive sleep routines
194 is an arbitrary address that uniquely identifies the event on which
195 the thread is being put to sleep.
196 All threads sleeping on a single
198 are woken up later by
200 often called from inside an interrupt routine, to indicate that the
201 resource the thread was blocking on is available now.
203 Several of the sleep functions including
206 and the locking primitive sleep routines specify an additional lock
208 The lock will be released before sleeping and reacquired
209 before the sleep routine returns.
215 the lock will not be reacquired before returning.
216 The lock is used to ensure that a condition can be checked atomically,
217 and that the current thread can be suspended without missing a
218 change to the condition, or an associated wakeup.
219 In addition, all of the sleep routines will fully drop the
223 while the thread is suspended and will reacquire the
225 mutex before the function returns.
227 .Ss lockmanager locks
228 Largly deprecated. See the
230 page for more information.
231 I don't know what the downsides are but I'm sure someone will fill in this part.
233 .Ss Interaction table.
234 The following table shows what you can and can not do if you hold
235 one of the synchronisation primatives discussed here:
236 (someone who knows what they are talking about should write this table)
237 .Bl -column ".Ic xxxxxxxxxxxxxxxxxxxx" ".Xr XXXXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXX" -offset indent
239 .Em "You have: You want:" Ta Spin_mtx Ta Slp_mtx Ta sx_lock Ta rw_lock Ta sleep
241 .It Ic SPIN mutex Ta \&ok Ta \&no Ta \&no Ta \&no Ta \&no-3
242 .It Ic Sleep mutex Ta \&ok Ta \&ok-1 Ta \&no Ta \&ok Ta \&no-3
243 .It Ic sx_lock Ta \&ok Ta \&no Ta \&ok-2 Ta \&no Ta \&ok-4
244 .It Ic rw_lock Ta \&ok Ta \&ok Ta \&no Ta \&ok-2 Ta \&no-3
248 Recursion is defined per lock. lock order is important.
251 readers can recurse tough writers can not. lock order is important.
254 There are calls atomically release this primative when going to sleep
255 and reacquire it on wakeup (e.g.
262 Though one can sleep holding an sx lock, one can also use
264 which atomically release this primative when going to sleep and reacquire it on wakeup.
265 .Ss Context mode table.
266 The next table shows what can be used in differnt contexts. at this time this is a
267 rather easy to remember table.
268 .Bl -column ".Ic Xxxxxxxxxxxxxxxxxxxx" ".Xr XXXXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXX" -offset indent
270 .Em "Context:" Ta Spin_mtx Ta Slp_mtx Ta sx_lock Ta rw_lock Ta sleep
272 .It interrupt: Ta \&ok Ta \&no Ta \&no Ta \&no Ta \&no
273 .It idle: Ta \&ok Ta \&no Ta \&no Ta \&no Ta \&no
284 .Xr LOCK_PROFILING 9 ,
288 functions appeared in