1.\" 2.\" Copyright (C) 2001 Jason Evans <jasone@FreeBSD.org>. 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(s), this list of conditions and the following disclaimer as 9.\" the first lines of this file unmodified other than the possible 10.\" addition of one or more copyright notices. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice(s), this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 16.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18.\" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 19.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22.\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 25.\" DAMAGE. 26.\" 27.\" $FreeBSD$ 28.\" 29.Dd August 14, 2001 30.Dt SX 9 31.Os 32.Sh NAME 33.Nm sx , 34.Nm sx_init , 35.Nm sx_destroy , 36.Nm sx_slock , 37.Nm sx_xlock , 38.Nm sx_try_slock , 39.Nm sx_try_xlock , 40.Nm sx_sunlock , 41.Nm sx_xunlock , 42.Nm sx_try_upgrade , 43.Nm sx_downgrade , 44.Nm sx_assert , 45.Nm SX_SYSINIT 46.Nd kernel shared/exclusive lock 47.Sh SYNOPSIS 48.In sys/param.h 49.In sys/lock.h 50.In sys/sx.h 51.Ft void 52.Fn sx_init "struct sx *sx" "const char *description" 53.Ft void 54.Fn sx_destroy "struct sx *sx" 55.Ft void 56.Fn sx_slock "struct sx *sx" 57.Ft void 58.Fn sx_xlock "struct sx *sx" 59.Ft int 60.Fn sx_try_slock "struct sx *sx" 61.Ft int 62.Fn sx_try_xlock "struct sx *sx" 63.Ft void 64.Fn sx_sunlock "struct sx *sx" 65.Ft void 66.Fn sx_xunlock "struct sx *sx" 67.Ft int 68.Fn sx_try_upgrade "struct sx *sx" 69.Ft void 70.Fn sx_downgrade "struct sx *sx" 71.Pp 72.Cd "options INVARIANTS" 73.Cd "options INVARIANT_SUPPORT" 74.Ft void 75.Fn sx_assert "struct sx *sx" "int what" 76.In sys/kernel.h 77.Fn SX_SYSINIT "name" "struct sx *sx" "const char *description" 78.Sh DESCRIPTION 79Shared/exclusive locks are used to protect data that are read far more often 80than they are written. 81Mutexes are inherently more efficient than shared/exclusive locks, so 82shared/exclusive locks should be used prudently. 83.Pp 84Shared/exclusive locks are created with 85.Fn sx_init , 86where 87.Fa sx 88is a pointer to space for a 89.Vt struct sx , 90and 91.Fa description 92is a pointer to a null-terminated character string that describes the 93shared/exclusive lock. 94Shared/exclusive locks are destroyed with 95.Fn sx_destroy . 96Threads acquire and release a shared lock by calling 97.Fn sx_slock 98or 99.Fn sx_try_slock 100and 101.Fn sx_sunlock . 102Threads acquire and release an exclusive lock by calling 103.Fn sx_xlock 104or 105.Fn sx_try_xlock 106and 107.Fn sx_xunlock . 108A thread can attempt to upgrade a currently held shared lock to an exclusive 109lock by calling 110.Fn sx_try_upgrade . 111A thread that has an exclusive lock can downgrade it to a shared lock by 112calling 113.Fn sx_downgrade . 114.Pp 115.Fn sx_try_slock 116and 117.Fn sx_try_xlock 118will return 0 if the shared/exclusive lock cannot be acquired immediately; 119otherwise the shared/exclusive lock will be acquired and a non-zero value will 120be returned. 121.Pp 122.Fn sx_try_upgrade 123will return 0 if the shared lock cannot be upgraded to an exclusive lock 124immediately; otherwise the exclusive lock will be acquired and a non-zero value 125will be returned. 126.Pp 127When compiled with 128.Cd "options INVARIANTS" 129and 130.Cd "options INVARIANT_SUPPORT" , 131the 132.Fn sx_assert 133function tests 134.Ar sx 135for the assertions specified in 136.Ar what 137and panics if they are not met. 138The following assertions are supported: 139.Bl -tag -width ".Dv SX_UNLOCKED" 140.It Dv SX_LOCKED 141Assert that the current thread has either a shared or an exclusive lock on the 142.Vt sx 143lock pointed to by the first argument. 144.It Dv SX_SLOCKED 145Assert that the current thread has a shared lock on the 146.Vt sx 147lock pointed to by 148the first argument. 149.It Dv SX_XLOCKED 150Assert that the current thread has an exclusive lock on the 151.Vt sx 152lock pointed to 153by the first argument. 154.It Dv SX_UNLOCKED 155Assert that the current thread has no lock on the 156.Vt sx 157lock pointed to 158by the first argument. 159.El 160.Pp 161The 162.Fn SX_SYSINIT 163macro is used to generate a call to the 164.Fn sx_sysinit 165routine at system startup in order to initialize a given 166.Fa sx 167lock. 168The parameters are the same as 169.Fn sx_init 170but with an additional argument, 171.Fa name , 172that is used in generating unique variable names for the related 173structures associated with the lock and the sysinit routine. 174.Pp 175A thread may not hold both a shared lock and an exclusive lock on the same 176lock simultaneously; 177attempting to do so will result in deadlock. 178.Sh CONTEXT 179A thread may hold a shared or exclusive lock on an 180.Nm 181lock while sleeping. 182.Sh SEE ALSO 183.Xr condvar 9 , 184.Xr mtx_pool 9 , 185.Xr mutex 9 , 186.Xr panic 9 , 187.Xr sema 9 188.Sh BUGS 189Currently there is no way to assert that a lock is not held. 190This is not possible in the 191.No non- Ns Dv WITNESS 192case for asserting that this thread 193does not hold a shared lock. 194In the 195.No non- Ns Dv WITNESS 196case, the 197.Dv SX_LOCKED 198and 199.Dv SX_SLOCKED 200assertions merely check that some thread holds a shared lock. 201They do not ensure that the current thread holds a shared lock. 202