xref: /dflybsd-src/share/man/man9/locking.9 (revision c04308e8fc22699d31171b18b2d5797ece796c6d)
1667ff97eSMarkus Pfeiffer.\"
2667ff97eSMarkus Pfeiffer.\" Copyright (c) 2014 Markus Pfeiffer
3667ff97eSMarkus Pfeiffer.\" All rights reserved.
4667ff97eSMarkus Pfeiffer.\"
5667ff97eSMarkus Pfeiffer.\" Redistribution and use in source and binary forms, with or without
6667ff97eSMarkus Pfeiffer.\" modification, are permitted provided that the following conditions
7667ff97eSMarkus Pfeiffer.\" are met:
8667ff97eSMarkus Pfeiffer.\" 1. Redistributions of source code must retain the above copyright
9667ff97eSMarkus Pfeiffer.\"    notice, this list of conditions and the following disclaimer.
10667ff97eSMarkus Pfeiffer.\" 2. Redistributions in binary form must reproduce the above copyright
11667ff97eSMarkus Pfeiffer.\"    notice, this list of conditions and the following disclaimer in the
12667ff97eSMarkus Pfeiffer.\"    documentation and/or other materials provided with the distribution.
13667ff97eSMarkus Pfeiffer.\"
14667ff97eSMarkus Pfeiffer.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15667ff97eSMarkus Pfeiffer.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16667ff97eSMarkus Pfeiffer.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17667ff97eSMarkus Pfeiffer.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18667ff97eSMarkus Pfeiffer.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19667ff97eSMarkus Pfeiffer.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20667ff97eSMarkus Pfeiffer.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21667ff97eSMarkus Pfeiffer.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22667ff97eSMarkus Pfeiffer.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23667ff97eSMarkus Pfeiffer.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24667ff97eSMarkus Pfeiffer.\" SUCH DAMAGE.
25667ff97eSMarkus Pfeiffer.\"
26667ff97eSMarkus Pfeiffer.\"
27667ff97eSMarkus Pfeiffer.Dd June 5, 2014
28667ff97eSMarkus Pfeiffer.Dt LOCKING 9
29667ff97eSMarkus Pfeiffer.Os
30667ff97eSMarkus Pfeiffer.Sh NAME
31667ff97eSMarkus Pfeiffer.Nm locking
32667ff97eSMarkus Pfeiffer.Nd introduction to kernel locking primitives
33667ff97eSMarkus Pfeiffer.Sh DESCRIPTION
34667ff97eSMarkus PfeifferThe
35667ff97eSMarkus Pfeiffer.Dx
36667ff97eSMarkus Pfeifferkernel provides several locking and synchronisation primitives, each with
37*c04308e8SMarkus Pfeifferdifferent characteristics and purposes.
38*c04308e8SMarkus PfeifferThis manpage aims at giving an
39667ff97eSMarkus Pfeifferoverview of the available locking primitives and their use cases.
40667ff97eSMarkus Pfeiffer.Sh CONDITION VARIABLES
41*c04308e8SMarkus PfeifferCondition variables are used to wait for conditions to occur.
42*c04308e8SMarkus PfeifferIn
43667ff97eSMarkus Pfeiffer.Dx
44667ff97eSMarkus Pfeiffercondition variables use spinlocks internally.
45*c04308e8SMarkus PfeifferThreads that wait on a condition variable are called waiters.
46*c04308e8SMarkus PfeifferEither just
47667ff97eSMarkus Pfeifferone or all waiters can be notified of changes to a condition variable.
48*c04308e8SMarkus PfeifferA condition variable can
49*c04308e8SMarkus Pfeiffer.Xr tsleep_interlock 9
50*c04308e8SMarkus Pfeifferwhen given a
51*c04308e8SMarkus Pfeiffer.Xr lockmgr 9
52*c04308e8SMarkus Pfeifferlock to avoid missing changes to it, or regular
53*c04308e8SMarkus Pfeiffer.Xr tsleep 9 .
54667ff97eSMarkus PfeifferSee
55*c04308e8SMarkus Pfeiffer.Xr condvar 9 .
56*c04308e8SMarkus Pfeifferfor further information.
57667ff97eSMarkus Pfeiffer.Sh CRITICAL SECTIONS
58667ff97eSMarkus PfeifferA critical section changes the priority of the current thread to
59*c04308e8SMarkus Pfeiffer.Dv TDPRIT_CRIT ,
60*c04308e8SMarkus Pfeiffereffectively avoiding preemption of the thread.
61667ff97eSMarkus PfeifferCritical sections are a per-cpu primitive, and there is no synchronisation
62667ff97eSMarkus Pfeifferor locking between CPUs.
63667ff97eSMarkus PfeifferSee
64*c04308e8SMarkus Pfeiffer.Xr crit_enter 9 .
65*c04308e8SMarkus Pfeiffer.Sh LOCKMGR LOCKS
66*c04308e8SMarkus Pfeiffer.Xr Lockmgr 9
67*c04308e8SMarkus Pfeifferlocks are the kitchen sink locking primitive for the
68*c04308e8SMarkus Pfeiffer.Dx
69*c04308e8SMarkus Pfeifferkernel.
70*c04308e8SMarkus PfeifferLockmgr locks should be used for
71*c04308e8SMarkus Pfeiffer.Fx
72*c04308e8SMarkus Pfeiffercompatibility when porting drivers that use
73*c04308e8SMarkus Pfeiffer.Fx Ap s
74*c04308e8SMarkus Pfeiffermutexes.
75*c04308e8SMarkus PfeifferSee
76*c04308e8SMarkus Pfeiffer.Xr lockmgr 9 .
77*c04308e8SMarkus Pfeifferfor more information.
78667ff97eSMarkus Pfeiffer.Sh LWKT SERIALIZING TOKENS
79*c04308e8SMarkus PfeifferLWKT serializing tokens use
80*c04308e8SMarkus Pfeiffer.Xr atomic_cmpset 9
81*c04308e8SMarkus Pfeifferinternally and are integrated with the LWKT serializer.
82*c04308e8SMarkus PfeifferThe scheduler takes care of acquiring a token before
83667ff97eSMarkus Pfeifferrescheduling, so a thread will not be run unless all tokens for it can be
84667ff97eSMarkus Pfeifferacquired.
85667ff97eSMarkus PfeifferTokens are not owned by a thread, but by the CPU, and threads are only given
86667ff97eSMarkus Pfeifferreferences to tokens.
87*c04308e8SMarkus PfeifferSee
88*c04308e8SMarkus Pfeiffer.Xr serializer 9 .
89*c04308e8SMarkus Pfeiffer.\".Sh LWKT MESSAGES
90667ff97eSMarkus Pfeiffer.Sh MPLOCK
91667ff97eSMarkus PfeifferThe mplock is an API wrapper for the MP token. The use of this should be
92667ff97eSMarkus Pfeifferavoided at all cost, because there is only one MP token for the whole system.
93*c04308e8SMarkus Pfeiffer.Sh MTX MUTEXES
94*c04308e8SMarkus PfeifferMtx mutexes are a locking primitive that is based around
95*c04308e8SMarkus Pfeiffer.Xr atomic_cmpset_int 9
96*c04308e8SMarkus Pfeifferinstead of spinlocks.
97*c04308e8SMarkus PfeifferThey are much faster and use less memory than
98*c04308e8SMarkus Pfeiffer.Xr lockmgr 9
99*c04308e8SMarkus Pfeifferlocks.
100*c04308e8SMarkus PfeifferMtx mutexes can always be recursive, shared/exclusive and can be held
101*c04308e8SMarkus Pfeifferacross blocking calls and sleeps.
102*c04308e8SMarkus PfeifferThey are also capable of passing ownership directly to a new owner
103*c04308e8SMarkus Pfeifferwithout wakeup.
104667ff97eSMarkus PfeifferSee
105*c04308e8SMarkus Pfeiffer.Xr mutex 9 .
106667ff97eSMarkus Pfeiffer.Sh SERIALIZERS
107667ff97eSMarkus PfeifferSerializers are used to serialize access to hardware and other subsystems.
108667ff97eSMarkus PfeifferSerializers are deprecated, and should not be used in new code.
109667ff97eSMarkus Pfeiffer.Sh SPINLOCKS
110*c04308e8SMarkus PfeifferSpinlocks employ a busy wait loop to acquire a lock.
111*c04308e8SMarkus PfeifferThis means that this type of lock is very lightweight,
112*c04308e8SMarkus Pfeifferbut should only be held for a very short time, since all contenders
113*c04308e8SMarkus Pfeifferwill be spinning and not sleeping.
114*c04308e8SMarkus PfeifferNo wakeup is necessary, because a waiter will be spinning already.
115667ff97eSMarkus PfeifferIf a thread tries to sleep while holding a spinlock, the kernel will panic.
116667ff97eSMarkus PfeifferSpinlocks cannot recurse.
117*c04308e8SMarkus Pfeiffer.Pp
118*c04308e8SMarkus PfeifferThey are mainly used to protect kernel structures, and to
119*c04308e8SMarkus Pfeifferimplement higher level locking primitives.
120*c04308e8SMarkus PfeifferSee
121*c04308e8SMarkus Pfeiffer.Xr spinlock 9 .
122*c04308e8SMarkus Pfeiffer.Sh SEE ALSO
123*c04308e8SMarkus Pfeiffer.Xr atomic 9 ,
124*c04308e8SMarkus Pfeiffer.Xr condvar 9 ,
125*c04308e8SMarkus Pfeiffer.Xr crit_enter 9 ,
126*c04308e8SMarkus Pfeiffer.Xr lockmgr 9 ,
127*c04308e8SMarkus Pfeiffer.Xr mutex 9 ,
128*c04308e8SMarkus Pfeiffer.Xr serializer 9 ,
129*c04308e8SMarkus Pfeiffer.Xr spinlock 9 ,
130*c04308e8SMarkus Pfeiffer.Xr tsleep 9
131667ff97eSMarkus Pfeiffer.Sh AUTHORS
132667ff97eSMarkus Pfeiffer.An -nosplit
133667ff97eSMarkus PfeifferThis manual page was written by
134667ff97eSMarkus Pfeiffer.An Markus Pfeiffer Aq Mt markus.pfeiffer@morphism.de ,
135667ff97eSMarkus Pfeifferbased on comments by various
136667ff97eSMarkus Pfeiffer.Dx
137667ff97eSMarkus Pfeifferauthors.
138