1996-04-03 07:41:27 +00:00
|
|
|
.\"
|
|
|
|
.\" 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.
|
|
|
|
.\"
|
1999-08-15 10:48:36 +00:00
|
|
|
.\" $Id: sleep.9,v 1.14 1999/02/01 22:55:54 mpp Exp $
|
1996-04-03 07:41:27 +00:00
|
|
|
.\" "
|
1998-12-21 10:29:28 +00:00
|
|
|
.Dd December 17, 1998
|
1996-04-03 07:41:27 +00:00
|
|
|
.Os
|
|
|
|
.Dt SLEEP 9
|
|
|
|
.Sh NAME
|
1996-04-05 21:08:40 +00:00
|
|
|
.Nm sleep ,
|
|
|
|
.Nm tsleep ,
|
1998-12-21 10:29:28 +00:00
|
|
|
.Nm asleep ,
|
|
|
|
.Nm await ,
|
1996-04-05 21:08:40 +00:00
|
|
|
.Nm wakeup
|
1996-04-03 07:41:27 +00:00
|
|
|
.Nd wait for events
|
|
|
|
.Sh SYNOPSIS
|
|
|
|
.Fd #include <sys/param.h>
|
|
|
|
.Fd #include <sys/systm.h>
|
|
|
|
.Fd #include <sys/proc.h>
|
|
|
|
.Ft int
|
1998-01-16 18:12:57 +00:00
|
|
|
.Fn tsleep "void *ident" "int priority" "const char *wmesg" "int timo"
|
1998-12-21 10:29:28 +00:00
|
|
|
.Ft int
|
|
|
|
.Fn asleep "void *ident" "int priority" "const char *wmesg" "int timo"
|
|
|
|
.Ft int
|
|
|
|
.Fn await "int priority" "int timo"
|
1996-04-03 07:41:27 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn wakeup "void *ident"
|
1997-04-09 05:39:32 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn wakeup_one "void *ident"
|
1996-04-03 07:41:27 +00:00
|
|
|
.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
|
1996-04-06 13:33:23 +00:00
|
|
|
the process is being asleep. All processes sleeping on a single
|
1996-04-03 07:41:27 +00:00
|
|
|
.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
|
1997-04-09 05:39:32 +00:00
|
|
|
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
|
1996-04-03 07:41:27 +00:00
|
|
|
.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
|
1996-04-05 23:23:25 +00:00
|
|
|
.Dv PCATCH
|
1996-04-03 07:41:27 +00:00
|
|
|
flag, signals are checked before and after sleeping, else signals are
|
|
|
|
not checked. Returns 0 if awakened,
|
1996-04-05 23:23:25 +00:00
|
|
|
.Dv EWOULDBLOCK
|
1996-04-03 07:41:27 +00:00
|
|
|
if the timeout expires. If
|
1996-04-05 23:23:25 +00:00
|
|
|
.Dv PCATCH
|
1996-04-03 07:41:27 +00:00
|
|
|
is set and a signal needs to be delivered,
|
1996-04-05 23:23:25 +00:00
|
|
|
.Dv ERESTART
|
1996-04-03 07:41:27 +00:00
|
|
|
is returned if the current system call should be restarted if
|
|
|
|
possible, and
|
1996-04-05 23:23:25 +00:00
|
|
|
.Dv EINTR
|
1996-04-03 07:41:27 +00:00
|
|
|
is returned if the system call should be interrupted by the signal
|
1996-04-05 23:23:25 +00:00
|
|
|
.Pq return Dv EINTR .
|
1996-04-03 07:41:27 +00:00
|
|
|
.Pp
|
|
|
|
.Nm Sleep
|
1996-04-07 08:37:54 +00:00
|
|
|
is the traditional form. It doesn't let you specify a timeout nor a
|
1996-04-03 07:41:27 +00:00
|
|
|
.Ar wmesg ,
|
|
|
|
hence its use is deprecated.
|
1998-12-21 10:29:28 +00:00
|
|
|
.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
|
1998-12-23 00:24:59 +00:00
|
|
|
implements the new asynchronous wait function. When
|
1998-12-21 10:29:28 +00:00
|
|
|
.Fn asleep
|
1998-12-23 00:24:59 +00:00
|
|
|
is called on an identifier it associates the process with that
|
|
|
|
identifier but does not block.
|
1998-12-21 10:29:28 +00:00
|
|
|
.Fn await
|
1998-12-23 00:24:59 +00:00
|
|
|
will actually block the process until
|
1998-12-21 10:29:28 +00:00
|
|
|
.Fn wakeup
|
1998-12-23 00:24:59 +00:00
|
|
|
is called on that identifier any time after the
|
|
|
|
.Fn asleep .
|
|
|
|
If
|
1998-12-21 10:29:28 +00:00
|
|
|
.Fn wakeup
|
1998-12-23 00:24:59 +00:00
|
|
|
is called after you
|
1998-12-21 10:29:28 +00:00
|
|
|
.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
|
1998-12-23 00:24:59 +00:00
|
|
|
.Fn asleep ,
|
1998-12-21 10:29:28 +00:00
|
|
|
the
|
|
|
|
.Fn await
|
1998-12-23 00:24:59 +00:00
|
|
|
is effectively a NOP but will also call
|
1998-12-21 10:29:28 +00:00
|
|
|
.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
|
1998-12-23 00:24:59 +00:00
|
|
|
call. If -1 is passed for the priority you must be prepared to catch signal
|
1998-12-21 10:29:28 +00:00
|
|
|
conditions if the prior call to
|
|
|
|
.Fn asleep
|
1998-12-23 00:24:59 +00:00
|
|
|
specified it in its priority. If -1 is passed for the timeout you must be
|
1998-12-21 10:29:28 +00:00
|
|
|
prepared to catch a timeout condition if the prior call to
|
|
|
|
.Fn asleep
|
1998-12-23 00:24:59 +00:00
|
|
|
specified a timeout. When you use -1, it is usually a good idea to not make
|
1998-12-21 10:29:28 +00:00
|
|
|
assumptions as to the arguments used by the prior
|
|
|
|
.Fn asleep
|
|
|
|
call.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn asleep
|
|
|
|
and
|
|
|
|
.Fn await
|
1998-12-23 00:24:59 +00:00
|
|
|
functions are mainly used by the kernel to shift the burden of blocking
|
|
|
|
away from extremely low level routines and to push it onto their callers.
|
|
|
|
This in turn allows more complex interlocking code to
|
|
|
|
.Em backout
|
|
|
|
of a temporary resource failure
|
|
|
|
(such as lack of memory) in order to release major locks prior to actually
|
|
|
|
blocking, and to then retry the operation on wakeup. This key feature is
|
|
|
|
expected to be heavily used in SMP situations in order to allow code to make
|
|
|
|
better use of spinlocks. A spinlock, by its very nature, cannot be used
|
|
|
|
around code that might block. It is hoped that these capabilities will
|
|
|
|
make it easier to migrate the SMP master locks deeper into the kernel.
|
|
|
|
.Pp
|
|
|
|
These routines may also be used to avoid nasty spl*() calls to get around
|
|
|
|
race conditions with simple conditional test/wait interlocks. You simple
|
|
|
|
call
|
|
|
|
.Fn asleep
|
1999-02-01 22:55:54 +00:00
|
|
|
prior to your test, then conditionally
|
1998-12-23 00:24:59 +00:00
|
|
|
.Fn await
|
|
|
|
only if the test fails. It is usually a good idea to cancel an
|
1998-12-21 10:29:28 +00:00
|
|
|
.Fn asleep
|
1998-12-23 00:24:59 +00:00
|
|
|
if you wind up never calling the related
|
1998-12-21 10:29:28 +00:00
|
|
|
.Fn await ,
|
1998-12-23 00:24:59 +00:00
|
|
|
but it is not required. If you do not want to waste cpu calling
|
|
|
|
.Fn asleep
|
|
|
|
unnecessarily, you can surround the whole thing with a second test. The
|
|
|
|
race condition is still handled by the inside
|
|
|
|
.Fn asleep
|
|
|
|
call.
|
1996-04-03 07:41:27 +00:00
|
|
|
.Sh RETURN VALUES
|
|
|
|
See above.
|
|
|
|
.Sh SEE ALSO
|
1998-12-23 00:24:59 +00:00
|
|
|
.Xr ps 1 ,
|
|
|
|
.Xr malloc 9
|
1996-04-03 07:41:27 +00:00
|
|
|
.Sh HISTORY
|
1996-04-07 08:37:54 +00:00
|
|
|
The sleep/wakeup process synchronization mechanism is very old. It
|
1996-04-03 07:41:27 +00:00
|
|
|
appeared in a very early version of Unix.
|
|
|
|
.Pp
|
|
|
|
.Nm Tsleep
|
|
|
|
appeared in
|
|
|
|
.Bx 4.4 .
|
1998-12-21 10:34:53 +00:00
|
|
|
.Pp
|
|
|
|
.Nm Asleep/await
|
1999-08-15 10:48:36 +00:00
|
|
|
first appeared in
|
|
|
|
.Fx 3.0
|
|
|
|
and is designed to shift the burden of blocking
|
1998-12-23 00:24:59 +00:00
|
|
|
away from extremely low level routines and push it up to their callers.
|
1996-04-03 07:41:27 +00:00
|
|
|
.Sh AUTHORS
|
|
|
|
This man page has been written by
|
|
|
|
.ie t J\(:org Wunsch.
|
1998-12-23 00:24:59 +00:00
|
|
|
.el Joerg Wunsch.
|
1999-08-15 10:48:36 +00:00
|
|
|
asleep/await designed and written by
|
|
|
|
.An Matthew Dillon.
|