xref: /freebsd/share/man/man9/rwlock.9 (revision 87569f75a91f298c52a71823c04d41cf53c88889)
1.\" Copyright (c) 2006 Gleb Smirnoff <glebius@FreeBSD.org>
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.\" $FreeBSD$
26.\"
27.Dd January 30, 2006
28.Dt RWLOCK 9
29.Os
30.Sh NAME
31.Nm rwlock
32.Nm rw_init ,
33.Nm rw_rlock ,
34.Nm rw_wlock ,
35.Nm rw_assert ,
36.Nm rw_runlock ,
37.Nm rw_wunlock ,
38.Nm rw_initialized ,
39.Nm rw_destroy ,
40.Nm RW_SYSINIT ,
41.Nd kernel synchronization primitives
42.Sh SYNOPSIS
43.In sys/param.h
44.In sys/lock.h
45.In sys/rwlock.h
46.Ft void
47.Fn rw_init "struct rwlock *rwlock" "const char *name"
48.Ft void
49.Fn rw_rlock "struct rwlock *rwlock"
50.Ft void
51.Fn rw_wlock "struct rwlock *rwlock"
52.Ft void
53.Fn rw_runlock "struct rwlock *rwlock"
54.Ft void
55.Fn rw_wunlock "struct rwlock *rwlock"
56.Ft int
57.Fn rw_initialized "struct rwlock *rwlock"
58.Ft void
59.Fn rw_destroy "struct rwlock *rwlock"
60.Pp
61.Cd "options INVARIANTS"
62.Cd "options INVARIANT_SUPPORT"
63.Ft void
64.Fn rw_assert "struct rwlock *rwlock" "int what"
65.In sys/kernel.h
66.Fn RW_SYSINIT "name" "struct rwlock *rwlock" "const char *description"
67.Sh DESCRIPTION
68Read/write locks are a method of thread synchronization, which
69allows several threads have shared access to protected data, or
70one thread have exclusive access.
71The threads that share access are known as
72.Em readers
73since they should only read the protected data, while thread
74with exclusive access is known as a
75.Em writer
76since it can modify protected data.
77.Pp
78Although the description of read/write locks looks very similar
79to description of
80.Xr sx 9
81locks, their usage pattern is different.
82The read/write locks can be treated as mutexes (see
83.Xr mutex 9 )
84with shared/exclusive semantics.
85Unlike
86.Xr sx 9 ,
87an
88.Nm
89can be locked while holding a non-spin mutex;
90.Nm
91cannot be held while sleeping.
92The
93.Nm
94locks have priority propagation like mutexes, but priority
95can be propagated only to exclusive holder.
96This limitation comes from the fact that shared owners
97are anonymous.
98Another important property is that shared holder of
99.Nm
100can recurse on it.
101.Ss Macros and Functions
102.Bl -tag -width indent
103.It Fn rw_init "struct rwlock *rwlock" "const char *name"
104Initialize structure located at
105.Fa rwlock
106as read/write lock, described by name
107.Fa name .
108The description is used solely for debugging purposes.
109This function must be used before any other manipulations
110with the lock.
111.It Fn rw_rlock "struct rwlock *rwlock"
112Lock the
113.Fa rwlock
114as reader.
115If any thread holds this lock exclusively, the current thread blocks,
116and its priority is propagated to exclusive holder.
117The
118.Fn rw_rlock
119function can be called when the thread has already acquired reader
120access on
121.Fa rwlock .
122This is called
123.Dq "recursing on a lock" .
124.It Fn rw_wlock "struct rwlock *rwlock"
125Lock the
126.Fa rwlock
127as writer.
128If there are any shared owners of the lock, the current thread blocks.
129The
130.Fn rw_wlock
131function cannot be called recursively.
132.It Fn rw_runlock "struct rwlock *rwlock"
133This function releases shared lock, previously acquired by
134.Fn rw_rlock .
135.It Fn rw_wunlock "struct rwlock *rwlock"
136This function releases exclusive lock, previously acquired by
137.Fn rw_wlock .
138.It Fn rw_initialized "struct rwlock *rwlock"
139This function returns non-zero if the
140.Fa rwlock
141has been initialized, and zero otherwise.
142.It Fn rw_destroy "struct rwlock *rwlock"
143This functions destroys a lock previously initialized with
144.Fn rw_init .
145The
146.Fa rwlock
147must be unlocked.
148.It Fn rw_assert "struct rwlock *rwlock" "int what"
149This function allows assertions specified in
150.Fa what
151to be made about
152.Fa rwlock .
153If the assertions are not true and the kernel is compiled
154with
155.Cd "options INVARIANTS"
156and
157.Cd "options INVARIANT_SUPPORT" ,
158the kernel will panic.
159Currently the following assertions are supported:
160.Bl -tag -width ".Dv RA_UNLOCKED"
161.It Dv RA_LOCKED
162Assert that current thread is either shared or exclusive owner
163of the
164.Nm
165pointed to by the first argument.
166.It Dv RA_RLOCKED
167Assert that current thread is shared owner of the
168.Nm
169pointed
170to by the first argument.
171.It Dv RA_WLOCKED
172Assert that current thread is exclusive owner of the
173.Nm
174pointed
175to by the first argument.
176.It Dv RA_UNLOCKED
177Assert that current thread is neither shared nor exclusive owner
178of the
179.Nm
180pointed to by the first argument.
181.El
182.El
183.Sh SEE ALSO
184.Xr condvar 9 ,
185.Xr mutex 9 ,
186.Xr panic 9 ,
187.Xr sema 9 ,
188.Xr sx 9
189.Sh HISTORY
190These
191functions appeared in
192.Fx 7.0 .
193.Sh AUTHORS
194.An -nosplit
195The
196.Nm
197facility was written by
198.An "John Baldwin" .
199This manual page was written by
200.An "Gleb Smirnoff" .
201