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.\" 27815e4772SEdward Tomasz Napierala.Dd January 29, 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. 390929bf8eSJulian Elischer.Pp 400929bf8eSJulian ElischerThese include: 410929bf8eSJulian Elischer.Bl -enum 420929bf8eSJulian Elischer.It 43815e4772SEdward Tomasz NapieralaMutexes 440929bf8eSJulian Elischer.It 45815e4772SEdward Tomasz NapieralaSpin mutexes 460929bf8eSJulian Elischer.It 47815e4772SEdward Tomasz NapieralaPool mutexes 480929bf8eSJulian Elischer.It 49815e4772SEdward Tomasz NapieralaShared/exclusive locks 500929bf8eSJulian Elischer.It 51815e4772SEdward Tomasz NapieralaReader/writer locks 520929bf8eSJulian Elischer.It 53815e4772SEdward Tomasz NapieralaRead-mostly locks 54f53d15feSStephan Uphoff.It 55815e4772SEdward Tomasz NapieralaCounting semaphores 560929bf8eSJulian Elischer.It 570929bf8eSJulian ElischerCondition variables 580929bf8eSJulian Elischer.It 590929bf8eSJulian ElischerSleep/wakeup 600929bf8eSJulian Elischer.It 610929bf8eSJulian ElischerGiant 620929bf8eSJulian Elischer.It 630929bf8eSJulian ElischerLockmanager locks 640929bf8eSJulian Elischer.El 650929bf8eSJulian Elischer.Pp 6653882c7cSJulian ElischerThe primitives interact and have a number of rules regarding how 6720f16ca0SJulian Elischerthey can and can not be combined. 68815e4772SEdward Tomasz NapieralaMany of these rules are checked using the 690929bf8eSJulian Elischer.Xr witness 4 700929bf8eSJulian Elischercode. 710929bf8eSJulian Elischer.Pp 7220f16ca0SJulian Elischer.Ss Mutexes 73815e4772SEdward Tomasz NapieralaMutexes are the most commonly used synchronization primitive in the kernel. 74815e4772SEdward Tomasz NapieralaThread acquires (locks) a mutex before accessing data shared with other 75815e4772SEdward Tomasz Napieralathreads (including interrupt threads), and releases (unlocks) it afterwards. 76561205dfSEdward Tomasz NapieralaIf the mutex cannot be acquired, the thread requesting it will sleep. 77561205dfSEdward Tomasz NapieralaMutexes fully support priority propagation. 78815e4772SEdward Tomasz Napierala.Pp 794ff467dcSEdward Tomasz NapieralaSee 80815e4772SEdward Tomasz Napierala.Xr mutex 9 814ff467dcSEdward Tomasz Napieralafor details. 82815e4772SEdward Tomasz Napierala.Ss Spin mutexes 83815e4772SEdward Tomasz NapieralaSpin mutexes are variation of basic mutexes; the main difference between 84561205dfSEdward Tomasz Napieralathe two is that spin mutexes never sleep - instead, they spin, waiting 85815e4772SEdward Tomasz Napieralafor the thread holding the lock, which runs on another CPU, to release it. 86815e4772SEdward Tomasz NapieralaDifferently from ordinary mutex, spin mutexes disable interrupts when acquired. 87815e4772SEdward Tomasz NapieralaSince disabling interrupts is expensive, they are also generally slower. 88815e4772SEdward Tomasz NapieralaSpin mutexes should only be used to protect data shared with primary 89815e4772SEdward Tomasz Napierala(INTR_FILTER) interrupt code. 90815e4772SEdward Tomasz NapieralaYou 91815e4772SEdward Tomasz Napierala.Em must not 92815e4772SEdward Tomasz Napieralado anything that deschedules the thread while you 93815e4772SEdward Tomasz Napieralaare holding a spin mutex. 94815e4772SEdward Tomasz Napierala.Ss Pool mutexes 95815e4772SEdward Tomasz NapieralaWith most synchronisaton primitives, such as mutexes, programmer must 96815e4772SEdward Tomasz Napieralaprovide a piece of allocated memory to hold the primitive. 97815e4772SEdward Tomasz NapieralaFor example, a mutex may be embedded inside the structure it protects. 98815e4772SEdward Tomasz NapieralaPool mutex is a variant of mutex without this requirement - to lock or unlock 99815e4772SEdward Tomasz Napieralaa pool mutex, one uses address of the structure being protected with it, 100815e4772SEdward Tomasz Napieralanot the mutex itself. 101815e4772SEdward Tomasz NapieralaPool mutexes are seldom used. 102815e4772SEdward Tomasz Napierala.Pp 1034ff467dcSEdward Tomasz NapieralaSee 104815e4772SEdward Tomasz Napierala.Xr mtx_pool 9 1054ff467dcSEdward Tomasz Napieralafor details. 106815e4772SEdward Tomasz Napierala.Ss Reader/writer locks 1070929bf8eSJulian ElischerReader/writer locks allow shared access to protected data by multiple threads, 1080929bf8eSJulian Elischeror exclusive access by a single thread. 1090929bf8eSJulian ElischerThe threads with shared access are known as 1100929bf8eSJulian Elischer.Em readers 11120f16ca0SJulian Elischersince they should only read the protected data. 1120929bf8eSJulian ElischerA thread with exclusive access is known as a 1130929bf8eSJulian Elischer.Em writer 11420f16ca0SJulian Elischersince it may modify protected data. 1150929bf8eSJulian Elischer.Pp 11620f16ca0SJulian ElischerReader/writer locks can be treated as mutexes (see above and 1170929bf8eSJulian Elischer.Xr mutex 9 ) 1180929bf8eSJulian Elischerwith shared/exclusive semantics. 11920f16ca0SJulian ElischerMore specifically, regular mutexes can be 12020f16ca0SJulian Elischerconsidered to be equivalent to a write-lock on an 12120f16ca0SJulian Elischer.Em rw_lock. 1220929bf8eSJulian ElischerThe 1230929bf8eSJulian Elischer.Em rw_lock 1240929bf8eSJulian Elischerlocks have priority propagation like mutexes, but priority 1250929bf8eSJulian Elischercan be propagated only to an exclusive holder. 1260929bf8eSJulian ElischerThis limitation comes from the fact that shared owners 1270929bf8eSJulian Elischerare anonymous. 1280929bf8eSJulian ElischerAnother important property is that shared holders of 1290929bf8eSJulian Elischer.Em rw_lock 13020f16ca0SJulian Elischercan recurse, but exclusive locks are not allowed to recurse. 13120f16ca0SJulian ElischerThis ability should not be used lightly and 13220f16ca0SJulian Elischer.Em may go away. 133815e4772SEdward Tomasz Napierala.Pp 1344ff467dcSEdward Tomasz NapieralaSee 135815e4772SEdward Tomasz Napierala.Xr rwlock 9 1364ff467dcSEdward Tomasz Napieralafor details. 137815e4772SEdward Tomasz Napierala.Ss Read-mostly locks 138f53d15feSStephan UphoffMostly reader locks are similar to 139561205dfSEdward Tomasz Napierala.Em reader/writer 140561205dfSEdward Tomasz Napieralalocks but optimized for very infrequent write locking. 141561205dfSEdward Tomasz Napierala.Em Read-mostly 142f53d15feSStephan Uphofflocks implement full priority propagation by tracking shared owners 143f53d15feSStephan Uphoffusing a lock user supplied 144f53d15feSStephan Uphoff.Em tracker 145f53d15feSStephan Uphoffdata structure. 146815e4772SEdward Tomasz Napierala.Pp 1474ff467dcSEdward Tomasz NapieralaSee 148815e4772SEdward Tomasz Napierala.Xr rmlock 9 1494ff467dcSEdward Tomasz Napieralafor details. 150815e4772SEdward Tomasz Napierala.Ss Shared/exclusive locks 151561205dfSEdward Tomasz NapieralaShared/exclusive locks are similar to reader/writer locks; the main difference 152561205dfSEdward Tomasz Napieralabetween them is that shared/exclusive locks may be held during unbounded sleep 153561205dfSEdward Tomasz Napierala(and may thus perform an unbounded sleep). 154561205dfSEdward Tomasz NapieralaThey are inherently less efficient than mutexes, reader/writer locks 155561205dfSEdward Tomasz Napieralaand read-mostly locks. They don't support priority propagation. 156561205dfSEdward Tomasz NapieralaThey should be considered to be closely related to 15720f16ca0SJulian Elischer.Xr sleep 9 . 15820f16ca0SJulian ElischerIn fact it could in some cases be 15920f16ca0SJulian Elischerconsidered a conditional sleep. 160815e4772SEdward Tomasz Napierala.Pp 1614ff467dcSEdward Tomasz NapieralaSee 162815e4772SEdward Tomasz Napierala.Xr sx 9 1634ff467dcSEdward Tomasz Napieralafor details. 164815e4772SEdward Tomasz Napierala.Ss Counting semaphores 165815e4772SEdward Tomasz NapieralaCounting semaphores provide a mechanism for synchronizing access 166815e4772SEdward Tomasz Napieralato a pool of resources. 167815e4772SEdward Tomasz NapieralaUnlike mutexes, semaphores do not have the concept of an owner, 168815e4772SEdward Tomasz Napieralaso they can be useful in situations where one thread needs 169815e4772SEdward Tomasz Napieralato acquire a resource, and another thread needs to release it. 170815e4772SEdward Tomasz NapieralaThey are largely deprecated. 171815e4772SEdward Tomasz Napierala.Pp 1724ff467dcSEdward Tomasz NapieralaSee 173815e4772SEdward Tomasz Napierala.Xr sema 9 1744ff467dcSEdward Tomasz Napieralafor details. 1750929bf8eSJulian Elischer.Ss Condition variables 1764ff78a9cSJulian ElischerCondition variables are used in conjunction with mutexes to wait for 1774ff78a9cSJulian Elischerconditions to occur. 1780929bf8eSJulian ElischerA thread must hold the mutex before calling the 1790929bf8eSJulian Elischer.Fn cv_wait* , 1800929bf8eSJulian Elischerfunctions. 1810929bf8eSJulian ElischerWhen a thread waits on a condition, the mutex 1820929bf8eSJulian Elischeris atomically released before the thread is blocked, then reacquired 1830929bf8eSJulian Elischerbefore the function call returns. 184815e4772SEdward Tomasz Napierala.Pp 1854ff467dcSEdward Tomasz NapieralaSee 186815e4772SEdward Tomasz Napierala.Xr condvar 9 1874ff467dcSEdward Tomasz Napieralafor details. 1880929bf8eSJulian Elischer.Ss Giant 189561205dfSEdward Tomasz NapieralaGiant is an instance of a mutex, with some special characteristics: 190fbe508ffSWarner Losh.Bl -enum 191fbe508ffSWarner Losh.It 192fbe508ffSWarner LoshIt is recursive. 193fbe508ffSWarner Losh.It 194fbe508ffSWarner LoshDrivers can request that Giant be locked around them, but this is 195fbe508ffSWarner Loshgoing away. 196fbe508ffSWarner Losh.It 197fbe508ffSWarner LoshYou can sleep while it has recursed, but other recursive locks cannot. 198fbe508ffSWarner Losh.It 19920f16ca0SJulian ElischerGiant must be locked first before other locks. 200fbe508ffSWarner Losh.It 201fbe508ffSWarner LoshThere are places in the kernel that drop Giant and pick it back up 202fbe508ffSWarner Loshagain. 203fbe508ffSWarner LoshSleep locks will do this before sleeping. 204561205dfSEdward Tomasz NapieralaParts of the network or VM code may do this as well, depending on the 205fbe508ffSWarner Loshsetting of a sysctl. 206fbe508ffSWarner LoshThis means that you cannot count on Giant keeping other code from 207fbe508ffSWarner Loshrunning if your code sleeps, even if you want it to. 208fbe508ffSWarner Losh.El 2090929bf8eSJulian Elischer.Ss Sleep/wakeup 2100929bf8eSJulian ElischerThe functions 2110929bf8eSJulian Elischer.Fn tsleep , 2120929bf8eSJulian Elischer.Fn msleep , 2130929bf8eSJulian Elischer.Fn msleep_spin , 2140929bf8eSJulian Elischer.Fn pause , 2150929bf8eSJulian Elischer.Fn wakeup , 2160929bf8eSJulian Elischerand 2170929bf8eSJulian Elischer.Fn wakeup_one 2180929bf8eSJulian Elischerhandle event-based thread blocking. 2194ff78a9cSJulian ElischerIf a thread must wait for an external event, it is put to sleep by 2200929bf8eSJulian Elischer.Fn tsleep , 2210929bf8eSJulian Elischer.Fn msleep , 2220929bf8eSJulian Elischer.Fn msleep_spin , 2230929bf8eSJulian Elischeror 2240929bf8eSJulian Elischer.Fn pause . 2250929bf8eSJulian ElischerThreads may also wait using one of the locking primitive sleep routines 2260929bf8eSJulian Elischer.Xr mtx_sleep 9 , 2270929bf8eSJulian Elischer.Xr rw_sleep 9 , 2280929bf8eSJulian Elischeror 2290929bf8eSJulian Elischer.Xr sx_sleep 9 . 2300929bf8eSJulian Elischer.Pp 2310929bf8eSJulian ElischerThe parameter 2320929bf8eSJulian Elischer.Fa chan 2330929bf8eSJulian Elischeris an arbitrary address that uniquely identifies the event on which 2340929bf8eSJulian Elischerthe thread is being put to sleep. 2350929bf8eSJulian ElischerAll threads sleeping on a single 2360929bf8eSJulian Elischer.Fa chan 2370929bf8eSJulian Elischerare woken up later by 2380929bf8eSJulian Elischer.Fn wakeup , 2390929bf8eSJulian Elischeroften called from inside an interrupt routine, to indicate that the 2400929bf8eSJulian Elischerresource the thread was blocking on is available now. 2410929bf8eSJulian Elischer.Pp 2420929bf8eSJulian ElischerSeveral of the sleep functions including 2430929bf8eSJulian Elischer.Fn msleep , 2440929bf8eSJulian Elischer.Fn msleep_spin , 2450929bf8eSJulian Elischerand the locking primitive sleep routines specify an additional lock 2460929bf8eSJulian Elischerparameter. 2470929bf8eSJulian ElischerThe lock will be released before sleeping and reacquired 2480929bf8eSJulian Elischerbefore the sleep routine returns. 2490929bf8eSJulian ElischerIf 2500929bf8eSJulian Elischer.Fa priority 2510929bf8eSJulian Elischerincludes the 2520929bf8eSJulian Elischer.Dv PDROP 2534ff78a9cSJulian Elischerflag, then the lock will not be reacquired before returning. 2540929bf8eSJulian ElischerThe lock is used to ensure that a condition can be checked atomically, 2550929bf8eSJulian Elischerand that the current thread can be suspended without missing a 2560929bf8eSJulian Elischerchange to the condition, or an associated wakeup. 2570929bf8eSJulian ElischerIn addition, all of the sleep routines will fully drop the 2580929bf8eSJulian Elischer.Va Giant 2590929bf8eSJulian Elischermutex 2600929bf8eSJulian Elischer(even if recursed) 2610929bf8eSJulian Elischerwhile the thread is suspended and will reacquire the 2620929bf8eSJulian Elischer.Va Giant 2630929bf8eSJulian Elischermutex before the function returns. 2640929bf8eSJulian Elischer.Pp 2654ff467dcSEdward Tomasz NapieralaSee 266815e4772SEdward Tomasz Napierala.Xr sleep 9 2674ff467dcSEdward Tomasz Napieralafor details. 268815e4772SEdward Tomasz Napierala.Pp 269815e4772SEdward Tomasz Napierala.Ss Lockmanager locks 270561205dfSEdward Tomasz NapieralaShared/exclusive locks, used mostly in 271815e4772SEdward Tomasz Napierala.Xr VFS 9 , 272815e4772SEdward Tomasz Napieralain particular as a 273815e4772SEdward Tomasz Napierala.Xr vnode 9 274815e4772SEdward Tomasz Napieralalock. 275815e4772SEdward Tomasz NapieralaThey have features other lock types don't have, such as sleep timeout, 276815e4772SEdward Tomasz Napieralawriter starvation avoidance, draining, and interlock mutex, but this makes them 277815e4772SEdward Tomasz Napieralacomplicated to implement; for this reason, they are deprecated. 278815e4772SEdward Tomasz Napierala.Pp 2794ff467dcSEdward Tomasz NapieralaSee 2800929bf8eSJulian Elischer.Xr lock 9 2814ff467dcSEdward Tomasz Napieralafor details. 282815e4772SEdward Tomasz Napierala.Sh INTERACTIONS 283561205dfSEdward Tomasz Napierala.Ss Bounded vs. unbounded sleep 284561205dfSEdward Tomasz NapieralaThe following primitives perform bounded sleep: mutexes, pool mutexes, 285561205dfSEdward Tomasz Napieralareader/writer locks and read-mostly locks. 286561205dfSEdward Tomasz Napierala.Pp 287561205dfSEdward Tomasz NapieralaThe following primitives block (perform unbounded sleep): shared/exclusive locks, 288561205dfSEdward Tomasz Napieralacounting semaphores, condition variables, sleep/wakeup and lockmanager locks. 289561205dfSEdward Tomasz Napierala.Pp 290561205dfSEdward Tomasz NapieralaIt is an error to do any operation that could result in any kind of sleep while 291561205dfSEdward Tomasz Napieralaholding spin mutex. 292561205dfSEdward Tomasz Napierala.Pp 293561205dfSEdward Tomasz NapieralaAs a general rule, it is an error to do any operation that could result 294561205dfSEdward Tomasz Napieralain unbounded sleep while holding any primitive from the 'bounded sleep' group. 295561205dfSEdward Tomasz NapieralaFor example, it is an error to try to acquire shared/exclusive lock while 296561205dfSEdward Tomasz Napieralaholding mutex, or to try to allocate memory with M_WAITOK while holding 297561205dfSEdward Tomasz Napieralaread-write lock. 298561205dfSEdward Tomasz Napierala.Pp 299561205dfSEdward Tomasz NapieralaAs a special case, it is possible to call 300561205dfSEdward Tomasz Napierala.Fn sleep 9 301561205dfSEdward Tomasz Napieralaor 302561205dfSEdward Tomasz Napierala.Fn mtx_sleep 9 303561205dfSEdward Tomasz Napieralawhile holding a mutex. 304561205dfSEdward Tomasz NapieralaIt will atomically drop the mutex and reacquire it 305561205dfSEdward Tomasz Napieralaas part of waking up. 306561205dfSEdward Tomasz NapieralaThis is often however a bad 307561205dfSEdward Tomasz Napieralaidea because it generally relies on you having 308561205dfSEdward Tomasz Napieralasuch a good knowledge of all the call graph above you 309561205dfSEdward Tomasz Napieralaand what assumptions it is making that there are a lot 310561205dfSEdward Tomasz Napieralaof ways to make hard-to-find mistakes. 311561205dfSEdward Tomasz NapieralaFor example you must re-test all the assumptions you made before, 312561205dfSEdward Tomasz Napieralaall the way up the call graph to where you got the lock. 313561205dfSEdward Tomasz NapieralaYou can not just assume that mtx_sleep can be inserted anywhere. 314561205dfSEdward Tomasz NapieralaIf any caller above you has any mutex or 315561205dfSEdward Tomasz Napieralarwlock, your sleep, will cause a panic. 316561205dfSEdward Tomasz NapieralaIf the sleep only happens rarely it may be years before the 317561205dfSEdward Tomasz Napieralabad code path is found. 318561205dfSEdward Tomasz Napierala.Ss Interaction table 3190929bf8eSJulian ElischerThe following table shows what you can and can not do if you hold 320f9d63aecSXin LIone of the synchronization primitives discussed here: 3210929bf8eSJulian Elischer(someone who knows what they are talking about should write this table) 322561205dfSEdward Tomasz Napierala.Bl -column ".Ic xxxxxxxxxxxxxxxxxxx" ".Xr XXXXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXX" -offset indent 3230929bf8eSJulian Elischer.It Xo 324561205dfSEdward Tomasz Napierala.Em "You have: You want:" Ta spin mtx Ta mutex Ta sx Ta rwlock Ta rmlock Ta sleep 3250929bf8eSJulian Elischer.Xc 326561205dfSEdward Tomasz Napierala.It Ic spin mtx Ta \&ok-1 Ta \&no Ta \&no Ta \&no Ta \&no Ta \&no-3 327561205dfSEdward Tomasz Napierala.It Ic mutex Ta \&ok Ta \&ok-1 Ta \&no Ta \&ok Ta \&ok Ta \&no-3 3284ff467dcSEdward Tomasz Napierala.It Ic sx Ta \&ok Ta \&ok Ta \&ok-2 Ta \&ok Ta \&ok Ta \&ok-4 329561205dfSEdward Tomasz Napierala.It Ic rwlock Ta \&ok Ta \&ok Ta \&no Ta \&ok-2 Ta \&ok Ta \&no-3 330561205dfSEdward Tomasz Napierala.It Ic rmlock Ta \&ok Ta \&ok Ta \&no Ta \&ok Ta \&ok-2 Ta \&no 3310929bf8eSJulian Elischer.El 3320929bf8eSJulian Elischer.Pp 3330929bf8eSJulian Elischer.Em *1 33420f16ca0SJulian ElischerRecursion is defined per lock. 33520f16ca0SJulian ElischerLock order is important. 3360929bf8eSJulian Elischer.Pp 3370929bf8eSJulian Elischer.Em *2 33820f16ca0SJulian Elischerreaders can recurse though writers can not. 33920f16ca0SJulian ElischerLock order is important. 3400929bf8eSJulian Elischer.Pp 3410929bf8eSJulian Elischer.Em *3 3424ff78a9cSJulian ElischerThere are calls atomically release this primitive when going to sleep 3430929bf8eSJulian Elischerand reacquire it on wakeup (e.g. 3440929bf8eSJulian Elischer.Fn mtx_sleep , 34534e1b9e5SJulian Elischer.Fn rw_sleep 3460929bf8eSJulian Elischerand 347f9d63aecSXin LI.Fn msleep_spin 348f9d63aecSXin LI). 3490929bf8eSJulian Elischer.Pp 3500929bf8eSJulian Elischer.Em *4 35134e1b9e5SJulian ElischerThough one can sleep holding an sx lock, one can also use 3520929bf8eSJulian Elischer.Fn sx_sleep 3534ff78a9cSJulian Elischerwhich atomically release this primitive when going to sleep and 3544ff78a9cSJulian Elischerreacquire it on wakeup. 355561205dfSEdward Tomasz Napierala.Ss Context mode table 3564ff78a9cSJulian ElischerThe next table shows what can be used in different contexts. 3574ff78a9cSJulian ElischerAt this time this is a rather easy to remember table. 358561205dfSEdward Tomasz Napierala.Bl -column ".Ic Xxxxxxxxxxxxxxxxxxx" ".Xr XXXXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXXX" ".Xr XXXXXX" -offset indent 359c2c54c3dSJulian Elischer.It Xo 360561205dfSEdward Tomasz Napierala.Em "Context:" Ta spin mtx Ta mutex Ta sx Ta rwlock Ta rmlock Ta sleep 361c2c54c3dSJulian Elischer.Xc 362f53d15feSStephan Uphoff.It interrupt: Ta \&ok Ta \&no Ta \&no Ta \&no Ta \&no Ta \&no 363f53d15feSStephan Uphoff.It idle: Ta \&ok Ta \&no Ta \&no Ta \&no Ta \&no Ta \&no 364c2c54c3dSJulian Elischer.El 3650929bf8eSJulian Elischer.Sh SEE ALSO 3660929bf8eSJulian Elischer.Xr condvar 9 , 3673111fc98SChristian Brueffer.Xr lock 9 , 3680929bf8eSJulian Elischer.Xr mtx_pool 9 , 36920f16ca0SJulian Elischer.Xr mutex 9 , 370f53d15feSStephan Uphoff.Xr rmlock 9 , 3710929bf8eSJulian Elischer.Xr rwlock 9 , 3720929bf8eSJulian Elischer.Xr sema 9 , 3730929bf8eSJulian Elischer.Xr sleep 9 , 3743111fc98SChristian Brueffer.Xr sx 9 , 375815e4772SEdward Tomasz Napierala.Xr witness 9 , 376815e4772SEdward Tomasz Napierala.Xr LOCK_PROFILING 9 3770929bf8eSJulian Elischer.Sh HISTORY 3780929bf8eSJulian ElischerThese 3790929bf8eSJulian Elischerfunctions appeared in 3800929bf8eSJulian Elischer.Bsx 4.1 3810929bf8eSJulian Elischerthrough 3820929bf8eSJulian Elischer.Fx 7.0 383815e4772SEdward Tomasz Napierala.Sh BUGS 384815e4772SEdward Tomasz NapieralaThere are too many locking primitives to choose from. 385