13182fc7bSGleb Smirnoff.\" Copyright (c) 2006 Gleb Smirnoff <glebius@FreeBSD.org> 23182fc7bSGleb Smirnoff.\" All rights reserved. 33182fc7bSGleb Smirnoff.\" 43182fc7bSGleb Smirnoff.\" Redistribution and use in source and binary forms, with or without 53182fc7bSGleb Smirnoff.\" modification, are permitted provided that the following conditions 63182fc7bSGleb Smirnoff.\" are met: 73182fc7bSGleb Smirnoff.\" 1. Redistributions of source code must retain the above copyright 83182fc7bSGleb Smirnoff.\" notice, this list of conditions and the following disclaimer. 93182fc7bSGleb Smirnoff.\" 2. Redistributions in binary form must reproduce the above copyright 103182fc7bSGleb Smirnoff.\" notice, this list of conditions and the following disclaimer in the 113182fc7bSGleb Smirnoff.\" documentation and/or other materials provided with the distribution. 123182fc7bSGleb Smirnoff.\" 133182fc7bSGleb Smirnoff.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 143182fc7bSGleb Smirnoff.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 153182fc7bSGleb Smirnoff.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 163182fc7bSGleb Smirnoff.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 173182fc7bSGleb Smirnoff.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 183182fc7bSGleb Smirnoff.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 193182fc7bSGleb Smirnoff.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 203182fc7bSGleb Smirnoff.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 213182fc7bSGleb Smirnoff.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 223182fc7bSGleb Smirnoff.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 233182fc7bSGleb Smirnoff.\" SUCH DAMAGE. 243182fc7bSGleb Smirnoff.\" 253182fc7bSGleb Smirnoff.\" $FreeBSD$ 263182fc7bSGleb Smirnoff.\" 273182fc7bSGleb Smirnoff.Dd January 30, 2006 283182fc7bSGleb Smirnoff.Dt RWLOCK 9 293182fc7bSGleb Smirnoff.Os 303182fc7bSGleb Smirnoff.Sh NAME 313182fc7bSGleb Smirnoff.Nm rwlock 323182fc7bSGleb Smirnoff.Nm rw_init , 333182fc7bSGleb Smirnoff.Nm rw_rlock , 343182fc7bSGleb Smirnoff.Nm rw_wlock , 353182fc7bSGleb Smirnoff.Nm rw_assert , 363182fc7bSGleb Smirnoff.Nm rw_runlock , 373182fc7bSGleb Smirnoff.Nm rw_wunlock , 383182fc7bSGleb Smirnoff.Nm rw_initialized , 393182fc7bSGleb Smirnoff.Nm rw_destroy , 403182fc7bSGleb Smirnoff.Nm RW_SYSINIT , 413182fc7bSGleb Smirnoff.Nd kernel synchronization primitives 423182fc7bSGleb Smirnoff.Sh SYNOPSIS 433182fc7bSGleb Smirnoff.In sys/param.h 443182fc7bSGleb Smirnoff.In sys/lock.h 453182fc7bSGleb Smirnoff.In sys/rwlock.h 463182fc7bSGleb Smirnoff.Ft void 473182fc7bSGleb Smirnoff.Fn rw_init "struct rwlock *rwlock" "const char *name" 483182fc7bSGleb Smirnoff.Ft void 493182fc7bSGleb Smirnoff.Fn rw_rlock "struct rwlock *rwlock" 503182fc7bSGleb Smirnoff.Ft void 513182fc7bSGleb Smirnoff.Fn rw_wlock "struct rwlock *rwlock" 523182fc7bSGleb Smirnoff.Ft void 533182fc7bSGleb Smirnoff.Fn rw_runlock "struct rwlock *rwlock" 543182fc7bSGleb Smirnoff.Ft void 553182fc7bSGleb Smirnoff.Fn rw_wunlock "struct rwlock *rwlock" 563182fc7bSGleb Smirnoff.Ft int 573182fc7bSGleb Smirnoff.Fn rw_initialized "struct rwlock *rwlock" 583182fc7bSGleb Smirnoff.Ft void 593182fc7bSGleb Smirnoff.Fn rw_destroy "struct rwlock *rwlock" 603182fc7bSGleb Smirnoff.Pp 613182fc7bSGleb Smirnoff.Cd "options INVARIANTS" 623182fc7bSGleb Smirnoff.Cd "options INVARIANT_SUPPORT" 633182fc7bSGleb Smirnoff.Ft void 643182fc7bSGleb Smirnoff.Fn rw_assert "struct rwlock *rwlock" "int what" 653182fc7bSGleb Smirnoff.In sys/kernel.h 663182fc7bSGleb Smirnoff.Fn RW_SYSINIT "name" "struct rwlock *rwlock" "const char *description" 673182fc7bSGleb Smirnoff.Sh DESCRIPTION 683182fc7bSGleb SmirnoffRead/write locks are a method of thread synchronization, which 693182fc7bSGleb Smirnoffallows several threads have shared access to protected data, or 703182fc7bSGleb Smirnoffone thread have exclusive access. 713182fc7bSGleb SmirnoffThe threads that share access are known as 723182fc7bSGleb Smirnoff.Em readers 733182fc7bSGleb Smirnoffsince they should only read the protected data, while thread 743182fc7bSGleb Smirnoffwith exclusive access is known as a 753182fc7bSGleb Smirnoff.Em writer 763182fc7bSGleb Smirnoffsince it can modify protected data. 773182fc7bSGleb Smirnoff.Pp 783182fc7bSGleb SmirnoffAlthough the description of read/write locks looks very similar 793182fc7bSGleb Smirnoffto description of 803182fc7bSGleb Smirnoff.Xr sx 9 813182fc7bSGleb Smirnofflocks, their usage pattern is different. 823182fc7bSGleb SmirnoffThe read/write locks can be treated as mutexes (see 833182fc7bSGleb Smirnoff.Xr mutex 9 ) 843182fc7bSGleb Smirnoffwith shared/exclusive semantics. 853182fc7bSGleb SmirnoffUnlike 863182fc7bSGleb Smirnoff.Xr sx 9 , 873182fc7bSGleb Smirnoffan 883182fc7bSGleb Smirnoff.Nm 893182fc7bSGleb Smirnoffcan be locked while holding a non-spin mutex; 903182fc7bSGleb Smirnoff.Nm 913182fc7bSGleb Smirnoffcannot be held while sleeping. 923182fc7bSGleb SmirnoffThe 933182fc7bSGleb Smirnoff.Nm 943182fc7bSGleb Smirnofflocks have priority propagation like mutexes, but priority 953182fc7bSGleb Smirnoffcan be propagated only to exclusive holder. 963182fc7bSGleb SmirnoffThis limitation comes from the fact that shared owners 973182fc7bSGleb Smirnoffare anonymous. 983182fc7bSGleb SmirnoffAnother important property is that shared holder of 993182fc7bSGleb Smirnoff.Nm 1003182fc7bSGleb Smirnoffcan recurse on it. 1013182fc7bSGleb Smirnoff.Ss Macros and Functions 1023182fc7bSGleb Smirnoff.Bl -tag -width indent 1033182fc7bSGleb Smirnoff.It Fn rw_init "struct rwlock *rwlock" "const char *name" 1043182fc7bSGleb SmirnoffInitialize structure located at 1053182fc7bSGleb Smirnoff.Fa rwlock 1063182fc7bSGleb Smirnoffas read/write lock, described by name 1073182fc7bSGleb Smirnoff.Fa name . 1083182fc7bSGleb SmirnoffThe description is used solely for debugging purposes. 1093182fc7bSGleb SmirnoffThis function must be used before any other manipulations 1103182fc7bSGleb Smirnoffwith the lock. 1113182fc7bSGleb Smirnoff.It Fn rw_rlock "struct rwlock *rwlock" 1123182fc7bSGleb SmirnoffLock the 1133182fc7bSGleb Smirnoff.Fa rwlock 1143182fc7bSGleb Smirnoffas reader. 1153182fc7bSGleb SmirnoffIf any thread holds this lock exclusively, the current thread blocks, 1163182fc7bSGleb Smirnoffand its priority is propagated to exclusive holder. 1173182fc7bSGleb SmirnoffThe 1183182fc7bSGleb Smirnoff.Fn rw_rlock 1193182fc7bSGleb Smirnofffunction can be called when the thread has already acquired reader 1203182fc7bSGleb Smirnoffaccess on 1213182fc7bSGleb Smirnoff.Fa rwlock . 1223182fc7bSGleb SmirnoffThis is called 1233182fc7bSGleb Smirnoff.Dq "recursing on a lock" . 1243182fc7bSGleb Smirnoff.It Fn rw_wlock "struct rwlock *rwlock" 1253182fc7bSGleb SmirnoffLock the 1263182fc7bSGleb Smirnoff.Fa rwlock 1273182fc7bSGleb Smirnoffas writer. 1283182fc7bSGleb SmirnoffIf there are any shared owners of the lock, the current thread blocks. 1293182fc7bSGleb SmirnoffThe 1303182fc7bSGleb Smirnoff.Fn rw_wlock 1313182fc7bSGleb Smirnofffunction cannot be called recursively. 1323182fc7bSGleb Smirnoff.It Fn rw_runlock "struct rwlock *rwlock" 1333182fc7bSGleb SmirnoffThis function releases shared lock, previously acquired by 1343182fc7bSGleb Smirnoff.Fn rw_rlock . 1353182fc7bSGleb Smirnoff.It Fn rw_wunlock "struct rwlock *rwlock" 1363182fc7bSGleb SmirnoffThis function releases exclusive lock, previously acquired by 1373182fc7bSGleb Smirnoff.Fn rw_wlock . 1383182fc7bSGleb Smirnoff.It Fn rw_initialized "struct rwlock *rwlock" 1393182fc7bSGleb SmirnoffThis function returns non-zero if the 1403182fc7bSGleb Smirnoff.Fa rwlock 1413182fc7bSGleb Smirnoffhas been initialized, and zero otherwise. 1423182fc7bSGleb Smirnoff.It Fn rw_destroy "struct rwlock *rwlock" 1433182fc7bSGleb SmirnoffThis functions destroys a lock previously initialized with 1443182fc7bSGleb Smirnoff.Fn rw_init . 1453182fc7bSGleb SmirnoffThe 1463182fc7bSGleb Smirnoff.Fa rwlock 1473182fc7bSGleb Smirnoffmust be unlocked. 1483182fc7bSGleb Smirnoff.It Fn rw_assert "struct rwlock *rwlock" "int what" 1493182fc7bSGleb SmirnoffThis function allows assertions specified in 1503182fc7bSGleb Smirnoff.Fa what 1513182fc7bSGleb Smirnoffto be made about 1523182fc7bSGleb Smirnoff.Fa rwlock . 1533182fc7bSGleb SmirnoffIf the assertions are not true and the kernel is compiled 1543182fc7bSGleb Smirnoffwith 1553182fc7bSGleb Smirnoff.Cd "options INVARIANTS" 1563182fc7bSGleb Smirnoffand 1573182fc7bSGleb Smirnoff.Cd "options INVARIANT_SUPPORT" , 1583182fc7bSGleb Smirnoffthe kernel will panic. 1593182fc7bSGleb SmirnoffCurrently the following assertions are supported: 1603182fc7bSGleb Smirnoff.Bl -tag -width ".Dv RA_UNLOCKED" 1613182fc7bSGleb Smirnoff.It Dv RA_LOCKED 1623182fc7bSGleb SmirnoffAssert that current thread is either shared or exclusive owner 1633182fc7bSGleb Smirnoffof the 1643182fc7bSGleb Smirnoff.Nm 1653182fc7bSGleb Smirnoffpointed to by the first argument. 1663182fc7bSGleb Smirnoff.It Dv RA_RLOCKED 1673182fc7bSGleb SmirnoffAssert that current thread is shared owner of the 1683182fc7bSGleb Smirnoff.Nm 1693182fc7bSGleb Smirnoffpointed 1703182fc7bSGleb Smirnoffto by the first argument. 1713182fc7bSGleb Smirnoff.It Dv RA_WLOCKED 1723182fc7bSGleb SmirnoffAssert that current thread is exclusive owner of the 1733182fc7bSGleb Smirnoff.Nm 1743182fc7bSGleb Smirnoffpointed 1753182fc7bSGleb Smirnoffto by the first argument. 1763182fc7bSGleb Smirnoff.It Dv RA_UNLOCKED 1773182fc7bSGleb SmirnoffAssert that current thread is neither shared nor exclusive owner 1783182fc7bSGleb Smirnoffof the 1793182fc7bSGleb Smirnoff.Nm 1803182fc7bSGleb Smirnoffpointed to by the first argument. 1813182fc7bSGleb Smirnoff.El 1823182fc7bSGleb Smirnoff.El 1833182fc7bSGleb Smirnoff.Sh SEE ALSO 1843182fc7bSGleb Smirnoff.Xr condvar 9 , 1853182fc7bSGleb Smirnoff.Xr mutex 9 , 1863182fc7bSGleb Smirnoff.Xr panic 9 , 1873182fc7bSGleb Smirnoff.Xr sema 9 , 1883182fc7bSGleb Smirnoff.Xr sx 9 1893182fc7bSGleb Smirnoff.Sh HISTORY 1903182fc7bSGleb SmirnoffThese 1913182fc7bSGleb Smirnofffunctions appeared in 1923182fc7bSGleb Smirnoff.Fx 7.0 . 1933182fc7bSGleb Smirnoff.Sh AUTHORS 1943182fc7bSGleb Smirnoff.An -nosplit 1953182fc7bSGleb SmirnoffThe 1963182fc7bSGleb Smirnoff.Nm 1973182fc7bSGleb Smirnofffacility was written by 1983182fc7bSGleb Smirnoff.An "John Baldwin" . 1993182fc7bSGleb SmirnoffThis manual page was written by 2003182fc7bSGleb Smirnoff.An "Gleb Smirnoff" . 201