kern_timeout.c revision 69147
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1991, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes * (c) UNIX System Laboratories, Inc.
51541Srgrimes * All or some portions of this file are derived from material licensed
61541Srgrimes * to the University of California by American Telephone and Telegraph
71541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with
81541Srgrimes * the permission of UNIX System Laboratories, Inc.
91541Srgrimes *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 3. All advertising materials mentioning features or use of this software
191541Srgrimes *    must display the following acknowledgement:
201541Srgrimes *	This product includes software developed by the University of
211541Srgrimes *	California, Berkeley and its contributors.
221541Srgrimes * 4. Neither the name of the University nor the names of its contributors
231541Srgrimes *    may be used to endorse or promote products derived from this software
241541Srgrimes *    without specific prior written permission.
251541Srgrimes *
261541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361541Srgrimes * SUCH DAMAGE.
371541Srgrimes *
3844510Swollman *	From: @(#)kern_clock.c	8.5 (Berkeley) 1/21/94
3950477Speter * $FreeBSD: head/sys/kern/kern_timeout.c 69147 2000-11-25 06:22:16Z jlemon $
401541Srgrimes */
411541Srgrimes
421541Srgrimes#include <sys/param.h>
431541Srgrimes#include <sys/systm.h>
4433392Sphk#include <sys/callout.h>
451541Srgrimes#include <sys/kernel.h>
4668840Sjhb#include <sys/mutex.h>
471541Srgrimes
4833392Sphk/*
4933392Sphk * TODO:
5033392Sphk *	allocate more timeout table slots when table overflows.
5133392Sphk */
5233392Sphk
5333392Sphk/* Exported to machdep.c and/or kern_clock.c.  */
5429680Sgibbsstruct callout *callout;
5529680Sgibbsstruct callout_list callfree;
5629680Sgibbsint callwheelsize, callwheelbits, callwheelmask;
5729680Sgibbsstruct callout_tailq *callwheel;
5833392Sphkint softticks;			/* Like ticks, but for softclock(). */
5968889Sjakestruct mtx callout_lock;
602112Swollman
6129680Sgibbsstatic struct callout *nextsoftcheck;	/* Next callout to be checked. */
621541Srgrimes
631541Srgrimes/*
6429680Sgibbs * The callout mechanism is based on the work of Adam M. Costello and
6529680Sgibbs * George Varghese, published in a technical report entitled "Redesigning
6629680Sgibbs * the BSD Callout and Timer Facilities" and modified slightly for inclusion
6729680Sgibbs * in FreeBSD by Justin T. Gibbs.  The original work on the data structures
6829680Sgibbs * used in this implementation was published by G.Varghese and A. Lauck in
6929680Sgibbs * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
7029680Sgibbs * the Efficient Implementation of a Timer Facility" in the Proceedings of
7129680Sgibbs * the 11th ACM Annual Symposium on Operating Systems Principles,
7229680Sgibbs * Austin, Texas Nov 1987.
7329680Sgibbs */
7432388Sphk
7529680Sgibbs/*
761541Srgrimes * Software (low priority) clock interrupt.
771541Srgrimes * Run periodic events from timeout queue.
781541Srgrimes */
791541Srgrimesvoid
8067551Sjhbsoftclock(void *dummy)
811541Srgrimes{
821541Srgrimes	register struct callout *c;
8329805Sgibbs	register struct callout_tailq *bucket;
841541Srgrimes	register int s;
8529805Sgibbs	register int curticks;
8633392Sphk	register int steps;	/* #steps since we last allowed interrupts */
871541Srgrimes
8833392Sphk#ifndef MAX_SOFTCLOCK_STEPS
8933392Sphk#define MAX_SOFTCLOCK_STEPS 100 /* Maximum allowed value of steps. */
9033392Sphk#endif /* MAX_SOFTCLOCK_STEPS */
9129680Sgibbs
9229680Sgibbs	steps = 0;
931541Srgrimes	s = splhigh();
9468889Sjake	mtx_enter(&callout_lock, MTX_SPIN);
9529680Sgibbs	while (softticks != ticks) {
9629805Sgibbs		softticks++;
9729805Sgibbs		/*
9829805Sgibbs		 * softticks may be modified by hard clock, so cache
9929805Sgibbs		 * it while we work on a given bucket.
10029805Sgibbs		 */
10129805Sgibbs		curticks = softticks;
10229805Sgibbs		bucket = &callwheel[curticks & callwheelmask];
10329805Sgibbs		c = TAILQ_FIRST(bucket);
10429680Sgibbs		while (c) {
10529805Sgibbs			if (c->c_time != curticks) {
10629680Sgibbs				c = TAILQ_NEXT(c, c_links.tqe);
10729680Sgibbs				++steps;
10829680Sgibbs				if (steps >= MAX_SOFTCLOCK_STEPS) {
10929680Sgibbs					nextsoftcheck = c;
11029805Sgibbs					/* Give interrupts a chance. */
11168889Sjake					mtx_exit(&callout_lock, MTX_SPIN);
11229680Sgibbs					splx(s);
11329680Sgibbs					s = splhigh();
11468889Sjake					mtx_enter(&callout_lock, MTX_SPIN);
11529680Sgibbs					c = nextsoftcheck;
11629680Sgibbs					steps = 0;
11729680Sgibbs				}
11829680Sgibbs			} else {
11929680Sgibbs				void (*c_func)(void *);
12029680Sgibbs				void *c_arg;
12168889Sjake				int c_flags;
12229680Sgibbs
12329680Sgibbs				nextsoftcheck = TAILQ_NEXT(c, c_links.tqe);
12429805Sgibbs				TAILQ_REMOVE(bucket, c, c_links.tqe);
12529680Sgibbs				c_func = c->c_func;
12629680Sgibbs				c_arg = c->c_arg;
12768889Sjake				c_flags = c->c_flags;
12829680Sgibbs				c->c_func = NULL;
12944510Swollman				if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
13044510Swollman					c->c_flags = CALLOUT_LOCAL_ALLOC;
13144510Swollman					SLIST_INSERT_HEAD(&callfree, c,
13244510Swollman							  c_links.sle);
13344510Swollman				} else {
13444510Swollman					c->c_flags =
13550673Sjlemon					    (c->c_flags & ~CALLOUT_PENDING);
13644510Swollman				}
13768889Sjake				mtx_exit(&callout_lock, MTX_SPIN);
13868889Sjake				if (!(c_flags & CALLOUT_MPSAFE))
13968889Sjake					mtx_enter(&Giant, MTX_DEF);
14029680Sgibbs				splx(s);
14129680Sgibbs				c_func(c_arg);
14229680Sgibbs				s = splhigh();
14368889Sjake				if (!(c_flags & CALLOUT_MPSAFE))
14468889Sjake					mtx_exit(&Giant, MTX_DEF);
14568889Sjake				mtx_enter(&callout_lock, MTX_SPIN);
14629680Sgibbs				steps = 0;
14729680Sgibbs				c = nextsoftcheck;
14829680Sgibbs			}
14929680Sgibbs		}
1501541Srgrimes	}
15129680Sgibbs	nextsoftcheck = NULL;
15268889Sjake	mtx_exit(&callout_lock, MTX_SPIN);
1531541Srgrimes	splx(s);
1541541Srgrimes}
1551541Srgrimes
1561541Srgrimes/*
1571541Srgrimes * timeout --
1581541Srgrimes *	Execute a function after a specified length of time.
1591541Srgrimes *
1601541Srgrimes * untimeout --
1611541Srgrimes *	Cancel previous timeout function call.
1621541Srgrimes *
16329680Sgibbs * callout_handle_init --
16429680Sgibbs *	Initialize a handle so that using it with untimeout is benign.
16529680Sgibbs *
1661541Srgrimes *	See AT&T BCI Driver Reference Manual for specification.  This
16729680Sgibbs *	implementation differs from that one in that although an
16829680Sgibbs *	identification value is returned from timeout, the original
16929680Sgibbs *	arguments to timeout as well as the identifier are used to
17029680Sgibbs *	identify entries for untimeout.
1711541Srgrimes */
17229680Sgibbsstruct callout_handle
17329680Sgibbstimeout(ftn, arg, to_ticks)
17433824Sbde	timeout_t *ftn;
1751541Srgrimes	void *arg;
17669147Sjlemon	int to_ticks;
1771541Srgrimes{
17829680Sgibbs	int s;
17929680Sgibbs	struct callout *new;
18029680Sgibbs	struct callout_handle handle;
1811541Srgrimes
1821541Srgrimes	s = splhigh();
18368889Sjake	mtx_enter(&callout_lock, MTX_SPIN);
1841541Srgrimes
1851541Srgrimes	/* Fill in the next free callout structure. */
18629680Sgibbs	new = SLIST_FIRST(&callfree);
18729680Sgibbs	if (new == NULL)
18829680Sgibbs		/* XXX Attempt to malloc first */
1891541Srgrimes		panic("timeout table full");
19029680Sgibbs	SLIST_REMOVE_HEAD(&callfree, c_links.sle);
19144510Swollman
19244510Swollman	callout_reset(new, to_ticks, ftn, arg);
1931541Srgrimes
19444510Swollman	handle.callout = new;
19568889Sjake	mtx_exit(&callout_lock, MTX_SPIN);
1961541Srgrimes	splx(s);
19729680Sgibbs	return (handle);
1981541Srgrimes}
1991541Srgrimes
2001541Srgrimesvoid
20129680Sgibbsuntimeout(ftn, arg, handle)
20233824Sbde	timeout_t *ftn;
2031541Srgrimes	void *arg;
20429680Sgibbs	struct callout_handle handle;
2051541Srgrimes{
2061541Srgrimes	register int s;
2071541Srgrimes
20829680Sgibbs	/*
20929680Sgibbs	 * Check for a handle that was initialized
21029680Sgibbs	 * by callout_handle_init, but never used
21129680Sgibbs	 * for a real timeout.
21229680Sgibbs	 */
21329680Sgibbs	if (handle.callout == NULL)
21429680Sgibbs		return;
21529680Sgibbs
2161541Srgrimes	s = splhigh();
21768889Sjake	mtx_enter(&callout_lock, MTX_SPIN);
21844510Swollman	if (handle.callout->c_func == ftn && handle.callout->c_arg == arg)
21944510Swollman		callout_stop(handle.callout);
22068889Sjake	mtx_exit(&callout_lock, MTX_SPIN);
2211541Srgrimes	splx(s);
2221541Srgrimes}
2231541Srgrimes
22424101Sbdevoid
22529680Sgibbscallout_handle_init(struct callout_handle *handle)
22629680Sgibbs{
22729680Sgibbs	handle->callout = NULL;
22829680Sgibbs}
22929680Sgibbs
23044510Swollman/*
23144510Swollman * New interface; clients allocate their own callout structures.
23244510Swollman *
23344510Swollman * callout_reset() - establish or change a timeout
23444510Swollman * callout_stop() - disestablish a timeout
23544510Swollman * callout_init() - initialize a callout structure so that it can
23644510Swollman *	safely be passed to callout_reset() and callout_stop()
23744510Swollman *
23850673Sjlemon * <sys/callout.h> defines three convenience macros:
23944510Swollman *
24050673Sjlemon * callout_active() - returns truth if callout has not been serviced
24150673Sjlemon * callout_pending() - returns truth if callout is still waiting for timeout
24250673Sjlemon * callout_deactivate() - marks the callout as having been serviced
24344510Swollman */
24444510Swollmanvoid
24569147Sjlemoncallout_reset(c, to_ticks, ftn, arg)
24644510Swollman	struct	callout *c;
24744510Swollman	int	to_ticks;
24844510Swollman	void	(*ftn) __P((void *));
24944510Swollman	void	*arg;
25044510Swollman{
25144510Swollman	int	s;
25244510Swollman
25344510Swollman	s = splhigh();
25468889Sjake	mtx_enter(&callout_lock, MTX_SPIN);
25544510Swollman	if (c->c_flags & CALLOUT_PENDING)
25644510Swollman		callout_stop(c);
25744510Swollman
25844510Swollman	/*
25944510Swollman	 * We could spl down here and back up at the TAILQ_INSERT_TAIL,
26044510Swollman	 * but there's no point since doing this setup doesn't take much
26150673Sjlemon	 * time.
26244510Swollman	 */
26344510Swollman	if (to_ticks <= 0)
26444510Swollman		to_ticks = 1;
26544510Swollman
26644510Swollman	c->c_arg = arg;
26769147Sjlemon	c->c_flags |= (CALLOUT_ACTIVE | CALLOUT_PENDING);
26844510Swollman	c->c_func = ftn;
26944510Swollman	c->c_time = ticks + to_ticks;
27044510Swollman	TAILQ_INSERT_TAIL(&callwheel[c->c_time & callwheelmask],
27144510Swollman			  c, c_links.tqe);
27268889Sjake	mtx_exit(&callout_lock, MTX_SPIN);
27344510Swollman	splx(s);
27444510Swollman}
27544510Swollman
27644510Swollmanvoid
27744510Swollmancallout_stop(c)
27844510Swollman	struct	callout *c;
27944510Swollman{
28044510Swollman	int	s;
28144510Swollman
28244510Swollman	s = splhigh();
28368889Sjake	mtx_enter(&callout_lock, MTX_SPIN);
28444510Swollman	/*
28544510Swollman	 * Don't attempt to delete a callout that's not on the queue.
28644510Swollman	 */
28744510Swollman	if (!(c->c_flags & CALLOUT_PENDING)) {
28850673Sjlemon		c->c_flags &= ~CALLOUT_ACTIVE;
28968889Sjake		mtx_exit(&callout_lock, MTX_SPIN);
29044510Swollman		splx(s);
29144510Swollman		return;
29244510Swollman	}
29350673Sjlemon	c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING);
29444510Swollman
29544510Swollman	if (nextsoftcheck == c) {
29644510Swollman		nextsoftcheck = TAILQ_NEXT(c, c_links.tqe);
29744510Swollman	}
29844510Swollman	TAILQ_REMOVE(&callwheel[c->c_time & callwheelmask], c, c_links.tqe);
29944510Swollman	c->c_func = NULL;
30044510Swollman
30144510Swollman	if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
30244510Swollman		SLIST_INSERT_HEAD(&callfree, c, c_links.sle);
30344510Swollman	}
30468889Sjake	mtx_exit(&callout_lock, MTX_SPIN);
30544510Swollman	splx(s);
30644510Swollman}
30744510Swollman
30844510Swollmanvoid
30969147Sjlemoncallout_init(c, mpsafe)
31044510Swollman	struct	callout *c;
31169147Sjlemon	int mpsafe;
31244510Swollman{
31344527Swollman	bzero(c, sizeof *c);
31469147Sjlemon	if (mpsafe)
31569147Sjlemon		c->c_flags |= CALLOUT_MPSAFE;
31644510Swollman}
31744510Swollman
31831950Snate#ifdef APM_FIXUP_CALLTODO
31931950Snate/*
32031950Snate * Adjust the kernel calltodo timeout list.  This routine is used after
32131950Snate * an APM resume to recalculate the calltodo timer list values with the
32231950Snate * number of hz's we have been sleeping.  The next hardclock() will detect
32331950Snate * that there are fired timers and run softclock() to execute them.
32431950Snate *
32531950Snate * Please note, I have not done an exhaustive analysis of what code this
32631950Snate * might break.  I am motivated to have my select()'s and alarm()'s that
32731950Snate * have expired during suspend firing upon resume so that the applications
32831950Snate * which set the timer can do the maintanence the timer was for as close
32931950Snate * as possible to the originally intended time.  Testing this code for a
33031950Snate * week showed that resuming from a suspend resulted in 22 to 25 timers
33131950Snate * firing, which seemed independant on whether the suspend was 2 hours or
33231950Snate * 2 days.  Your milage may vary.   - Ken Key <key@cs.utk.edu>
33331950Snate */
33431950Snatevoid
33531950Snateadjust_timeout_calltodo(time_change)
33631950Snate    struct timeval *time_change;
33731950Snate{
33831950Snate	register struct callout *p;
33931950Snate	unsigned long delta_ticks;
34031950Snate	int s;
34131950Snate
34231950Snate	/*
34331950Snate	 * How many ticks were we asleep?
34436127Sbde	 * (stolen from tvtohz()).
34531950Snate	 */
34631950Snate
34731950Snate	/* Don't do anything */
34831950Snate	if (time_change->tv_sec < 0)
34931950Snate		return;
35031950Snate	else if (time_change->tv_sec <= LONG_MAX / 1000000)
35131950Snate		delta_ticks = (time_change->tv_sec * 1000000 +
35231950Snate			       time_change->tv_usec + (tick - 1)) / tick + 1;
35331950Snate	else if (time_change->tv_sec <= LONG_MAX / hz)
35431950Snate		delta_ticks = time_change->tv_sec * hz +
35531950Snate			      (time_change->tv_usec + (tick - 1)) / tick + 1;
35631950Snate	else
35731950Snate		delta_ticks = LONG_MAX;
35831950Snate
35931950Snate	if (delta_ticks > INT_MAX)
36031950Snate		delta_ticks = INT_MAX;
36131950Snate
36231950Snate	/*
36331950Snate	 * Now rip through the timer calltodo list looking for timers
36431950Snate	 * to expire.
36531950Snate	 */
36631950Snate
36731950Snate	/* don't collide with softclock() */
36831950Snate	s = splhigh();
36968889Sjake	mtx_enter(&callout_lock, MTX_SPIN);
37031950Snate	for (p = calltodo.c_next; p != NULL; p = p->c_next) {
37131950Snate		p->c_time -= delta_ticks;
37231950Snate
37331950Snate		/* Break if the timer had more time on it than delta_ticks */
37431950Snate		if (p->c_time > 0)
37531950Snate			break;
37631950Snate
37731950Snate		/* take back the ticks the timer didn't use (p->c_time <= 0) */
37831950Snate		delta_ticks = -p->c_time;
37931950Snate	}
38068889Sjake	mtx_exit(&callout_lock, MTX_SPIN);
38131950Snate	splx(s);
38231950Snate
38331950Snate	return;
38431950Snate}
38531950Snate#endif /* APM_FIXUP_CALLTODO */
386