10929bf8eSJulian Elischer.\" Copyright (c) 2007 Julian Elischer (julian - freebsd org ) 20929bf8eSJulian Elischer.\" All rights reserved. 30929bf8eSJulian Elischer.\" 40929bf8eSJulian Elischer.\" Redistribution and use in source and binary forms, with or without 50929bf8eSJulian Elischer.\" modification, are permitted provided that the following conditions 60929bf8eSJulian Elischer.\" are met: 70929bf8eSJulian Elischer.\" 1. Redistributions of source code must retain the above copyright 80929bf8eSJulian Elischer.\" notice, this list of conditions and the following disclaimer. 90929bf8eSJulian Elischer.\" 2. Redistributions in binary form must reproduce the above copyright 100929bf8eSJulian Elischer.\" notice, this list of conditions and the following disclaimer in the 110929bf8eSJulian Elischer.\" documentation and/or other materials provided with the distribution. 120929bf8eSJulian Elischer.\" 130929bf8eSJulian Elischer.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 140929bf8eSJulian Elischer.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 150929bf8eSJulian Elischer.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 160929bf8eSJulian Elischer.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 170929bf8eSJulian Elischer.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 180929bf8eSJulian Elischer.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 190929bf8eSJulian Elischer.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 200929bf8eSJulian Elischer.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 210929bf8eSJulian Elischer.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 220929bf8eSJulian Elischer.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 230929bf8eSJulian Elischer.\" SUCH DAMAGE. 240929bf8eSJulian Elischer.\" 250929bf8eSJulian Elischer.\" $FreeBSD$ 260929bf8eSJulian Elischer.\" 2718183ca9SEdward Tomasz Napierala.Dd May 25, 2012 280929bf8eSJulian Elischer.Dt LOCKING 9 290929bf8eSJulian Elischer.Os 300929bf8eSJulian Elischer.Sh NAME 3146d98f57SXin LI.Nm locking 320929bf8eSJulian Elischer.Nd kernel synchronization primitives 330929bf8eSJulian Elischer.Sh DESCRIPTION 340929bf8eSJulian ElischerThe 350929bf8eSJulian Elischer.Em FreeBSD 360929bf8eSJulian Elischerkernel is written to run across multiple CPUs and as such requires 374ff78a9cSJulian Elischerseveral different synchronization primitives to allow the developers 380929bf8eSJulian Elischerto safely access and manipulate the many data types required. 3920f16ca0SJulian Elischer.Ss Mutexes 40*2139f5e0SJulian ElischerMutexes (also erroneously called "sleep mutexes") are the most commonly used 41ddcd2bc9SEdward Tomasz Napieralasynchronization primitive in the kernel. 42*2139f5e0SJulian ElischerA thread acquires (locks) a mutex before accessing data shared with other 43815e4772SEdward Tomasz Napieralathreads (including interrupt threads), and releases (unlocks) it afterwards. 44*2139f5e0SJulian ElischerIf the mutex cannot be acquired, the thread requesting it will wait. 45*2139f5e0SJulian ElischerMutexes are by default adaptive, meaning that 46*2139f5e0SJulian Elischerif the owner of a contended mutex is currently running on another CPU, 47*2139f5e0SJulian Elischerthen a thread attempting to acquire the mutex will briefly spin 48*2139f5e0SJulian Elischerin the hope that the owner is only briefly holding it, 49*2139f5e0SJulian Elischerand might release it shortly. 50*2139f5e0SJulian ElischerIf the owner does not do so, the waiting thread proceeds to yield the processor, 51*2139f5e0SJulian Elischerallowing other threads to run. 52*2139f5e0SJulian ElischerIf the owner is not currently actually running then the spin step is skipped. 53561205dfSEdward Tomasz NapieralaMutexes fully support priority propagation. 54815e4772SEdward Tomasz Napierala.Pp 554ff467dcSEdward Tomasz NapieralaSee 56815e4772SEdward Tomasz Napierala.Xr mutex 9 574ff467dcSEdward Tomasz Napieralafor details. 58815e4772SEdward Tomasz Napierala.Ss Spin mutexes 59815e4772SEdward Tomasz NapieralaSpin mutexes are variation of basic mutexes; the main difference between 60*2139f5e0SJulian Elischerthe two is that spin mutexes never yield the processor - instead, they spin, 61*2139f5e0SJulian Elischerwaiting for the thread holding the lock, 62*2139f5e0SJulian Elischer(which must be running on another CPU), to release it. 63*2139f5e0SJulian ElischerSpin mutexes disable interrupts while the held so as to not get pre-empted. 64815e4772SEdward Tomasz NapieralaSince disabling interrupts is expensive, they are also generally slower. 65b06cfd40SJoel DahlSpin mutexes should be used only when necessary, e.g. to protect data shared 66ddcd2bc9SEdward Tomasz Napieralawith interrupt filter code (see 67ddcd2bc9SEdward Tomasz Napierala.Xr bus_setup_intr 9 68ddcd2bc9SEdward Tomasz Napieralafor details). 69815e4772SEdward Tomasz Napierala.Ss Pool mutexes 70b06cfd40SJoel DahlWith most synchronization primitives, such as mutexes, programmer must 71815e4772SEdward Tomasz Napieralaprovide a piece of allocated memory to hold the primitive. 72815e4772SEdward Tomasz NapieralaFor example, a mutex may be embedded inside the structure it protects. 73815e4772SEdward Tomasz NapieralaPool mutex is a variant of mutex without this requirement - to lock or unlock 74815e4772SEdward Tomasz Napieralaa pool mutex, one uses address of the structure being protected with it, 75815e4772SEdward Tomasz Napieralanot the mutex itself. 76815e4772SEdward Tomasz NapieralaPool mutexes are seldom used. 77815e4772SEdward Tomasz Napierala.Pp 784ff467dcSEdward Tomasz NapieralaSee 79815e4772SEdward Tomasz Napierala.Xr mtx_pool 9 804ff467dcSEdward Tomasz Napieralafor details. 81815e4772SEdward Tomasz Napierala.Ss Reader/writer locks 820929bf8eSJulian ElischerReader/writer locks allow shared access to protected data by multiple threads, 830929bf8eSJulian Elischeror exclusive access by a single thread. 840929bf8eSJulian ElischerThe threads with shared access are known as 850929bf8eSJulian Elischer.Em readers 8620f16ca0SJulian Elischersince they should only read the protected data. 870929bf8eSJulian ElischerA thread with exclusive access is known as a 880929bf8eSJulian Elischer.Em writer 8920f16ca0SJulian Elischersince it may modify protected data. 900929bf8eSJulian Elischer.Pp 9120f16ca0SJulian ElischerReader/writer locks can be treated as mutexes (see above and 920929bf8eSJulian Elischer.Xr mutex 9 ) 930929bf8eSJulian Elischerwith shared/exclusive semantics. 9420f16ca0SJulian ElischerMore specifically, regular mutexes can be 9520f16ca0SJulian Elischerconsidered to be equivalent to a write-lock on an 9620f16ca0SJulian Elischer.Em rw_lock. 970929bf8eSJulian ElischerThe 980929bf8eSJulian Elischer.Em rw_lock 990929bf8eSJulian Elischerlocks have priority propagation like mutexes, but priority 1000929bf8eSJulian Elischercan be propagated only to an exclusive holder. 1010929bf8eSJulian ElischerThis limitation comes from the fact that shared owners 1020929bf8eSJulian Elischerare anonymous. 1030929bf8eSJulian ElischerAnother important property is that shared holders of 1040929bf8eSJulian Elischer.Em rw_lock 10520f16ca0SJulian Elischercan recurse, but exclusive locks are not allowed to recurse. 10620f16ca0SJulian ElischerThis ability should not be used lightly and 10720f16ca0SJulian Elischer.Em may go away. 108815e4772SEdward Tomasz Napierala.Pp 1094ff467dcSEdward Tomasz NapieralaSee 110815e4772SEdward Tomasz Napierala.Xr rwlock 9 1114ff467dcSEdward Tomasz Napieralafor details. 112815e4772SEdward Tomasz Napierala.Ss Read-mostly locks 113f53d15feSStephan UphoffMostly reader locks are similar to 114561205dfSEdward Tomasz Napierala.Em reader/writer 115561205dfSEdward Tomasz Napieralalocks but optimized for very infrequent write locking. 116561205dfSEdward Tomasz Napierala.Em Read-mostly 117f53d15feSStephan Uphofflocks implement full priority propagation by tracking shared owners 11888238a08SEdward Tomasz Napieralausing a caller-supplied 119f53d15feSStephan Uphoff.Em tracker 120f53d15feSStephan Uphoffdata structure. 121815e4772SEdward Tomasz Napierala.Pp 1224ff467dcSEdward Tomasz NapieralaSee 123815e4772SEdward Tomasz Napierala.Xr rmlock 9 1244ff467dcSEdward Tomasz Napieralafor details. 125815e4772SEdward Tomasz Napierala.Ss Shared/exclusive locks 126561205dfSEdward Tomasz NapieralaShared/exclusive locks are similar to reader/writer locks; the main difference 127561205dfSEdward Tomasz Napieralabetween them is that shared/exclusive locks may be held during unbounded sleep 128561205dfSEdward Tomasz Napierala(and may thus perform an unbounded sleep). 129561205dfSEdward Tomasz NapieralaThey are inherently less efficient than mutexes, reader/writer locks 130b1321833SEdward Tomasz Napieralaand read-mostly locks. 131b1321833SEdward Tomasz NapieralaThey don't support priority propagation. 132561205dfSEdward Tomasz NapieralaThey should be considered to be closely related to 13320f16ca0SJulian Elischer.Xr sleep 9 . 134*2139f5e0SJulian ElischerThey could in some cases be 13520f16ca0SJulian Elischerconsidered a conditional sleep. 136815e4772SEdward Tomasz Napierala.Pp 1374ff467dcSEdward Tomasz NapieralaSee 138815e4772SEdward Tomasz Napierala.Xr sx 9 1394ff467dcSEdward Tomasz Napieralafor details. 140815e4772SEdward Tomasz Napierala.Ss Counting semaphores 141815e4772SEdward Tomasz NapieralaCounting semaphores provide a mechanism for synchronizing access 142815e4772SEdward Tomasz Napieralato a pool of resources. 143815e4772SEdward Tomasz NapieralaUnlike mutexes, semaphores do not have the concept of an owner, 144815e4772SEdward Tomasz Napieralaso they can be useful in situations where one thread needs 145815e4772SEdward Tomasz Napieralato acquire a resource, and another thread needs to release it. 146815e4772SEdward Tomasz NapieralaThey are largely deprecated. 147815e4772SEdward Tomasz Napierala.Pp 1484ff467dcSEdward Tomasz NapieralaSee 149815e4772SEdward Tomasz Napierala.Xr sema 9 1504ff467dcSEdward Tomasz Napieralafor details. 1510929bf8eSJulian Elischer.Ss Condition variables 1524ff78a9cSJulian ElischerCondition variables are used in conjunction with mutexes to wait for 1534ff78a9cSJulian Elischerconditions to occur. 1540929bf8eSJulian ElischerA thread must hold the mutex before calling the 1550929bf8eSJulian Elischer.Fn cv_wait* , 1560929bf8eSJulian Elischerfunctions. 1570929bf8eSJulian ElischerWhen a thread waits on a condition, the mutex 158*2139f5e0SJulian Elischeris atomically released before the thread thread yields the processor, 159*2139f5e0SJulian Elischerthen reacquired before the function call returns. 160815e4772SEdward Tomasz Napierala.Pp 1614ff467dcSEdward Tomasz NapieralaSee 162815e4772SEdward Tomasz Napierala.Xr condvar 9 1634ff467dcSEdward Tomasz Napieralafor details. 1640929bf8eSJulian Elischer.Ss Giant 165561205dfSEdward Tomasz NapieralaGiant is an instance of a mutex, with some special characteristics: 166fbe508ffSWarner Losh.Bl -enum 167fbe508ffSWarner Losh.It 168fbe508ffSWarner LoshIt is recursive. 169fbe508ffSWarner Losh.It 1700b0b48c0SEdward Tomasz NapieralaDrivers and filesystems can request that Giant be locked around them 171b1321833SEdward Tomasz Napieralaby not marking themselves MPSAFE. 172b1321833SEdward Tomasz NapieralaNote that infrastructure to do this is slowly going away as non-MPSAFE 173b1321833SEdward Tomasz Napieraladrivers either became properly locked or disappear. 174fbe508ffSWarner Losh.It 17520f16ca0SJulian ElischerGiant must be locked first before other locks. 176fbe508ffSWarner Losh.It 1770b0b48c0SEdward Tomasz NapieralaIt is OK to hold Giant while performing unbounded sleep; in such case, 1780b0b48c0SEdward Tomasz NapieralaGiant will be dropped before sleeping and picked up after wakeup. 1790b0b48c0SEdward Tomasz Napierala.It 180fbe508ffSWarner LoshThere are places in the kernel that drop Giant and pick it back up 181fbe508ffSWarner Loshagain. 182fbe508ffSWarner LoshSleep locks will do this before sleeping. 183561205dfSEdward Tomasz NapieralaParts of the network or VM code may do this as well, depending on the 184fbe508ffSWarner Loshsetting of a sysctl. 185fbe508ffSWarner LoshThis means that you cannot count on Giant keeping other code from 186fbe508ffSWarner Loshrunning if your code sleeps, even if you want it to. 187fbe508ffSWarner Losh.El 1880929bf8eSJulian Elischer.Ss Sleep/wakeup 1890929bf8eSJulian ElischerThe functions 1900929bf8eSJulian Elischer.Fn tsleep , 1910929bf8eSJulian Elischer.Fn msleep , 1920929bf8eSJulian Elischer.Fn msleep_spin , 1930929bf8eSJulian Elischer.Fn pause , 1940929bf8eSJulian Elischer.Fn wakeup , 1950929bf8eSJulian Elischerand 1960929bf8eSJulian Elischer.Fn wakeup_one 1970929bf8eSJulian Elischerhandle event-based thread blocking. 1984ff78a9cSJulian ElischerIf a thread must wait for an external event, it is put to sleep by 1990929bf8eSJulian Elischer.Fn tsleep , 2000929bf8eSJulian Elischer.Fn msleep , 2010929bf8eSJulian Elischer.Fn msleep_spin , 2020929bf8eSJulian Elischeror 2030929bf8eSJulian Elischer.Fn pause . 2040929bf8eSJulian ElischerThreads may also wait using one of the locking primitive sleep routines 2050929bf8eSJulian Elischer.Xr mtx_sleep 9 , 2060929bf8eSJulian Elischer.Xr rw_sleep 9 , 2070929bf8eSJulian Elischeror 2080929bf8eSJulian Elischer.Xr sx_sleep 9 . 2090929bf8eSJulian Elischer.Pp 2100929bf8eSJulian ElischerThe parameter 2110929bf8eSJulian Elischer.Fa chan 2120929bf8eSJulian Elischeris an arbitrary address that uniquely identifies the event on which 2130929bf8eSJulian Elischerthe thread is being put to sleep. 2140929bf8eSJulian ElischerAll threads sleeping on a single 2150929bf8eSJulian Elischer.Fa chan 2160929bf8eSJulian Elischerare woken up later by 2170929bf8eSJulian Elischer.Fn wakeup , 2180929bf8eSJulian Elischeroften called from inside an interrupt routine, to indicate that the 2190929bf8eSJulian Elischerresource the thread was blocking on is available now. 2200929bf8eSJulian Elischer.Pp 2210929bf8eSJulian ElischerSeveral of the sleep functions including 2220929bf8eSJulian Elischer.Fn msleep , 2230929bf8eSJulian Elischer.Fn msleep_spin , 2240929bf8eSJulian Elischerand the locking primitive sleep routines specify an additional lock 2250929bf8eSJulian Elischerparameter. 2260929bf8eSJulian ElischerThe lock will be released before sleeping and reacquired 2270929bf8eSJulian Elischerbefore the sleep routine returns. 2280929bf8eSJulian ElischerIf 2290929bf8eSJulian Elischer.Fa priority 2300929bf8eSJulian Elischerincludes the 2310929bf8eSJulian Elischer.Dv PDROP 2324ff78a9cSJulian Elischerflag, then the lock will not be reacquired before returning. 2330929bf8eSJulian ElischerThe lock is used to ensure that a condition can be checked atomically, 2340929bf8eSJulian Elischerand that the current thread can be suspended without missing a 2350929bf8eSJulian Elischerchange to the condition, or an associated wakeup. 2360929bf8eSJulian ElischerIn addition, all of the sleep routines will fully drop the 2370929bf8eSJulian Elischer.Va Giant 2380929bf8eSJulian Elischermutex 2390929bf8eSJulian Elischer(even if recursed) 2400929bf8eSJulian Elischerwhile the thread is suspended and will reacquire the 2410929bf8eSJulian Elischer.Va Giant 2420929bf8eSJulian Elischermutex before the function returns. 2430929bf8eSJulian Elischer.Pp 2444ff467dcSEdward Tomasz NapieralaSee 245815e4772SEdward Tomasz Napierala.Xr sleep 9 2464ff467dcSEdward Tomasz Napieralafor details. 247815e4772SEdward Tomasz Napierala.Ss Lockmanager locks 248561205dfSEdward Tomasz NapieralaShared/exclusive locks, used mostly in 249815e4772SEdward Tomasz Napierala.Xr VFS 9 , 250815e4772SEdward Tomasz Napieralain particular as a 251815e4772SEdward Tomasz Napierala.Xr vnode 9 252815e4772SEdward Tomasz Napieralalock. 253815e4772SEdward Tomasz NapieralaThey have features other lock types don't have, such as sleep timeout, 254815e4772SEdward Tomasz Napieralawriter starvation avoidance, draining, and interlock mutex, but this makes them 255815e4772SEdward Tomasz Napieralacomplicated to implement; for this reason, they are deprecated. 256815e4772SEdward Tomasz Napierala.Pp 2574ff467dcSEdward Tomasz NapieralaSee 2580929bf8eSJulian Elischer.Xr lock 9 2594ff467dcSEdward Tomasz Napieralafor details. 260815e4772SEdward Tomasz Napierala.Sh INTERACTIONS 261ddcd2bc9SEdward Tomasz NapieralaThe primitives interact and have a number of rules regarding how 262ddcd2bc9SEdward Tomasz Napieralathey can and can not be combined. 263ddcd2bc9SEdward Tomasz NapieralaMany of these rules are checked using the 264ddcd2bc9SEdward Tomasz Napierala.Xr witness 4 265ddcd2bc9SEdward Tomasz Napieralacode. 266561205dfSEdward Tomasz Napierala.Ss Bounded vs. unbounded sleep 267*2139f5e0SJulian ElischerThe following primitives perform bounded sleep: 268*2139f5e0SJulian Elischer mutexes, pool mutexes, reader/writer locks and read-mostly locks. 269561205dfSEdward Tomasz Napierala.Pp 270*2139f5e0SJulian ElischerThe following primitives may perform an unbounded sleep: 271*2139f5e0SJulian Elischershared/exclusive locks, counting semaphores, condition variables, sleep/wakeup and lockmanager locks. 272561205dfSEdward Tomasz Napierala.Pp 273*2139f5e0SJulian ElischerIt is an error to do any operation that could result in yielding the processor 274*2139f5e0SJulian Elischerwhile holding a spin mutex. 275561205dfSEdward Tomasz Napierala.Pp 276561205dfSEdward Tomasz NapieralaAs a general rule, it is an error to do any operation that could result 277561205dfSEdward Tomasz Napieralain unbounded sleep while holding any primitive from the 'bounded sleep' group. 278561205dfSEdward Tomasz NapieralaFor example, it is an error to try to acquire shared/exclusive lock while 279561205dfSEdward Tomasz Napieralaholding mutex, or to try to allocate memory with M_WAITOK while holding 280561205dfSEdward Tomasz Napieralaread-write lock. 281561205dfSEdward Tomasz Napierala.Pp 282561205dfSEdward Tomasz NapieralaAs a special case, it is possible to call 28388238a08SEdward Tomasz Napierala.Fn sleep 284561205dfSEdward Tomasz Napieralaor 28588238a08SEdward Tomasz Napierala.Fn mtx_sleep 28688238a08SEdward Tomasz Napieralawhile holding a single mutex. 28788238a08SEdward Tomasz NapieralaIt will atomically drop that mutex and reacquire it as part of waking up. 28888238a08SEdward Tomasz NapieralaThis is often a bad idea because it generally relies on the programmer having 28988238a08SEdward Tomasz Napieralagood knowledge of all of the call graph above the place where 29088238a08SEdward Tomasz Napierala.Fn mtx_sleep 29188238a08SEdward Tomasz Napieralais being called and assumptions the calling code has made. 2920dded339SGlen BarberBecause the lock gets dropped during sleep, one must re-test all 29388238a08SEdward Tomasz Napieralathe assumptions that were made before, all the way up the call graph to the 29488238a08SEdward Tomasz Napieralaplace where the lock was acquired. 2950b0b48c0SEdward Tomasz Napierala.Pp 296*2139f5e0SJulian ElischerIt is an error to do any operation that could result in yielding of 297*2139f5e0SJulian Elischerthe processor when running inside an interrupt filter. 298ddcd2bc9SEdward Tomasz Napierala.Pp 2990b0b48c0SEdward Tomasz NapieralaIt is an error to do any operation that could result in unbounded sleep when 3000b0b48c0SEdward Tomasz Napieralarunning inside an interrupt thread. 301561205dfSEdward Tomasz Napierala.Ss Interaction table 302ddcd2bc9SEdward Tomasz NapieralaThe following table shows what you can and can not do while holding 303ddcd2bc9SEdward Tomasz Napieralaone of the synchronization primitives discussed: 304*2139f5e0SJulian Elischer.Bl -column ".Ic xxxxxxxxxxxxxxxx" ".Xr XXXXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXX" -offset indent 305*2139f5e0SJulian Elischer.It Em " You want:" Ta spin-mtx Ta mutex Ta rwlock Ta rmlock Ta sx Ta sleep 306*2139f5e0SJulian Elischer.It Em "You have: " Ta ------ Ta ------ Ta ------ Ta ------ Ta ------ Ta ------ 307ddcd2bc9SEdward Tomasz Napierala.It spin mtx Ta \&ok-1 Ta \&no Ta \&no Ta \&no Ta \&no Ta \&no-3 308*2139f5e0SJulian Elischer.It mutex Ta \&ok Ta \&ok-1 Ta \&ok Ta \&ok Ta \&no Ta \&no-3 309*2139f5e0SJulian Elischer.It rwlock Ta \&ok Ta \&ok Ta \&ok-2 Ta \&ok Ta \&no Ta \&no-3 310*2139f5e0SJulian Elischer.It rmlock Ta \&ok Ta \&ok Ta \&ok Ta \&ok-2 Ta \&no-5 Ta \&no-5 311*2139f5e0SJulian Elischer.It sx Ta \&ok Ta \&ok Ta \&ok Ta \&ok Ta \&no-2 Ta \&ok-4 3120929bf8eSJulian Elischer.El 3130929bf8eSJulian Elischer.Pp 3140929bf8eSJulian Elischer.Em *1 31520f16ca0SJulian ElischerRecursion is defined per lock. 31620f16ca0SJulian ElischerLock order is important. 3170929bf8eSJulian Elischer.Pp 3180929bf8eSJulian Elischer.Em *2 31988238a08SEdward Tomasz NapieralaReaders can recurse though writers can not. 32020f16ca0SJulian ElischerLock order is important. 3210929bf8eSJulian Elischer.Pp 3220929bf8eSJulian Elischer.Em *3 32388238a08SEdward Tomasz NapieralaThere are calls that atomically release this primitive when going to sleep 3240929bf8eSJulian Elischerand reacquire it on wakeup (e.g. 3250929bf8eSJulian Elischer.Fn mtx_sleep , 32634e1b9e5SJulian Elischer.Fn rw_sleep 3270929bf8eSJulian Elischerand 32873bbeaa5SGlen Barber.Fn msleep_spin ) . 3290929bf8eSJulian Elischer.Pp 3300929bf8eSJulian Elischer.Em *4 33134e1b9e5SJulian ElischerThough one can sleep holding an sx lock, one can also use 3320929bf8eSJulian Elischer.Fn sx_sleep 33388238a08SEdward Tomasz Napieralawhich will atomically release this primitive when going to sleep and 3344ff78a9cSJulian Elischerreacquire it on wakeup. 33536058c09SMax Laier.Pp 33636058c09SMax Laier.Em *5 33736058c09SMax Laier.Em Read-mostly 33836058c09SMax Laierlocks can be initialized to support sleeping while holding a write lock. 33936058c09SMax LaierSee 34036058c09SMax Laier.Xr rmlock 9 34136058c09SMax Laierfor details. 342561205dfSEdward Tomasz Napierala.Ss Context mode table 3434ff78a9cSJulian ElischerThe next table shows what can be used in different contexts. 3444ff78a9cSJulian ElischerAt this time this is a rather easy to remember table. 345561205dfSEdward Tomasz Napierala.Bl -column ".Ic Xxxxxxxxxxxxxxxxxxx" ".Xr XXXXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXX" -offset indent 3464008d26aSJoel Dahl.It Em "Context:" Ta spin mtx Ta mutex Ta sx Ta rwlock Ta rmlock Ta sleep 347ddcd2bc9SEdward Tomasz Napierala.It interrupt filter: Ta \&ok Ta \&no Ta \&no Ta \&no Ta \&no Ta \&no 3486b85cd6dSEdward Tomasz Napierala.It interrupt thread: Ta \&ok Ta \&ok Ta \&no Ta \&ok Ta \&ok Ta \&no 349ddcd2bc9SEdward Tomasz Napierala.It callout: Ta \&ok Ta \&ok Ta \&no Ta \&ok Ta \&no Ta \&no 350ddcd2bc9SEdward Tomasz Napierala.It syscall: Ta \&ok Ta \&ok Ta \&ok Ta \&ok Ta \&ok Ta \&ok 351c2c54c3dSJulian Elischer.El 3520929bf8eSJulian Elischer.Sh SEE ALSO 35388238a08SEdward Tomasz Napierala.Xr witness 4 , 3540929bf8eSJulian Elischer.Xr condvar 9 , 3553111fc98SChristian Brueffer.Xr lock 9 , 3560929bf8eSJulian Elischer.Xr mtx_pool 9 , 35720f16ca0SJulian Elischer.Xr mutex 9 , 358f53d15feSStephan Uphoff.Xr rmlock 9 , 3590929bf8eSJulian Elischer.Xr rwlock 9 , 3600929bf8eSJulian Elischer.Xr sema 9 , 3610929bf8eSJulian Elischer.Xr sleep 9 , 3623111fc98SChristian Brueffer.Xr sx 9 , 3638de599ecSEdward Tomasz Napierala.Xr BUS_SETUP_INTR 9 , 364815e4772SEdward Tomasz Napierala.Xr LOCK_PROFILING 9 3650929bf8eSJulian Elischer.Sh HISTORY 3660929bf8eSJulian ElischerThese 3670929bf8eSJulian Elischerfunctions appeared in 3680929bf8eSJulian Elischer.Bsx 4.1 3690929bf8eSJulian Elischerthrough 37073bbeaa5SGlen Barber.Fx 7.0 . 371815e4772SEdward Tomasz Napierala.Sh BUGS 372815e4772SEdward Tomasz NapieralaThere are too many locking primitives to choose from. 373