1.\" $OpenBSD: tsleep.9,v 1.9 2014/01/22 07:32:47 jmc Exp $ 2.\" $NetBSD: sleep.9,v 1.11 1999/03/24 06:15:12 mycroft Exp $ 3.\" 4.\" Copyright (c) 1996 The NetBSD Foundation, Inc. 5.\" All rights reserved. 6.\" 7.\" This code is derived from software contributed to The NetBSD Foundation 8.\" by Paul Kranenburg. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29.\" POSSIBILITY OF SUCH DAMAGE. 30.\" 31.Dd $Mdocdate: January 22 2014 $ 32.Dt TSLEEP 9 33.Os 34.Sh NAME 35.Nm tsleep , 36.Nm msleep , 37.Nm wakeup , 38.Nm wakeup_n , 39.Nm wakeup_one 40.Nd process context sleep and wakeup 41.Sh SYNOPSIS 42.In sys/param.h 43.In sys/systm.h 44.Ft int 45.Fn "tsleep" "void *ident" "int priority" "const char *wmesg" "int timo" 46.Ft int 47.Fn "msleep" "void *ident" "struct mutex *mtx" "int priority" "const char *wmesg" "int timo" 48.Ft void 49.Fn "wakeup" "void *ident" 50.Ft void 51.Fn "wakeup_n" "void *ident" "int count" 52.Ft void 53.Fn "wakeup_one" "void *ident" 54.Sh DESCRIPTION 55These functions implement voluntary context switching. 56.Fn tsleep 57and 58.Fn msleep 59are used throughout the kernel whenever processing in the current context 60cannot continue for any of the following reasons: 61.Bl -bullet -offset indent 62.It 63The current process needs to await the results of a pending I/O operation. 64.It 65The current process needs resources 66.Pq e.g. memory 67which are temporarily unavailable. 68.It 69The current process wants access to data structures which are locked by 70other processes. 71.El 72.Pp 73The 74.Fn wakeup , 75.Fn wakeup_n , 76and 77.Fn wakeup_one 78functions are used to notify sleeping processes of possible changes to the 79condition that caused them to go to sleep. 80Typically, an awakened process will -- after it has acquired a context 81again -- retry the action that blocked its operation to see if the 82.Dq blocking 83condition has cleared. 84.Pp 85The 86.Fn tsleep 87function takes the following arguments: 88.Bl -tag -width priority 89.It Fa ident 90An identifier of the 91.Dq wait channel 92representing the resource for which the current process needs to wait. 93This typically is the virtual address of some kernel data structure related 94to the resource for which the process is contending. 95The same identifier must be used in a call to 96.Fn wakeup 97to get the process going again. 98.Fa ident 99should not be 100.Dv NULL . 101.It Fa priority 102The process priority to be used when the process is awakened and put on 103the queue of runnable processes. 104This mechanism is used to optimize 105.Dq throughput 106of processes executing in kernel mode. 107If the flag 108.Dv PCATCH 109is OR'ed into 110.Fa priority 111the process checks for posted signals before and after sleeping. 112.It Fa wmesg 113A pointer to a character string indicating the reason a process is sleeping. 114The kernel does not use the string, but makes it available 115.Pq through the process structure field Li p_wmesg 116for user level utilities such as 117.Xr ps 1 . 118.It Fa timo 119If non-zero, the process will sleep for at most 120.Li timo/hz 121seconds. 122If this amount of time elapses and no 123.Fn wakeup "ident" 124has occurred, and no signal 125.Pq if Dv PCATCH No was set 126was posted, 127.Fn tsleep 128will return 129.Er EWOULDBLOCK . 130.El 131.Pp 132The 133.Fn msleep 134function behaves just like 135.Fn tsleep , 136but takes an additional argument: 137.Bl -tag -width priority 138.It Fa mtx 139A mutex that will be unlocked when the process is safely 140on the sleep queue. 141The mutex will be relocked at the end of msleep unless the 142.Dv PNORELOCK 143flag is set in the 144.Fa priority 145argument. 146.El 147.Pp 148The 149.Fn wakeup 150function will mark all processes which are currently sleeping on the identifier 151.Fa ident 152as runnable. 153Eventually, each of the processes will resume execution in the kernel 154context, causing a return from 155.Fn tsleep . 156Note that processes returning from sleep should always re-evaluate the 157conditions that blocked them, since a call to 158.Fn wakeup 159merely signals a 160.Em possible 161change to the blocking conditions. 162For example, when two or more processes are waiting for an exclusive lock, 163only one of them will succeed in acquiring the lock when it is released. 164All others will have to go back to sleep and wait for the next opportunity. 165.Pp 166The 167.Fn wakeup_n 168and 169.Fn wakeup_one 170functions behave similarly to 171.Fn wakeup 172except that only 173.Fa count 174or one process, respectively, is marked runnable. 175.Sh RETURN VALUES 176.Fn tsleep 177and 178.Fn msleep 179return 0 if they return as a result of a 180.Fn wakeup . 181If they return as a result of a signal, the return value is 182.Er ERESTART 183if the signal has the 184.Dv SA_RESTART 185property 186.Pq see Xr sigaction 2 , 187and 188.Er EINTR 189otherwise. 190If they return as a result of a timeout, the return value is 191.Er EWOULDBLOCK . 192.Sh CODE REFERENCES 193These functions are implemented in the file 194.Pa sys/kern/kern_synch.c . 195.Sh SEE ALSO 196.Xr hz 9 , 197.Xr mi_switch 9 , 198.Xr timeout 9 199