1.\" $OpenBSD: tsleep.9,v 1.6 2010/04/08 00:16:28 tedu 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: April 8 2010 $ 32.Dt TSLEEP 9 33.Os 34.Sh NAME 35.Nm tsleep , 36.Nm msleep , 37.Nm wakeup 38.Nd process context sleep and wakeup 39.Sh SYNOPSIS 40.Fd #include <sys/param.h> 41.Fd #include <sys/systm.h> 42.Ft int 43.Fn "tsleep" "void *ident" "int priority" "const char *wmesg" "int timo" 44.Ft int 45.Fn "msleep" "void *ident" "struct mutex *mtx" "int priority" "const char *wmesg" "int timo" 46.Ft void 47.Fn "wakeup" "void *ident" 48.Sh DESCRIPTION 49These functions implement voluntary context switching. 50.Fn tsleep 51and 52.Fn msleep 53are used throughout the kernel whenever processing in the current context 54cannot continue for any of the following reasons: 55.Bl -bullet -offset indent 56.It 57The current process needs to await the results of a pending I/O operation. 58.It 59The current process needs resources 60.Pq e.g. memory 61which are temporarily unavailable. 62.It 63The current process wants access to data structures which are locked by 64other processes. 65.El 66.Pp 67The function 68.Fn wakeup 69is used to notify sleeping processes of possible changes to the condition 70that caused them to go to sleep. 71Typically, an awakened process will -- after it has acquired a context 72again -- retry the action that blocked its operation to see if the 73.Dq blocking 74condition has cleared. 75.Pp 76The 77.Fn tsleep 78function takes the following arguments: 79.Bl -tag -width priority 80.It Fa ident 81An identifier of the 82.Dq wait channel 83representing the resource for which the current process needs to wait. 84This typically is the virtual address of some kernel data structure related 85to the resource for which the process is contending. 86The same identifier must be used in a call to 87.Fn wakeup 88to get the process going again. 89.Fa ident 90should not be 91.Dv NULL . 92.It Fa priority 93The process priority to be used when the process is awakened and put on 94the queue of runnable processes. 95This mechanism is used to optimize 96.Dq throughput 97of processes executing in kernel mode. 98If the flag 99.Dv PCATCH 100is OR'ed into 101.Fa priority 102the process checks for posted signals before and after sleeping. 103.It Fa wmesg 104A pointer to a character string indicating the reason a process is sleeping. 105The kernel does not use the string, but makes it available 106.Pq through the process structure field Li p_wmesg 107for user level utilities such as 108.Xr ps 1 . 109.It Fa timo 110If non-zero, the process will sleep for at most 111.Li timo/hz 112seconds. 113If this amount of time elapses and no 114.Fn wakeup "ident" 115has occurred, and no signal 116.Pq if Dv PCATCH No was set 117was posted, 118.Fn tsleep 119will return 120.Er EWOULDBLOCK . 121.El 122.Pp 123The 124.Fn msleep 125function behaves just like 126.Fn tsleep , 127but takes an additional argument: 128.Bl -tag -width priority 129.It Fa mtx 130A mutex that will be unlocked when the process is safely 131on the sleep queue. 132The mutex will be relocked at the end of msleep unless the 133.Dv PNORELOCK 134flag is set in the 135.Fa priority 136argument. 137.El 138.Pp 139The 140.Fn wakeup 141function will mark all processes which are currently sleeping on the identifier 142.Fa ident 143as runnable. 144Eventually, each of the processes will resume execution in the kernel 145context, causing a return from 146.Fn tsleep . 147Note that processes returning from sleep should always re-evaluate the 148conditions that blocked them, since a call to 149.Fn wakeup 150merely signals a 151.Em possible 152change to the blocking conditions. 153For example, when two or more processes are waiting for an exclusive lock, 154only one of them will succeed in acquiring the lock when it is released. 155All others will have to go back to sleep and wait for the next opportunity. 156.Sh RETURN VALUES 157.Fn tsleep 158and 159.Fn msleep 160return 0 if they return as a result of a 161.Fn wakeup . 162If they return as a result of a signal, the return value is 163.Er ERESTART 164if the signal has the 165.Dv SA_RESTART 166property 167.Pq see Xr sigaction 2 , 168and 169.Er EINTR 170otherwise. 171If they return as a result of a timeout, the return value is 172.Er EWOULDBLOCK . 173.Sh CODE REFERENCES 174These functions are implemented in the file 175.Pa sys/kern/kern_synch.c . 176.Sh SEE ALSO 177.Xr hz 9 , 178.Xr mi_switch 9 , 179.Xr timeout 9 180