1997-09-21 22:12:19 +00:00
|
|
|
.\" $NetBSD: timeout.9,v 1.2 1996/06/23 22:32:34 pk Exp $
|
1996-04-13 16:59:07 +00:00
|
|
|
.\"
|
1997-09-21 22:12:19 +00:00
|
|
|
.\" Copyright (c) 1996 The NetBSD Foundation, Inc.
|
1996-04-13 16:59:07 +00:00
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
1997-09-21 22:12:19 +00:00
|
|
|
.\" This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
.\" by Paul Kranenburg.
|
|
|
|
.\"
|
1996-04-13 16:59:07 +00:00
|
|
|
.\" 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.
|
1997-09-21 22:12:19 +00:00
|
|
|
.\" 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.
|
1996-04-13 16:59:07 +00:00
|
|
|
.\"
|
1997-09-21 22:12:19 +00:00
|
|
|
.\" 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
|
|
|
|
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS 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.
|
1996-04-13 16:59:07 +00:00
|
|
|
.\"
|
1999-08-28 00:22:10 +00:00
|
|
|
.\" $FreeBSD$
|
1998-01-16 18:45:52 +00:00
|
|
|
.\"
|
2008-08-02 17:42:38 +00:00
|
|
|
.Dd August 2, 2008
|
1996-04-13 16:59:07 +00:00
|
|
|
.Dt TIMEOUT 9
|
2001-07-10 15:31:11 +00:00
|
|
|
.Os
|
1996-04-13 16:59:07 +00:00
|
|
|
.Sh NAME
|
2000-08-16 19:53:38 +00:00
|
|
|
.Nm timeout ,
|
|
|
|
.Nm untimeout ,
|
|
|
|
.Nm callout_handle_init ,
|
|
|
|
.Nm callout_init ,
|
2005-02-07 02:47:33 +00:00
|
|
|
.Nm callout_init_mtx ,
|
2007-11-20 12:21:36 +00:00
|
|
|
.Nm callout_init_rw ,
|
2000-08-16 19:53:38 +00:00
|
|
|
.Nm callout_stop ,
|
2004-04-06 23:08:49 +00:00
|
|
|
.Nm callout_drain ,
|
2005-01-23 17:42:48 +00:00
|
|
|
.Nm callout_reset ,
|
2008-08-02 17:42:38 +00:00
|
|
|
.Nm callout_schedule ,
|
2005-01-23 17:42:48 +00:00
|
|
|
.Nm callout_pending ,
|
|
|
|
.Nm callout_active ,
|
|
|
|
.Nm callout_deactivate
|
1997-09-21 22:12:19 +00:00
|
|
|
.Nd execute a function after a specified length of time
|
1996-04-13 16:59:07 +00:00
|
|
|
.Sh SYNOPSIS
|
2001-10-01 16:09:29 +00:00
|
|
|
.In sys/types.h
|
|
|
|
.In sys/systm.h
|
2000-12-29 09:18:45 +00:00
|
|
|
.Pp
|
|
|
|
.Bd -literal
|
|
|
|
typedef void timeout_t (void *);
|
|
|
|
.Ed
|
1997-09-21 22:12:19 +00:00
|
|
|
.Ft struct callout_handle
|
1998-01-16 18:45:52 +00:00
|
|
|
.Fn timeout "timeout_t *func" "void *arg" "int ticks"
|
1996-04-13 16:59:07 +00:00
|
|
|
.Ft void
|
1999-12-27 16:22:20 +00:00
|
|
|
.Fn callout_handle_init "struct callout_handle *handle"
|
2000-12-29 09:18:45 +00:00
|
|
|
.Pp
|
|
|
|
.Bd -literal
|
|
|
|
struct callout_handle handle = CALLOUT_HANDLE_INITIALIZER(&handle)
|
|
|
|
.Ed
|
1996-04-13 16:59:07 +00:00
|
|
|
.Ft void
|
1998-01-16 18:45:52 +00:00
|
|
|
.Fn untimeout "timeout_t *func" "void *arg" "struct callout_handle handle"
|
1999-12-27 16:22:20 +00:00
|
|
|
.Ft void
|
2001-01-31 12:17:45 +00:00
|
|
|
.Fn callout_init "struct callout *c" "int mpsafe"
|
2005-02-07 02:47:33 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn callout_init_mtx "struct callout *c" "struct mtx *mtx" "int flags"
|
2007-11-20 12:21:36 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn callout_init_rw "struct callout *c" "struct rwlock *rw" "int flags"
|
2001-08-23 23:52:59 +00:00
|
|
|
.Ft int
|
1999-12-27 16:22:20 +00:00
|
|
|
.Fn callout_stop "struct callout *c"
|
2004-04-06 23:08:49 +00:00
|
|
|
.Ft int
|
|
|
|
.Fn callout_drain "struct callout *c"
|
2005-09-08 14:20:39 +00:00
|
|
|
.Ft int
|
1999-12-27 16:22:20 +00:00
|
|
|
.Fn callout_reset "struct callout *c" "int ticks" "timeout_t *func" "void *arg"
|
2005-01-23 17:42:48 +00:00
|
|
|
.Ft int
|
2008-08-02 17:42:38 +00:00
|
|
|
.Fn callout_schedule "struct callout *c" "int ticks"
|
|
|
|
.Ft int
|
2004-12-29 08:01:24 +00:00
|
|
|
.Fn callout_pending "struct callout *c"
|
2005-01-23 17:42:48 +00:00
|
|
|
.Ft int
|
|
|
|
.Fn callout_active "struct callout *c"
|
|
|
|
.Fn callout_deactivate "struct callout *c"
|
1996-04-13 16:59:07 +00:00
|
|
|
.Sh DESCRIPTION
|
1997-09-21 22:12:19 +00:00
|
|
|
The function
|
|
|
|
.Fn timeout
|
|
|
|
schedules a call to the function given by the argument
|
|
|
|
.Fa func
|
|
|
|
to take place after
|
|
|
|
.Fa ticks Ns No /hz
|
1999-12-27 16:22:20 +00:00
|
|
|
seconds.
|
|
|
|
Non-positive values of
|
1997-09-21 22:12:19 +00:00
|
|
|
.Fa ticks
|
|
|
|
are silently converted to the value
|
|
|
|
.Sq 1 .
|
|
|
|
.Fa func
|
|
|
|
should be a pointer to a function that takes a
|
|
|
|
.Fa void *
|
|
|
|
argument.
|
|
|
|
Upon invocation,
|
|
|
|
.Fa func
|
|
|
|
will receive
|
|
|
|
.Fa arg
|
1999-12-27 16:22:20 +00:00
|
|
|
as its only argument.
|
1997-09-21 22:12:19 +00:00
|
|
|
The return value from
|
|
|
|
.Fn timeout
|
|
|
|
is a
|
|
|
|
.Ft struct callout_handle
|
2001-07-14 19:41:16 +00:00
|
|
|
which can be used in conjunction with the
|
1997-09-21 22:12:19 +00:00
|
|
|
.Fn untimeout
|
|
|
|
function to request that a scheduled timeout be canceled.
|
2003-10-01 21:32:42 +00:00
|
|
|
The
|
|
|
|
.Fn timeout
|
2004-06-16 08:33:57 +00:00
|
|
|
call is the old style and new code should use the
|
|
|
|
.Fn callout_*
|
|
|
|
functions.
|
1996-04-13 16:59:07 +00:00
|
|
|
.Pp
|
1997-09-21 22:12:19 +00:00
|
|
|
The function
|
|
|
|
.Fn callout_handle_init
|
|
|
|
can be used to initialize a handle to a state which will cause
|
2005-12-01 19:16:35 +00:00
|
|
|
any calls to
|
|
|
|
.Fn untimeout
|
|
|
|
with that handle to return with no side
|
1997-09-21 22:12:19 +00:00
|
|
|
effects.
|
|
|
|
.Pp
|
|
|
|
Assigning a callout handle the value of
|
|
|
|
.Fn CALLOUT_HANDLE_INITIALIZER
|
|
|
|
performs the same function as
|
|
|
|
.Fn callout_handle_init
|
|
|
|
and is provided for use on statically declared or global callout handles.
|
|
|
|
.Pp
|
|
|
|
The function
|
|
|
|
.Fn untimeout
|
|
|
|
cancels the timeout associated with
|
|
|
|
.Fa handle
|
|
|
|
using the
|
|
|
|
.Fa func
|
1996-04-13 16:59:07 +00:00
|
|
|
and
|
1997-09-21 22:12:19 +00:00
|
|
|
.Fa arg
|
|
|
|
arguments to validate the handle.
|
|
|
|
If the handle does not correspond to a timeout with
|
|
|
|
the function
|
|
|
|
.Fa func
|
|
|
|
taking the argument
|
|
|
|
.Fa arg
|
|
|
|
no action is taken.
|
|
|
|
.Fa handle
|
|
|
|
must be initialized by a previous call to
|
|
|
|
.Fn timeout ,
|
|
|
|
.Fn callout_handle_init ,
|
|
|
|
or assigned the value of
|
|
|
|
.Fn CALLOUT_HANDLE_INITIALIZER "&handle"
|
|
|
|
before being passed to
|
|
|
|
.Fn untimeout .
|
2005-12-01 19:16:35 +00:00
|
|
|
The behavior of calling
|
|
|
|
.Fn untimeout
|
|
|
|
with an uninitialized handle
|
1997-09-21 22:12:19 +00:00
|
|
|
is undefined.
|
2003-10-01 21:32:42 +00:00
|
|
|
The
|
|
|
|
.Fn untimeout
|
2004-06-16 08:33:57 +00:00
|
|
|
call is the old style and new code should use the
|
|
|
|
.Fn callout_*
|
|
|
|
functions.
|
1996-04-13 16:59:07 +00:00
|
|
|
.Pp
|
1997-09-21 22:12:19 +00:00
|
|
|
As handles are recycled by the system, it is possible (although unlikely)
|
|
|
|
that a handle from one invocation of
|
|
|
|
.Fn timeout
|
|
|
|
may match the handle of another invocation of
|
|
|
|
.Fn timeout
|
|
|
|
if both calls used the same function pointer and argument, and the first
|
|
|
|
timeout is expired or canceled before the second call.
|
|
|
|
The timeout facility offers O(1) running time for
|
1996-04-13 16:59:07 +00:00
|
|
|
.Fn timeout
|
|
|
|
and
|
1997-09-21 22:12:19 +00:00
|
|
|
.Fn untimeout .
|
|
|
|
Timeouts are executed from
|
|
|
|
.Fn softclock
|
2005-01-23 17:42:48 +00:00
|
|
|
with the
|
|
|
|
.Va Giant
|
|
|
|
lock held.
|
1999-12-27 16:22:20 +00:00
|
|
|
Thus they are protected from re-entrancy.
|
|
|
|
.Pp
|
|
|
|
The functions
|
|
|
|
.Fn callout_init ,
|
2005-02-07 02:47:33 +00:00
|
|
|
.Fn callout_init_mtx ,
|
2007-11-20 12:21:36 +00:00
|
|
|
.Fn callout_init_rw ,
|
2004-04-06 23:08:49 +00:00
|
|
|
.Fn callout_stop ,
|
2008-08-02 17:42:38 +00:00
|
|
|
.Fn callout_drain ,
|
1999-12-27 16:22:20 +00:00
|
|
|
.Fn callout_reset
|
2008-08-02 17:42:38 +00:00
|
|
|
and
|
|
|
|
.Fn callout_schedule
|
2001-07-14 19:41:16 +00:00
|
|
|
are low-level routines for clients who wish to allocate their own
|
1999-12-27 16:22:20 +00:00
|
|
|
callout structures.
|
|
|
|
.Pp
|
|
|
|
The function
|
|
|
|
.Fn callout_init
|
|
|
|
initializes a callout so it can be passed to
|
2004-04-06 23:08:49 +00:00
|
|
|
.Fn callout_stop ,
|
2008-08-02 17:42:38 +00:00
|
|
|
.Fn callout_drain ,
|
|
|
|
.Fn callout_reset
|
1999-12-27 16:22:20 +00:00
|
|
|
or
|
2008-08-02 17:42:38 +00:00
|
|
|
.Fn callout_schedule
|
1999-12-27 16:22:20 +00:00
|
|
|
without any side effects.
|
2001-01-31 12:17:45 +00:00
|
|
|
If the
|
|
|
|
.Fa mpsafe
|
|
|
|
argument is zero,
|
|
|
|
the callout structure is not considered to be
|
|
|
|
.Dq multi-processor safe ;
|
|
|
|
that is,
|
|
|
|
the Giant lock will be acquired before calling the callout function,
|
|
|
|
and released when the callout function returns.
|
1999-12-27 16:22:20 +00:00
|
|
|
.Pp
|
2005-02-07 02:47:33 +00:00
|
|
|
The
|
|
|
|
.Fn callout_init_mtx
|
|
|
|
function may be used as an alternative to
|
|
|
|
.Fn callout_init .
|
|
|
|
The parameter
|
|
|
|
.Fa mtx
|
|
|
|
specifies a mutex that is to be acquired by the callout subsystem
|
|
|
|
before calling the callout function, and released when the callout
|
|
|
|
function returns.
|
|
|
|
The following
|
|
|
|
.Fa flags
|
|
|
|
may be specified:
|
2005-06-15 13:31:23 +00:00
|
|
|
.Bl -tag -width ".Dv CALLOUT_RETURNUNLOCKED"
|
2005-02-07 02:47:33 +00:00
|
|
|
.It Dv CALLOUT_RETURNUNLOCKED
|
|
|
|
The callout function will release
|
|
|
|
.Fa mtx
|
|
|
|
itself, so the callout subsystem should not attempt to unlock it
|
|
|
|
after the callout function returns.
|
|
|
|
.El
|
|
|
|
.Pp
|
2007-11-20 12:21:36 +00:00
|
|
|
The
|
|
|
|
.Fn callout_init_rw
|
|
|
|
function serves the need of using rwlocks in conujunction with callouts.
|
|
|
|
The function does basically the same as
|
|
|
|
.Fn callout_init_mtx
|
|
|
|
with the possibility of specifying an extra
|
|
|
|
.Fa rw
|
|
|
|
argument.
|
|
|
|
The usable lock classes are currently limited to mutexes and rwlocks,
|
|
|
|
because callout handlers run in softclock swi, so they cannot sleep nor
|
|
|
|
acquire sleepable locks like sx or lockmgr.
|
|
|
|
The following
|
|
|
|
.Fa flags
|
|
|
|
may be specified:
|
|
|
|
.Bl -tag -width ".Dv CALLOUT_SHAREDLOCK"
|
|
|
|
.It Dv CALLOUT_SHAREDLOCK
|
|
|
|
The lock is only acquired in read mode when running the callout handler.
|
|
|
|
It has no effects when used in conjuction with
|
|
|
|
.Fa mtx .
|
|
|
|
.El
|
|
|
|
.Pp
|
1999-12-27 16:22:20 +00:00
|
|
|
The function
|
|
|
|
.Fn callout_stop
|
|
|
|
cancels a callout if it is currently pending.
|
2001-08-23 23:52:59 +00:00
|
|
|
If the callout is pending, then
|
|
|
|
.Fn callout_stop
|
|
|
|
will return a non-zero value.
|
2005-01-23 17:42:48 +00:00
|
|
|
If the callout is not set, has already been serviced or is currently
|
|
|
|
being serviced, then zero will be returned.
|
2005-02-07 02:47:33 +00:00
|
|
|
If the callout has an associated mutex, then that mutex must be
|
|
|
|
held when this function is called.
|
1999-12-27 16:22:20 +00:00
|
|
|
.Pp
|
|
|
|
The function
|
2004-04-06 23:08:49 +00:00
|
|
|
.Fn callout_drain
|
|
|
|
is identical to
|
|
|
|
.Fn callout_stop
|
|
|
|
except that it will wait for the callout to be completed if it is
|
2004-05-05 10:47:19 +00:00
|
|
|
already in progress.
|
|
|
|
This function MUST NOT be called while holding any
|
2004-04-06 23:08:49 +00:00
|
|
|
locks on which the callout might block, or deadlock will result.
|
2005-01-23 17:42:48 +00:00
|
|
|
Note that if the callout subsystem has already begun processing this
|
|
|
|
callout, then the callout function may be invoked during the execution of
|
|
|
|
.Fn callout_drain .
|
|
|
|
However, the callout subsystem does guarantee that the callout will be
|
|
|
|
fully stopped before
|
|
|
|
.Fn callout_drain
|
|
|
|
returns.
|
2004-04-06 23:08:49 +00:00
|
|
|
.Pp
|
|
|
|
The function
|
1999-12-27 16:22:20 +00:00
|
|
|
.Fn callout_reset
|
2005-01-28 21:18:14 +00:00
|
|
|
first performs the equivalent of
|
1999-12-27 16:22:20 +00:00
|
|
|
.Fn callout_stop
|
|
|
|
to disestablish the callout, and then establishes a new callout in the
|
2001-07-14 19:41:16 +00:00
|
|
|
same manner as
|
1999-12-27 16:22:20 +00:00
|
|
|
.Fn timeout .
|
2005-09-08 14:20:39 +00:00
|
|
|
If there was already a pending callout and it was rescheduled, then
|
|
|
|
.Fn callout_reset
|
|
|
|
will return a non-zero value.
|
2005-02-07 02:47:33 +00:00
|
|
|
If the callout has an associated mutex, then that mutex must be
|
|
|
|
held when this function is called.
|
2008-08-02 17:42:38 +00:00
|
|
|
The function
|
|
|
|
.Fn callout_schedule
|
|
|
|
(re)schedules an existing callout for a new period of time;
|
|
|
|
it is equivalent to calling
|
|
|
|
.Fn callout_reset
|
|
|
|
with the
|
|
|
|
.Fa func
|
|
|
|
and
|
|
|
|
.Fa arg
|
|
|
|
parameters extracted from the callout structure (though possibly with
|
|
|
|
lower overhead).
|
2004-12-29 08:01:24 +00:00
|
|
|
.Pp
|
2005-01-23 17:42:48 +00:00
|
|
|
The macros
|
|
|
|
.Fn callout_pending ,
|
|
|
|
.Fn callout_active
|
|
|
|
and
|
|
|
|
.Fn callout_deactivate
|
|
|
|
provide access to the current state of the callout.
|
|
|
|
Careful use of these macros can avoid many of the race conditions
|
|
|
|
that are inherent in asynchronous timer facilities; see
|
|
|
|
.Sx "Avoiding Race Conditions"
|
|
|
|
below for further details.
|
|
|
|
The
|
|
|
|
.Fn callout_pending
|
|
|
|
macro checks whether a callout is
|
|
|
|
.Em pending ;
|
|
|
|
a callout is considered
|
|
|
|
.Em pending
|
|
|
|
when a timeout has been set but the time has not yet arrived.
|
|
|
|
Note that once the timeout time arrives and the callout subsystem
|
|
|
|
starts to process this callout,
|
|
|
|
.Fn callout_pending
|
|
|
|
will return
|
|
|
|
.Dv FALSE
|
|
|
|
even though the callout function may not have finished (or even begun)
|
|
|
|
executing.
|
|
|
|
The
|
|
|
|
.Fn callout_active
|
|
|
|
macro checks whether a callout is marked as
|
|
|
|
.Em active ,
|
|
|
|
and the
|
|
|
|
.Fn callout_deactivate
|
|
|
|
macro clears the callout's
|
|
|
|
.Em active
|
|
|
|
flag.
|
|
|
|
The callout subsystem marks a callout as
|
|
|
|
.Em active
|
|
|
|
when a timeout is set and it clears the
|
|
|
|
.Em active
|
|
|
|
flag in
|
|
|
|
.Fn callout_stop
|
|
|
|
and
|
|
|
|
.Fn callout_drain ,
|
|
|
|
but it
|
|
|
|
.Em does not
|
|
|
|
clear it when a callout expires normally via the execution of the
|
|
|
|
callout function.
|
|
|
|
.Ss "Avoiding Race Conditions"
|
|
|
|
The callout subsystem invokes callout functions from its own timer
|
|
|
|
context.
|
|
|
|
Without some kind of synchronization it is possible that a callout
|
|
|
|
function will be invoked concurrently with an attempt to stop or reset
|
|
|
|
the callout by another thread.
|
|
|
|
In particular, since callout functions typically acquire a mutex as
|
|
|
|
their first action, the callout function may have already been invoked,
|
|
|
|
but be blocked waiting for that mutex at the time that another thread
|
|
|
|
tries to reset or stop the callout.
|
|
|
|
.Pp
|
|
|
|
The callout subsystem provides a number of mechanisms to address these
|
|
|
|
synchronization concerns:
|
2005-01-28 21:18:14 +00:00
|
|
|
.Bl -enum -offset indent
|
2005-01-23 17:42:48 +00:00
|
|
|
.It
|
2005-02-07 02:47:33 +00:00
|
|
|
If the callout has an associated mutex that was specified using the
|
|
|
|
.Fn callout_init_mtx
|
|
|
|
function (or implicitly specified as the
|
|
|
|
.Va Giant
|
|
|
|
mutex using
|
|
|
|
.Fn callout_init
|
|
|
|
with
|
|
|
|
.Fa mpsafe
|
|
|
|
set to
|
|
|
|
.Dv FALSE ) ,
|
|
|
|
then this mutex is used to avoid the race conditions.
|
|
|
|
The associated mutex must be acquired by the caller before calling
|
|
|
|
.Fn callout_stop
|
|
|
|
or
|
|
|
|
.Fn callout_reset
|
|
|
|
and it is guaranteed that the callout will be correctly stopped
|
|
|
|
or reset as expected.
|
|
|
|
Note that it is still necessary to use
|
|
|
|
.Fn callout_drain
|
|
|
|
before destroying the callout or its associated mutex.
|
|
|
|
.It
|
2005-01-23 17:42:48 +00:00
|
|
|
The return value from
|
|
|
|
.Fn callout_stop
|
2005-09-08 14:20:39 +00:00
|
|
|
and
|
|
|
|
.Fn callout_reset
|
2005-01-23 17:42:48 +00:00
|
|
|
indicates whether or not the callout was removed.
|
|
|
|
If it is known that the callout was set and the callout function has
|
|
|
|
not yet executed, then a return value of
|
|
|
|
.Dv FALSE
|
|
|
|
indicates that the callout function is about to be called.
|
|
|
|
For example:
|
|
|
|
.Bd -literal -offset indent
|
|
|
|
if (sc->sc_flags & SCFLG_CALLOUT_RUNNING) {
|
|
|
|
if (callout_stop(&sc->sc_callout)) {
|
|
|
|
sc->sc_flags &= ~SCFLG_CALLOUT_RUNNING;
|
|
|
|
/* successfully stopped */
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* callout has expired and callout
|
|
|
|
* function is about to be executed
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.Ed
|
|
|
|
.It
|
|
|
|
The
|
|
|
|
.Fn callout_pending ,
|
|
|
|
.Fn callout_active
|
|
|
|
and
|
|
|
|
.Fn callout_deactivate
|
|
|
|
macros can be used together to work around the race conditions.
|
|
|
|
When a callout's timeout is set, the callout subsystem marks the
|
|
|
|
callout as both
|
|
|
|
.Em active
|
|
|
|
and
|
|
|
|
.Em pending .
|
|
|
|
When the timeout time arrives, the callout subsystem begins processing
|
|
|
|
the callout by first clearing the
|
|
|
|
.Em pending
|
|
|
|
flag.
|
|
|
|
It then invokes the callout function without changing the
|
|
|
|
.Em active
|
|
|
|
flag, and does not clear the
|
|
|
|
.Em active
|
|
|
|
flag even after the callout function returns.
|
|
|
|
The mechanism described here requires the callout function itself to
|
|
|
|
clear the
|
2005-01-28 21:18:14 +00:00
|
|
|
.Em active
|
2005-01-23 17:42:48 +00:00
|
|
|
flag using the
|
|
|
|
.Fn callout_deactivate
|
|
|
|
macro.
|
|
|
|
The
|
|
|
|
.Fn callout_stop
|
|
|
|
and
|
|
|
|
.Fn callout_drain
|
|
|
|
functions always clear both the
|
2005-01-28 21:18:14 +00:00
|
|
|
.Em active
|
2005-01-23 17:42:48 +00:00
|
|
|
and
|
2005-01-28 21:18:14 +00:00
|
|
|
.Em pending
|
2005-01-23 17:42:48 +00:00
|
|
|
flags before returning.
|
|
|
|
.Pp
|
|
|
|
The callout function should first check the
|
|
|
|
.Em pending
|
|
|
|
flag and return without action if
|
2004-12-29 08:01:24 +00:00
|
|
|
.Fn callout_pending
|
2005-01-23 17:42:48 +00:00
|
|
|
returns
|
|
|
|
.Dv TRUE .
|
|
|
|
This indicates that the callout was rescheduled using
|
|
|
|
.Fn callout_reset
|
|
|
|
just before the callout function was invoked.
|
|
|
|
If
|
|
|
|
.Fn callout_active
|
|
|
|
returns
|
|
|
|
.Dv FALSE
|
|
|
|
then the callout function should also return without action.
|
|
|
|
This indicates that the callout has been stopped.
|
|
|
|
Finally, the callout function should call
|
|
|
|
.Fn callout_deactivate
|
|
|
|
to clear the
|
|
|
|
.Em active
|
|
|
|
flag.
|
|
|
|
For example:
|
|
|
|
.Bd -literal -offset indent
|
|
|
|
mtx_lock(&sc->sc_mtx);
|
|
|
|
if (callout_pending(&sc->sc_callout)) {
|
|
|
|
/* callout was reset */
|
|
|
|
mtx_unlock(&sc->sc_mtx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!callout_active(&sc->sc_callout)) {
|
|
|
|
/* callout was stopped */
|
|
|
|
mtx_unlock(&sc->sc_mtx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
callout_deactivate(&sc->sc_callout);
|
|
|
|
/* rest of callout function */
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
Together with appropriate synchronization, such as the mutex used above,
|
|
|
|
this approach permits the
|
|
|
|
.Fn callout_stop
|
|
|
|
and
|
|
|
|
.Fn callout_reset
|
|
|
|
functions to be used at any time without races.
|
|
|
|
For example:
|
|
|
|
.Bd -literal -offset indent
|
|
|
|
mtx_lock(&sc->sc_mtx);
|
|
|
|
callout_stop(&sc->sc_callout);
|
|
|
|
/* The callout is effectively stopped now. */
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
If the callout is still pending then these functions operate normally,
|
|
|
|
but if processing of the callout has already begun then the tests in
|
|
|
|
the callout function cause it to return without further action.
|
|
|
|
Synchronization between the callout function and other code ensures that
|
|
|
|
stopping or resetting the callout will never be attempted while the
|
|
|
|
callout function is past the
|
|
|
|
.Fn callout_deactivate
|
|
|
|
call.
|
|
|
|
.Pp
|
|
|
|
The above technique additionally ensures that the
|
|
|
|
.Em active
|
|
|
|
flag always reflects whether the callout is effectively enabled or
|
|
|
|
disabled.
|
|
|
|
If
|
|
|
|
.Fn callout_active
|
|
|
|
returns false, then the callout is effectively disabled, since even if
|
|
|
|
the callout subsystem is actually just about to invoke the callout
|
|
|
|
function, the callout function will return without action.
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
There is one final race condition that must be considered when a
|
|
|
|
callout is being stopped for the last time.
|
|
|
|
In this case it may not be safe to let the callout function itself
|
|
|
|
detect that the callout was stopped, since it may need to access
|
|
|
|
data objects that have already been destroyed or recycled.
|
|
|
|
To ensure that the callout is completely finished, a call to
|
|
|
|
.Fn callout_drain
|
|
|
|
should be used.
|
2001-08-23 23:52:59 +00:00
|
|
|
.Sh RETURN VALUES
|
|
|
|
The
|
|
|
|
.Fn timeout
|
|
|
|
function returns a
|
|
|
|
.Ft struct callout_handle
|
|
|
|
that can be passed to
|
|
|
|
.Fn untimeout .
|
|
|
|
The
|
|
|
|
.Fn callout_stop
|
2004-04-06 23:08:49 +00:00
|
|
|
and
|
|
|
|
.Fn callout_drain
|
|
|
|
functions return non-zero if the callout was still pending when it was
|
2003-10-01 21:32:42 +00:00
|
|
|
called or zero otherwise.
|
1996-04-13 16:59:07 +00:00
|
|
|
.Sh HISTORY
|
1997-09-21 22:12:19 +00:00
|
|
|
The current timeout and untimeout routines are based on the work of
|
2000-11-14 11:20:58 +00:00
|
|
|
.An Adam M. Costello
|
|
|
|
and
|
|
|
|
.An George Varghese ,
|
|
|
|
published in a technical report entitled
|
1997-09-21 22:12:19 +00:00
|
|
|
.%T "Redesigning the BSD Callout and Timer Facilities"
|
2000-11-14 11:20:58 +00:00
|
|
|
and modified slightly for inclusion in
|
|
|
|
.Fx
|
|
|
|
by
|
|
|
|
.An Justin T. Gibbs .
|
1997-09-21 22:12:19 +00:00
|
|
|
The original work on the data structures used in this implementation
|
2000-11-14 11:20:58 +00:00
|
|
|
was published by
|
|
|
|
.An G. Varghese
|
|
|
|
and
|
|
|
|
.An A. Lauck
|
|
|
|
in the paper
|
2001-07-14 19:41:16 +00:00
|
|
|
.%T "Hashed and Hierarchical Timing Wheels: Data Structures for the Efficient Implementation of a Timer Facility"
|
1997-09-21 22:12:19 +00:00
|
|
|
in the
|
|
|
|
.%B "Proceedings of the 11th ACM Annual Symposium on Operating Systems Principles" .
|
2001-08-14 10:01:54 +00:00
|
|
|
The current implementation replaces the long standing
|
|
|
|
.Bx
|
|
|
|
linked list
|
1997-09-21 22:12:19 +00:00
|
|
|
callout mechanism which offered O(n) insertion and removal running time
|
|
|
|
but did not generate or require handles for untimeout operations.
|