2001-07-14 19:41:16 +00:00
|
|
|
.\"
|
2001-08-13 16:33:00 +00:00
|
|
|
.\" Copyright (C) 2001 Jason Evans <jasone@FreeBSD.org>. All rights reserved.
|
2001-07-14 19:41:16 +00:00
|
|
|
.\"
|
2001-03-05 19:59:41 +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(s), this list of conditions and the following disclaimer as
|
2001-07-14 19:41:16 +00:00
|
|
|
.\" the first lines of this file unmodified other than the possible
|
2001-03-05 19:59:41 +00:00
|
|
|
.\" addition of one or more copyright notices.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice(s), this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``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 THE COPYRIGHT HOLDER(S) 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.
|
|
|
|
.\"
|
|
|
|
.\" $FreeBSD$
|
|
|
|
.\"
|
2009-05-29 01:49:27 +00:00
|
|
|
.Dd May 28, 2009
|
2001-03-05 19:59:41 +00:00
|
|
|
.Dt SX 9
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm sx ,
|
|
|
|
.Nm sx_init ,
|
2007-03-31 23:23:42 +00:00
|
|
|
.Nm sx_init_flags ,
|
2001-03-05 19:59:41 +00:00
|
|
|
.Nm sx_destroy ,
|
|
|
|
.Nm sx_slock ,
|
|
|
|
.Nm sx_xlock ,
|
2007-11-25 01:55:53 +00:00
|
|
|
.Nm sx_slock_sig ,
|
|
|
|
.Nm sx_xlock_sig ,
|
2001-08-07 04:29:53 +00:00
|
|
|
.Nm sx_try_slock ,
|
|
|
|
.Nm sx_try_xlock ,
|
2001-03-05 19:59:41 +00:00
|
|
|
.Nm sx_sunlock ,
|
2001-08-14 05:10:07 +00:00
|
|
|
.Nm sx_xunlock ,
|
2007-03-09 16:52:26 +00:00
|
|
|
.Nm sx_unlock ,
|
2001-08-14 05:10:07 +00:00
|
|
|
.Nm sx_try_upgrade ,
|
2001-10-23 22:51:59 +00:00
|
|
|
.Nm sx_downgrade ,
|
2007-03-09 22:41:01 +00:00
|
|
|
.Nm sx_sleep ,
|
2007-05-19 20:24:32 +00:00
|
|
|
.Nm sx_xholder ,
|
2006-07-12 15:40:35 +00:00
|
|
|
.Nm sx_xlocked ,
|
2007-03-09 16:52:26 +00:00
|
|
|
.Nm sx_assert ,
|
2002-04-02 17:21:00 +00:00
|
|
|
.Nm SX_SYSINIT
|
2001-04-04 12:00:29 +00:00
|
|
|
.Nd kernel shared/exclusive lock
|
2001-03-05 19:59:41 +00:00
|
|
|
.Sh SYNOPSIS
|
2001-10-04 09:01:49 +00:00
|
|
|
.In sys/param.h
|
2001-10-01 16:09:29 +00:00
|
|
|
.In sys/lock.h
|
|
|
|
.In sys/sx.h
|
2001-03-05 19:59:41 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn sx_init "struct sx *sx" "const char *description"
|
|
|
|
.Ft void
|
2007-03-31 23:23:42 +00:00
|
|
|
.Fn sx_init_flags "struct sx *sx" "const char *description" "int opts"
|
|
|
|
.Ft void
|
2001-03-05 19:59:41 +00:00
|
|
|
.Fn sx_destroy "struct sx *sx"
|
|
|
|
.Ft void
|
|
|
|
.Fn sx_slock "struct sx *sx"
|
|
|
|
.Ft void
|
|
|
|
.Fn sx_xlock "struct sx *sx"
|
2001-08-07 04:29:53 +00:00
|
|
|
.Ft int
|
2007-11-25 01:55:53 +00:00
|
|
|
.Fn sx_slock_sig "struct sx *sx"
|
|
|
|
.Ft int
|
|
|
|
.Fn sx_xlock_sig "struct sx *sx"
|
|
|
|
.Ft int
|
2001-08-07 04:29:53 +00:00
|
|
|
.Fn sx_try_slock "struct sx *sx"
|
|
|
|
.Ft int
|
|
|
|
.Fn sx_try_xlock "struct sx *sx"
|
2001-03-05 19:59:41 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn sx_sunlock "struct sx *sx"
|
|
|
|
.Ft void
|
|
|
|
.Fn sx_xunlock "struct sx *sx"
|
2007-03-09 16:52:26 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn sx_unlock "struct sx *sx"
|
2001-08-13 21:25:30 +00:00
|
|
|
.Ft int
|
|
|
|
.Fn sx_try_upgrade "struct sx *sx"
|
|
|
|
.Ft void
|
|
|
|
.Fn sx_downgrade "struct sx *sx"
|
2006-07-12 15:40:35 +00:00
|
|
|
.Ft int
|
2007-03-09 22:41:01 +00:00
|
|
|
.Fn sx_sleep "void *chan" "struct sx *sx" "int priority" "const char *wmesg" "int timo"
|
2007-05-19 20:24:32 +00:00
|
|
|
.Ft "struct thread *"
|
|
|
|
.Fn sx_xholder "struct sx *sx"
|
2007-03-09 22:41:01 +00:00
|
|
|
.Ft int
|
2006-07-12 15:40:35 +00:00
|
|
|
.Fn sx_xlocked "struct sx *sx"
|
2007-03-09 16:52:26 +00:00
|
|
|
.Pp
|
2004-07-11 16:08:25 +00:00
|
|
|
.Cd "options INVARIANTS"
|
|
|
|
.Cd "options INVARIANT_SUPPORT"
|
2007-03-09 16:52:26 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn sx_assert "struct sx *sx" "int what"
|
|
|
|
.In sys/kernel.h
|
|
|
|
.Fn SX_SYSINIT "name" "struct sx *sx" "const char *description"
|
2001-03-05 19:59:41 +00:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
Shared/exclusive locks are used to protect data that are read far more often
|
|
|
|
than they are written.
|
2007-03-31 23:23:42 +00:00
|
|
|
Shared/exclusive locks do not implement priority propagation like mutexes and
|
|
|
|
reader/writer locks to prevent priority inversions, so
|
2001-03-05 19:59:41 +00:00
|
|
|
shared/exclusive locks should be used prudently.
|
|
|
|
.Pp
|
2007-03-31 23:23:42 +00:00
|
|
|
Shared/exclusive locks are created with either
|
|
|
|
.Fn sx_init
|
|
|
|
or
|
|
|
|
.Fn sx_init_flags
|
2001-03-05 19:59:41 +00:00
|
|
|
where
|
|
|
|
.Fa sx
|
|
|
|
is a pointer to space for a
|
|
|
|
.Vt struct sx ,
|
|
|
|
and
|
|
|
|
.Fa description
|
|
|
|
is a pointer to a null-terminated character string that describes the
|
|
|
|
shared/exclusive lock.
|
2007-03-31 23:23:42 +00:00
|
|
|
The
|
|
|
|
.Fa opts
|
|
|
|
argument to
|
|
|
|
.Fn sx_init_flags
|
|
|
|
specifies a set of optional flags to alter the behavior of
|
|
|
|
.Fa sx .
|
|
|
|
It contains one or more of the following flags:
|
2009-05-29 01:49:27 +00:00
|
|
|
.Bl -tag -width SX_NOADAPTIVE
|
|
|
|
.It Dv SX_NOADAPTIVE
|
|
|
|
If the kernel is not compiled with
|
|
|
|
.Cd "options NO_ADAPTIVE_SX" ,
|
2007-03-31 23:23:42 +00:00
|
|
|
then lock operations for
|
|
|
|
.Fa sx
|
|
|
|
will spin instead of sleeping while an exclusive lock holder is executing on
|
|
|
|
another CPU.
|
|
|
|
.It Dv SX_DUPOK
|
|
|
|
Witness should not log messages about duplicate locks being acquired.
|
|
|
|
.It Dv SX_NOWITNESS
|
|
|
|
Instruct
|
|
|
|
.Xr witness 4
|
|
|
|
to ignore this lock.
|
|
|
|
.It Dv SX_NOPROFILE
|
|
|
|
Do not profile this lock.
|
2007-05-19 16:38:10 +00:00
|
|
|
.It Dv SX_RECURSE
|
|
|
|
Allow threads to recursively acquire exclusive locks for
|
|
|
|
.Fa sx .
|
2007-03-31 23:23:42 +00:00
|
|
|
.It Dv SX_QUIET
|
|
|
|
Do not log any operations for this lock via
|
|
|
|
.Xr ktr 4 .
|
|
|
|
.El
|
|
|
|
.Pp
|
2001-03-05 19:59:41 +00:00
|
|
|
Shared/exclusive locks are destroyed with
|
|
|
|
.Fn sx_destroy .
|
2007-03-31 23:23:42 +00:00
|
|
|
The lock
|
|
|
|
.Fa sx
|
|
|
|
must not be locked by any thread when it is destroyed.
|
|
|
|
.Pp
|
2001-03-05 19:59:41 +00:00
|
|
|
Threads acquire and release a shared lock by calling
|
2007-11-25 01:55:53 +00:00
|
|
|
.Fn sx_slock ,
|
|
|
|
.Fn sx_slock_sig
|
2001-08-07 04:29:53 +00:00
|
|
|
or
|
|
|
|
.Fn sx_try_slock
|
2001-03-05 19:59:41 +00:00
|
|
|
and
|
2004-07-11 16:08:25 +00:00
|
|
|
.Fn sx_sunlock
|
|
|
|
or
|
|
|
|
.Fn sx_unlock .
|
2001-03-05 19:59:41 +00:00
|
|
|
Threads acquire and release an exclusive lock by calling
|
2007-11-25 01:55:53 +00:00
|
|
|
.Fn sx_xlock ,
|
|
|
|
.Fn sx_xlock_sig
|
2001-08-07 04:29:53 +00:00
|
|
|
or
|
|
|
|
.Fn sx_try_xlock
|
2001-03-05 19:59:41 +00:00
|
|
|
and
|
2004-07-11 16:08:25 +00:00
|
|
|
.Fn sx_xunlock
|
|
|
|
or
|
|
|
|
.Fn sx_unlock .
|
2004-02-24 21:36:39 +00:00
|
|
|
A thread can attempt to upgrade a currently held shared lock to an exclusive
|
2001-08-13 21:25:30 +00:00
|
|
|
lock by calling
|
|
|
|
.Fn sx_try_upgrade .
|
2004-02-24 21:36:39 +00:00
|
|
|
A thread that has an exclusive lock can downgrade it to a shared lock by
|
2001-08-13 21:25:30 +00:00
|
|
|
calling
|
|
|
|
.Fn sx_downgrade .
|
2002-04-17 21:26:43 +00:00
|
|
|
.Pp
|
2001-08-07 04:29:53 +00:00
|
|
|
.Fn sx_try_slock
|
|
|
|
and
|
|
|
|
.Fn sx_try_xlock
|
|
|
|
will return 0 if the shared/exclusive lock cannot be acquired immediately;
|
|
|
|
otherwise the shared/exclusive lock will be acquired and a non-zero value will
|
|
|
|
be returned.
|
|
|
|
.Pp
|
2001-08-13 21:25:30 +00:00
|
|
|
.Fn sx_try_upgrade
|
|
|
|
will return 0 if the shared lock cannot be upgraded to an exclusive lock
|
|
|
|
immediately; otherwise the exclusive lock will be acquired and a non-zero value
|
|
|
|
will be returned.
|
|
|
|
.Pp
|
2007-11-25 01:55:53 +00:00
|
|
|
.Fn sx_slock_sig
|
|
|
|
and
|
|
|
|
.Fn sx_xlock_sig
|
|
|
|
do the same as their normal versions but performing an interruptible sleep.
|
|
|
|
They return a non-zero value if the sleep has been interrupted by a signal
|
|
|
|
or an interrupt, otherwise 0.
|
|
|
|
.Pp
|
2007-03-09 22:41:01 +00:00
|
|
|
A thread can atomically release a shared/exclusive lock while waiting for an
|
|
|
|
event by calling
|
|
|
|
.Fn sx_sleep .
|
|
|
|
For more details on the parameters to this function,
|
|
|
|
see
|
|
|
|
.Xr sleep 9 .
|
|
|
|
.Pp
|
2004-01-01 05:16:35 +00:00
|
|
|
When compiled with
|
|
|
|
.Cd "options INVARIANTS"
|
|
|
|
and
|
|
|
|
.Cd "options INVARIANT_SUPPORT" ,
|
|
|
|
the
|
2001-10-23 22:51:59 +00:00
|
|
|
.Fn sx_assert
|
2004-01-01 05:16:35 +00:00
|
|
|
function tests
|
2004-06-16 08:33:57 +00:00
|
|
|
.Fa sx
|
2004-01-01 05:16:35 +00:00
|
|
|
for the assertions specified in
|
2004-06-16 08:33:57 +00:00
|
|
|
.Fa what ,
|
2004-01-01 05:16:35 +00:00
|
|
|
and panics if they are not met.
|
2007-03-31 23:23:42 +00:00
|
|
|
One of the following assertions must be specified:
|
2007-05-19 21:26:05 +00:00
|
|
|
.Bl -tag -width ".Dv SA_UNLOCKED"
|
|
|
|
.It Dv SA_LOCKED
|
2001-10-23 22:51:59 +00:00
|
|
|
Assert that the current thread has either a shared or an exclusive lock on the
|
2001-11-21 11:44:36 +00:00
|
|
|
.Vt sx
|
|
|
|
lock pointed to by the first argument.
|
2007-05-19 21:26:05 +00:00
|
|
|
.It Dv SA_SLOCKED
|
2001-11-21 11:44:36 +00:00
|
|
|
Assert that the current thread has a shared lock on the
|
|
|
|
.Vt sx
|
|
|
|
lock pointed to by
|
2001-10-23 22:51:59 +00:00
|
|
|
the first argument.
|
2007-05-19 21:26:05 +00:00
|
|
|
.It Dv SA_XLOCKED
|
2001-11-21 11:44:36 +00:00
|
|
|
Assert that the current thread has an exclusive lock on the
|
|
|
|
.Vt sx
|
|
|
|
lock pointed to
|
2001-10-23 22:51:59 +00:00
|
|
|
by the first argument.
|
2007-05-19 21:26:05 +00:00
|
|
|
.It Dv SA_UNLOCKED
|
2004-02-04 08:24:05 +00:00
|
|
|
Assert that the current thread has no lock on the
|
|
|
|
.Vt sx
|
|
|
|
lock pointed to
|
|
|
|
by the first argument.
|
2001-10-23 22:51:59 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
2007-03-31 23:23:42 +00:00
|
|
|
In addition, one of the following optional assertions may be included with
|
|
|
|
either an
|
2007-05-19 21:26:05 +00:00
|
|
|
.Dv SA_LOCKED ,
|
|
|
|
.Dv SA_SLOCKED ,
|
2007-03-31 23:23:42 +00:00
|
|
|
or
|
2007-05-19 21:26:05 +00:00
|
|
|
.Dv SA_XLOCKED
|
2007-03-31 23:23:42 +00:00
|
|
|
assertion:
|
2007-05-19 21:26:05 +00:00
|
|
|
.Bl -tag -width ".Dv SA_NOTRECURSED"
|
|
|
|
.It Dv SA_RECURSED
|
2007-03-31 23:23:42 +00:00
|
|
|
Assert that the current thread has a recursed lock on
|
|
|
|
.Fa sx .
|
2007-05-19 21:26:05 +00:00
|
|
|
.It Dv SA_NOTRECURSED
|
2007-03-31 23:23:42 +00:00
|
|
|
Assert that the current thread does not have a recursed lock on
|
|
|
|
.Fa sx .
|
|
|
|
.El
|
|
|
|
.Pp
|
2007-05-19 20:24:32 +00:00
|
|
|
.Fn sx_xholder
|
|
|
|
will return a pointer to the thread which currently holds an exclusive lock on
|
|
|
|
.Fa sx .
|
|
|
|
If no thread holds an exclusive lock on
|
|
|
|
.Fa sx ,
|
|
|
|
then
|
|
|
|
.Dv NULL
|
|
|
|
is returned instead.
|
|
|
|
.Pp
|
2006-07-12 15:40:35 +00:00
|
|
|
.Fn sx_xlocked
|
2007-03-09 22:41:01 +00:00
|
|
|
will return non-zero if the current thread holds the exclusive lock;
|
2006-07-12 15:40:35 +00:00
|
|
|
otherwise, it will return zero.
|
|
|
|
.Pp
|
2004-07-11 16:08:25 +00:00
|
|
|
For ease of programming,
|
|
|
|
.Fn sx_unlock
|
|
|
|
is provided as a macro frontend to the respective functions,
|
|
|
|
.Fn sx_sunlock
|
|
|
|
and
|
|
|
|
.Fn sx_xunlock .
|
|
|
|
Algorithms that are aware of what state the lock is in should use either
|
|
|
|
of the two specific functions for a minor performance benefit.
|
|
|
|
.Pp
|
2003-12-27 14:01:08 +00:00
|
|
|
The
|
|
|
|
.Fn SX_SYSINIT
|
|
|
|
macro is used to generate a call to the
|
|
|
|
.Fn sx_sysinit
|
|
|
|
routine at system startup in order to initialize a given
|
|
|
|
.Fa sx
|
|
|
|
lock.
|
|
|
|
The parameters are the same as
|
|
|
|
.Fn sx_init
|
|
|
|
but with an additional argument,
|
|
|
|
.Fa name ,
|
|
|
|
that is used in generating unique variable names for the related
|
|
|
|
structures associated with the lock and the sysinit routine.
|
|
|
|
.Pp
|
2004-02-24 21:36:39 +00:00
|
|
|
A thread may not hold both a shared lock and an exclusive lock on the same
|
|
|
|
lock simultaneously;
|
2001-03-05 19:59:41 +00:00
|
|
|
attempting to do so will result in deadlock.
|
2003-06-12 09:06:25 +00:00
|
|
|
.Sh CONTEXT
|
2004-02-24 21:36:39 +00:00
|
|
|
A thread may hold a shared or exclusive lock on an
|
|
|
|
.Nm
|
|
|
|
lock while sleeping.
|
2005-01-05 22:04:12 +00:00
|
|
|
As a result, an
|
|
|
|
.Nm
|
|
|
|
lock may not be acquired while holding a mutex.
|
|
|
|
Otherwise, if one thread slept while holding an
|
|
|
|
.Nm
|
|
|
|
lock while another thread blocked on the same
|
|
|
|
.Nm
|
|
|
|
lock after acquiring a mutex, then the second thread would effectively
|
|
|
|
end up sleeping while holding a mutex, which is not allowed.
|
2001-03-05 19:59:41 +00:00
|
|
|
.Sh SEE ALSO
|
2007-03-30 18:07:26 +00:00
|
|
|
.Xr locking 9 ,
|
|
|
|
.Xr lock 9 ,
|
2001-09-17 07:24:48 +00:00
|
|
|
.Xr mutex 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
|
2001-10-23 22:51:59 +00:00
|
|
|
.Sh BUGS
|
|
|
|
Currently there is no way to assert that a lock is not held.
|
2001-11-21 11:47:55 +00:00
|
|
|
This is not possible in the
|
|
|
|
.No non- Ns Dv WITNESS
|
|
|
|
case for asserting that this thread
|
2001-10-23 22:51:59 +00:00
|
|
|
does not hold a shared lock.
|
2001-11-21 11:47:55 +00:00
|
|
|
In the
|
|
|
|
.No non- Ns Dv WITNESS
|
|
|
|
case, the
|
2007-05-19 21:26:05 +00:00
|
|
|
.Dv SA_LOCKED
|
2001-10-23 22:51:59 +00:00
|
|
|
and
|
2007-05-19 21:26:05 +00:00
|
|
|
.Dv SA_SLOCKED
|
2001-10-23 22:51:59 +00:00
|
|
|
assertions merely check that some thread holds a shared lock.
|
|
|
|
They do not ensure that the current thread holds a shared lock.
|