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$
|
|
|
|
.\"
|
2001-08-14 16:01:00 +00:00
|
|
|
.Dd August 14, 2001
|
2001-03-05 19:59:41 +00:00
|
|
|
.Dt SX 9
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm sx ,
|
|
|
|
.Nm sx_init ,
|
|
|
|
.Nm sx_destroy ,
|
|
|
|
.Nm sx_slock ,
|
|
|
|
.Nm sx_xlock ,
|
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 ,
|
|
|
|
.Nm sx_try_upgrade ,
|
2001-10-23 22:51:59 +00:00
|
|
|
.Nm sx_downgrade ,
|
2002-04-02 17:21:00 +00:00
|
|
|
.Nm sx_assert,
|
|
|
|
.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/mutex.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
|
|
|
|
.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
|
|
|
|
.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"
|
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"
|
2001-10-23 22:51:59 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn sx_assert "struct sx *sx" "int what"
|
2002-04-12 03:55:43 +00:00
|
|
|
.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.
|
|
|
|
Mutexes are inherently more efficient than shared/exclusive locks, so
|
|
|
|
shared/exclusive locks should be used prudently.
|
|
|
|
.Pp
|
|
|
|
Shared/exclusive locks are created with
|
|
|
|
.Fn sx_init ,
|
|
|
|
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.
|
|
|
|
Shared/exclusive locks are destroyed with
|
|
|
|
.Fn sx_destroy .
|
|
|
|
Threads acquire and release a shared lock by calling
|
|
|
|
.Fn sx_slock
|
2001-08-07 04:29:53 +00:00
|
|
|
or
|
|
|
|
.Fn sx_try_slock
|
2001-03-05 19:59:41 +00:00
|
|
|
and
|
|
|
|
.Fn sx_sunlock .
|
|
|
|
Threads acquire and release an exclusive lock by calling
|
|
|
|
.Fn sx_xlock
|
2001-08-07 04:29:53 +00:00
|
|
|
or
|
|
|
|
.Fn sx_try_xlock
|
2001-03-05 19:59:41 +00:00
|
|
|
and
|
|
|
|
.Fn sx_xunlock .
|
2001-08-13 21:25:30 +00:00
|
|
|
A thread can attempt to upgrade a currently owned shared lock to an exclusive
|
|
|
|
lock by calling
|
|
|
|
.Fn sx_try_upgrade .
|
|
|
|
A thread that owns an exclusive lock can downgrade it to a shared lock by
|
|
|
|
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
|
2001-10-23 22:51:59 +00:00
|
|
|
The
|
|
|
|
.Fn sx_assert
|
|
|
|
function tests specified conditions and panics if they are not met and the
|
|
|
|
kernel is compiled with
|
|
|
|
.Dv INVARIANTS .
|
2002-04-02 17:21:00 +00:00
|
|
|
.Pp
|
|
|
|
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 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
|
2001-10-23 22:51:59 +00:00
|
|
|
The following assertions are supported:
|
2001-11-21 11:44:36 +00:00
|
|
|
.Bl -tag -width ".Dv SX_XLOCKED"
|
2001-10-23 22:51:59 +00:00
|
|
|
.It Dv SX_LOCKED
|
|
|
|
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.
|
2001-10-23 22:51:59 +00:00
|
|
|
.It Dv SX_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.
|
|
|
|
.It Dv SX_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.
|
|
|
|
.El
|
|
|
|
.Pp
|
2001-03-05 19:59:41 +00:00
|
|
|
A thread may not own a shared lock and an exclusive lock simultaneously;
|
|
|
|
attempting to do so will result in deadlock.
|
|
|
|
.Sh SEE ALSO
|
2001-09-17 07:24:48 +00:00
|
|
|
.Xr condvar 9 ,
|
2002-03-28 12:51:06 +00:00
|
|
|
.Xr mtx_pool 9 ,
|
2001-09-17 07:24:48 +00:00
|
|
|
.Xr mutex 9 ,
|
|
|
|
.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
|
2001-10-23 22:51:59 +00:00
|
|
|
.Dv SX_LOCKED
|
|
|
|
and
|
|
|
|
.Dv SX_SLOCKED
|
|
|
|
assertions merely check that some thread holds a shared lock.
|
|
|
|
They do not ensure that the current thread holds a shared lock.
|