1.\" Copyright (c) 2007 Stephan Uphoff <ups@FreeBSD.org> 2.\" Copyright (c) 2006 Gleb Smirnoff <glebius@FreeBSD.org> 3.\" All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 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. 13.\" 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 24.\" SUCH DAMAGE. 25.\" 26.\" $FreeBSD$ 27.\" 28.\" Based on rwlock.9 man page 29.Dd November 16, 2011 30.Dt RMLOCK 9 31.Os 32.Sh NAME 33.Nm rmlock , 34.Nm rm_init , 35.Nm rm_init_flags , 36.Nm rm_destroy , 37.Nm rm_rlock , 38.Nm rm_try_rlock , 39.Nm rm_wlock , 40.Nm rm_runlock , 41.Nm rm_wunlock , 42.Nm rm_wowned , 43.Nm RM_SYSINIT 44.Nd kernel reader/writer lock optimized for mostly read access patterns 45.Sh SYNOPSIS 46.In sys/param.h 47.In sys/lock.h 48.In sys/rmlock.h 49.Ft void 50.Fn rm_init "struct rmlock *rm" "const char *name" 51.Ft void 52.Fn rm_init_flags "struct rmlock *rm" "const char *name" "int opts" 53.Ft void 54.Fn rm_destroy "struct rmlock *rm" 55.Ft void 56.Fn rm_rlock "struct rmlock *rm" "struct rm_priotracker* tracker" 57.Ft int 58.Fn rm_try_rlock "struct rmlock *rm" "struct rm_priotracker* tracker" 59.Ft void 60.Fn rm_wlock "struct rmlock *rm" 61.Ft void 62.Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker" 63.Ft void 64.Fn rm_wunlock "struct rmlock *rm" 65.Ft int 66.Fn rm_wowned "const struct rmlock *rm" 67.In sys/kernel.h 68.Fn RM_SYSINIT "name" "struct rmlock *rm" "const char *desc" "int opts" 69.Sh DESCRIPTION 70Mostly reader locks allow shared access to protected data by multiple threads, 71or exclusive access by a single thread. 72The threads with shared access are known as 73.Em readers 74since they only read the protected data. 75A thread with exclusive access is known as a 76.Em writer 77since it can modify protected data. 78.Pp 79Read mostly locks are designed to be efficient for locks almost exclusively 80used as reader locks and as such should be used for protecting data that 81rarely changes. 82Acquiring an exclusive lock after the lock had been locked for shared access 83is an expensive operation. 84.Pp 85Although reader/writer locks look very similar to 86.Xr sx 9 87locks, their usage pattern is different. 88Reader/writer locks can be treated as mutexes (see 89.Xr mutex 9 ) 90with shared/exclusive semantics unless initialized with 91.Dv RM_SLEEPABLE . 92Unlike 93.Xr sx 9 , 94an 95.Nm 96can be locked while holding a non-spin mutex, and an 97.Nm 98cannot be held while sleeping, again unless initialized with 99.Dv RM_SLEEPABLE . 100The 101.Nm 102locks have full priority propagation like mutexes. 103The 104.Va rm_priotracker 105structure argument supplied in 106.Fn rm_rlock 107and 108.Fn rm_runlock 109is used to keep track of the read owner(s). 110Another important property is that shared holders of 111.Nm 112can recurse if the lock has been initialized with the 113.Dv LO_RECURSABLE 114option, however exclusive locks are not allowed to recurse. 115.Ss Macros and Functions 116.Bl -tag -width indent 117.It Fn rm_init "struct rmlock *rm" "const char *name" 118Initialize structure located at 119.Fa rm 120as mostly reader lock, described by 121.Fa name . 122The name description is used solely for debugging purposes. 123This function must be called before any other operations 124on the lock. 125.It Fn rm_init_flags "struct rmlock *rm" "const char *name" "int opts" 126Initialize the rm lock just like the 127.Fn rm_init 128function, but specifying a set of optional flags to alter the 129behaviour of 130.Fa rm , 131through the 132.Fa opts 133argument. 134It contains one or more of the following flags: 135.Bl -tag -width ".Dv RM_NOWITNESS" 136.It Dv RM_NOWITNESS 137Instruct 138.Xr witness 4 139to ignore this lock. 140.It Dv RM_RECURSE 141Allow threads to recursively acquire exclusive locks for 142.Fa rm . 143.It Dv RM_SLEEPABLE 144Allow writers to sleep while holding the lock. 145Readers must not sleep while holding the lock and can avoid to sleep on 146taking the lock by using 147.Fn rm_try_rlock 148instead of 149.Fn rm_rlock . 150.El 151.It Fn rm_rlock "struct rmlock *rm" "struct rm_priotracker* tracker" 152Lock 153.Fa rm 154as a reader. 155Using 156.Fa tracker 157to track read owners of a lock for priority propagation. 158This data structure is only used internally by 159.Nm 160and must persist until 161.Fn rm_runlock 162has been called. 163This data structure can be allocated on the stack since 164rmlocks cannot be held while sleeping. 165If any thread holds this lock exclusively, the current thread blocks, 166and its priority is propagated to the exclusive holder. 167If the lock was initialized with the 168.Dv LO_RECURSABLE 169option the 170.Fn rm_rlock 171function can be called when the thread has already acquired reader 172access on 173.Fa rm . 174This is called 175.Dq "recursing on a lock" . 176.It Fn rm_try_rlock "struct rmlock *rm" "struct rm_priotracker* tracker" 177Try to lock 178.Fa rm 179as a reader. 180.Fn rm_try_rlock 181will return 0 if the lock cannot be acquired immediately; 182otherwise the lock will be acquired and a non-zero value will be returned. 183Note that 184.Fn rm_try_rlock 185may fail even while the lock is not currently held by a writer. 186.It Fn rm_wlock "struct rmlock *rm" 187Lock 188.Fa rm 189as a writer. 190If there are any shared owners of the lock, the current thread blocks. 191The 192.Fn rm_wlock 193function cannot be called recursively. 194.It Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker" 195This function releases a shared lock previously acquired by 196.Fn rm_rlock . 197The 198.Fa tracker 199argument must match the 200.Fa tracker 201argument used for acquiring the shared lock 202.It Fn rm_wunlock "struct rmlock *rm" 203This function releases an exclusive lock previously acquired by 204.Fn rm_wlock . 205.It Fn rm_destroy "struct rmlock *rm" 206This functions destroys a lock previously initialized with 207.Fn rm_init . 208The 209.Fa rm 210lock must be unlocked. 211.It Fn rm_wowned "const struct rmlock *rm" 212This function returns a non-zero value if the current thread owns an 213exclusive lock on 214.Fa rm . 215.El 216.Sh SEE ALSO 217.Xr locking 9 , 218.Xr mutex 9 , 219.Xr panic 9 , 220.Xr rwlock 9 , 221.Xr sema 9 , 222.Xr sx 9 223.Sh HISTORY 224These 225functions appeared in 226.Fx 7.0 . 227.Sh AUTHORS 228.An -nosplit 229The 230.Nm 231facility was written by 232.An "Stephan Uphoff" . 233This manual page was written by 234.An "Gleb Smirnoff" 235for rwlock and modified to reflect rmlock by 236.An "Stephan Uphoff" . 237.Sh BUGS 238The 239.Nm 240implementation is currently not optimized for single processor systems. 241.Pp 242.Fn rm_try_rlock 243can fail transiently even when there is no writer, while another reader 244updates the state on the local CPU. 245.Pp 246The 247.Nm 248implementation uses a single per CPU list shared by all 249rmlocks in the system. 250If rmlocks become popular, hashing to multiple per CPU queues may 251be needed to speed up the writer lock process. 252.Pp 253The 254.Nm 255can currently not be used as a lock argument for condition variable 256wait functions. 257