2008-04-30 17:10:46 +04:00
|
|
|
.\" $NetBSD: rwlock.9,v 1.10 2008/04/30 13:10:58 martin Exp $
|
2006-11-13 19:22:11 +03:00
|
|
|
.\"
|
2007-02-02 06:40:07 +03:00
|
|
|
.\" Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
|
2006-11-13 19:22:11 +03:00
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
.\" by Andrew Doran.
|
|
|
|
.\"
|
|
|
|
.\" 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.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
.\" ``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 FOUNDATION OR CONTRIBUTORS
|
|
|
|
.\" 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.
|
|
|
|
.\"
|
2007-12-05 08:22:22 +03:00
|
|
|
.Dd December 4, 2007
|
2006-11-13 19:22:11 +03:00
|
|
|
.Dt RWLOCK 9
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm rw ,
|
|
|
|
.Nm rw_init ,
|
|
|
|
.Nm rw_destroy ,
|
|
|
|
.Nm rw_enter ,
|
|
|
|
.Nm rw_exit ,
|
|
|
|
.Nm rw_tryenter ,
|
|
|
|
.Nm rw_tryupgrade ,
|
|
|
|
.Nm rw_downgrade ,
|
|
|
|
.Nm rw_read_held ,
|
|
|
|
.Nm rw_write_held ,
|
|
|
|
.Nm rw_lock_held
|
|
|
|
.Nd reader / writer lock primitives
|
|
|
|
.Sh SYNOPSIS
|
|
|
|
.In sys/rwlock.h
|
|
|
|
.Ft void
|
|
|
|
.Fn rw_init "krwlock_t *rw"
|
|
|
|
.Ft void
|
|
|
|
.Fn rw_destroy "krwlock_t *rw"
|
|
|
|
.Ft void
|
2007-08-08 13:34:40 +04:00
|
|
|
.Fn rw_enter "krwlock_t *rw" "const krw_t op"
|
2006-11-13 19:22:11 +03:00
|
|
|
.Ft void
|
|
|
|
.Fn rw_exit "krwlock_t *rw"
|
|
|
|
.Ft int
|
2007-08-08 13:34:40 +04:00
|
|
|
.Fn rw_tryenter "krwlock_t *rw" "const krw_t op"
|
2006-11-13 19:22:11 +03:00
|
|
|
.Ft int
|
|
|
|
.Fn rw_tryupgrade "krwlock_t *rw"
|
|
|
|
.Ft void
|
|
|
|
.Fn rw_downgrade "krwlock_t *rw"
|
|
|
|
.Ft int
|
|
|
|
.Fn rw_read_held "krwlock_t *rw"
|
|
|
|
.Ft int
|
|
|
|
.Fn rw_write_held "krwlock_t *rw"
|
|
|
|
.Ft int
|
|
|
|
.Fn rw_lock_held "krwlock_t *rw"
|
|
|
|
.Pp
|
|
|
|
.Cd "options DIAGNOSTIC"
|
|
|
|
.Cd "options LOCKDEBUG"
|
|
|
|
.Sh DESCRIPTION
|
|
|
|
Reader / writer locks (RW locks) are used in the kernel to synchronize access
|
2007-12-05 08:22:22 +03:00
|
|
|
to an object among LWPs (lightweight processes) and soft interrupt handlers.
|
2006-11-13 19:22:11 +03:00
|
|
|
.Pp
|
|
|
|
In addition to the capabilities provided by mutexes, RW locks distinguish
|
|
|
|
between read (shared) and write (exclusive) access.
|
|
|
|
RW locks are intended to provide protection for kernel data
|
|
|
|
or objects that are read much more frequently than updated.
|
|
|
|
For objects that are updated as frequently as they are read, mutexes should
|
|
|
|
be used to guarantee atomic access.
|
|
|
|
.Pp
|
|
|
|
RW locks are in one of three distinct states at any given time:
|
|
|
|
.Bl -tag -width cdosrunrundo
|
|
|
|
.It Dv Unlocked
|
|
|
|
The lock is not held.
|
|
|
|
.It Dv Read locked
|
|
|
|
The lock holders intend to read the protected object.
|
2006-12-23 09:51:41 +03:00
|
|
|
Multiple callers may hold a RW lock with
|
|
|
|
.Dq read intent
|
|
|
|
simultaneously.
|
2006-11-13 19:22:11 +03:00
|
|
|
.It Dv Write locked
|
|
|
|
The lock holder intends to update the protected object.
|
2006-12-23 09:51:41 +03:00
|
|
|
Only one caller may hold a RW lock with
|
|
|
|
.Dq write intent .
|
2006-11-13 19:22:11 +03:00
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Vt krwlock_t
|
|
|
|
type provides storage for the RW lock object.
|
|
|
|
This should be treated as an opaque object and not examined directly by
|
|
|
|
consumers.
|
2007-12-05 08:22:22 +03:00
|
|
|
.Pp
|
|
|
|
Note that the these interfaces must not be used from a hardware
|
|
|
|
interrupt handler.
|
2006-11-13 19:22:11 +03:00
|
|
|
.Sh OPTIONS AND MACROS
|
|
|
|
.Bl -tag -width abcd
|
|
|
|
.It Cd "options DIANOSTIC"
|
|
|
|
.Pp
|
|
|
|
Kernels compiled with the
|
|
|
|
.Dv DIAGNOSTIC
|
|
|
|
option perform basic sanity checks on RW lock operations.
|
|
|
|
.It Cd "options LOCKDEBUG"
|
|
|
|
.Pp
|
|
|
|
Kernels compiled with the
|
|
|
|
.Dv LOCKDEBUG
|
|
|
|
option perform potentially CPU intensive sanity checks
|
|
|
|
on RW lock operations.
|
|
|
|
.El
|
|
|
|
.Sh FUNCTIONS
|
|
|
|
.Bl -tag -width abcd
|
|
|
|
.It Fn rw_init "rw"
|
|
|
|
.Pp
|
|
|
|
Initialize a lock for use.
|
2006-12-23 09:51:41 +03:00
|
|
|
No other operations can be performed on the lock until it has been
|
|
|
|
initialized.
|
2006-11-13 19:22:11 +03:00
|
|
|
.It Fn rw_destroy "rw"
|
|
|
|
.Pp
|
|
|
|
Release resources used by a lock.
|
|
|
|
The lock may not be used after it has been destroyed.
|
|
|
|
.It Fn rw_enter "rw" "op"
|
|
|
|
.Pp
|
2006-12-23 09:36:19 +03:00
|
|
|
If
|
2006-11-13 19:22:11 +03:00
|
|
|
.Dv RW_READER
|
2006-12-23 09:36:19 +03:00
|
|
|
is specified as the argument to
|
2006-11-13 19:22:11 +03:00
|
|
|
.Fa op ,
|
|
|
|
acquire a read lock.
|
|
|
|
If the lock is write held, the caller will block and not return until the
|
|
|
|
hold is acquired.
|
|
|
|
Callers must not recursively acquire read locks.
|
|
|
|
.Pp
|
2006-12-23 09:36:19 +03:00
|
|
|
If
|
2006-11-13 19:22:11 +03:00
|
|
|
.Dv RW_WRITER
|
|
|
|
is specified, acquire a write lock.
|
|
|
|
If the lock is already held, the caller will block and not return until the
|
|
|
|
hold is acquired.
|
|
|
|
.Pp
|
|
|
|
RW locks and other types of locks must always be acquired in a
|
|
|
|
consistent order with respect to each other.
|
2006-12-23 09:36:19 +03:00
|
|
|
Otherwise, the potential for system deadlock exists.
|
2006-11-13 19:22:11 +03:00
|
|
|
.It Fn rw_exit "rw"
|
|
|
|
.Pp
|
|
|
|
Release a lock.
|
|
|
|
The lock must have been previously acquired by the caller.
|
|
|
|
.It Fn rw_tryenter "rw" "op"
|
|
|
|
.Pp
|
|
|
|
Try to acquire a lock, but do not block if the lock is already held.
|
|
|
|
If the lock is acquired successfully, return non-zero.
|
|
|
|
Otherwise, return zero.
|
|
|
|
.Pp
|
2006-12-23 09:36:19 +03:00
|
|
|
Valid arguments to
|
2006-11-13 19:22:11 +03:00
|
|
|
.Fa op
|
2006-12-23 09:36:19 +03:00
|
|
|
are
|
2007-08-27 19:43:13 +04:00
|
|
|
.Dv RW_READER
|
2006-11-13 19:22:11 +03:00
|
|
|
or
|
2007-08-27 19:43:13 +04:00
|
|
|
.Dv RW_WRITER .
|
2006-11-13 19:22:11 +03:00
|
|
|
.It Fn rw_tryupgrade "rw"
|
|
|
|
.Pp
|
|
|
|
Try to upgrade a lock from one read hold to a write hold.
|
|
|
|
If the lock is upgraded successfully, returns non-zero.
|
|
|
|
Otherwise, returns zero.
|
|
|
|
.It Fn rw_downgrade "rw"
|
|
|
|
.Pp
|
|
|
|
Downgrade a lock from a write hold to a read hold.
|
|
|
|
.It Fn rw_write_held "rw"
|
|
|
|
.It Fn rw_read_held "rw"
|
|
|
|
.It Fn rw_lock_held "rw"
|
|
|
|
.Pp
|
2006-12-23 09:36:19 +03:00
|
|
|
Test the lock's condition and return non-zero if the lock is held
|
2006-11-13 19:22:11 +03:00
|
|
|
(potentially by the current LWP) and matches the specified condition.
|
|
|
|
Otherwise, return zero.
|
|
|
|
.Pp
|
|
|
|
These functions must never be used to make locking decisions at run time:
|
|
|
|
they are provided only for diagnostic purposes.
|
2006-11-13 21:28:15 +03:00
|
|
|
.El
|
2006-11-13 19:22:11 +03:00
|
|
|
.Sh CODE REFERENCES
|
|
|
|
This section describes places within the
|
|
|
|
.Nx
|
2006-11-13 21:28:15 +03:00
|
|
|
source tree where code implementing RW locks can be found.
|
2006-11-13 19:22:11 +03:00
|
|
|
All pathnames are relative to
|
|
|
|
.Pa /usr/src .
|
|
|
|
.Pp
|
2006-12-23 09:36:19 +03:00
|
|
|
The core of the RW lock implementation is in
|
2006-11-13 19:22:11 +03:00
|
|
|
.Pa sys/kern/kern_rwlock.c .
|
|
|
|
.Pp
|
|
|
|
The header file
|
|
|
|
.Pa sys/sys/rwlock.h
|
|
|
|
describes the public interface, and interfaces that machine-dependent
|
|
|
|
code must provide to support RW locks.
|
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr condvar 9 ,
|
2007-02-02 06:40:07 +03:00
|
|
|
.Xr mb 9 ,
|
2006-12-23 09:51:41 +03:00
|
|
|
.Xr mutex 9
|
|
|
|
.Rs
|
|
|
|
.%A Jim Mauro
|
|
|
|
.%A Richard McDougall
|
|
|
|
.%T Solaris Internals: Core Kernel Architecture
|
|
|
|
.%I Prentice Hall
|
|
|
|
.%D 2001
|
|
|
|
.%O ISBN 0-13-022496-0
|
|
|
|
.Re
|
2006-11-13 19:22:11 +03:00
|
|
|
.Sh HISTORY
|
2007-02-02 06:40:07 +03:00
|
|
|
The RW lock primitives first appeared in
|
2006-11-13 19:22:11 +03:00
|
|
|
.Nx 5.0 .
|