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.\" 27ddcd2bc9SEdward Tomasz Napierala.Dd February 13, 2010 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 40ddcd2bc9SEdward Tomasz NapieralaMutexes (also called "sleep mutexes") are the most commonly used 41ddcd2bc9SEdward Tomasz Napieralasynchronization primitive in the kernel. 42815e4772SEdward Tomasz NapieralaThread acquires (locks) a mutex before accessing data shared with other 43815e4772SEdward Tomasz Napieralathreads (including interrupt threads), and releases (unlocks) it afterwards. 44561205dfSEdward Tomasz NapieralaIf the mutex cannot be acquired, the thread requesting it will sleep. 45561205dfSEdward Tomasz NapieralaMutexes fully support priority propagation. 46815e4772SEdward Tomasz Napierala.Pp 474ff467dcSEdward Tomasz NapieralaSee 48815e4772SEdward Tomasz Napierala.Xr mutex 9 494ff467dcSEdward Tomasz Napieralafor details. 50815e4772SEdward Tomasz Napierala.Ss Spin mutexes 51815e4772SEdward Tomasz NapieralaSpin mutexes are variation of basic mutexes; the main difference between 52561205dfSEdward Tomasz Napieralathe two is that spin mutexes never sleep - instead, they spin, waiting 53815e4772SEdward Tomasz Napieralafor the thread holding the lock, which runs on another CPU, to release it. 54815e4772SEdward Tomasz NapieralaDifferently from ordinary mutex, spin mutexes disable interrupts when acquired. 55815e4772SEdward Tomasz NapieralaSince disabling interrupts is expensive, they are also generally slower. 56ddcd2bc9SEdward Tomasz NapieralaSpin mutexes should be used only when neccessary, e.g. to protect data shared 57ddcd2bc9SEdward Tomasz Napieralawith interrupt filter code (see 58ddcd2bc9SEdward Tomasz Napierala.Xr bus_setup_intr 9 59ddcd2bc9SEdward Tomasz Napieralafor details). 60815e4772SEdward Tomasz Napierala.Ss Pool mutexes 61815e4772SEdward Tomasz NapieralaWith most synchronisaton primitives, such as mutexes, programmer must 62815e4772SEdward Tomasz Napieralaprovide a piece of allocated memory to hold the primitive. 63815e4772SEdward Tomasz NapieralaFor example, a mutex may be embedded inside the structure it protects. 64815e4772SEdward Tomasz NapieralaPool mutex is a variant of mutex without this requirement - to lock or unlock 65815e4772SEdward Tomasz Napieralaa pool mutex, one uses address of the structure being protected with it, 66815e4772SEdward Tomasz Napieralanot the mutex itself. 67815e4772SEdward Tomasz NapieralaPool mutexes are seldom used. 68815e4772SEdward Tomasz Napierala.Pp 694ff467dcSEdward Tomasz NapieralaSee 70815e4772SEdward Tomasz Napierala.Xr mtx_pool 9 714ff467dcSEdward Tomasz Napieralafor details. 72815e4772SEdward Tomasz Napierala.Ss Reader/writer locks 730929bf8eSJulian ElischerReader/writer locks allow shared access to protected data by multiple threads, 740929bf8eSJulian Elischeror exclusive access by a single thread. 750929bf8eSJulian ElischerThe threads with shared access are known as 760929bf8eSJulian Elischer.Em readers 7720f16ca0SJulian Elischersince they should only read the protected data. 780929bf8eSJulian ElischerA thread with exclusive access is known as a 790929bf8eSJulian Elischer.Em writer 8020f16ca0SJulian Elischersince it may modify protected data. 810929bf8eSJulian Elischer.Pp 8220f16ca0SJulian ElischerReader/writer locks can be treated as mutexes (see above and 830929bf8eSJulian Elischer.Xr mutex 9 ) 840929bf8eSJulian Elischerwith shared/exclusive semantics. 8520f16ca0SJulian ElischerMore specifically, regular mutexes can be 8620f16ca0SJulian Elischerconsidered to be equivalent to a write-lock on an 8720f16ca0SJulian Elischer.Em rw_lock. 880929bf8eSJulian ElischerThe 890929bf8eSJulian Elischer.Em rw_lock 900929bf8eSJulian Elischerlocks have priority propagation like mutexes, but priority 910929bf8eSJulian Elischercan be propagated only to an exclusive holder. 920929bf8eSJulian ElischerThis limitation comes from the fact that shared owners 930929bf8eSJulian Elischerare anonymous. 940929bf8eSJulian ElischerAnother important property is that shared holders of 950929bf8eSJulian Elischer.Em rw_lock 9620f16ca0SJulian Elischercan recurse, but exclusive locks are not allowed to recurse. 9720f16ca0SJulian ElischerThis ability should not be used lightly and 9820f16ca0SJulian Elischer.Em may go away. 99815e4772SEdward Tomasz Napierala.Pp 1004ff467dcSEdward Tomasz NapieralaSee 101815e4772SEdward Tomasz Napierala.Xr rwlock 9 1024ff467dcSEdward Tomasz Napieralafor details. 103815e4772SEdward Tomasz Napierala.Ss Read-mostly locks 104f53d15feSStephan UphoffMostly reader locks are similar to 105561205dfSEdward Tomasz Napierala.Em reader/writer 106561205dfSEdward Tomasz Napieralalocks but optimized for very infrequent write locking. 107561205dfSEdward Tomasz Napierala.Em Read-mostly 108f53d15feSStephan Uphofflocks implement full priority propagation by tracking shared owners 109f53d15feSStephan Uphoffusing a lock user supplied 110f53d15feSStephan Uphoff.Em tracker 111f53d15feSStephan Uphoffdata structure. 112815e4772SEdward Tomasz Napierala.Pp 1134ff467dcSEdward Tomasz NapieralaSee 114815e4772SEdward Tomasz Napierala.Xr rmlock 9 1154ff467dcSEdward Tomasz Napieralafor details. 116815e4772SEdward Tomasz Napierala.Ss Shared/exclusive locks 117561205dfSEdward Tomasz NapieralaShared/exclusive locks are similar to reader/writer locks; the main difference 118561205dfSEdward Tomasz Napieralabetween them is that shared/exclusive locks may be held during unbounded sleep 119561205dfSEdward Tomasz Napierala(and may thus perform an unbounded sleep). 120561205dfSEdward Tomasz NapieralaThey are inherently less efficient than mutexes, reader/writer locks 121b1321833SEdward Tomasz Napieralaand read-mostly locks. 122b1321833SEdward Tomasz NapieralaThey don't support priority propagation. 123561205dfSEdward Tomasz NapieralaThey should be considered to be closely related to 12420f16ca0SJulian Elischer.Xr sleep 9 . 12520f16ca0SJulian ElischerIn fact it could in some cases be 12620f16ca0SJulian Elischerconsidered a conditional sleep. 127815e4772SEdward Tomasz Napierala.Pp 1284ff467dcSEdward Tomasz NapieralaSee 129815e4772SEdward Tomasz Napierala.Xr sx 9 1304ff467dcSEdward Tomasz Napieralafor details. 131815e4772SEdward Tomasz Napierala.Ss Counting semaphores 132815e4772SEdward Tomasz NapieralaCounting semaphores provide a mechanism for synchronizing access 133815e4772SEdward Tomasz Napieralato a pool of resources. 134815e4772SEdward Tomasz NapieralaUnlike mutexes, semaphores do not have the concept of an owner, 135815e4772SEdward Tomasz Napieralaso they can be useful in situations where one thread needs 136815e4772SEdward Tomasz Napieralato acquire a resource, and another thread needs to release it. 137815e4772SEdward Tomasz NapieralaThey are largely deprecated. 138815e4772SEdward Tomasz Napierala.Pp 1394ff467dcSEdward Tomasz NapieralaSee 140815e4772SEdward Tomasz Napierala.Xr sema 9 1414ff467dcSEdward Tomasz Napieralafor details. 1420929bf8eSJulian Elischer.Ss Condition variables 1434ff78a9cSJulian ElischerCondition variables are used in conjunction with mutexes to wait for 1444ff78a9cSJulian Elischerconditions to occur. 1450929bf8eSJulian ElischerA thread must hold the mutex before calling the 1460929bf8eSJulian Elischer.Fn cv_wait* , 1470929bf8eSJulian Elischerfunctions. 1480929bf8eSJulian ElischerWhen a thread waits on a condition, the mutex 1490929bf8eSJulian Elischeris atomically released before the thread is blocked, then reacquired 1500929bf8eSJulian Elischerbefore the function call returns. 151815e4772SEdward Tomasz Napierala.Pp 1524ff467dcSEdward Tomasz NapieralaSee 153815e4772SEdward Tomasz Napierala.Xr condvar 9 1544ff467dcSEdward Tomasz Napieralafor details. 1550929bf8eSJulian Elischer.Ss Giant 156561205dfSEdward Tomasz NapieralaGiant is an instance of a mutex, with some special characteristics: 157fbe508ffSWarner Losh.Bl -enum 158fbe508ffSWarner Losh.It 159fbe508ffSWarner LoshIt is recursive. 160fbe508ffSWarner Losh.It 1610b0b48c0SEdward Tomasz NapieralaDrivers and filesystems can request that Giant be locked around them 162b1321833SEdward Tomasz Napieralaby not marking themselves MPSAFE. 163b1321833SEdward Tomasz NapieralaNote that infrastructure to do this is slowly going away as non-MPSAFE 164b1321833SEdward Tomasz Napieraladrivers either became properly locked or disappear. 165fbe508ffSWarner Losh.It 16620f16ca0SJulian ElischerGiant must be locked first before other locks. 167fbe508ffSWarner Losh.It 1680b0b48c0SEdward Tomasz NapieralaIt is OK to hold Giant while performing unbounded sleep; in such case, 1690b0b48c0SEdward Tomasz NapieralaGiant will be dropped before sleeping and picked up after wakeup. 1700b0b48c0SEdward Tomasz Napierala.It 171fbe508ffSWarner LoshThere are places in the kernel that drop Giant and pick it back up 172fbe508ffSWarner Loshagain. 173fbe508ffSWarner LoshSleep locks will do this before sleeping. 174561205dfSEdward Tomasz NapieralaParts of the network or VM code may do this as well, depending on the 175fbe508ffSWarner Loshsetting of a sysctl. 176fbe508ffSWarner LoshThis means that you cannot count on Giant keeping other code from 177fbe508ffSWarner Loshrunning if your code sleeps, even if you want it to. 178fbe508ffSWarner Losh.El 1790929bf8eSJulian Elischer.Ss Sleep/wakeup 1800929bf8eSJulian ElischerThe functions 1810929bf8eSJulian Elischer.Fn tsleep , 1820929bf8eSJulian Elischer.Fn msleep , 1830929bf8eSJulian Elischer.Fn msleep_spin , 1840929bf8eSJulian Elischer.Fn pause , 1850929bf8eSJulian Elischer.Fn wakeup , 1860929bf8eSJulian Elischerand 1870929bf8eSJulian Elischer.Fn wakeup_one 1880929bf8eSJulian Elischerhandle event-based thread blocking. 1894ff78a9cSJulian ElischerIf a thread must wait for an external event, it is put to sleep by 1900929bf8eSJulian Elischer.Fn tsleep , 1910929bf8eSJulian Elischer.Fn msleep , 1920929bf8eSJulian Elischer.Fn msleep_spin , 1930929bf8eSJulian Elischeror 1940929bf8eSJulian Elischer.Fn pause . 1950929bf8eSJulian ElischerThreads may also wait using one of the locking primitive sleep routines 1960929bf8eSJulian Elischer.Xr mtx_sleep 9 , 1970929bf8eSJulian Elischer.Xr rw_sleep 9 , 1980929bf8eSJulian Elischeror 1990929bf8eSJulian Elischer.Xr sx_sleep 9 . 2000929bf8eSJulian Elischer.Pp 2010929bf8eSJulian ElischerThe parameter 2020929bf8eSJulian Elischer.Fa chan 2030929bf8eSJulian Elischeris an arbitrary address that uniquely identifies the event on which 2040929bf8eSJulian Elischerthe thread is being put to sleep. 2050929bf8eSJulian ElischerAll threads sleeping on a single 2060929bf8eSJulian Elischer.Fa chan 2070929bf8eSJulian Elischerare woken up later by 2080929bf8eSJulian Elischer.Fn wakeup , 2090929bf8eSJulian Elischeroften called from inside an interrupt routine, to indicate that the 2100929bf8eSJulian Elischerresource the thread was blocking on is available now. 2110929bf8eSJulian Elischer.Pp 2120929bf8eSJulian ElischerSeveral of the sleep functions including 2130929bf8eSJulian Elischer.Fn msleep , 2140929bf8eSJulian Elischer.Fn msleep_spin , 2150929bf8eSJulian Elischerand the locking primitive sleep routines specify an additional lock 2160929bf8eSJulian Elischerparameter. 2170929bf8eSJulian ElischerThe lock will be released before sleeping and reacquired 2180929bf8eSJulian Elischerbefore the sleep routine returns. 2190929bf8eSJulian ElischerIf 2200929bf8eSJulian Elischer.Fa priority 2210929bf8eSJulian Elischerincludes the 2220929bf8eSJulian Elischer.Dv PDROP 2234ff78a9cSJulian Elischerflag, then the lock will not be reacquired before returning. 2240929bf8eSJulian ElischerThe lock is used to ensure that a condition can be checked atomically, 2250929bf8eSJulian Elischerand that the current thread can be suspended without missing a 2260929bf8eSJulian Elischerchange to the condition, or an associated wakeup. 2270929bf8eSJulian ElischerIn addition, all of the sleep routines will fully drop the 2280929bf8eSJulian Elischer.Va Giant 2290929bf8eSJulian Elischermutex 2300929bf8eSJulian Elischer(even if recursed) 2310929bf8eSJulian Elischerwhile the thread is suspended and will reacquire the 2320929bf8eSJulian Elischer.Va Giant 2330929bf8eSJulian Elischermutex before the function returns. 2340929bf8eSJulian Elischer.Pp 2354ff467dcSEdward Tomasz NapieralaSee 236815e4772SEdward Tomasz Napierala.Xr sleep 9 2374ff467dcSEdward Tomasz Napieralafor details. 238815e4772SEdward Tomasz Napierala.Pp 239815e4772SEdward Tomasz Napierala.Ss Lockmanager locks 240561205dfSEdward Tomasz NapieralaShared/exclusive locks, used mostly in 241815e4772SEdward Tomasz Napierala.Xr VFS 9 , 242815e4772SEdward Tomasz Napieralain particular as a 243815e4772SEdward Tomasz Napierala.Xr vnode 9 244815e4772SEdward Tomasz Napieralalock. 245815e4772SEdward Tomasz NapieralaThey have features other lock types don't have, such as sleep timeout, 246815e4772SEdward Tomasz Napieralawriter starvation avoidance, draining, and interlock mutex, but this makes them 247815e4772SEdward Tomasz Napieralacomplicated to implement; for this reason, they are deprecated. 248815e4772SEdward Tomasz Napierala.Pp 2494ff467dcSEdward Tomasz NapieralaSee 2500929bf8eSJulian Elischer.Xr lock 9 2514ff467dcSEdward Tomasz Napieralafor details. 252815e4772SEdward Tomasz Napierala.Sh INTERACTIONS 253ddcd2bc9SEdward Tomasz NapieralaThe primitives interact and have a number of rules regarding how 254ddcd2bc9SEdward Tomasz Napieralathey can and can not be combined. 255ddcd2bc9SEdward Tomasz NapieralaMany of these rules are checked using the 256ddcd2bc9SEdward Tomasz Napierala.Xr witness 4 257ddcd2bc9SEdward Tomasz Napieralacode. 258561205dfSEdward Tomasz Napierala.Ss Bounded vs. unbounded sleep 259561205dfSEdward Tomasz NapieralaThe following primitives perform bounded sleep: mutexes, pool mutexes, 260561205dfSEdward Tomasz Napieralareader/writer locks and read-mostly locks. 261561205dfSEdward Tomasz Napierala.Pp 262561205dfSEdward Tomasz NapieralaThe following primitives block (perform unbounded sleep): shared/exclusive locks, 263561205dfSEdward Tomasz Napieralacounting semaphores, condition variables, sleep/wakeup and lockmanager locks. 264561205dfSEdward Tomasz Napierala.Pp 265561205dfSEdward Tomasz NapieralaIt is an error to do any operation that could result in any kind of sleep while 266561205dfSEdward Tomasz Napieralaholding spin mutex. 267561205dfSEdward Tomasz Napierala.Pp 268561205dfSEdward Tomasz NapieralaAs a general rule, it is an error to do any operation that could result 269561205dfSEdward Tomasz Napieralain unbounded sleep while holding any primitive from the 'bounded sleep' group. 270561205dfSEdward Tomasz NapieralaFor example, it is an error to try to acquire shared/exclusive lock while 271561205dfSEdward Tomasz Napieralaholding mutex, or to try to allocate memory with M_WAITOK while holding 272561205dfSEdward Tomasz Napieralaread-write lock. 273561205dfSEdward Tomasz Napierala.Pp 274561205dfSEdward Tomasz NapieralaAs a special case, it is possible to call 275561205dfSEdward Tomasz Napierala.Fn sleep 9 276561205dfSEdward Tomasz Napieralaor 277561205dfSEdward Tomasz Napierala.Fn mtx_sleep 9 278561205dfSEdward Tomasz Napieralawhile holding a mutex. 279561205dfSEdward Tomasz NapieralaIt will atomically drop the mutex and reacquire it 280561205dfSEdward Tomasz Napieralaas part of waking up. 281561205dfSEdward Tomasz NapieralaThis is often however a bad 282561205dfSEdward Tomasz Napieralaidea because it generally relies on you having 283561205dfSEdward Tomasz Napieralasuch a good knowledge of all the call graph above you 284561205dfSEdward Tomasz Napieralaand what assumptions it is making that there are a lot 285561205dfSEdward Tomasz Napieralaof ways to make hard-to-find mistakes. 286561205dfSEdward Tomasz NapieralaFor example you must re-test all the assumptions you made before, 287561205dfSEdward Tomasz Napieralaall the way up the call graph to where you got the lock. 288561205dfSEdward Tomasz NapieralaYou can not just assume that mtx_sleep can be inserted anywhere. 289561205dfSEdward Tomasz NapieralaIf any caller above you has any mutex or 290561205dfSEdward Tomasz Napieralarwlock, your sleep, will cause a panic. 291561205dfSEdward Tomasz NapieralaIf the sleep only happens rarely it may be years before the 292561205dfSEdward Tomasz Napieralabad code path is found. 2930b0b48c0SEdward Tomasz Napierala.Pp 294ddcd2bc9SEdward Tomasz NapieralaIt is an error to do any operation that could result in any kind of sleep when 295ddcd2bc9SEdward Tomasz Napieralarunning inside an interrupt filter. 296ddcd2bc9SEdward Tomasz Napierala.Pp 2970b0b48c0SEdward Tomasz NapieralaIt is an error to do any operation that could result in unbounded sleep when 2980b0b48c0SEdward Tomasz Napieralarunning inside an interrupt thread. 299561205dfSEdward Tomasz Napierala.Ss Interaction table 300ddcd2bc9SEdward Tomasz NapieralaThe following table shows what you can and can not do while holding 301ddcd2bc9SEdward Tomasz Napieralaone of the synchronization primitives discussed: 302561205dfSEdward Tomasz Napierala.Bl -column ".Ic xxxxxxxxxxxxxxxxxxx" ".Xr XXXXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXX" -offset indent 3030929bf8eSJulian Elischer.It Xo 304561205dfSEdward Tomasz Napierala.Em "You have: You want:" Ta spin mtx Ta mutex Ta sx Ta rwlock Ta rmlock Ta sleep 3050929bf8eSJulian Elischer.Xc 306ddcd2bc9SEdward Tomasz Napierala.It spin mtx Ta \&ok-1 Ta \&no Ta \&no Ta \&no Ta \&no Ta \&no-3 307ddcd2bc9SEdward Tomasz Napierala.It mutex Ta \&ok Ta \&ok-1 Ta \&no Ta \&ok Ta \&ok Ta \&no-3 308ddcd2bc9SEdward Tomasz Napierala.It sx Ta \&ok Ta \&ok Ta \&ok-2 Ta \&ok Ta \&ok Ta \&ok-4 309ddcd2bc9SEdward Tomasz Napierala.It rwlock Ta \&ok Ta \&ok Ta \&no Ta \&ok-2 Ta \&ok Ta \&no-3 310ddcd2bc9SEdward Tomasz Napierala.It rmlock Ta \&ok Ta \&ok Ta \&no Ta \&ok Ta \&ok-2 Ta \&no 3110929bf8eSJulian Elischer.El 3120929bf8eSJulian Elischer.Pp 3130929bf8eSJulian Elischer.Em *1 31420f16ca0SJulian ElischerRecursion is defined per lock. 31520f16ca0SJulian ElischerLock order is important. 3160929bf8eSJulian Elischer.Pp 3170929bf8eSJulian Elischer.Em *2 31820f16ca0SJulian Elischerreaders can recurse though writers can not. 31920f16ca0SJulian ElischerLock order is important. 3200929bf8eSJulian Elischer.Pp 3210929bf8eSJulian Elischer.Em *3 3224ff78a9cSJulian ElischerThere are calls atomically release this primitive when going to sleep 3230929bf8eSJulian Elischerand reacquire it on wakeup (e.g. 3240929bf8eSJulian Elischer.Fn mtx_sleep , 32534e1b9e5SJulian Elischer.Fn rw_sleep 3260929bf8eSJulian Elischerand 327f9d63aecSXin LI.Fn msleep_spin 328f9d63aecSXin LI). 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 3334ff78a9cSJulian Elischerwhich atomically release this primitive when going to sleep and 3344ff78a9cSJulian Elischerreacquire it on wakeup. 335561205dfSEdward Tomasz Napierala.Ss Context mode table 3364ff78a9cSJulian ElischerThe next table shows what can be used in different contexts. 3374ff78a9cSJulian ElischerAt this time this is a rather easy to remember table. 338561205dfSEdward Tomasz Napierala.Bl -column ".Ic Xxxxxxxxxxxxxxxxxxx" ".Xr XXXXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXX" -offset indent 339c2c54c3dSJulian Elischer.It Xo 340561205dfSEdward Tomasz Napierala.Em "Context:" Ta spin mtx Ta mutex Ta sx Ta rwlock Ta rmlock Ta sleep 341c2c54c3dSJulian Elischer.Xc 342ddcd2bc9SEdward Tomasz Napierala.It interrupt filter: Ta \&ok Ta \&no Ta \&no Ta \&no Ta \&no Ta \&no 343ddcd2bc9SEdward Tomasz Napierala.It ithread: Ta \&ok Ta \&ok Ta \&no Ta \&ok Ta \&ok Ta \&no 344ddcd2bc9SEdward Tomasz Napierala.It callout: Ta \&ok Ta \&ok Ta \&no Ta \&ok Ta \&no Ta \&no 345ddcd2bc9SEdward Tomasz Napierala.It syscall: Ta \&ok Ta \&ok Ta \&ok Ta \&ok Ta \&ok Ta \&ok 346c2c54c3dSJulian Elischer.El 3470929bf8eSJulian Elischer.Sh SEE ALSO 3480929bf8eSJulian Elischer.Xr condvar 9 , 3493111fc98SChristian Brueffer.Xr lock 9 , 3500929bf8eSJulian Elischer.Xr mtx_pool 9 , 35120f16ca0SJulian Elischer.Xr mutex 9 , 352f53d15feSStephan Uphoff.Xr rmlock 9 , 3530929bf8eSJulian Elischer.Xr rwlock 9 , 3540929bf8eSJulian Elischer.Xr sema 9 , 3550929bf8eSJulian Elischer.Xr sleep 9 , 3563111fc98SChristian Brueffer.Xr sx 9 , 357815e4772SEdward Tomasz Napierala.Xr witness 9 , 358815e4772SEdward Tomasz Napierala.Xr LOCK_PROFILING 9 3590929bf8eSJulian Elischer.Sh HISTORY 3600929bf8eSJulian ElischerThese 3610929bf8eSJulian Elischerfunctions appeared in 3620929bf8eSJulian Elischer.Bsx 4.1 3630929bf8eSJulian Elischerthrough 3640929bf8eSJulian Elischer.Fx 7.0 365815e4772SEdward Tomasz Napierala.Sh BUGS 366815e4772SEdward Tomasz NapieralaThere are too many locking primitives to choose from. 367