2001-06-21 15:59:00 +04:00
|
|
|
.\" $NetBSD: sleep.9,v 1.13 2001/06/21 11:59:01 wiz Exp $
|
1996-06-23 18:04:43 +04:00
|
|
|
.\"
|
|
|
|
.\" Copyright (c) 1996 The NetBSD Foundation, Inc.
|
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
.\" by Paul Kranenburg.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\" 3. All advertising materials mentioning features or use of this software
|
|
|
|
.\" must display the following acknowledgement:
|
|
|
|
.\" This product includes software developed by the NetBSD
|
|
|
|
.\" Foundation, Inc. and its contributors.
|
|
|
|
.\" 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
.\" contributors may be used to endorse or promote products derived
|
|
|
|
.\" from this software without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
1997-10-09 01:59:52 +04:00
|
|
|
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
1996-06-23 18:04:43 +04:00
|
|
|
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
.\" POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
.\"
|
|
|
|
.Dd June 23, 1996
|
|
|
|
.Dt SLEEP 9
|
1999-03-16 03:40:46 +03:00
|
|
|
.Os
|
1996-06-23 18:04:43 +04:00
|
|
|
.Sh NAME
|
2001-04-27 11:54:23 +04:00
|
|
|
.Nm ltsleep ,
|
1997-11-05 15:13:44 +03:00
|
|
|
.Nm sleep ,
|
|
|
|
.Nm tsleep ,
|
|
|
|
.Nm wakeup
|
|
|
|
.Nd process context sleep and and wakeup
|
1996-06-23 18:04:43 +04:00
|
|
|
.Sh SYNOPSIS
|
|
|
|
.Ft int
|
2001-04-27 11:54:23 +04:00
|
|
|
.Fn "ltsleep" "void *ident" "int priority" "const char *wmesg" "int timo" "__volatile struct simplelock *slock"
|
|
|
|
.Ft int
|
1999-01-16 19:39:52 +03:00
|
|
|
.Fn "tsleep" "void *ident" "int priority" "const char *wmesg" "int timo"
|
1996-06-23 18:04:43 +04:00
|
|
|
.Ft void
|
1996-10-30 08:32:10 +03:00
|
|
|
.Fn "sleep" "void *ident" "int priority"
|
1996-06-23 18:04:43 +04:00
|
|
|
.Ft void
|
|
|
|
.Fn "wakeup" "void *ident"
|
|
|
|
.Sh DESCRIPTION
|
|
|
|
These functions implement voluntary context switching.
|
2001-04-27 11:54:23 +04:00
|
|
|
.Fn ltsleep ,
|
1996-06-23 18:04:43 +04:00
|
|
|
.Fn tsleep
|
|
|
|
and
|
|
|
|
.Fn sleep
|
|
|
|
are used throughout the kernel whenever processing in the current context
|
|
|
|
can not continue for any of the following reasons:
|
|
|
|
.Bl -bullet -offset indent
|
|
|
|
.It
|
|
|
|
The current process needs to await the results of a pending I/O operation.
|
|
|
|
.It
|
|
|
|
The current process needs resources
|
|
|
|
.Pq e.g. memory
|
|
|
|
which are temporarily unavailable.
|
|
|
|
.It
|
|
|
|
The current process wants access to data-structures which are locked by
|
|
|
|
other processes.
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The function
|
|
|
|
.Fn wakeup
|
|
|
|
is used to notify sleeping processes of possible changes to the condition
|
1999-03-07 01:09:29 +03:00
|
|
|
that caused them to go to sleep.
|
|
|
|
Typically, an awakened process will -- after it has acquired a context
|
|
|
|
again -- retry the action that blocked its operation to see if the
|
1996-06-23 18:04:43 +04:00
|
|
|
.Dq blocking
|
|
|
|
condition has cleared.
|
|
|
|
.Pp
|
|
|
|
The
|
2001-04-27 11:54:23 +04:00
|
|
|
.Fn ltsleep
|
1996-06-23 18:04:43 +04:00
|
|
|
function takes the following arguments:
|
|
|
|
.Bl -tag -width priority
|
1996-10-30 08:32:10 +03:00
|
|
|
.It Fa ident
|
1996-06-23 18:04:43 +04:00
|
|
|
An identifier of the
|
|
|
|
.Dq wait channel
|
1999-03-07 01:09:29 +03:00
|
|
|
representing the resource for which the current process needs to wait.
|
|
|
|
This typically is the virtual address of some kernel data-structure related
|
|
|
|
to the resource for which the process is contending.
|
|
|
|
The same identifier must be used in a call to
|
1996-06-23 18:04:43 +04:00
|
|
|
.Fn wakeup
|
|
|
|
to get the process going again.
|
|
|
|
.Fa ident
|
|
|
|
should not be
|
|
|
|
.Dv NULL .
|
|
|
|
.It Fa priority
|
|
|
|
The process priority to be used when the process is awakened and put on
|
1999-03-07 01:09:29 +03:00
|
|
|
the queue of runnable processes.
|
|
|
|
This mechanism is used to optimize
|
1996-06-23 18:04:43 +04:00
|
|
|
.Dq throughput
|
1999-03-07 01:09:29 +03:00
|
|
|
of processes executing in kernel mode.
|
|
|
|
If the flag
|
1996-06-23 18:04:43 +04:00
|
|
|
.Dv PCATCH
|
|
|
|
is OR'ed into
|
|
|
|
.Fa priority
|
|
|
|
the process checks for posted signals before and after sleeping.
|
2001-04-27 11:54:23 +04:00
|
|
|
If the flag
|
|
|
|
.Dv PNORELOCK
|
|
|
|
is OR'ed into
|
|
|
|
.Fa priority ,
|
|
|
|
.Fa slock
|
|
|
|
is NOT re-locked after process resume.
|
1996-06-23 18:04:43 +04:00
|
|
|
.It Fa wmesg
|
|
|
|
A pointer to a character string indicating the reason a process is sleeping.
|
|
|
|
The kernel does not use the string, but makes it available
|
|
|
|
.Pq through the process structure field Li p_wmesg
|
|
|
|
for user level utilities such as
|
|
|
|
.Xr ps 1 .
|
|
|
|
.It Fa timo
|
|
|
|
If non-zero, the process will sleep for at most
|
|
|
|
.Li timo/hz
|
1999-03-07 01:09:29 +03:00
|
|
|
seconds.
|
|
|
|
If this amount of time elapses and no
|
1996-06-23 18:04:43 +04:00
|
|
|
.Fn wakeup "ident"
|
1997-02-06 08:05:21 +03:00
|
|
|
has occurred, and no signal
|
|
|
|
.Pq if Dv PCATCH No was set
|
1996-06-23 18:04:43 +04:00
|
|
|
was posted,
|
|
|
|
.Fn tsleep
|
|
|
|
will return
|
|
|
|
.Er EWOULDBLOCK .
|
2001-04-27 11:54:23 +04:00
|
|
|
.It Fa slock
|
|
|
|
If not NULL, the
|
|
|
|
.Fa slock
|
|
|
|
interlock is unlocked once the scheduler lock is acquired. Unless
|
2001-06-21 15:59:00 +04:00
|
|
|
.Dv PNORELOCK
|
|
|
|
was set,
|
2001-04-27 11:54:23 +04:00
|
|
|
.Fa slock
|
|
|
|
is locked again once
|
|
|
|
the process is resumed from sleep. This provides wakeup-before-sleep
|
|
|
|
condition protection facility.
|
1996-06-23 18:04:43 +04:00
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The
|
2001-04-27 11:54:23 +04:00
|
|
|
.Fn tsleep
|
|
|
|
function is functionally equivalent to:
|
|
|
|
.Bd -literal -offset indent
|
|
|
|
ltsleep(ident, priority, wmesg, timo, NULL)
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
The
|
1996-06-23 18:04:43 +04:00
|
|
|
.Fn sleep
|
2001-06-21 15:59:00 +04:00
|
|
|
function puts the process in an uninterruptible sleep.
|
1999-03-07 01:09:29 +03:00
|
|
|
It is functionally equivalent to:
|
1996-06-23 18:04:43 +04:00
|
|
|
.Bd -literal -offset indent
|
2001-04-27 11:54:23 +04:00
|
|
|
ltsleep(ident, priority & PRIMASK, 0, 0, NULL)
|
1996-06-23 18:04:43 +04:00
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn wakeup
|
|
|
|
function will mark all processes which are currently sleeping on the identifier
|
|
|
|
.Fa ident
|
1999-03-07 01:09:29 +03:00
|
|
|
as runnable.
|
|
|
|
Eventually, each of the processes will resume execution in the kernel
|
|
|
|
context, causing a return from
|
1996-06-23 18:04:43 +04:00
|
|
|
.Fn [t]sleep .
|
|
|
|
Note that processes returning from sleep should always re-evaluate the
|
|
|
|
conditions that blocked them, since a call to
|
|
|
|
.Fn wakeup
|
|
|
|
merely signals a
|
|
|
|
.Em possible
|
1999-03-07 01:09:29 +03:00
|
|
|
change to the blocking conditions.
|
|
|
|
For example, when two or more processes are waiting for an exclusive lock,
|
|
|
|
only one of them will succeed in acquiring the lock when it is released.
|
|
|
|
All others will have to go back to sleep and wait for the next opportunity.
|
1996-06-23 18:04:43 +04:00
|
|
|
.Sh RETURN VALUES
|
2001-04-27 11:54:23 +04:00
|
|
|
.Fn ltsleep
|
1997-02-06 08:05:21 +03:00
|
|
|
returns 0 if it returns as a result of a
|
1996-06-23 18:04:43 +04:00
|
|
|
.Fn wakeup .
|
|
|
|
If a
|
2001-04-27 11:54:23 +04:00
|
|
|
.Fn ltsleep
|
1996-06-23 18:04:43 +04:00
|
|
|
returns as a result of a signal, the return value is
|
|
|
|
.Er ERESTART
|
|
|
|
if the signal has the
|
|
|
|
.Dv SA_RESTART
|
1997-02-06 08:05:21 +03:00
|
|
|
property
|
|
|
|
.Pq see Xr sigaction 2 ,
|
|
|
|
and
|
1996-06-23 18:04:43 +04:00
|
|
|
.Er EINTR
|
|
|
|
otherwise.
|
|
|
|
If
|
2001-04-27 11:54:23 +04:00
|
|
|
.Fn ltsleep
|
1997-02-06 08:05:21 +03:00
|
|
|
returns because of a timeout it returns
|
|
|
|
.Er EWOULDBLOCK .
|
2001-04-27 11:54:23 +04:00
|
|
|
.Sh HISTORY
|
|
|
|
.Fn ltsleep
|
|
|
|
appeared in
|
|
|
|
.Nx 1.5 .
|