1f53d15feSStephan Uphoff.\" Copyright (c) 2007 Stephan Uphoff <ups@FreeBSD.org> 2f53d15feSStephan Uphoff.\" Copyright (c) 2006 Gleb Smirnoff <glebius@FreeBSD.org> 3f53d15feSStephan Uphoff.\" All rights reserved. 4f53d15feSStephan Uphoff.\" 5f53d15feSStephan Uphoff.\" Redistribution and use in source and binary forms, with or without 6f53d15feSStephan Uphoff.\" modification, are permitted provided that the following conditions 7f53d15feSStephan Uphoff.\" are met: 8f53d15feSStephan Uphoff.\" 1. Redistributions of source code must retain the above copyright 9f53d15feSStephan Uphoff.\" notice, this list of conditions and the following disclaimer. 10f53d15feSStephan Uphoff.\" 2. Redistributions in binary form must reproduce the above copyright 11f53d15feSStephan Uphoff.\" notice, this list of conditions and the following disclaimer in the 12f53d15feSStephan Uphoff.\" documentation and/or other materials provided with the distribution. 13f53d15feSStephan Uphoff.\" 14f53d15feSStephan Uphoff.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15f53d15feSStephan Uphoff.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16f53d15feSStephan Uphoff.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17f53d15feSStephan Uphoff.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18f53d15feSStephan Uphoff.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19f53d15feSStephan Uphoff.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20f53d15feSStephan Uphoff.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21f53d15feSStephan Uphoff.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22f53d15feSStephan Uphoff.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23f53d15feSStephan Uphoff.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24f53d15feSStephan Uphoff.\" SUCH DAMAGE. 25f53d15feSStephan Uphoff.\" 26f53d15feSStephan Uphoff.\" $FreeBSD$ 27f53d15feSStephan Uphoff.\" 28f53d15feSStephan Uphoff.\" Based on rwlock.9 man page 29f53d15feSStephan Uphoff 30f53d15feSStephan Uphoff.Dd August 22, 2007 31f53d15feSStephan Uphoff.Dt RMLOCK 9 32f53d15feSStephan Uphoff.Os 33f53d15feSStephan Uphoff.Sh NAME 34f53d15feSStephan Uphoff.Nm rmlock , 35f53d15feSStephan Uphoff.Nm rm_init , 36f53d15feSStephan Uphoff.Nm rm_destroy , 37f53d15feSStephan Uphoff.Nm rm_rlock , 38f53d15feSStephan Uphoff.Nm rm_wlock , 39f53d15feSStephan Uphoff.Nm rm_runlock , 40f53d15feSStephan Uphoff.Nm rm_wunlock , 41f53d15feSStephan Uphoff.Nm RM_SYSINIT 42f53d15feSStephan Uphoff.Nd mostly read lock - a kernel reader/writer lock optimized for mostly read access patterns 43f53d15feSStephan Uphoff.Sh SYNOPSIS 44f53d15feSStephan Uphoff.In sys/param.h 45f53d15feSStephan Uphoff.In sys/lock.h 46f53d15feSStephan Uphoff.In sys/rmlock.h 47f53d15feSStephan Uphoff.Ft void 48f53d15feSStephan Uphoff.Fn rm_init "struct rmlock *rm" "const char *name" "int opts" 49f53d15feSStephan Uphoff.Ft void 50f53d15feSStephan Uphoff.Fn rm_destroy "struct rmlock *rm" 51f53d15feSStephan Uphoff.Ft void 52f53d15feSStephan Uphoff.Fn rm_rlock "struct rmlock *rm" "struct rm_priotracker* tracker" 53f53d15feSStephan Uphoff.Ft void 54f53d15feSStephan Uphoff.Fn rm_wlock "struct rmlock *rm" 55f53d15feSStephan Uphoff.Ft void 56f53d15feSStephan Uphoff.Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker" 57f53d15feSStephan Uphoff.Ft void 58f53d15feSStephan Uphoff.Fn rm_wunlock "struct rmlock *rm" 59f53d15feSStephan Uphoff.In sys/kernel.h 60f53d15feSStephan Uphoff.Fn RM_SYSINIT "name" "struct rmlock *rm" "const char *desc" "int opts" 61f53d15feSStephan Uphoff.Sh DESCRIPTION 62f53d15feSStephan UphoffMostly reader locks allow shared access to protected data by multiple threads, 63f53d15feSStephan Uphoffor exclusive access by a single thread. 64f53d15feSStephan UphoffThe threads with shared access are known as 65f53d15feSStephan Uphoff.Em readers 66f53d15feSStephan Uphoffsince they only read the protected data. 67f53d15feSStephan UphoffA thread with exclusive access is known as a 68f53d15feSStephan Uphoff.Em writer 69f53d15feSStephan Uphoffsince it can modify protected data. 70f53d15feSStephan Uphoff.Pp 71989cbe40SHiten PandyaRead mostly locks are designed to be efficient for locks almost exclusively 72989cbe40SHiten Pandyaused as reader locks and as such should be used for protecting data that 73989cbe40SHiten Pandyararely changes. 74989cbe40SHiten PandyaAcquiring an exclusive lock after the lock had been locked for shared access 75989cbe40SHiten Pandyais an expensive operation. 76f53d15feSStephan Uphoff.Pp 77f53d15feSStephan UphoffAlthough reader/writer locks look very similar to 78f53d15feSStephan Uphoff.Xr sx 9 79f53d15feSStephan Uphofflocks, their usage pattern is different. 80f53d15feSStephan UphoffReader/writer locks can be treated as mutexes (see 81f53d15feSStephan Uphoff.Xr mutex 9 ) 82f53d15feSStephan Uphoffwith shared/exclusive semantics. 83f53d15feSStephan UphoffUnlike 84f53d15feSStephan Uphoff.Xr sx 9 , 85f53d15feSStephan Uphoffan 86f53d15feSStephan Uphoff.Nm 87f53d15feSStephan Uphoffcan be locked while holding a non-spin mutex, and an 88f53d15feSStephan Uphoff.Nm 89f53d15feSStephan Uphoffcannot be held while sleeping. 90f53d15feSStephan UphoffThe 91f53d15feSStephan Uphoff.Nm 92989cbe40SHiten Pandyalocks have full priority propagation like mutexes. The 93989cbe40SHiten Pandya.Va rm_priotracker 94989cbe40SHiten Pandyastructure argument supplied in 95989cbe40SHiten Pandya.Fn rm_rlock 96989cbe40SHiten Pandyaand 97989cbe40SHiten Pandya.Fn rm_runlock 98989cbe40SHiten Pandyais used to keep track of the read owner(s). 99f53d15feSStephan UphoffAnother important property is that shared holders of 100f53d15feSStephan Uphoff.Nm 101989cbe40SHiten Pandyacan recurse if the lock has been initialized with the 102989cbe40SHiten Pandya.Dv LO_RECURSABLE 103989cbe40SHiten Pandyaoption, however exclusive locks are not allowed to recurse. 104f53d15feSStephan Uphoff.Ss Macros and Functions 105f53d15feSStephan Uphoff.Bl -tag -width indent 106f53d15feSStephan Uphoff.It Fn rm_init "struct rmlock *rm" "const char *name" "int opts" 107f53d15feSStephan UphoffInitialize structure located at 108f53d15feSStephan Uphoff.Fa rm 109989cbe40SHiten Pandyaas mostly reader lock, described by 110f53d15feSStephan Uphoff.Fa name . 111989cbe40SHiten PandyaOptionally allowing readers to recurse by setting 112989cbe40SHiten Pandya.Dv LO_RECURSABLE 113989cbe40SHiten Pandyain 114f53d15feSStephan Uphoff.Fa opts 115f53d15feSStephan UphoffThe name description is used solely for debugging purposes. 116f53d15feSStephan UphoffThis function must be called before any other operations 117f53d15feSStephan Uphoffon the lock. 118f53d15feSStephan Uphoff.It Fn rm_rlock "struct rmlock *rm" "struct rm_priotracker* tracker" 119f53d15feSStephan UphoffLock 120f53d15feSStephan Uphoff.Fa rm 121f53d15feSStephan Uphoffas a reader. Using 122f53d15feSStephan Uphoff.Fa tracker 123f53d15feSStephan Uphoffto track read owners of a lock for priority propagation. 124989cbe40SHiten PandyaThis data structure is only used internally by 125989cbe40SHiten Pandya.Nm 126989cbe40SHiten Pandyaand must persist until rm_runlock has been called. 127989cbe40SHiten PandyaThis data structure can be allocated on the stack since 128f53d15feSStephan Uphoffrmlocks cannot be held while sleeping. 129f53d15feSStephan UphoffIf any thread holds this lock exclusively, the current thread blocks, 130f53d15feSStephan Uphoffand its priority is propagated to the exclusive holder. 131989cbe40SHiten PandyaIf the lock was initialized with the 132989cbe40SHiten Pandya.Dv LO_RECURSABLE 133989cbe40SHiten Pandyaoption the 134f53d15feSStephan Uphoff.Fn rm_rlock 135f53d15feSStephan Uphofffunction can be called when the thread has already acquired reader 136f53d15feSStephan Uphoffaccess on 137f53d15feSStephan Uphoff.Fa rm . 138f53d15feSStephan UphoffThis is called 139f53d15feSStephan Uphoff.Dq "recursing on a lock" . 140f53d15feSStephan Uphoff.It Fn rm_wlock "struct rmlock *rm" 141f53d15feSStephan UphoffLock 142f53d15feSStephan Uphoff.Fa rm 143f53d15feSStephan Uphoffas a writer. 144f53d15feSStephan UphoffIf there are any shared owners of the lock, the current thread blocks. 145f53d15feSStephan UphoffThe 146f53d15feSStephan Uphoff.Fn rm_wlock 147f53d15feSStephan Uphofffunction cannot be called recursively. 148f53d15feSStephan Uphoff.It Fn rm_runlock "struct rmlock *rm" "struct rm_priotracker* tracker" 149f53d15feSStephan UphoffThis function releases a shared lock previously acquired by 150f53d15feSStephan Uphoff.Fn rm_rlock . 151f53d15feSStephan UphoffThe 152f53d15feSStephan Uphoff.Fa tracker 153f53d15feSStephan Uphoffargument must match the 154f53d15feSStephan Uphoff.Fa tracker 155f53d15feSStephan Uphoffargument used for acquiring the shared lock 156f53d15feSStephan Uphoff.It Fn rm_wunlock "struct rmlock *rm" 157f53d15feSStephan UphoffThis function releases an exclusive lock previously acquired by 158f53d15feSStephan Uphoff.Fn rm_wlock . 159f53d15feSStephan Uphoff.It Fn rm_destroy "struct rmlock *rm" 160f53d15feSStephan UphoffThis functions destroys a lock previously initialized with 161f53d15feSStephan Uphoff.Fn rm_init . 162f53d15feSStephan UphoffThe 163f53d15feSStephan Uphoff.Fa rm 164f53d15feSStephan Uphofflock must be unlocked. 165f53d15feSStephan Uphoff.El 166f53d15feSStephan Uphoff.El 167f53d15feSStephan Uphoff.Sh SEE ALSO 168f53d15feSStephan Uphoff.Xr locking 9 , 169f53d15feSStephan Uphoff.Xr mutex 9 , 170f53d15feSStephan Uphoff.Xr panic 9 , 171f53d15feSStephan Uphoff.Xr rwlock 9, 172f53d15feSStephan Uphoff.Xr sema 9 , 173f53d15feSStephan Uphoff.Xr sx 9 174f53d15feSStephan Uphoff.Sh HISTORY 175f53d15feSStephan UphoffThese 176f53d15feSStephan Uphofffunctions appeared in 177f53d15feSStephan Uphoff.Fx 7.0 . 178f53d15feSStephan Uphoff.Sh AUTHORS 179f53d15feSStephan Uphoff.An -nosplit 180f53d15feSStephan UphoffThe 181f53d15feSStephan Uphoff.Nm 182f53d15feSStephan Uphofffacility was written by 183f53d15feSStephan Uphoff.An "Stephan Uphoff" . 184f53d15feSStephan UphoffThis manual page was written by 185f53d15feSStephan Uphoff.An "Gleb Smirnoff" 186989cbe40SHiten Pandyafor rwlock and modified to reflect rmlock by 187f53d15feSStephan Uphoff.An "Stephan Uphoff" . 188f53d15feSStephan Uphoff.Sh BUGS 189989cbe40SHiten Pandya.Pp 190989cbe40SHiten PandyaThe 191989cbe40SHiten Pandya.Nm 192989cbe40SHiten Pandyaimplementation is currently not optimized for single processor systems. 193989cbe40SHiten Pandya.Pp 194989cbe40SHiten PandyaThe 195989cbe40SHiten Pandya.Nm 196989cbe40SHiten Pandyaimplementation uses a single per cpu list shared by all 197989cbe40SHiten Pandyarmlocks in the system. 198989cbe40SHiten PandyaIf rmlocks become popular, hashing to multiple per cpu queues may 199989cbe40SHiten Pandyabe needed to speed up the writer lock process. 200989cbe40SHiten Pandya.Pp 201989cbe40SHiten PandyaThe 202989cbe40SHiten Pandya.Nm 203989cbe40SHiten Pandyacan currently not be used as a lock argument for condition variable 204989cbe40SHiten Pandyawait functions. 205