2001-07-14 19:41:16 +00:00
|
|
|
.\"
|
2000-09-07 01:33:02 +00:00
|
|
|
.\" Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
|
2001-07-14 19:41:16 +00:00
|
|
|
.\"
|
2000-09-07 01:33:02 +00:00
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\" 3. Berkeley Software Design Inc's name may not be used to endorse or
|
|
|
|
.\" promote products derived from this software without specific prior
|
|
|
|
.\" written permission.
|
2001-07-14 19:41:16 +00:00
|
|
|
.\"
|
2005-02-09 18:07:17 +00:00
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
|
|
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
|
|
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
2000-09-07 01:33:02 +00:00
|
|
|
.\"
|
|
|
|
.\" from BSDI $Id: mutex.4,v 1.1.2.3 1998/04/27 22:53:13 ewv Exp $
|
|
|
|
.\" $FreeBSD$
|
|
|
|
.\"
|
2011-11-16 21:51:17 +00:00
|
|
|
.Dd November 16, 2011
|
2000-09-07 01:33:02 +00:00
|
|
|
.Dt MUTEX 9
|
2000-12-14 08:55:32 +00:00
|
|
|
.Os
|
2000-09-07 01:33:02 +00:00
|
|
|
.Sh NAME
|
2000-10-30 19:49:26 +00:00
|
|
|
.Nm mutex ,
|
|
|
|
.Nm mtx_init ,
|
2007-03-09 16:54:03 +00:00
|
|
|
.Nm mtx_destroy ,
|
2001-02-12 03:14:57 +00:00
|
|
|
.Nm mtx_lock ,
|
|
|
|
.Nm mtx_lock_spin ,
|
|
|
|
.Nm mtx_lock_flags ,
|
|
|
|
.Nm mtx_lock_spin_flags ,
|
|
|
|
.Nm mtx_trylock ,
|
|
|
|
.Nm mtx_trylock_flags ,
|
|
|
|
.Nm mtx_unlock ,
|
|
|
|
.Nm mtx_unlock_spin ,
|
|
|
|
.Nm mtx_unlock_flags ,
|
|
|
|
.Nm mtx_unlock_spin_flags ,
|
2007-03-09 22:41:01 +00:00
|
|
|
.Nm mtx_sleep ,
|
2001-03-30 22:59:02 +00:00
|
|
|
.Nm mtx_initialized ,
|
2000-10-30 19:49:26 +00:00
|
|
|
.Nm mtx_owned ,
|
2001-02-12 03:14:57 +00:00
|
|
|
.Nm mtx_recursed ,
|
2004-07-05 17:12:53 +00:00
|
|
|
.Nm mtx_assert ,
|
2002-04-02 17:21:00 +00:00
|
|
|
.Nm MTX_SYSINIT
|
2000-09-07 01:33:02 +00:00
|
|
|
.Nd kernel synchronization primitives
|
|
|
|
.Sh SYNOPSIS
|
2001-10-01 16:09:29 +00:00
|
|
|
.In sys/param.h
|
|
|
|
.In sys/lock.h
|
|
|
|
.In sys/mutex.h
|
2001-03-29 10:06:50 +00:00
|
|
|
.Ft void
|
2002-04-12 05:55:17 +00:00
|
|
|
.Fn mtx_init "struct mtx *mutex" "const char *name" "const char *type" "int opts"
|
2000-09-25 11:21:49 +00:00
|
|
|
.Ft void
|
2007-03-09 16:54:03 +00:00
|
|
|
.Fn mtx_destroy "struct mtx *mutex"
|
|
|
|
.Ft void
|
2001-02-12 03:14:57 +00:00
|
|
|
.Fn mtx_lock "struct mtx *mutex"
|
|
|
|
.Ft void
|
|
|
|
.Fn mtx_lock_spin "struct mtx *mutex"
|
|
|
|
.Ft void
|
|
|
|
.Fn mtx_lock_flags "struct mtx *mutex" "int flags"
|
|
|
|
.Ft void
|
|
|
|
.Fn mtx_lock_spin_flags "struct mtx *mutex" "int flags"
|
|
|
|
.Ft int
|
|
|
|
.Fn mtx_trylock "struct mtx *mutex"
|
2001-02-02 00:49:17 +00:00
|
|
|
.Ft int
|
2001-02-12 03:14:57 +00:00
|
|
|
.Fn mtx_trylock_flags "struct mtx *mutex" "int flags"
|
2000-09-07 01:33:02 +00:00
|
|
|
.Ft void
|
2001-02-12 03:14:57 +00:00
|
|
|
.Fn mtx_unlock "struct mtx *mutex"
|
|
|
|
.Ft void
|
|
|
|
.Fn mtx_unlock_spin "struct mtx *mutex"
|
|
|
|
.Ft void
|
|
|
|
.Fn mtx_unlock_flags "struct mtx *mutex" "int flags"
|
|
|
|
.Ft void
|
|
|
|
.Fn mtx_unlock_spin_flags "struct mtx *mutex" "int flags"
|
2000-09-07 01:33:02 +00:00
|
|
|
.Ft int
|
2007-03-09 22:41:01 +00:00
|
|
|
.Fn mtx_sleep "void *chan" "struct mtx *mtx" "int priority" "const char *wmesg" "int timo"
|
|
|
|
.Ft int
|
2011-11-16 21:51:17 +00:00
|
|
|
.Fn mtx_initialized "const struct mtx *mutex"
|
2001-03-30 22:59:02 +00:00
|
|
|
.Ft int
|
2011-11-16 21:51:17 +00:00
|
|
|
.Fn mtx_owned "const struct mtx *mutex"
|
2001-02-12 03:14:57 +00:00
|
|
|
.Ft int
|
2011-11-16 21:51:17 +00:00
|
|
|
.Fn mtx_recursed "const struct mtx *mutex"
|
2004-01-01 05:16:35 +00:00
|
|
|
.Pp
|
|
|
|
.Cd "options INVARIANTS"
|
|
|
|
.Cd "options INVARIANT_SUPPORT"
|
2000-09-25 11:21:49 +00:00
|
|
|
.Ft void
|
2011-11-16 21:51:17 +00:00
|
|
|
.Fn mtx_assert "const struct mtx *mutex" "int what"
|
2003-12-28 01:39:18 +00:00
|
|
|
.In sys/kernel.h
|
2006-07-09 09:46:43 +00:00
|
|
|
.Fn MTX_SYSINIT "name" "struct mtx *mtx" "const char *description" "int opts"
|
2000-09-07 01:33:02 +00:00
|
|
|
.Sh DESCRIPTION
|
2005-09-13 15:16:50 +00:00
|
|
|
Mutexes are the most basic and primary method of thread synchronization.
|
2000-09-25 11:21:49 +00:00
|
|
|
The major design considerations for mutexes are:
|
|
|
|
.Bl -enum
|
|
|
|
.It
|
|
|
|
Acquiring and releasing uncontested mutexes should be as cheap
|
|
|
|
as possible.
|
|
|
|
.It
|
|
|
|
They must have the information and storage space to support
|
|
|
|
priority propagation.
|
|
|
|
.It
|
2005-09-13 15:16:50 +00:00
|
|
|
A thread must be able to recursively acquire a mutex,
|
2001-02-12 03:14:57 +00:00
|
|
|
provided that the mutex is initialized to support recursion.
|
2000-09-25 11:21:49 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
There are currently two flavors of mutexes, those that context switch
|
|
|
|
when they block and those that do not.
|
|
|
|
.Pp
|
2001-02-12 03:14:57 +00:00
|
|
|
By default,
|
|
|
|
.Dv MTX_DEF
|
|
|
|
mutexes will context switch when they are already held.
|
2005-09-13 15:16:50 +00:00
|
|
|
As an optimization,
|
|
|
|
they may spin for some amount
|
2000-09-25 11:21:49 +00:00
|
|
|
of time before context switching.
|
2005-09-13 15:16:50 +00:00
|
|
|
It is important to remember that since a thread may be preempted at any time,
|
2001-02-12 03:14:57 +00:00
|
|
|
the possible context switch introduced by acquiring a mutex is guaranteed
|
2005-02-13 22:25:33 +00:00
|
|
|
to not break anything that is not already broken.
|
2000-09-25 11:21:49 +00:00
|
|
|
.Pp
|
2001-02-12 03:14:57 +00:00
|
|
|
Mutexes which do not context switch are
|
|
|
|
.Dv MTX_SPIN
|
|
|
|
mutexes.
|
2005-09-13 15:16:50 +00:00
|
|
|
These should only be used to protect data shared with primary interrupt
|
|
|
|
code.
|
2011-01-06 21:14:34 +00:00
|
|
|
This includes interrupt filters and low level scheduling code.
|
2005-09-13 15:16:50 +00:00
|
|
|
In all architectures both acquiring and releasing of a
|
2000-09-25 11:21:49 +00:00
|
|
|
uncontested spin mutex is more expensive than the same operation
|
2005-01-06 16:16:17 +00:00
|
|
|
on a non-spin mutex.
|
2000-09-25 11:21:49 +00:00
|
|
|
In order to protect an interrupt service routine from blocking
|
2005-09-13 15:16:50 +00:00
|
|
|
against itself all interrupts are either blocked or deferred on a processor
|
|
|
|
while holding a spin lock.
|
2000-10-30 19:49:26 +00:00
|
|
|
It is permissible to hold multiple spin mutexes.
|
2000-09-25 11:21:49 +00:00
|
|
|
.Pp
|
|
|
|
Once a spin mutex has been acquired it is not permissible to acquire a
|
|
|
|
blocking mutex.
|
|
|
|
.Pp
|
|
|
|
The storage needed to implement a mutex is provided by a
|
2001-02-12 10:56:50 +00:00
|
|
|
.Vt struct mtx .
|
2000-09-25 11:21:49 +00:00
|
|
|
In general this should be treated as an opaque object and
|
|
|
|
referenced only with the mutex primitives.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn mtx_init
|
|
|
|
function must be used to initialize a mutex
|
2005-09-13 15:16:50 +00:00
|
|
|
before it can be passed to any of the other mutex functions.
|
2000-09-25 11:21:49 +00:00
|
|
|
The
|
2002-04-12 05:55:17 +00:00
|
|
|
.Fa name
|
|
|
|
option is used to identify the lock in debugging output etc.
|
|
|
|
The
|
|
|
|
.Fa type
|
|
|
|
option is used by the witness code to classify a mutex when doing checks
|
|
|
|
of lock ordering.
|
|
|
|
If
|
|
|
|
.Fa type
|
|
|
|
is
|
|
|
|
.Dv NULL ,
|
|
|
|
.Fa name
|
|
|
|
is used in its place.
|
|
|
|
The pointer passed in as
|
|
|
|
.Fa name
|
|
|
|
and
|
|
|
|
.Fa type
|
2000-09-25 11:21:49 +00:00
|
|
|
is saved rather than the data it points to.
|
|
|
|
The data pointed to must remain stable
|
|
|
|
until the mutex is destroyed.
|
2001-02-12 03:14:57 +00:00
|
|
|
The
|
2001-07-14 19:41:16 +00:00
|
|
|
.Fa opts
|
2001-02-12 10:56:50 +00:00
|
|
|
argument is used to set the type of mutex.
|
|
|
|
It may contain either
|
2001-02-12 03:14:57 +00:00
|
|
|
.Dv MTX_DEF
|
|
|
|
or
|
|
|
|
.Dv MTX_SPIN
|
2001-02-12 10:56:50 +00:00
|
|
|
but not both.
|
2002-06-06 19:08:11 +00:00
|
|
|
See below for additional initialization options.
|
2000-09-25 11:21:49 +00:00
|
|
|
It is not permissible to pass the same
|
2001-02-12 10:56:50 +00:00
|
|
|
.Fa mutex
|
2000-09-25 11:21:49 +00:00
|
|
|
to
|
|
|
|
.Fn mtx_init
|
|
|
|
multiple times without intervening calls to
|
|
|
|
.Fn mtx_destroy .
|
|
|
|
.Pp
|
2000-09-07 01:33:02 +00:00
|
|
|
The
|
2001-02-12 03:14:57 +00:00
|
|
|
.Fn mtx_lock
|
|
|
|
function acquires a
|
|
|
|
.Dv MTX_DEF
|
|
|
|
mutual exclusion lock
|
2000-09-07 01:33:02 +00:00
|
|
|
on behalf of the currently running kernel thread.
|
|
|
|
If another kernel thread is holding the mutex,
|
|
|
|
the caller will be disconnected from the CPU
|
|
|
|
until the mutex is available
|
2005-02-15 23:52:35 +00:00
|
|
|
(i.e., it will block).
|
2001-02-12 03:14:57 +00:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn mtx_lock_spin
|
|
|
|
function acquires a
|
|
|
|
.Dv MTX_SPIN
|
|
|
|
mutual exclusion lock
|
|
|
|
on behalf of the currently running kernel thread.
|
|
|
|
If another kernel thread is holding the mutex,
|
|
|
|
the caller will spin until the mutex becomes available.
|
|
|
|
Interrupts are disabled during the spin and remain disabled
|
|
|
|
following the acquiring of the lock.
|
2000-09-07 01:33:02 +00:00
|
|
|
.Pp
|
|
|
|
It is possible for the same thread to recursively acquire a mutex
|
2001-02-12 03:14:57 +00:00
|
|
|
with no ill effects, provided that the
|
|
|
|
.Dv MTX_RECURSE
|
|
|
|
bit was passed to
|
|
|
|
.Fn mtx_init
|
|
|
|
during the initialization of the mutex.
|
2000-09-07 01:33:02 +00:00
|
|
|
.Pp
|
2000-10-30 19:49:26 +00:00
|
|
|
The
|
2001-02-12 03:14:57 +00:00
|
|
|
.Fn mtx_lock_flags
|
|
|
|
and
|
|
|
|
.Fn mtx_lock_spin_flags
|
|
|
|
functions acquire a
|
|
|
|
.Dv MTX_DEF
|
|
|
|
or
|
|
|
|
.Dv MTX_SPIN
|
|
|
|
lock, respectively, and also accept a
|
2001-02-12 10:56:50 +00:00
|
|
|
.Fa flags
|
2001-02-12 03:14:57 +00:00
|
|
|
argument.
|
2013-08-09 11:24:29 +00:00
|
|
|
In both cases, the only flags presently available for lock acquires are
|
2013-08-10 06:39:42 +00:00
|
|
|
.Dv MTX_QUIET
|
2013-08-09 11:24:29 +00:00
|
|
|
and
|
|
|
|
.Dv MTX_RECURSE .
|
2001-02-12 03:14:57 +00:00
|
|
|
If the
|
|
|
|
.Dv MTX_QUIET
|
|
|
|
bit is turned on in the
|
2001-02-12 10:56:50 +00:00
|
|
|
.Fa flags
|
|
|
|
argument, then if
|
|
|
|
.Dv KTR_LOCK
|
|
|
|
tracing is being done,
|
2001-02-12 03:14:57 +00:00
|
|
|
it will be silenced during the lock acquire.
|
2013-08-09 11:24:29 +00:00
|
|
|
If the
|
|
|
|
.Dv MTX_RECURSE
|
|
|
|
bit is turned on in the
|
|
|
|
.Fa flags
|
|
|
|
argument, then the mutex can be acquired recursively.
|
|
|
|
.Pp
|
2001-02-12 03:14:57 +00:00
|
|
|
The
|
|
|
|
.Fn mtx_trylock
|
2005-09-13 15:16:50 +00:00
|
|
|
attempts to acquire the
|
|
|
|
.Dv MTX_DEF
|
|
|
|
mutex pointed to by
|
2001-02-12 10:56:50 +00:00
|
|
|
.Fa mutex .
|
2001-08-08 18:32:06 +00:00
|
|
|
If the mutex cannot be immediately acquired
|
2001-05-06 22:04:17 +00:00
|
|
|
.Fn mtx_trylock
|
2000-09-25 11:21:49 +00:00
|
|
|
will return 0,
|
|
|
|
otherwise the mutex will be acquired
|
|
|
|
and a non-zero value will be returned.
|
|
|
|
.Pp
|
2000-09-07 01:33:02 +00:00
|
|
|
The
|
2001-02-12 03:14:57 +00:00
|
|
|
.Fn mtx_trylock_flags
|
|
|
|
function has the same behavior as
|
|
|
|
.Fn mtx_trylock
|
|
|
|
but should be used when the caller desires to pass in a
|
2001-02-12 10:56:50 +00:00
|
|
|
.Fa flags
|
|
|
|
value.
|
|
|
|
Presently, the only valid value in the
|
2001-02-12 03:14:57 +00:00
|
|
|
.Fn mtx_trylock
|
|
|
|
case is
|
|
|
|
.Dv MTX_QUIET ,
|
2001-07-14 19:41:16 +00:00
|
|
|
and its effects are identical to those described for
|
2001-02-12 03:14:57 +00:00
|
|
|
.Fn mtx_lock
|
|
|
|
above.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn mtx_unlock
|
|
|
|
function releases a
|
|
|
|
.Dv MTX_DEF
|
2005-09-13 15:16:50 +00:00
|
|
|
mutual exclusion lock.
|
|
|
|
The current thread may be preempted if a higher priority thread is waiting
|
|
|
|
for the mutex.
|
2000-09-07 01:33:02 +00:00
|
|
|
.Pp
|
2000-09-25 11:21:49 +00:00
|
|
|
The
|
2001-02-12 03:14:57 +00:00
|
|
|
.Fn mtx_unlock_spin
|
|
|
|
function releases a
|
|
|
|
.Dv MTX_SPIN
|
2005-09-13 15:16:50 +00:00
|
|
|
mutual exclusion lock.
|
2001-02-12 03:14:57 +00:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn mtx_unlock_flags
|
|
|
|
and
|
|
|
|
.Fn mtx_unlock_spin_flags
|
|
|
|
functions behave in exactly the same way as do the standard mutex
|
|
|
|
unlock routines above, while also allowing a
|
2001-02-12 10:56:50 +00:00
|
|
|
.Fa flags
|
2002-09-24 20:45:58 +00:00
|
|
|
argument which may specify
|
|
|
|
.Dv MTX_QUIET .
|
2001-02-12 10:56:50 +00:00
|
|
|
The behavior of
|
2001-02-12 03:14:57 +00:00
|
|
|
.Dv MTX_QUIET
|
|
|
|
is identical to its behavior in the mutex lock routines.
|
|
|
|
.Pp
|
|
|
|
The
|
2000-09-25 11:21:49 +00:00
|
|
|
.Fn mtx_destroy
|
|
|
|
function is used to destroy
|
2001-02-12 10:56:50 +00:00
|
|
|
.Fa mutex
|
2000-09-25 11:21:49 +00:00
|
|
|
so the data associated with it may be freed
|
|
|
|
or otherwise overwritten.
|
|
|
|
Any mutex which is destroyed
|
|
|
|
must previously have been initialized with
|
|
|
|
.Fn mtx_init .
|
|
|
|
It is permissible to have a single hold count
|
|
|
|
on a mutex when it is destroyed.
|
|
|
|
It is not permissible to hold the mutex recursively,
|
2005-09-13 15:16:50 +00:00
|
|
|
or have another thread blocked on the mutex
|
2000-09-25 11:21:49 +00:00
|
|
|
when it is destroyed.
|
|
|
|
.Pp
|
|
|
|
The
|
2007-03-09 22:41:01 +00:00
|
|
|
.Fn mtx_sleep
|
|
|
|
function is used to atomically release
|
|
|
|
.Fa mtx
|
|
|
|
while waiting for an event.
|
|
|
|
For more details on the parameters to this function,
|
|
|
|
see
|
|
|
|
.Xr sleep 9 .
|
|
|
|
.Pp
|
|
|
|
The
|
2001-03-30 22:59:02 +00:00
|
|
|
.Fn mtx_initialized
|
|
|
|
function returns non-zero if
|
|
|
|
.Fa mutex
|
|
|
|
has been initialized and zero otherwise.
|
|
|
|
.Pp
|
|
|
|
The
|
2000-09-25 11:21:49 +00:00
|
|
|
.Fn mtx_owned
|
|
|
|
function returns non-zero
|
2005-09-13 15:16:50 +00:00
|
|
|
if the current thread holds
|
2001-02-12 10:56:50 +00:00
|
|
|
.Fa mutex .
|
2005-09-13 15:16:50 +00:00
|
|
|
If the current thread does not hold
|
2001-02-12 10:56:50 +00:00
|
|
|
.Fa mutex
|
2000-09-25 11:21:49 +00:00
|
|
|
zero is returned.
|
|
|
|
.Pp
|
|
|
|
The
|
2001-02-12 03:14:57 +00:00
|
|
|
.Fn mtx_recursed
|
|
|
|
function returns non-zero if the
|
2001-02-12 10:56:50 +00:00
|
|
|
.Fa mutex
|
2001-02-12 03:14:57 +00:00
|
|
|
is recursed.
|
|
|
|
This check should only be made if the running thread already owns
|
2001-02-12 10:56:50 +00:00
|
|
|
.Fa mutex .
|
2001-02-12 03:14:57 +00:00
|
|
|
.Pp
|
|
|
|
The
|
2000-09-25 11:21:49 +00:00
|
|
|
.Fn mtx_assert
|
2004-01-01 05:16:35 +00:00
|
|
|
function allows assertions specified in
|
2004-06-16 08:33:57 +00:00
|
|
|
.Fa what
|
2004-01-01 05:16:35 +00:00
|
|
|
to be made about
|
2001-02-12 10:56:50 +00:00
|
|
|
.Fa mutex .
|
2000-10-26 23:53:12 +00:00
|
|
|
If the assertions are not true and the kernel is compiled with
|
2004-01-01 05:16:35 +00:00
|
|
|
.Cd "options INVARIANTS"
|
|
|
|
and
|
|
|
|
.Cd "options INVARIANT_SUPPORT" ,
|
|
|
|
the kernel will panic.
|
2000-09-25 11:21:49 +00:00
|
|
|
Currently the following assertions are supported:
|
2000-12-14 08:55:32 +00:00
|
|
|
.Bl -tag -width MA_NOTRECURSED
|
|
|
|
.It Dv MA_OWNED
|
2000-09-25 11:21:49 +00:00
|
|
|
Assert that the current thread
|
|
|
|
holds the mutex
|
|
|
|
pointed to by the first argument.
|
2000-12-14 08:55:32 +00:00
|
|
|
.It Dv MA_NOTOWNED
|
2000-09-25 11:21:49 +00:00
|
|
|
Assert that the current thread
|
|
|
|
does not hold the mutex
|
|
|
|
pointed to by the first argument.
|
2000-12-14 08:55:32 +00:00
|
|
|
.It Dv MA_RECURSED
|
2000-12-13 23:30:43 +00:00
|
|
|
Assert that the current thread has recursed on the mutex
|
|
|
|
pointed to by the first argument.
|
2002-01-09 11:43:48 +00:00
|
|
|
This assertion is only valid in conjunction with
|
2000-12-13 23:30:43 +00:00
|
|
|
.Dv MA_OWNED .
|
2000-12-14 08:55:32 +00:00
|
|
|
.It Dv MA_NOTRECURSED
|
2000-12-13 23:30:43 +00:00
|
|
|
Assert that the current thread has not recursed on the mutex
|
|
|
|
pointed to by the first argument.
|
2002-01-09 11:43:48 +00:00
|
|
|
This assertion is only valid in conjunction with
|
2000-12-13 23:30:43 +00:00
|
|
|
.Dv MA_OWNED .
|
2000-09-25 11:21:49 +00:00
|
|
|
.El
|
2002-04-02 17:21:00 +00:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn MTX_SYSINIT
|
2002-05-29 17:50:04 +00:00
|
|
|
macro is used to generate a call to the
|
2002-04-02 17:21:00 +00:00
|
|
|
.Fn mtx_sysinit
|
2002-05-29 17:50:04 +00:00
|
|
|
routine at system startup in order to initialize a given mutex lock.
|
|
|
|
The parameters are the same as
|
2002-04-02 17:21:00 +00:00
|
|
|
.Fn mtx_init
|
|
|
|
but with an additional argument,
|
2002-05-29 17:50:04 +00:00
|
|
|
.Fa name ,
|
2002-04-02 17:21:00 +00:00
|
|
|
that is used in generating unique variable names for the related structures associated with the lock and the sysinit routine.
|
2001-02-12 03:14:57 +00:00
|
|
|
.Ss The Default Mutex Type
|
|
|
|
Most kernel code should use the default lock type,
|
2005-09-13 15:16:50 +00:00
|
|
|
.Dv MTX_DEF .
|
|
|
|
The default lock type will allow the thread
|
2000-09-07 01:33:02 +00:00
|
|
|
to be disconnected from the CPU
|
2005-09-13 15:16:50 +00:00
|
|
|
if the lock is already held by another thread.
|
|
|
|
The implementation
|
2000-09-07 01:33:02 +00:00
|
|
|
may treat the lock as a short term spin lock
|
|
|
|
under some circumstances.
|
|
|
|
However, it is always safe to use these forms of locks
|
|
|
|
in an interrupt thread
|
|
|
|
without fear of deadlock
|
|
|
|
against an interrupted thread on the same CPU.
|
2001-02-12 03:14:57 +00:00
|
|
|
.Ss The Spin Mutex Type
|
2001-07-14 19:41:16 +00:00
|
|
|
A
|
2001-02-12 03:14:57 +00:00
|
|
|
.Dv MTX_SPIN
|
|
|
|
mutex will not relinquish the CPU
|
2000-09-07 01:33:02 +00:00
|
|
|
when it cannot immediately get the requested lock,
|
|
|
|
but will loop, waiting for the mutex to be released by another CPU.
|
|
|
|
This could result in deadlock
|
2005-09-13 15:16:50 +00:00
|
|
|
if another thread interrupted the thread which held a mutex
|
|
|
|
and then tried to acquire the mutex.
|
|
|
|
For this reason spin locks disable all interrupts on the local CPU.
|
2000-09-07 01:33:02 +00:00
|
|
|
.Pp
|
|
|
|
Spin locks are fairly specialized locks
|
2005-09-13 15:16:50 +00:00
|
|
|
that are intended to be held for very short periods of time.
|
|
|
|
Their primary purpose is to protect portions of the code
|
|
|
|
that implement other synchronization primitives such as default mutexes,
|
|
|
|
thread scheduling, and interrupt threads.
|
2001-02-12 03:14:57 +00:00
|
|
|
.Ss Initialization Options
|
|
|
|
The options passed in the
|
2001-02-12 10:56:50 +00:00
|
|
|
.Fa opts
|
2001-02-12 03:14:57 +00:00
|
|
|
argument of
|
|
|
|
.Fn mtx_init
|
|
|
|
specify the mutex type.
|
2005-09-13 15:16:50 +00:00
|
|
|
One of the
|
|
|
|
.Dv MTX_DEF
|
|
|
|
or
|
|
|
|
.Dv MTX_SPIN
|
|
|
|
options is required and only one of those two options may be specified.
|
2001-02-12 03:14:57 +00:00
|
|
|
The possibilities are:
|
2001-03-30 22:59:02 +00:00
|
|
|
.Bl -tag -width MTX_NOWITNESS
|
2000-09-07 01:33:02 +00:00
|
|
|
.It Dv MTX_DEF
|
2005-09-13 15:16:50 +00:00
|
|
|
Default mutexes
|
2000-09-07 01:33:02 +00:00
|
|
|
will always allow the current thread to be suspended
|
|
|
|
to avoid deadlock conditions against interrupt threads.
|
2005-09-13 15:16:50 +00:00
|
|
|
The implementation of this lock type
|
2000-09-07 01:33:02 +00:00
|
|
|
may spin for a while before suspending the current thread.
|
|
|
|
.It Dv MTX_SPIN
|
2005-09-13 15:16:50 +00:00
|
|
|
Spin mutexes
|
2000-09-07 01:33:02 +00:00
|
|
|
will never relinquish the CPU.
|
2001-02-12 03:14:57 +00:00
|
|
|
All interrupts are disabled on the local CPU
|
2000-09-07 01:33:02 +00:00
|
|
|
while any spin lock is held.
|
2001-02-12 03:14:57 +00:00
|
|
|
.It Dv MTX_RECURSE
|
2005-09-13 15:16:50 +00:00
|
|
|
Specifies that the initialized mutex is allowed to recurse.
|
|
|
|
This bit must be present if the mutex is permitted to recurse.
|
2001-03-30 22:59:02 +00:00
|
|
|
.It Dv MTX_QUIET
|
|
|
|
Do not log any mutex operations for this lock.
|
|
|
|
.It Dv MTX_NOWITNESS
|
|
|
|
Instruct
|
|
|
|
.Xr witness 4
|
|
|
|
to ignore this lock.
|
2002-06-06 19:08:11 +00:00
|
|
|
.It Dv MTX_DUPOK
|
|
|
|
Witness should not log messages about duplicate locks being acquired.
|
2006-12-21 22:42:18 +00:00
|
|
|
.It Dv MTX_NOPROFILE
|
|
|
|
Do not profile this lock.
|
2000-09-07 01:33:02 +00:00
|
|
|
.El
|
2001-02-12 03:14:57 +00:00
|
|
|
.Ss Lock and Unlock Flags
|
|
|
|
The flags passed to the
|
|
|
|
.Fn mtx_lock_flags ,
|
|
|
|
.Fn mtx_lock_spin_flags ,
|
|
|
|
.Fn mtx_unlock_flags ,
|
|
|
|
and
|
2001-07-14 19:41:16 +00:00
|
|
|
.Fn mtx_unlock_spin_flags
|
2001-02-12 03:14:57 +00:00
|
|
|
functions provide some basic options to the caller,
|
|
|
|
and are often used only under special circumstances to modify lock or
|
2001-02-12 10:56:50 +00:00
|
|
|
unlock behavior.
|
|
|
|
Standard locking and unlocking should be performed with the
|
2001-02-12 03:14:57 +00:00
|
|
|
.Fn mtx_lock ,
|
|
|
|
.Fn mtx_lock_spin ,
|
|
|
|
.Fn mtx_unlock ,
|
|
|
|
and
|
|
|
|
.Fn mtx_unlock_spin
|
2001-02-12 10:56:50 +00:00
|
|
|
functions.
|
2002-09-24 20:45:58 +00:00
|
|
|
Only if a flag is required should the corresponding
|
|
|
|
flags-accepting routines be used.
|
2000-09-07 01:33:02 +00:00
|
|
|
.Pp
|
|
|
|
Options that modify mutex behavior:
|
2002-09-24 20:45:58 +00:00
|
|
|
.Bl -tag -width MTX_QUIET
|
2000-12-13 23:30:43 +00:00
|
|
|
.It Dv MTX_QUIET
|
2001-03-30 22:59:02 +00:00
|
|
|
This option is used to quiet logging messages during individual mutex
|
|
|
|
operations.
|
2000-12-13 23:30:43 +00:00
|
|
|
This can be used to trim superfluous logging messages for debugging purposes.
|
2000-09-07 01:33:02 +00:00
|
|
|
.El
|
2002-10-21 12:54:13 +00:00
|
|
|
.Ss Giant
|
2003-05-21 15:49:01 +00:00
|
|
|
If
|
|
|
|
.Va Giant
|
|
|
|
must be acquired, it must be acquired prior to acquiring
|
2002-10-21 12:54:13 +00:00
|
|
|
other mutexes.
|
2003-05-21 15:49:01 +00:00
|
|
|
Put another way: it is impossible to acquire
|
|
|
|
.Va Giant
|
|
|
|
non-recursively while
|
2002-10-21 12:54:13 +00:00
|
|
|
holding another mutex.
|
2003-05-21 15:49:01 +00:00
|
|
|
It is possible to acquire other mutexes while holding
|
|
|
|
.Va Giant ,
|
|
|
|
and it is possible to acquire
|
|
|
|
.Va Giant
|
|
|
|
recursively while holding other mutexes.
|
2002-10-21 12:54:13 +00:00
|
|
|
.Ss Sleeping
|
2003-05-21 15:49:01 +00:00
|
|
|
Sleeping while holding a mutex (except for
|
|
|
|
.Va Giant )
|
2005-09-13 15:16:50 +00:00
|
|
|
is never safe
|
2002-10-21 12:54:13 +00:00
|
|
|
and should be avoided.
|
|
|
|
There are numerous assertions which will fail if this is attempted.
|
|
|
|
.Ss Functions Which Access Memory in Userspace
|
2003-05-21 15:49:01 +00:00
|
|
|
No mutexes should be held (except for
|
|
|
|
.Va Giant )
|
|
|
|
across functions which
|
2002-10-21 12:54:13 +00:00
|
|
|
access memory in userspace, such as
|
|
|
|
.Xr copyin 9 ,
|
|
|
|
.Xr copyout 9 ,
|
|
|
|
.Xr uiomove 9 ,
|
|
|
|
.Xr fuword 9 ,
|
|
|
|
etc.
|
|
|
|
No locks are needed when calling these functions.
|
2001-09-02 00:45:41 +00:00
|
|
|
.Sh SEE ALSO
|
2001-09-17 07:24:48 +00:00
|
|
|
.Xr condvar 9 ,
|
2006-11-11 08:41:39 +00:00
|
|
|
.Xr LOCK_PROFILING 9 ,
|
2007-03-30 18:07:26 +00:00
|
|
|
.Xr locking 9 ,
|
2002-03-28 12:51:06 +00:00
|
|
|
.Xr mtx_pool 9 ,
|
2004-01-01 05:16:35 +00:00
|
|
|
.Xr panic 9 ,
|
2006-02-01 20:30:55 +00:00
|
|
|
.Xr rwlock 9 ,
|
2001-09-17 07:24:48 +00:00
|
|
|
.Xr sema 9 ,
|
2007-03-09 22:41:01 +00:00
|
|
|
.Xr sleep 9 ,
|
2001-09-04 10:18:45 +00:00
|
|
|
.Xr sx 9
|
2000-09-07 01:33:02 +00:00
|
|
|
.Sh HISTORY
|
|
|
|
These
|
2000-12-14 08:55:32 +00:00
|
|
|
functions appeared in
|
2001-08-13 17:07:40 +00:00
|
|
|
.Bsx 4.1
|
|
|
|
and
|
2001-07-14 19:41:16 +00:00
|
|
|
.Fx 5.0 .
|