freebsd-skq/share/man/man9/sleep.9
1998-12-21 10:34:53 +00:00

217 lines
6.6 KiB
Groff

.\"
.\" Copyright (c) 1996 Joerg Wunsch
.\"
.\" All rights reserved.
.\"
.\" 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.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR 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.
.\"
.\" $Id: sleep.9,v 1.11 1998/12/21 10:29:28 dillon Exp $
.\" "
.Dd December 17, 1998
.Os
.Dt SLEEP 9
.Sh NAME
.Nm sleep ,
.Nm tsleep ,
.Nm asleep ,
.Nm await ,
.Nm wakeup
.Nd wait for events
.Sh SYNOPSIS
.Fd #include <sys/param.h>
.Fd #include <sys/systm.h>
.Fd #include <sys/proc.h>
.Ft int
.Fn tsleep "void *ident" "int priority" "const char *wmesg" "int timo"
.Ft int
.Fn asleep "void *ident" "int priority" "const char *wmesg" "int timo"
.Ft int
.Fn await "int priority" "int timo"
.Ft void
.Fn wakeup "void *ident"
.Ft void
.Fn wakeup_one "void *ident"
.Sh DESCRIPTION
The functions
.Fn tsleep
and
.Fn wakeup
handle event-based process blocking. If a process must wait for an
external event, it is put on sleep by
.Nm tsleep .
The parameter
.Ar ident
is an arbitrary address that uniquely identifies the event on which
the process is being asleep. All processes sleeping on a single
.Ar ident
are woken up later by
.Nm wakeup ,
often called from inside an interrupt routine, to indicate that the
resource the process was blocking on is available now.
.Pp
The parameter
.Ar wmesg
is a string describing the sleep condition for tools like
.Xr ps 1 .
Due to the limited space of those programs to display arbitrary strings,
this message should not be longer than 6 characters.
.Pp
The
.Fn wakeup_one
function is used to make the first process in the queue that is
sleeping on the parameter
.Fa ident
runnable. This can prevent the system from becoming saturated
when a large number of processes are sleeping on the same address,
but only one of them can actually do any useful work when made
runnable.
.Pp
.Nm Tsleep
is the general sleep call. Suspends the current process until a wakeup is
performed on the specified identifier. The process will then be made
runnable with the specified
.Ar priority .
Sleeps at most
.Ar timo
\&/ hz seconds (0 means no timeout). If
.Ar pri
includes the
.Dv PCATCH
flag, signals are checked before and after sleeping, else signals are
not checked. Returns 0 if awakened,
.Dv EWOULDBLOCK
if the timeout expires. If
.Dv PCATCH
is set and a signal needs to be delivered,
.Dv ERESTART
is returned if the current system call should be restarted if
possible, and
.Dv EINTR
is returned if the system call should be interrupted by the signal
.Pq return Dv EINTR .
.Pp
.Nm Sleep
is the traditional form. It doesn't let you specify a timeout nor a
.Ar wmesg ,
hence its use is deprecated.
.Pp
.Nm Asleep
implements the new asynchronous sleep function. It takes the same arguments
as
.Fn tsleep
and places the process on the appropriate wait queue, but
.Fn asleep
leaves the process runnable and returns immediately. The caller is then
expected to, at some point in the future, call
.Fn await
to actually wait for the previously queued wait condition.
If
.Fn asleep
is called several times, only the most recent call is effective.
.Fn asleep
may be called with an
.Ar ident
value of NULL
to remove any previously queued condition.
.Pp
.Nm Await
implements the new asynchronous wait function. If you
.Fn asleep
on an identifier,
.Fn await
will actually block the process until someone calls
.Fn wakeup
on that identifier. If someone calls
.Fn wakeup
after you
.Fn asleep
but before you
.Fn await
then the
.Fn await
call is effectively a NOP.
If
.Fn await
is called multiple times without an intervening
.Fn asleep
the
.Fn await
is effective a NOP, but will call
.Fn mswitch
for safety. The
.Fn await
function allows you to override the priority and timeout values to be used.
If the value -1 is specified for an argument, the value is taken from the
previous
.Fn asleep
call. If you pass -1 for the priority you must be prepared to catch signal
conditions if the prior call to
.Fn asleep
specified it in its priority. If you pass -1 for the timeout you must be
prepared to catch a timeout condition if the prior call to
.Fn asleep
specified a timeout. When you use -1, you should generally not make
assumptions as to the arguments used by the prior
.Fn asleep
call.
.Pp
The
.Fn asleep
and
.Fn await
functions are used by the kernel code for various purposes but the main one is
to allow complex interlocking code to 'backout' of a temporary resource failure
(such as lack of memory or trying to access a block that is not in the buffer
cache) in order to release major locks prior to blocking, and to then retry
the call that failed on wakeup. This involves subroutines deep in the kernel
calling
.Fn asleep
and returning a temporary failure, then popping back up through a number
of call levels before calling
.Fn await ,
then retrying. The kernel might also use these functions to avoid using
spinlocks in a check-condition interlock. That is, in case the case where
the kernel wishes to check the condition of something and then block on it.
To avoid the race between the check and the blocking, the kernel can first
check the condition, then call
.Fn asleep ,
then check the condition a second time before calling
.Fn await .
The overlap makes the race condition impossible.
.Sh RETURN VALUES
See above.
.Sh SEE ALSO
.Xr ps 1
.Sh HISTORY
The sleep/wakeup process synchronization mechanism is very old. It
appeared in a very early version of Unix.
.Pp
.Nm Tsleep
appeared in
.Bx 4.4 .
.Pp
.Nm Asleep/await
first appeared in FreeBSD-3.0.1
.Sh AUTHORS
This man page has been written by
.ie t J\(:org Wunsch.
.el Joerg Wunsch. asleep/await portions were written by Matt Dillon