xref: /netbsd-src/share/man/man9/rwlock.9 (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1.\"	$NetBSD: rwlock.9,v 1.9 2007/12/05 05:22:22 ad Exp $
2.\"
3.\" Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Andrew Doran.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
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.
17.\" 3. All advertising materials mentioning features or use of this software
18.\"    must display the following acknowledgement:
19.\"        This product includes software developed by the NetBSD
20.\"        Foundation, Inc. and its contributors.
21.\" 4. Neither the name of The NetBSD Foundation nor the names of its
22.\"    contributors may be used to endorse or promote products derived
23.\"    from this software without specific prior written permission.
24.\"
25.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35.\" POSSIBILITY OF SUCH DAMAGE.
36.\"
37.Dd December 4, 2007
38.Dt RWLOCK 9
39.Os
40.Sh NAME
41.Nm rw ,
42.Nm rw_init ,
43.Nm rw_destroy ,
44.Nm rw_enter ,
45.Nm rw_exit ,
46.Nm rw_tryenter ,
47.Nm rw_tryupgrade ,
48.Nm rw_downgrade ,
49.Nm rw_read_held ,
50.Nm rw_write_held ,
51.Nm rw_lock_held
52.Nd reader / writer lock primitives
53.Sh SYNOPSIS
54.In sys/rwlock.h
55.Ft void
56.Fn rw_init "krwlock_t *rw"
57.Ft void
58.Fn rw_destroy "krwlock_t *rw"
59.Ft void
60.Fn rw_enter "krwlock_t *rw" "const krw_t op"
61.Ft void
62.Fn rw_exit "krwlock_t *rw"
63.Ft int
64.Fn rw_tryenter "krwlock_t *rw" "const krw_t op"
65.Ft int
66.Fn rw_tryupgrade "krwlock_t *rw"
67.Ft void
68.Fn rw_downgrade "krwlock_t *rw"
69.Ft int
70.Fn rw_read_held "krwlock_t *rw"
71.Ft int
72.Fn rw_write_held "krwlock_t *rw"
73.Ft int
74.Fn rw_lock_held "krwlock_t *rw"
75.Pp
76.Cd "options DIAGNOSTIC"
77.Cd "options LOCKDEBUG"
78.Sh DESCRIPTION
79Reader / writer locks (RW locks) are used in the kernel to synchronize access
80to an object among LWPs (lightweight processes) and soft interrupt handlers.
81.Pp
82In addition to the capabilities provided by mutexes, RW locks distinguish
83between read (shared) and write (exclusive) access.
84RW locks are intended to provide protection for kernel data
85or objects that are read much more frequently than updated.
86For objects that are updated as frequently as they are read, mutexes should
87be used to guarantee atomic access.
88.Pp
89RW locks are in one of three distinct states at any given time:
90.Bl -tag -width cdosrunrundo
91.It Dv Unlocked
92The lock is not held.
93.It Dv Read locked
94The lock holders intend to read the protected object.
95Multiple callers may hold a RW lock with
96.Dq read intent
97simultaneously.
98.It Dv Write locked
99The lock holder intends to update the protected object.
100Only one caller may hold a RW lock with
101.Dq write intent .
102.El
103.Pp
104The
105.Vt krwlock_t
106type provides storage for the RW lock object.
107This should be treated as an opaque object and not examined directly by
108consumers.
109.Pp
110Note that the these interfaces must not be used from a hardware
111interrupt handler.
112.Sh OPTIONS AND MACROS
113.Bl -tag -width abcd
114.It Cd "options DIANOSTIC"
115.Pp
116Kernels compiled with the
117.Dv DIAGNOSTIC
118option perform basic sanity checks on RW lock operations.
119.It Cd "options LOCKDEBUG"
120.Pp
121Kernels compiled with the
122.Dv LOCKDEBUG
123option perform potentially CPU intensive sanity checks
124on RW lock operations.
125.El
126.Sh FUNCTIONS
127.Bl -tag -width abcd
128.It Fn rw_init "rw"
129.Pp
130Initialize a lock for use.
131No other operations can be performed on the lock until it has been
132initialized.
133.It Fn rw_destroy "rw"
134.Pp
135Release resources used by a lock.
136The lock may not be used after it has been destroyed.
137.It Fn rw_enter "rw" "op"
138.Pp
139If
140.Dv RW_READER
141is specified as the argument to
142.Fa op ,
143acquire a read lock.
144If the lock is write held, the caller will block and not return until the
145hold is acquired.
146Callers must not recursively acquire read locks.
147.Pp
148If
149.Dv RW_WRITER
150is specified, acquire a write lock.
151If the lock is already held, the caller will block and not return until the
152hold is acquired.
153.Pp
154RW locks and other types of locks must always be acquired in a
155consistent order with respect to each other.
156Otherwise, the potential for system deadlock exists.
157.It Fn rw_exit "rw"
158.Pp
159Release a lock.
160The lock must have been previously acquired by the caller.
161.It Fn rw_tryenter "rw" "op"
162.Pp
163Try to acquire a lock, but do not block if the lock is already held.
164If the lock is acquired successfully, return non-zero.
165Otherwise, return zero.
166.Pp
167Valid arguments to
168.Fa op
169are
170.Dv RW_READER
171or
172.Dv RW_WRITER .
173.It Fn rw_tryupgrade "rw"
174.Pp
175Try to upgrade a lock from one read hold to a write hold.
176If the lock is upgraded successfully, returns non-zero.
177Otherwise, returns zero.
178.It Fn rw_downgrade "rw"
179.Pp
180Downgrade a lock from a write hold to a read hold.
181.It Fn rw_write_held "rw"
182.It Fn rw_read_held "rw"
183.It Fn rw_lock_held "rw"
184.Pp
185Test the lock's condition and return non-zero if the lock is held
186(potentially by the current LWP) and matches the specified condition.
187Otherwise, return zero.
188.Pp
189These functions must never be used to make locking decisions at run time:
190they are provided only for diagnostic purposes.
191.El
192.Sh CODE REFERENCES
193This section describes places within the
194.Nx
195source tree where code implementing RW locks can be found.
196All pathnames are relative to
197.Pa /usr/src .
198.Pp
199The core of the RW lock implementation is in
200.Pa sys/kern/kern_rwlock.c .
201.Pp
202The header file
203.Pa sys/sys/rwlock.h
204describes the public interface, and interfaces that machine-dependent
205code must provide to support RW locks.
206.Sh SEE ALSO
207.Xr condvar 9 ,
208.Xr mb 9 ,
209.Xr mutex 9
210.Rs
211.%A Jim Mauro
212.%A Richard McDougall
213.%T Solaris Internals: Core Kernel Architecture
214.%I Prentice Hall
215.%D 2001
216.%O ISBN 0-13-022496-0
217.Re
218.Sh HISTORY
219The RW lock primitives first appeared in
220.Nx 5.0 .
221