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 March 28, 2016 30.Dt SX 9 31.Os 32.Sh NAME 33.Nm sx , 34.Nm sx_init , 35.Nm sx_init_flags , 36.Nm sx_destroy , 37.Nm sx_slock , 38.Nm sx_xlock , 39.Nm sx_slock_sig , 40.Nm sx_xlock_sig , 41.Nm sx_try_slock , 42.Nm sx_try_xlock , 43.Nm sx_sunlock , 44.Nm sx_xunlock , 45.Nm sx_unlock , 46.Nm sx_try_upgrade , 47.Nm sx_downgrade , 48.Nm sx_sleep , 49.Nm sx_xholder , 50.Nm sx_xlocked , 51.Nm sx_assert , 52.Nm SX_SYSINIT 53.Nd kernel shared/exclusive lock 54.Sh SYNOPSIS 55.In sys/param.h 56.In sys/lock.h 57.In sys/sx.h 58.Ft void 59.Fn sx_init "struct sx *sx" "const char *description" 60.Ft void 61.Fn sx_init_flags "struct sx *sx" "const char *description" "int opts" 62.Ft void 63.Fn sx_destroy "struct sx *sx" 64.Ft void 65.Fn sx_slock "struct sx *sx" 66.Ft void 67.Fn sx_xlock "struct sx *sx" 68.Ft int 69.Fn sx_slock_sig "struct sx *sx" 70.Ft int 71.Fn sx_xlock_sig "struct sx *sx" 72.Ft int 73.Fn sx_try_slock "struct sx *sx" 74.Ft int 75.Fn sx_try_xlock "struct sx *sx" 76.Ft void 77.Fn sx_sunlock "struct sx *sx" 78.Ft void 79.Fn sx_xunlock "struct sx *sx" 80.Ft void 81.Fn sx_unlock "struct sx *sx" 82.Ft int 83.Fn sx_try_upgrade "struct sx *sx" 84.Ft void 85.Fn sx_downgrade "struct sx *sx" 86.Ft int 87.Fn sx_sleep "void *chan" "struct sx *sx" "int priority" "const char *wmesg" "int timo" 88.Ft "struct thread *" 89.Fn sx_xholder "struct sx *sx" 90.Ft int 91.Fn sx_xlocked "const struct sx *sx" 92.Pp 93.Cd "options INVARIANTS" 94.Cd "options INVARIANT_SUPPORT" 95.Ft void 96.Fn sx_assert "const struct sx *sx" "int what" 97.In sys/kernel.h 98.Fn SX_SYSINIT "name" "struct sx *sx" "const char *description" 99.Sh DESCRIPTION 100Shared/exclusive locks are used to protect data that are read far more often 101than they are written. 102Shared/exclusive locks do not implement priority propagation like mutexes and 103reader/writer locks to prevent priority inversions, so 104shared/exclusive locks should be used prudently. 105.Pp 106Shared/exclusive locks are created with either 107.Fn sx_init 108or 109.Fn sx_init_flags 110where 111.Fa sx 112is a pointer to space for a 113.Vt struct sx , 114and 115.Fa description 116is a pointer to a null-terminated character string that describes the 117shared/exclusive lock. 118The 119.Fa opts 120argument to 121.Fn sx_init_flags 122specifies a set of optional flags to alter the behavior of 123.Fa sx . 124It contains one or more of the following flags: 125.Bl -tag -width SX_NOADAPTIVE 126.It Dv SX_NOADAPTIVE 127Disable adaptive spinning, rather than sleeping, for lock operations 128while an exclusive lock holder is executing on another CPU. 129Adaptive spinning is the default unless the kernel is compiled with 130.Cd "options NO_ADAPTIVE_SX" . 131.It Dv SX_DUPOK 132Witness should not log messages about duplicate locks being acquired. 133.It Dv SX_NOWITNESS 134Instruct 135.Xr witness 4 136to ignore this lock. 137.It Dv SX_NOPROFILE 138Do not profile this lock. 139.It Dv SX_RECURSE 140Allow threads to recursively acquire exclusive locks for 141.Fa sx . 142.It Dv SX_QUIET 143Do not log any operations for this lock via 144.Xr ktr 4 . 145.It Dv SX_NEW 146If the kernel has been compiled with 147.Cd "options INVARIANTS" , 148.Fn sx_init 149will assert that the 150.Fa sx 151has not been initialized multiple times without intervening calls to 152.Fn sx_destroy 153unless this option is specified. 154.El 155.Pp 156Shared/exclusive locks are destroyed with 157.Fn sx_destroy . 158The lock 159.Fa sx 160must not be locked by any thread when it is destroyed. 161.Pp 162Threads acquire and release a shared lock by calling 163.Fn sx_slock , 164.Fn sx_slock_sig 165or 166.Fn sx_try_slock 167and 168.Fn sx_sunlock 169or 170.Fn sx_unlock . 171Threads acquire and release an exclusive lock by calling 172.Fn sx_xlock , 173.Fn sx_xlock_sig 174or 175.Fn sx_try_xlock 176and 177.Fn sx_xunlock 178or 179.Fn sx_unlock . 180A thread can attempt to upgrade a currently held shared lock to an exclusive 181lock by calling 182.Fn sx_try_upgrade . 183A thread that has an exclusive lock can downgrade it to a shared lock by 184calling 185.Fn sx_downgrade . 186.Pp 187.Fn sx_try_slock 188and 189.Fn sx_try_xlock 190will return 0 if the shared/exclusive lock cannot be acquired immediately; 191otherwise the shared/exclusive lock will be acquired and a non-zero value will 192be returned. 193.Pp 194.Fn sx_try_upgrade 195will return 0 if the shared lock cannot be upgraded to an exclusive lock 196immediately; otherwise the exclusive lock will be acquired and a non-zero value 197will be returned. 198.Pp 199.Fn sx_slock_sig 200and 201.Fn sx_xlock_sig 202do the same as their normal versions but performing an interruptible sleep. 203They return a non-zero value if the sleep has been interrupted by a signal 204or an interrupt, otherwise 0. 205.Pp 206A thread can atomically release a shared/exclusive lock while waiting for an 207event by calling 208.Fn sx_sleep . 209For more details on the parameters to this function, 210see 211.Xr sleep 9 . 212.Pp 213When compiled with 214.Cd "options INVARIANTS" 215and 216.Cd "options INVARIANT_SUPPORT" , 217the 218.Fn sx_assert 219function tests 220.Fa sx 221for the assertions specified in 222.Fa what , 223and panics if they are not met. 224One of the following assertions must be specified: 225.Bl -tag -width ".Dv SA_UNLOCKED" 226.It Dv SA_LOCKED 227Assert that the current thread has either a shared or an exclusive lock on the 228.Vt sx 229lock pointed to by the first argument. 230.It Dv SA_SLOCKED 231Assert that the current thread has a shared lock on the 232.Vt sx 233lock pointed to by 234the first argument. 235.It Dv SA_XLOCKED 236Assert that the current thread has an exclusive lock on the 237.Vt sx 238lock pointed to 239by the first argument. 240.It Dv SA_UNLOCKED 241Assert that the current thread has no lock on the 242.Vt sx 243lock pointed to 244by the first argument. 245.El 246.Pp 247In addition, one of the following optional assertions may be included with 248either an 249.Dv SA_LOCKED , 250.Dv SA_SLOCKED , 251or 252.Dv SA_XLOCKED 253assertion: 254.Bl -tag -width ".Dv SA_NOTRECURSED" 255.It Dv SA_RECURSED 256Assert that the current thread has a recursed lock on 257.Fa sx . 258.It Dv SA_NOTRECURSED 259Assert that the current thread does not have a recursed lock on 260.Fa sx . 261.El 262.Pp 263.Fn sx_xholder 264will return a pointer to the thread which currently holds an exclusive lock on 265.Fa sx . 266If no thread holds an exclusive lock on 267.Fa sx , 268then 269.Dv NULL 270is returned instead. 271.Pp 272.Fn sx_xlocked 273will return non-zero if the current thread holds the exclusive lock; 274otherwise, it will return zero. 275.Pp 276For ease of programming, 277.Fn sx_unlock 278is provided as a macro frontend to the respective functions, 279.Fn sx_sunlock 280and 281.Fn sx_xunlock . 282Algorithms that are aware of what state the lock is in should use either 283of the two specific functions for a minor performance benefit. 284.Pp 285The 286.Fn SX_SYSINIT 287macro is used to generate a call to the 288.Fn sx_sysinit 289routine at system startup in order to initialize a given 290.Fa sx 291lock. 292The parameters are the same as 293.Fn sx_init 294but with an additional argument, 295.Fa name , 296that is used in generating unique variable names for the related 297structures associated with the lock and the sysinit routine. 298.Pp 299A thread may not hold both a shared lock and an exclusive lock on the same 300lock simultaneously; 301attempting to do so will result in deadlock. 302.Sh CONTEXT 303A thread may hold a shared or exclusive lock on an 304.Nm 305lock while sleeping. 306As a result, an 307.Nm 308lock may not be acquired while holding a mutex. 309Otherwise, if one thread slept while holding an 310.Nm 311lock while another thread blocked on the same 312.Nm 313lock after acquiring a mutex, then the second thread would effectively 314end up sleeping while holding a mutex, which is not allowed. 315.Sh SEE ALSO 316.Xr lock 9 , 317.Xr locking 9 , 318.Xr mutex 9 , 319.Xr panic 9 , 320.Xr rwlock 9 , 321.Xr sema 9 322.Sh BUGS 323A kernel without 324.Dv WITNESS 325cannot assert whether the current thread does or does not hold a shared lock. 326.Dv SA_LOCKED 327and 328.Dv SA_SLOCKED 329can only assert that 330.Em any 331thread holds a shared lock. 332They cannot ensure that the current thread holds a shared lock. 333Further, 334.Dv SA_UNLOCKED 335can only assert that the current thread does not hold an exclusive lock. 336