1 .\" Copyright (c) 2007 Stephan Uphoff <ups@FreeBSD.org>
2 .\" Copyright (c) 2006 Gleb Smirnoff <glebius@FreeBSD.org>
3 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
14 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" Based on rwlock.9 man page
42 .Nd kernel reader/writer lock optimized for mostly read access patterns
48 .Fn rm_init "struct rmlock *rm" "const char *name" "int opts"
50 .Fn rm_destroy "struct rmlock *rm"
52 .Fn rm_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
54 .Fn rm_wlock "struct rmlock *rm"
56 .Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker"
58 .Fn rm_wunlock "struct rmlock *rm"
60 .Fn rm_wowned "struct rmlock *rm"
62 .Fn RM_SYSINIT "name" "struct rmlock *rm" "const char *desc" "int opts"
64 Mostly reader locks allow shared access to protected data by multiple threads,
65 or exclusive access by a single thread.
66 The threads with shared access are known as
68 since they only read the protected data.
69 A thread with exclusive access is known as a
71 since it can modify protected data.
73 Read mostly locks are designed to be efficient for locks almost exclusively
74 used as reader locks and as such should be used for protecting data that
76 Acquiring an exclusive lock after the lock had been locked for shared access
77 is an expensive operation.
79 Although reader/writer locks look very similar to
81 locks, their usage pattern is different.
82 Reader/writer locks can be treated as mutexes (see
84 with shared/exclusive semantics.
89 can be locked while holding a non-spin mutex, and an
91 cannot be held while sleeping.
94 locks have full priority propagation like mutexes.
97 structure argument supplied in
101 is used to keep track of the read owner(s).
102 Another important property is that shared holders of
104 can recurse if the lock has been initialized with the
106 option, however exclusive locks are not allowed to recurse.
107 .Ss Macros and Functions
108 .Bl -tag -width indent
109 .It Fn rm_init "struct rmlock *rm" "const char *name" "int opts"
110 Initialize structure located at
112 as mostly reader lock, described by
114 Optionally allowing readers to recurse by setting
118 The name description is used solely for debugging purposes.
119 This function must be called before any other operations
121 .It Fn rm_rlock "struct rmlock *rm" "struct rm_priotracker* tracker"
127 to track read owners of a lock for priority propagation.
128 This data structure is only used internally by
130 and must persist until
133 This data structure can be allocated on the stack since
134 rmlocks cannot be held while sleeping.
135 If any thread holds this lock exclusively, the current thread blocks,
136 and its priority is propagated to the exclusive holder.
137 If the lock was initialized with the
141 function can be called when the thread has already acquired reader
145 .Dq "recursing on a lock" .
146 .It Fn rm_wlock "struct rmlock *rm"
150 If there are any shared owners of the lock, the current thread blocks.
153 function cannot be called recursively.
154 .It Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker"
155 This function releases a shared lock previously acquired by
159 argument must match the
161 argument used for acquiring the shared lock
162 .It Fn rm_wunlock "struct rmlock *rm"
163 This function releases an exclusive lock previously acquired by
165 .It Fn rm_destroy "struct rmlock *rm"
166 This functions destroys a lock previously initialized with
170 lock must be unlocked.
171 .It Fn rm_wowned "struct rmlock *rm"
172 This function returns a non-zero value if the current thread owns an
185 functions appeared in
191 facility was written by
192 .An "Stephan Uphoff" .
193 This manual page was written by
195 for rwlock and modified to reflect rmlock by
196 .An "Stephan Uphoff" .
200 implementation is currently not optimized for single processor systems.
204 implementation uses a single per CPU list shared by all
205 rmlocks in the system.
206 If rmlocks become popular, hashing to multiple per CPU queues may
207 be needed to speed up the writer lock process.
211 can currently not be used as a lock argument for condition variable