1.\" 2.\" Copyright (c) 1996 Joerg Wunsch 3.\" 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, 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 DEVELOPERS ``AS IS'' AND ANY EXPRESS OR 16.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 19.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25.\" 26.\" $FreeBSD$ 27.\" 28.Dd February 27, 2007 29.Os 30.Dt SLEEP 9 31.Sh NAME 32.Nm msleep , 33.Nm msleep_spin , 34.Nm pause , 35.Nm tsleep , 36.Nm wakeup 37.Nd wait for events 38.Sh SYNOPSIS 39.In sys/param.h 40.In sys/systm.h 41.In sys/proc.h 42.Ft int 43.Fn msleep "void *chan" "struct mtx *mtx" "int priority" "const char *wmesg" "int timo" 44.Ft int 45.Fn msleep_spin "void *chan" "struct mtx *mtx" "const char *wmesg" "int timo" 46.Ft void 47.Fn pause "const char *wmesg" "int timo" 48.Ft int 49.Fn tsleep "void *chan" "int priority" "const char *wmesg" "int timo" 50.Ft void 51.Fn wakeup "void *chan" 52.Ft void 53.Fn wakeup_one "void *chan" 54.Sh DESCRIPTION 55The functions 56.Fn tsleep , 57.Fn msleep , 58.Fn msleep_spin , 59.Fn pause , 60.Fn wakeup , 61and 62.Fn wakeup_one 63handle event-based thread blocking. 64If a thread must wait for an 65external event, it is put to sleep by 66.Fn tsleep , 67.Fn msleep , 68.Fn msleep_spin , 69or 70.Fn pause . 71Threads may also wait using one of the locking primitive sleep routines 72.Xr mtx_sleep 9 , 73.Xr rw_sleep 9 , 74or 75.Xr sx_sleep 9 . 76.Pp 77The parameter 78.Fa chan 79is an arbitrary address that uniquely identifies the event on which 80the thread is being put to sleep. 81All threads sleeping on a single 82.Fa chan 83are woken up later by 84.Fn wakeup , 85often called from inside an interrupt routine, to indicate that the 86resource the thread was blocking on is available now. 87.Pp 88The parameter 89.Fa priority 90specifies a new priority for the thread as well as some optional flags. 91If the new priority is not 0, 92then the thread will be made 93runnable with the specified 94.Fa priority 95when it resumes. 96If 97.Fa priority 98includes the 99.Dv PCATCH 100flag, signals are checked before and after sleeping, otherwise signals are 101not checked. 102If 103.Dv PCATCH 104is set and a signal needs to be delivered, 105.Er ERESTART 106is returned if the current system call should be restarted if 107possible, and 108.Er EINTR 109is returned if the system call should be interrupted by the signal 110(return 111.Er EINTR ) . 112.Pp 113The parameter 114.Fa wmesg 115is a string describing the sleep condition for tools like 116.Xr ps 1 . 117Due to the limited space of those programs to display arbitrary strings, 118this message should not be longer than 6 characters. 119.Pp 120The parameter 121.Fa timo 122specifies a timeout for the sleep. 123If 124.Fa timo 125is not 0, 126then the thread will sleep for at most 127.Fa timo No / Va hz 128seconds. 129If the timeout expires, 130then the sleep function will return 131.Er EWOULDBLOCK . 132.Pp 133Several of the sleep functions including 134.Fn msleep , 135.Fn msleep_spin , 136and the locking primitive sleep routines specify an additional lock 137parameter. 138The lock will be released before sleeping and reacquired 139before the sleep routine returns. 140If 141.Fa priority 142includes the 143.Dv PDROP 144flag, then 145the lock will not be reacquired before returning. 146The lock is used to ensure that a condition can be checked atomically, 147and that the current thread can be suspended without missing a 148change to the condition, or an associated wakeup. 149In addition, all of the sleep routines will fully drop the 150.Va Giant 151mutex 152(even if recursed) 153while the thread is suspended and will reacquire the 154.Va Giant 155mutex before the function returns. 156.Pp 157To avoid lost wakeups, 158either a lock should be used to protect against races, 159or a timeout should be specified to place an upper bound on the delay due 160to a lost wakeup. 161As a result, 162the 163.Fn tsleep 164function should only be invoked with a timeout of 0 when the 165.Va Giant 166mutex is held. 167.Pp 168The 169.Fn msleep 170function requires that 171.Fa mtx 172reference a default, i.e. non-spin, mutex. 173Its use is deprecated in favor of 174.Xr mtx_sleep 9 175which provides identical behavior. 176.Pp 177The 178.Fn msleep_spin 179function requires that 180.Fa mtx 181reference a spin mutex. 182The 183.Fn msleep_spin 184function does not accept a 185.Fa priority 186parameter and thus does not support changing the current thread's priority, 187the 188.Dv PDROP 189flag, 190or catching signals via the 191.Dv PCATCH 192flag. 193.Pp 194The 195.Fn pause 196function is a wrapper around 197.Fn tsleep 198that suspends execution of the current thread for the indicated timeout. 199The thread can not be awakened early by signals or calls to 200.Fn wakeup 201or 202.Fn wakeup_one . 203.Pp 204The 205.Fn wakeup_one 206function makes the first thread in the queue that is sleeping on the 207parameter 208.Fa chan 209runnable. 210This reduces the load when a large number of threads are sleeping on 211the same address, but only one of them can actually do any useful work 212when made runnable. 213.Pp 214Due to the way it works, the 215.Fn wakeup_one 216function requires that only related threads sleep on a specific 217.Fa chan 218address. 219It is the programmer's responsibility to choose a unique 220.Fa chan 221value. 222The older 223.Fn wakeup 224function did not require this, though it was never good practice 225for threads to share a 226.Fa chan 227value. 228When converting from 229.Fn wakeup 230to 231.Fn wakeup_one , 232pay particular attention to ensure that no other threads wait on the 233same 234.Fa chan . 235.Sh RETURN VALUES 236If the thread is awakened by a call to 237.Fn wakeup 238or 239.Fn wakeup_one , 240the 241.Fn msleep , 242.Fn msleep_spin , 243.Fn tsleep , 244and locking primitive sleep functions return 0. 245Otherwise, a non-zero error code is returned. 246.Sh ERRORS 247.Fn msleep , 248.Fn msleep_spin , 249.Fn tsleep , 250and the locking primitive sleep functions will fail if: 251.Bl -tag -width Er 252.It Bq Er EINTR 253The 254.Dv PCATCH 255flag was specified, a signal was caught, and the system call should be 256interrupted. 257.It Bq Er ERESTART 258The 259.Dv PCATCH 260flag was specified, a signal was caught, and the system call should be 261restarted. 262.It Bq Er EWOULDBLOCK 263A non-zero timeout was specified and the timeout expired. 264.El 265.Sh SEE ALSO 266.Xr ps 1 , 267.Xr locking 9 , 268.Xr malloc 9 , 269.Xr mi_switch 9 , 270.Xr mtx_sleep 9 , 271.Xr rw_sleep 9 , 272.Xr sx_sleep 9 273.Sh HISTORY 274The functions 275.Fn sleep 276and 277.Fn wakeup 278were present in 279.At v1 . 280They were probably also present in the preceding 281PDP-7 version of 282.Ux . 283They were the basic process synchronization model. 284.Pp 285The 286.Fn tsleep 287function appeared in 288.Bx 4.4 289and added the parameters 290.Fa wmesg 291and 292.Fa timo . 293The 294.Fn sleep 295function was removed in 296.Fx 2.2 . 297The 298.Fn wakeup_one 299function appeared in 300.Fx 2.2 . 301The 302.Fn msleep 303function appeared in 304.Fx 5.0 , 305and the 306.Fn msleep_spin 307function appeared in 308.Fx 6.2 . 309The 310.Fn pause 311function appeared in 312.Fx 7.0 . 313.Sh AUTHORS 314.An -nosplit 315This manual page was written by 316.An J\(:org Wunsch Aq joerg@FreeBSD.org . 317