1.\" 2.\" Copyright (c) 2014 Markus Pfeiffer 3.\" All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in the 12.\" documentation and/or other materials provided with the distribution. 13.\" 14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24.\" SUCH DAMAGE. 25.\" 26.\" 27.Dd June 5, 2014 28.Dt LOCKING 9 29.Os 30.Sh NAME 31.Nm locking 32.Nd introduction to kernel locking primitives 33.Sh DESCRIPTION 34The 35.Dx 36kernel provides several locking and synchronisation primitives, each with 37different characteristics and purposes. 38This manpage aims at giving an 39overview of the available locking primitives and their use cases. 40.Sh CONDITION VARIABLES 41Condition variables are used to wait for conditions to occur. 42In 43.Dx 44condition variables use spinlocks internally. 45Threads that wait on a condition variable are called waiters. 46Either just 47one or all waiters can be notified of changes to a condition variable. 48A condition variable can 49.Xr tsleep_interlock 9 50when given a 51.Xr lockmgr 9 52lock to avoid missing changes to it, or regular 53.Xr tsleep 9 . 54See 55.Xr condvar 9 . 56for further information. 57.Sh CRITICAL SECTIONS 58A critical section changes the priority of the current thread to 59.Dv TDPRIT_CRIT , 60effectively avoiding preemption of the thread. 61Critical sections are a per-cpu primitive, and there is no synchronisation 62or locking between CPUs. 63See 64.Xr crit_enter 9 . 65.Sh LOCKMGR LOCKS 66.Xr Lockmgr 9 67locks are the kitchen sink locking primitive for the 68.Dx 69kernel. 70Lockmgr locks should be used for 71.Fx 72compatibility when porting drivers that use 73.Fx Ap s 74mutexes. 75See 76.Xr lockmgr 9 . 77for more information. 78.Sh LWKT SERIALIZING TOKENS 79LWKT serializing tokens use 80.Xr atomic_cmpset 9 81internally and are integrated with the LWKT serializer. 82The scheduler takes care of acquiring a token before 83rescheduling, so a thread will not be run unless all tokens for it can be 84acquired. 85Tokens are not owned by a thread, but by the CPU, and threads are only given 86references to tokens. 87See 88.Xr serializer 9 . 89.\".Sh LWKT MESSAGES 90.Sh MPLOCK 91The mplock is an API wrapper for the MP token. The use of this should be 92avoided at all cost, because there is only one MP token for the whole system. 93.Sh MTX MUTEXES 94Mtx mutexes are a locking primitive that is based around 95.Xr atomic_cmpset_int 9 96instead of spinlocks. 97They are much faster and use less memory than 98.Xr lockmgr 9 99locks. 100Mtx mutexes can always be recursive, shared/exclusive and can be held 101across blocking calls and sleeps. 102They are also capable of passing ownership directly to a new owner 103without wakeup. 104See 105.Xr mutex 9 . 106.Sh SERIALIZERS 107Serializers are used to serialize access to hardware and other subsystems. 108Serializers are deprecated, and should not be used in new code. 109.Sh SPINLOCKS 110Spinlocks employ a busy wait loop to acquire a lock. 111This means that this type of lock is very lightweight, 112but should only be held for a very short time, since all contenders 113will be spinning and not sleeping. 114No wakeup is necessary, because a waiter will be spinning already. 115If a thread tries to sleep while holding a spinlock, the kernel will panic. 116Spinlocks cannot recurse. 117.Pp 118They are mainly used to protect kernel structures, and to 119implement higher level locking primitives. 120See 121.Xr spinlock 9 . 122.Sh SEE ALSO 123.Xr atomic 9 , 124.Xr condvar 9 , 125.Xr crit_enter 9 , 126.Xr lockmgr 9 , 127.Xr mutex 9 , 128.Xr serializer 9 , 129.Xr spinlock 9 , 130.Xr tsleep 9 131.Sh AUTHORS 132.An -nosplit 133This manual page was written by 134.An Markus Pfeiffer Aq Mt markus.pfeiffer@morphism.de , 135based on comments by various 136.Dx 137authors. 138