2 .\" Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. Berkeley Software Design Inc's name may not be used to endorse or
13 .\" promote products derived from this software without specific prior
14 .\" written permission.
16 .\" THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" from BSDI $Id: mutex.4,v 1.1.2.3 1998/04/27 22:53:13 ewv Exp $
41 .Nm mtx_lock_spin_flags ,
43 .Nm mtx_trylock_flags ,
46 .Nm mtx_unlock_flags ,
47 .Nm mtx_unlock_spin_flags ,
54 .Nd kernel synchronization primitives
60 .Fn mtx_init "struct mtx *mutex" "const char *name" "const char *type" "int opts"
62 .Fn mtx_destroy "struct mtx *mutex"
64 .Fn mtx_lock "struct mtx *mutex"
66 .Fn mtx_lock_spin "struct mtx *mutex"
68 .Fn mtx_lock_flags "struct mtx *mutex" "int flags"
70 .Fn mtx_lock_spin_flags "struct mtx *mutex" "int flags"
72 .Fn mtx_trylock "struct mtx *mutex"
74 .Fn mtx_trylock_flags "struct mtx *mutex" "int flags"
76 .Fn mtx_unlock "struct mtx *mutex"
78 .Fn mtx_unlock_spin "struct mtx *mutex"
80 .Fn mtx_unlock_flags "struct mtx *mutex" "int flags"
82 .Fn mtx_unlock_spin_flags "struct mtx *mutex" "int flags"
84 .Fn mtx_sleep "void *chan" "struct mtx *mtx" "int priority" "const char *wmesg" "int timo"
86 .Fn mtx_initialized "struct mtx *mutex"
88 .Fn mtx_owned "struct mtx *mutex"
90 .Fn mtx_recursed "struct mtx *mutex"
92 .Cd "options INVARIANTS"
93 .Cd "options INVARIANT_SUPPORT"
95 .Fn mtx_assert "struct mtx *mutex" "int what"
97 .Fn MTX_SYSINIT "name" "struct mtx *mtx" "const char *description" "int opts"
99 Mutexes are the most basic and primary method of thread synchronization.
100 The major design considerations for mutexes are:
103 Acquiring and releasing uncontested mutexes should be as cheap
106 They must have the information and storage space to support
107 priority propagation.
109 A thread must be able to recursively acquire a mutex,
110 provided that the mutex is initialized to support recursion.
113 There are currently two flavors of mutexes, those that context switch
114 when they block and those that do not.
118 mutexes will context switch when they are already held.
120 they may spin for some amount
121 of time before context switching.
122 It is important to remember that since a thread may be preempted at any time,
123 the possible context switch introduced by acquiring a mutex is guaranteed
124 to not break anything that is not already broken.
126 Mutexes which do not context switch are
129 These should only be used to protect data shared with primary interrupt
133 interrupt handlers and low level scheduling code.
134 In all architectures both acquiring and releasing of a
135 uncontested spin mutex is more expensive than the same operation
137 In order to protect an interrupt service routine from blocking
138 against itself all interrupts are either blocked or deferred on a processor
139 while holding a spin lock.
140 It is permissible to hold multiple spin mutexes.
142 Once a spin mutex has been acquired it is not permissible to acquire a
145 The storage needed to implement a mutex is provided by a
147 In general this should be treated as an opaque object and
148 referenced only with the mutex primitives.
152 function must be used to initialize a mutex
153 before it can be passed to any of the other mutex functions.
156 option is used to identify the lock in debugging output etc.
159 option is used by the witness code to classify a mutex when doing checks
166 is used in its place.
167 The pointer passed in as
171 is saved rather than the data it points to.
172 The data pointed to must remain stable
173 until the mutex is destroyed.
176 argument is used to set the type of mutex.
177 It may contain either
182 See below for additional initialization options.
183 It is not permissible to pass the same
187 multiple times without intervening calls to
194 mutual exclusion lock
195 on behalf of the currently running kernel thread.
196 If another kernel thread is holding the mutex,
197 the caller will be disconnected from the CPU
198 until the mutex is available
199 (i.e., it will block).
205 mutual exclusion lock
206 on behalf of the currently running kernel thread.
207 If another kernel thread is holding the mutex,
208 the caller will spin until the mutex becomes available.
209 Interrupts are disabled during the spin and remain disabled
210 following the acquiring of the lock.
212 It is possible for the same thread to recursively acquire a mutex
213 with no ill effects, provided that the
217 during the initialization of the mutex.
222 .Fn mtx_lock_spin_flags
227 lock, respectively, and also accept a
230 In both cases, the only flag presently available for lock acquires is
234 bit is turned on in the
238 tracing is being done,
239 it will be silenced during the lock acquire.
243 attempts to acquire the
247 If the mutex cannot be immediately acquired
250 otherwise the mutex will be acquired
251 and a non-zero value will be returned.
254 .Fn mtx_trylock_flags
255 function has the same behavior as
257 but should be used when the caller desires to pass in a
260 Presently, the only valid value in the
264 and its effects are identical to those described for
272 mutual exclusion lock.
273 The current thread may be preempted if a higher priority thread is waiting
280 mutual exclusion lock.
285 .Fn mtx_unlock_spin_flags
286 functions behave in exactly the same way as do the standard mutex
287 unlock routines above, while also allowing a
289 argument which may specify
293 is identical to its behavior in the mutex lock routines.
297 function is used to destroy
299 so the data associated with it may be freed
300 or otherwise overwritten.
301 Any mutex which is destroyed
302 must previously have been initialized with
304 It is permissible to have a single hold count
305 on a mutex when it is destroyed.
306 It is not permissible to hold the mutex recursively,
307 or have another thread blocked on the mutex
308 when it is destroyed.
312 function is used to atomically release
314 while waiting for an event.
315 For more details on the parameters to this function,
321 function returns non-zero if
323 has been initialized and zero otherwise.
327 function returns non-zero
328 if the current thread holds
330 If the current thread does not hold
336 function returns non-zero if the
339 This check should only be made if the running thread already owns
344 function allows assertions specified in
348 If the assertions are not true and the kernel is compiled with
349 .Cd "options INVARIANTS"
351 .Cd "options INVARIANT_SUPPORT" ,
352 the kernel will panic.
353 Currently the following assertions are supported:
354 .Bl -tag -width MA_NOTRECURSED
356 Assert that the current thread
358 pointed to by the first argument.
360 Assert that the current thread
361 does not hold the mutex
362 pointed to by the first argument.
364 Assert that the current thread has recursed on the mutex
365 pointed to by the first argument.
366 This assertion is only valid in conjunction with
368 .It Dv MA_NOTRECURSED
369 Assert that the current thread has not recursed on the mutex
370 pointed to by the first argument.
371 This assertion is only valid in conjunction with
377 macro is used to generate a call to the
379 routine at system startup in order to initialize a given mutex lock.
380 The parameters are the same as
382 but with an additional argument,
384 that is used in generating unique variable names for the related structures associated with the lock and the sysinit routine.
385 .Ss The Default Mutex Type
386 Most kernel code should use the default lock type,
388 The default lock type will allow the thread
389 to be disconnected from the CPU
390 if the lock is already held by another thread.
392 may treat the lock as a short term spin lock
393 under some circumstances.
394 However, it is always safe to use these forms of locks
395 in an interrupt thread
396 without fear of deadlock
397 against an interrupted thread on the same CPU.
398 .Ss The Spin Mutex Type
401 mutex will not relinquish the CPU
402 when it cannot immediately get the requested lock,
403 but will loop, waiting for the mutex to be released by another CPU.
404 This could result in deadlock
405 if another thread interrupted the thread which held a mutex
406 and then tried to acquire the mutex.
407 For this reason spin locks disable all interrupts on the local CPU.
409 Spin locks are fairly specialized locks
410 that are intended to be held for very short periods of time.
411 Their primary purpose is to protect portions of the code
412 that implement other synchronization primitives such as default mutexes,
413 thread scheduling, and interrupt threads.
414 .Ss Initialization Options
415 The options passed in the
419 specify the mutex type.
424 options is required and only one of those two options may be specified.
425 The possibilities are:
426 .Bl -tag -width MTX_NOWITNESS
429 will always allow the current thread to be suspended
430 to avoid deadlock conditions against interrupt threads.
431 The implementation of this lock type
432 may spin for a while before suspending the current thread.
435 will never relinquish the CPU.
436 All interrupts are disabled on the local CPU
437 while any spin lock is held.
439 Specifies that the initialized mutex is allowed to recurse.
440 This bit must be present if the mutex is permitted to recurse.
442 Do not log any mutex operations for this lock.
448 Witness should not log messages about duplicate locks being acquired.
450 Do not profile this lock.
452 .Ss Lock and Unlock Flags
453 The flags passed to the
455 .Fn mtx_lock_spin_flags ,
456 .Fn mtx_unlock_flags ,
458 .Fn mtx_unlock_spin_flags
459 functions provide some basic options to the caller,
460 and are often used only under special circumstances to modify lock or
462 Standard locking and unlocking should be performed with the
469 Only if a flag is required should the corresponding
470 flags-accepting routines be used.
472 Options that modify mutex behavior:
473 .Bl -tag -width MTX_QUIET
475 This option is used to quiet logging messages during individual mutex
477 This can be used to trim superfluous logging messages for debugging purposes.
482 must be acquired, it must be acquired prior to acquiring
484 Put another way: it is impossible to acquire
486 non-recursively while
487 holding another mutex.
488 It is possible to acquire other mutexes while holding
490 and it is possible to acquire
492 recursively while holding other mutexes.
494 Sleeping while holding a mutex (except for
497 and should be avoided.
498 There are numerous assertions which will fail if this is attempted.
499 .Ss Functions Which Access Memory in Userspace
500 No mutexes should be held (except for
502 across functions which
503 access memory in userspace, such as
509 No locks are needed when calling these functions.
512 .Xr LOCK_PROFILING 9 ,
522 functions appeared in