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-28 00:22:10 +00:00
|
|
|
.\" $FreeBSD$
|
2003-02-24 22:53:26 +00:00
|
|
|
.\"
|
2007-02-27 23:09:31 +00:00
|
|
|
.Dd February 27, 2007
|
1996-04-03 07:41:27 +00:00
|
|
|
.Os
|
|
|
|
.Dt SLEEP 9
|
|
|
|
.Sh NAME
|
2000-09-11 00:52:31 +00:00
|
|
|
.Nm msleep ,
|
2006-01-03 17:00:38 +00:00
|
|
|
.Nm msleep_spin ,
|
2007-02-23 16:22:09 +00:00
|
|
|
.Nm pause ,
|
1996-04-05 21:08:40 +00:00
|
|
|
.Nm tsleep ,
|
|
|
|
.Nm wakeup
|
1996-04-03 07:41:27 +00:00
|
|
|
.Nd wait for events
|
|
|
|
.Sh SYNOPSIS
|
2001-10-01 16:09:29 +00:00
|
|
|
.In sys/param.h
|
|
|
|
.In sys/systm.h
|
|
|
|
.In sys/proc.h
|
1996-04-03 07:41:27 +00:00
|
|
|
.Ft int
|
2006-01-03 17:00:38 +00:00
|
|
|
.Fn msleep "void *chan" "struct mtx *mtx" "int priority" "const char *wmesg" "int timo"
|
|
|
|
.Ft int
|
|
|
|
.Fn msleep_spin "void *chan" "struct mtx *mtx" "const char *wmesg" "int timo"
|
1996-04-03 07:41:27 +00:00
|
|
|
.Ft void
|
2007-02-23 16:22:09 +00:00
|
|
|
.Fn pause "const char *wmesg" "int timo"
|
|
|
|
.Ft int
|
|
|
|
.Fn tsleep "void *chan" "int priority" "const char *wmesg" "int timo"
|
|
|
|
.Ft void
|
2006-01-03 17:00:38 +00:00
|
|
|
.Fn wakeup "void *chan"
|
1997-04-09 05:39:32 +00:00
|
|
|
.Ft void
|
2006-01-03 17:00:38 +00:00
|
|
|
.Fn wakeup_one "void *chan"
|
1996-04-03 07:41:27 +00:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
The functions
|
2006-04-17 19:11:12 +00:00
|
|
|
.Fn tsleep ,
|
|
|
|
.Fn msleep ,
|
|
|
|
.Fn msleep_spin ,
|
2007-02-23 16:22:09 +00:00
|
|
|
.Fn pause ,
|
2006-04-17 19:11:12 +00:00
|
|
|
.Fn wakeup ,
|
1996-04-03 07:41:27 +00:00
|
|
|
and
|
2006-04-17 19:11:12 +00:00
|
|
|
.Fn wakeup_one
|
|
|
|
handle event-based thread blocking.
|
|
|
|
If a thread must wait for an
|
|
|
|
external event, it is put to sleep by
|
|
|
|
.Fn tsleep ,
|
|
|
|
.Fn msleep ,
|
2007-02-23 16:22:09 +00:00
|
|
|
.Fn msleep_spin ,
|
2006-04-17 19:11:12 +00:00
|
|
|
or
|
2007-02-23 16:22:09 +00:00
|
|
|
.Fn pause .
|
1996-04-03 07:41:27 +00:00
|
|
|
The parameter
|
2006-01-03 17:00:38 +00:00
|
|
|
.Fa chan
|
1996-04-03 07:41:27 +00:00
|
|
|
is an arbitrary address that uniquely identifies the event on which
|
2007-02-23 16:22:09 +00:00
|
|
|
the thread is being put to sleep.
|
2006-04-17 19:11:12 +00:00
|
|
|
All threads sleeping on a single
|
2006-01-03 17:00:38 +00:00
|
|
|
.Fa chan
|
1996-04-03 07:41:27 +00:00
|
|
|
are woken up later by
|
2003-02-24 22:53:26 +00:00
|
|
|
.Fn wakeup ,
|
1996-04-03 07:41:27 +00:00
|
|
|
often called from inside an interrupt routine, to indicate that the
|
2006-04-17 19:11:12 +00:00
|
|
|
resource the thread was blocking on is available now.
|
1996-04-03 07:41:27 +00:00
|
|
|
.Pp
|
|
|
|
The parameter
|
2003-05-31 14:07:25 +00:00
|
|
|
.Fa wmesg
|
1996-04-03 07:41:27 +00:00
|
|
|
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
|
2006-04-17 19:11:12 +00:00
|
|
|
.Fn msleep
|
2003-01-03 23:06:10 +00:00
|
|
|
function is the general sleep call.
|
2006-04-17 19:11:12 +00:00
|
|
|
It suspends the current thread until a wakeup is
|
2003-01-03 22:39:39 +00:00
|
|
|
performed on the specified identifier.
|
2006-04-17 19:11:12 +00:00
|
|
|
The
|
|
|
|
.Fa mtx
|
|
|
|
parameter is a mutex which will be released before sleeping and reacquired
|
|
|
|
before
|
|
|
|
.Fn msleep
|
|
|
|
returns.
|
|
|
|
If
|
|
|
|
.Fa priority
|
|
|
|
includes the
|
|
|
|
.Dv PDROP
|
|
|
|
flag, the
|
|
|
|
.Fa mtx
|
|
|
|
parameter will not be reacquired before returning.
|
|
|
|
The mutex is used to ensure that a condition can be checked atomically,
|
|
|
|
and that the current thread can be suspended without missing a
|
|
|
|
change to the condition, or an associated wakeup.
|
|
|
|
If
|
|
|
|
.Fa priority
|
|
|
|
is not 0,
|
|
|
|
then the thread will be made
|
1996-04-03 07:41:27 +00:00
|
|
|
runnable with the specified
|
2006-04-17 19:11:12 +00:00
|
|
|
.Fa priority
|
|
|
|
when it resumes.
|
|
|
|
If
|
2003-05-31 14:07:25 +00:00
|
|
|
.Fa timo
|
2006-04-17 19:11:12 +00:00
|
|
|
is not 0,
|
|
|
|
then the thread will sleep for at most
|
2006-09-18 15:24:20 +00:00
|
|
|
.Fa timo No / Va hz
|
|
|
|
seconds.
|
2005-01-07 02:41:45 +00:00
|
|
|
If the
|
|
|
|
.Va Giant
|
2006-02-22 20:50:33 +00:00
|
|
|
lock is not held and
|
|
|
|
.Fa mtx
|
|
|
|
is
|
|
|
|
.Dv NULL ,
|
|
|
|
then
|
2005-01-07 02:41:45 +00:00
|
|
|
.Fa timo
|
|
|
|
must be non-zero.
|
2003-01-03 22:39:39 +00:00
|
|
|
If
|
2004-02-17 13:31:36 +00:00
|
|
|
.Fa priority
|
1996-04-03 07:41:27 +00:00
|
|
|
includes the
|
1996-04-05 23:23:25 +00:00
|
|
|
.Dv PCATCH
|
2006-01-03 17:00:38 +00:00
|
|
|
flag, signals are checked before and after sleeping, otherwise signals are
|
2003-10-23 02:33:03 +00:00
|
|
|
not checked.
|
2006-04-17 19:11:12 +00:00
|
|
|
The
|
|
|
|
.Fn msleep
|
|
|
|
function returns 0 if awakened,
|
2000-11-22 16:11:48 +00:00
|
|
|
.Er EWOULDBLOCK
|
2003-01-03 22:39:39 +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,
|
2000-11-22 16:11:48 +00:00
|
|
|
.Er ERESTART
|
1996-04-03 07:41:27 +00:00
|
|
|
is returned if the current system call should be restarted if
|
|
|
|
possible, and
|
2000-11-22 16:11:48 +00:00
|
|
|
.Er EINTR
|
1996-04-03 07:41:27 +00:00
|
|
|
is returned if the system call should be interrupted by the signal
|
2001-08-07 15:48:51 +00:00
|
|
|
(return
|
|
|
|
.Er EINTR ) .
|
1996-04-03 07:41:27 +00:00
|
|
|
.Pp
|
2003-01-03 23:06:10 +00:00
|
|
|
The
|
2006-04-17 19:11:12 +00:00
|
|
|
.Fn tsleep
|
|
|
|
function is a variation on
|
|
|
|
.Fn msleep .
|
|
|
|
It is identical to invoking
|
2003-02-24 22:53:26 +00:00
|
|
|
.Fn msleep
|
2006-04-17 19:11:12 +00:00
|
|
|
with a
|
|
|
|
.Dv NULL
|
2003-05-31 14:07:25 +00:00
|
|
|
.Fa mtx
|
2006-04-17 19:11:12 +00:00
|
|
|
parameter.
|
2006-01-03 17:00:38 +00:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn msleep_spin
|
|
|
|
function is another variation on
|
|
|
|
.Fn msleep .
|
2006-04-17 19:11:12 +00:00
|
|
|
This function accepts a spin mutex rather than a default mutex for its
|
2006-01-03 17:00:38 +00:00
|
|
|
.Fa mtx
|
|
|
|
parameter.
|
|
|
|
It is also more limited in that it does not accept a
|
|
|
|
.Fa priority
|
|
|
|
parameter.
|
|
|
|
Thus, it will not change the priority of a sleeping thread,
|
|
|
|
and it does not support the
|
|
|
|
.Dv PDROP
|
|
|
|
and
|
|
|
|
.Dv PCATCH
|
|
|
|
flags.
|
2007-02-23 16:22:09 +00:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn pause
|
|
|
|
function is a wrapper around
|
|
|
|
.Fn tsleep
|
|
|
|
that suspends execution of the current thread for the indicated timeout.
|
|
|
|
The thread can not be awakened early by signals or calls to
|
|
|
|
.Fn wakeup
|
|
|
|
or
|
2007-02-27 16:21:01 +00:00
|
|
|
.Fn wakeup_one .
|
2007-03-04 23:42:58 +00:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn wakeup_one
|
|
|
|
function makes the first thread in the queue that is sleeping on the
|
|
|
|
parameter
|
|
|
|
.Fa chan
|
|
|
|
runnable.
|
|
|
|
This reduces the load when a large number of threads are sleeping on
|
|
|
|
the same address, but only one of them can actually do any useful work
|
|
|
|
when made runnable.
|
|
|
|
.Pp
|
|
|
|
Due to the way it works, the
|
|
|
|
.Fn wakeup_one
|
|
|
|
function requires that only related threads sleep on a specific
|
|
|
|
.Fa chan
|
|
|
|
address.
|
|
|
|
It is the programmer's responsibility to choose a unique
|
|
|
|
.Fa chan
|
|
|
|
value.
|
2007-03-05 00:27:30 +00:00
|
|
|
The older
|
|
|
|
.Fn wakeup
|
|
|
|
function did not require this, though it was never good practice to
|
|
|
|
for threads to share a
|
2007-03-04 23:42:58 +00:00
|
|
|
.Fa chan
|
|
|
|
value.
|
|
|
|
When converting from
|
|
|
|
.Fn wakeup
|
|
|
|
to
|
|
|
|
.Fn wakeup_one ,
|
2007-03-05 00:27:30 +00:00
|
|
|
pay particular attention to ensure that no other threads wait on the
|
|
|
|
same
|
2007-03-04 23:42:58 +00:00
|
|
|
.Fa chan .
|
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 ,
|
2000-08-15 15:14:13 +00:00
|
|
|
.Xr malloc 9 ,
|
|
|
|
.Xr mi_switch 9
|
1996-04-03 07:41:27 +00:00
|
|
|
.Sh HISTORY
|
2007-02-27 05:39:22 +00:00
|
|
|
The functions
|
|
|
|
.Fn sleep
|
|
|
|
and
|
|
|
|
.Fn wakeup
|
2007-02-27 16:21:01 +00:00
|
|
|
were present in
|
2007-02-27 05:39:22 +00:00
|
|
|
.At v1 .
|
|
|
|
They were probably also present in the preceding
|
2007-02-27 16:21:01 +00:00
|
|
|
PDP-7 version of
|
|
|
|
.Ux .
|
2007-02-27 05:39:22 +00:00
|
|
|
They were the basic process synchronization model.
|
1996-04-03 07:41:27 +00:00
|
|
|
.Pp
|
2003-01-03 22:37:10 +00:00
|
|
|
The
|
2003-02-24 22:53:26 +00:00
|
|
|
.Fn tsleep
|
2003-01-03 22:37:10 +00:00
|
|
|
function appeared in
|
2007-02-27 05:39:22 +00:00
|
|
|
.Bx 4.4
|
|
|
|
and added the parameters
|
|
|
|
.Fa wmesg
|
|
|
|
and
|
|
|
|
.Fa timo .
|
2006-01-03 17:00:38 +00:00
|
|
|
The
|
2007-02-27 23:09:31 +00:00
|
|
|
.Fn sleep
|
|
|
|
function was removed in
|
|
|
|
.Fx 2.2 .
|
|
|
|
The
|
2006-04-17 19:11:12 +00:00
|
|
|
.Fn wakeup_one
|
|
|
|
function appeared in
|
|
|
|
.Fx 2.2 .
|
|
|
|
The
|
2006-01-03 17:00:38 +00:00
|
|
|
.Fn msleep
|
|
|
|
function appeared in
|
|
|
|
.Fx 5.0 ,
|
|
|
|
and the
|
|
|
|
.Fn msleep_spin
|
|
|
|
function appeared in
|
2007-02-23 16:22:09 +00:00
|
|
|
.Fx 6.2 .
|
2007-02-27 16:21:01 +00:00
|
|
|
The
|
2007-02-23 16:22:09 +00:00
|
|
|
.Fn pause
|
|
|
|
function appeared in
|
2006-01-03 17:00:38 +00:00
|
|
|
.Fx 7.0 .
|
1996-04-03 07:41:27 +00:00
|
|
|
.Sh AUTHORS
|
2000-11-22 09:35:58 +00:00
|
|
|
.An -nosplit
|
2005-06-28 20:15:19 +00:00
|
|
|
This manual page was written by
|
2003-01-03 22:37:10 +00:00
|
|
|
.An J\(:org Wunsch Aq joerg@FreeBSD.org .
|