1 .\" $NetBSD: rwlock.9,v 1.13 2009/03/23 22:22:40 wiz Exp $
3 .\" Copyright (c) 2006, 2007, 2009 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
45 .Nd reader / writer lock primitives
49 .Fn rw_init "krwlock_t *rw"
51 .Fn rw_destroy "krwlock_t *rw"
53 .Fn rw_enter "krwlock_t *rw" "const krw_t op"
55 .Fn rw_exit "krwlock_t *rw"
57 .Fn rw_tryenter "krwlock_t *rw" "const krw_t op"
59 .Fn rw_tryupgrade "krwlock_t *rw"
61 .Fn rw_downgrade "krwlock_t *rw"
63 .Fn rw_read_held "krwlock_t *rw"
65 .Fn rw_write_held "krwlock_t *rw"
67 .Fn rw_lock_held "krwlock_t *rw"
69 .Cd "options DIAGNOSTIC"
70 .Cd "options LOCKDEBUG"
72 Reader / writer locks (RW locks) are used in the kernel to synchronize access
73 to an object among LWPs (lightweight processes) and soft interrupt handlers.
75 In addition to the capabilities provided by mutexes, RW locks distinguish
76 between read (shared) and write (exclusive) access.
78 RW locks are in one of three distinct states at any given time:
79 .Bl -tag -width cdosrunrundo
83 The lock holders intend to read the protected object.
84 Multiple callers may hold a RW lock with
88 The lock holder intends to update the protected object.
89 Only one caller may hold a RW lock with
95 type provides storage for the RW lock object.
96 This should be treated as an opaque object and not examined directly by
99 Note that these interfaces must not be used from a hardware
101 .Sh OPTIONS AND MACROS
103 .It Cd "options DIANOSTIC"
105 Kernels compiled with the
107 option perform basic sanity checks on RW lock operations.
108 .It Cd "options LOCKDEBUG"
110 Kernels compiled with the
112 option perform potentially CPU intensive sanity checks
113 on RW lock operations.
119 Initialize a lock for use.
120 No other operations can be performed on the lock until it has been
122 .It Fn rw_destroy "rw"
124 Release resources used by a lock.
125 The lock may not be used after it has been destroyed.
126 .It Fn rw_enter "rw" "op"
130 is specified as the argument to
133 If the lock is write held, the caller will block and not return until the
135 Callers must not recursively acquire read locks.
139 is specified, acquire a write lock.
140 If the lock is already held, the caller will block and not return until the
143 RW locks and other types of locks must always be acquired in a
144 consistent order with respect to each other.
145 Otherwise, the potential for system deadlock exists.
149 The lock must have been previously acquired by the caller.
150 .It Fn rw_tryenter "rw" "op"
152 Try to acquire a lock, but do not block if the lock is already held.
153 If the lock is acquired successfully, return non-zero.
154 Otherwise, return zero.
162 .It Fn rw_tryupgrade "rw"
164 Try to upgrade a lock from one read hold to a write hold.
165 If the lock is upgraded successfully, returns non-zero.
166 Otherwise, returns zero.
167 .It Fn rw_downgrade "rw"
169 Downgrade a lock from a write hold to a read hold.
170 .It Fn rw_write_held "rw"
171 .It Fn rw_read_held "rw"
172 .It Fn rw_lock_held "rw"
174 Test the lock's condition and return non-zero if the lock is held
175 (potentially by the current LWP) and matches the specified condition.
176 Otherwise, return zero.
178 These functions must never be used to make locking decisions at run time:
179 they are provided only for diagnostic purposes.
181 .Sh PERFORMANCE CONSIDERATIONS
182 RW locks are subject to high cache contention on multiprocessor systems,
183 and scale poorly when the write:read ratio is not strongly in favour of
185 Ideally, RW locks should only be used in settings when the following three
189 The data object(s) protected by the RW lock are read much more frequently
192 The read-side hold time for the RW lock is long (in the order of thousands
193 of processor clock cycles).
195 Strong synchronization semantics are required: there is no scope for
196 lockless, lazy or optimistic synchronization.
199 Generally speaking, it is better to organise code paths and/or
200 data flows such that fewer and weaker synchronization points are required
201 to ensure correct operation.
203 This section describes places within the
205 source tree where code implementing RW locks can be found.
206 All pathnames are relative to
209 The core of the RW lock implementation is in
210 .Pa sys/kern/kern_rwlock.c .
214 describes the public interface, and interfaces that machine-dependent
215 code must provide to support RW locks.
223 .%A Richard McDougall
224 .%T Solaris Internals: Core Kernel Architecture
227 .%O ISBN 0-13-022496-0
230 The RW lock primitives first appeared in