kern_timeout.c revision 44527
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 3944527Swollman * $Id: kern_timeout.c,v 1.56 1999/03/06 04:46:19 wollman Exp $ 401541Srgrimes */ 411541Srgrimes 421541Srgrimes#include <sys/param.h> 431541Srgrimes#include <sys/systm.h> 4433392Sphk#include <sys/callout.h> 451541Srgrimes#include <sys/kernel.h> 461541Srgrimes 4733392Sphk/* 4833392Sphk * TODO: 4933392Sphk * allocate more timeout table slots when table overflows. 5033392Sphk */ 5133392Sphk 5233392Sphk/* Exported to machdep.c and/or kern_clock.c. */ 5329680Sgibbsstruct callout *callout; 5429680Sgibbsstruct callout_list callfree; 5529680Sgibbsint callwheelsize, callwheelbits, callwheelmask; 5629680Sgibbsstruct callout_tailq *callwheel; 5733392Sphkint softticks; /* Like ticks, but for softclock(). */ 582112Swollman 5929680Sgibbsstatic struct callout *nextsoftcheck; /* Next callout to be checked. */ 601541Srgrimes 611541Srgrimes/* 6229680Sgibbs * The callout mechanism is based on the work of Adam M. Costello and 6329680Sgibbs * George Varghese, published in a technical report entitled "Redesigning 6429680Sgibbs * the BSD Callout and Timer Facilities" and modified slightly for inclusion 6529680Sgibbs * in FreeBSD by Justin T. Gibbs. The original work on the data structures 6629680Sgibbs * used in this implementation was published by G.Varghese and A. Lauck in 6729680Sgibbs * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for 6829680Sgibbs * the Efficient Implementation of a Timer Facility" in the Proceedings of 6929680Sgibbs * the 11th ACM Annual Symposium on Operating Systems Principles, 7029680Sgibbs * Austin, Texas Nov 1987. 7129680Sgibbs */ 7232388Sphk 7329680Sgibbs/* 741541Srgrimes * Software (low priority) clock interrupt. 751541Srgrimes * Run periodic events from timeout queue. 761541Srgrimes */ 771541Srgrimesvoid 7832391Sphksoftclock() 791541Srgrimes{ 801541Srgrimes register struct callout *c; 8129805Sgibbs register struct callout_tailq *bucket; 821541Srgrimes register int s; 8329805Sgibbs register int curticks; 8433392Sphk register int steps; /* #steps since we last allowed interrupts */ 851541Srgrimes 8633392Sphk#ifndef MAX_SOFTCLOCK_STEPS 8733392Sphk#define MAX_SOFTCLOCK_STEPS 100 /* Maximum allowed value of steps. */ 8833392Sphk#endif /* MAX_SOFTCLOCK_STEPS */ 8929680Sgibbs 9029680Sgibbs steps = 0; 911541Srgrimes s = splhigh(); 9229680Sgibbs while (softticks != ticks) { 9329805Sgibbs softticks++; 9429805Sgibbs /* 9529805Sgibbs * softticks may be modified by hard clock, so cache 9629805Sgibbs * it while we work on a given bucket. 9729805Sgibbs */ 9829805Sgibbs curticks = softticks; 9929805Sgibbs bucket = &callwheel[curticks & callwheelmask]; 10029805Sgibbs c = TAILQ_FIRST(bucket); 10129680Sgibbs while (c) { 10229805Sgibbs if (c->c_time != curticks) { 10329680Sgibbs c = TAILQ_NEXT(c, c_links.tqe); 10429680Sgibbs ++steps; 10529680Sgibbs if (steps >= MAX_SOFTCLOCK_STEPS) { 10629680Sgibbs nextsoftcheck = c; 10729805Sgibbs /* Give interrupts a chance. */ 10829680Sgibbs splx(s); 10929680Sgibbs s = splhigh(); 11029680Sgibbs c = nextsoftcheck; 11129680Sgibbs steps = 0; 11229680Sgibbs } 11329680Sgibbs } else { 11429680Sgibbs void (*c_func)(void *); 11529680Sgibbs void *c_arg; 11629680Sgibbs 11729680Sgibbs nextsoftcheck = TAILQ_NEXT(c, c_links.tqe); 11829805Sgibbs TAILQ_REMOVE(bucket, c, c_links.tqe); 11929680Sgibbs c_func = c->c_func; 12029680Sgibbs c_arg = c->c_arg; 12129680Sgibbs c->c_func = NULL; 12244510Swollman if (c->c_flags & CALLOUT_LOCAL_ALLOC) { 12344510Swollman c->c_flags = CALLOUT_LOCAL_ALLOC; 12444510Swollman SLIST_INSERT_HEAD(&callfree, c, 12544510Swollman c_links.sle); 12644510Swollman } else { 12744510Swollman c->c_flags = 12844510Swollman (c->c_flags & ~CALLOUT_PENDING) 12944510Swollman | CALLOUT_FIRED; 13044510Swollman } 13129680Sgibbs splx(s); 13229680Sgibbs c_func(c_arg); 13329680Sgibbs s = splhigh(); 13429680Sgibbs steps = 0; 13529680Sgibbs c = nextsoftcheck; 13629680Sgibbs } 13729680Sgibbs } 1381541Srgrimes } 13929680Sgibbs nextsoftcheck = NULL; 1401541Srgrimes splx(s); 1411541Srgrimes} 1421541Srgrimes 1431541Srgrimes/* 1441541Srgrimes * timeout -- 1451541Srgrimes * Execute a function after a specified length of time. 1461541Srgrimes * 1471541Srgrimes * untimeout -- 1481541Srgrimes * Cancel previous timeout function call. 1491541Srgrimes * 15029680Sgibbs * callout_handle_init -- 15129680Sgibbs * Initialize a handle so that using it with untimeout is benign. 15229680Sgibbs * 1531541Srgrimes * See AT&T BCI Driver Reference Manual for specification. This 15429680Sgibbs * implementation differs from that one in that although an 15529680Sgibbs * identification value is returned from timeout, the original 15629680Sgibbs * arguments to timeout as well as the identifier are used to 15729680Sgibbs * identify entries for untimeout. 1581541Srgrimes */ 15929680Sgibbsstruct callout_handle 16029680Sgibbstimeout(ftn, arg, to_ticks) 16133824Sbde timeout_t *ftn; 1621541Srgrimes void *arg; 16329680Sgibbs register int to_ticks; 1641541Srgrimes{ 16529680Sgibbs int s; 16629680Sgibbs struct callout *new; 16729680Sgibbs struct callout_handle handle; 1681541Srgrimes 1691541Srgrimes s = splhigh(); 1701541Srgrimes 1711541Srgrimes /* Fill in the next free callout structure. */ 17229680Sgibbs new = SLIST_FIRST(&callfree); 17329680Sgibbs if (new == NULL) 17429680Sgibbs /* XXX Attempt to malloc first */ 1751541Srgrimes panic("timeout table full"); 17629680Sgibbs SLIST_REMOVE_HEAD(&callfree, c_links.sle); 17744510Swollman 17844510Swollman callout_reset(new, to_ticks, ftn, arg); 1791541Srgrimes 18044510Swollman handle.callout = new; 1811541Srgrimes splx(s); 18229680Sgibbs return (handle); 1831541Srgrimes} 1841541Srgrimes 1851541Srgrimesvoid 18629680Sgibbsuntimeout(ftn, arg, handle) 18733824Sbde timeout_t *ftn; 1881541Srgrimes void *arg; 18929680Sgibbs struct callout_handle handle; 1901541Srgrimes{ 1911541Srgrimes register int s; 1921541Srgrimes 19329680Sgibbs /* 19429680Sgibbs * Check for a handle that was initialized 19529680Sgibbs * by callout_handle_init, but never used 19629680Sgibbs * for a real timeout. 19729680Sgibbs */ 19829680Sgibbs if (handle.callout == NULL) 19929680Sgibbs return; 20029680Sgibbs 2011541Srgrimes s = splhigh(); 20244510Swollman if (handle.callout->c_func == ftn && handle.callout->c_arg == arg) 20344510Swollman callout_stop(handle.callout); 2041541Srgrimes splx(s); 2051541Srgrimes} 2061541Srgrimes 20724101Sbdevoid 20829680Sgibbscallout_handle_init(struct callout_handle *handle) 20929680Sgibbs{ 21029680Sgibbs handle->callout = NULL; 21129680Sgibbs} 21229680Sgibbs 21344510Swollman/* 21444510Swollman * New interface; clients allocate their own callout structures. 21544510Swollman * 21644510Swollman * callout_reset() - establish or change a timeout 21744510Swollman * callout_stop() - disestablish a timeout 21844510Swollman * callout_init() - initialize a callout structure so that it can 21944510Swollman * safely be passed to callout_reset() and callout_stop() 22044510Swollman * 22144510Swollman * <sys/callout.h> defines two convenience macros: 22244510Swollman * 22344510Swollman * callout_pending() - returns number of ticks until callout fires, or 0 22444510Swollman * if not scheduled 22544510Swollman * callout_fired() - returns truth if callout has already been fired 22644510Swollman */ 22744510Swollmanvoid 22844510Swollmancallout_reset(c, to_ticks, ftn, arg) 22944510Swollman struct callout *c; 23044510Swollman int to_ticks; 23144510Swollman void (*ftn) __P((void *)); 23244510Swollman void *arg; 23344510Swollman{ 23444510Swollman int s; 23544510Swollman 23644510Swollman s = splhigh(); 23744510Swollman if (c->c_flags & CALLOUT_PENDING) 23844510Swollman callout_stop(c); 23944510Swollman 24044510Swollman /* 24144510Swollman * We could spl down here and back up at the TAILQ_INSERT_TAIL, 24244510Swollman * but there's no point since doing this setup doesn't take much 24344510Swollman ^ time. 24444510Swollman */ 24544510Swollman if (to_ticks <= 0) 24644510Swollman to_ticks = 1; 24744510Swollman 24844510Swollman c->c_arg = arg; 24944510Swollman c->c_flags = (c->c_flags & ~CALLOUT_FIRED) | CALLOUT_PENDING; 25044510Swollman c->c_func = ftn; 25144510Swollman c->c_time = ticks + to_ticks; 25244510Swollman TAILQ_INSERT_TAIL(&callwheel[c->c_time & callwheelmask], 25344510Swollman c, c_links.tqe); 25444510Swollman splx(s); 25544510Swollman 25644510Swollman} 25744510Swollman 25844510Swollmanvoid 25944510Swollmancallout_stop(c) 26044510Swollman struct callout *c; 26144510Swollman{ 26244510Swollman int s; 26344510Swollman 26444510Swollman s = splhigh(); 26544510Swollman /* 26644510Swollman * Don't attempt to delete a callout that's not on the queue. 26744510Swollman */ 26844510Swollman if (!(c->c_flags & CALLOUT_PENDING)) { 26944510Swollman splx(s); 27044510Swollman return; 27144510Swollman } 27244510Swollman c->c_flags &= ~CALLOUT_PENDING; 27344510Swollman 27444510Swollman if (nextsoftcheck == c) { 27544510Swollman nextsoftcheck = TAILQ_NEXT(c, c_links.tqe); 27644510Swollman } 27744510Swollman TAILQ_REMOVE(&callwheel[c->c_time & callwheelmask], c, c_links.tqe); 27844510Swollman c->c_func = NULL; 27944510Swollman 28044510Swollman if (c->c_flags & CALLOUT_LOCAL_ALLOC) { 28144510Swollman SLIST_INSERT_HEAD(&callfree, c, c_links.sle); 28244510Swollman } 28344510Swollman splx(s); 28444510Swollman} 28544510Swollman 28644510Swollmanvoid 28744510Swollmancallout_init(c) 28844510Swollman struct callout *c; 28944510Swollman{ 29044527Swollman bzero(c, sizeof *c); 29144510Swollman} 29244510Swollman 29331950Snate#ifdef APM_FIXUP_CALLTODO 29431950Snate/* 29531950Snate * Adjust the kernel calltodo timeout list. This routine is used after 29631950Snate * an APM resume to recalculate the calltodo timer list values with the 29731950Snate * number of hz's we have been sleeping. The next hardclock() will detect 29831950Snate * that there are fired timers and run softclock() to execute them. 29931950Snate * 30031950Snate * Please note, I have not done an exhaustive analysis of what code this 30131950Snate * might break. I am motivated to have my select()'s and alarm()'s that 30231950Snate * have expired during suspend firing upon resume so that the applications 30331950Snate * which set the timer can do the maintanence the timer was for as close 30431950Snate * as possible to the originally intended time. Testing this code for a 30531950Snate * week showed that resuming from a suspend resulted in 22 to 25 timers 30631950Snate * firing, which seemed independant on whether the suspend was 2 hours or 30731950Snate * 2 days. Your milage may vary. - Ken Key <key@cs.utk.edu> 30831950Snate */ 30931950Snatevoid 31031950Snateadjust_timeout_calltodo(time_change) 31131950Snate struct timeval *time_change; 31231950Snate{ 31331950Snate register struct callout *p; 31431950Snate unsigned long delta_ticks; 31531950Snate int s; 31631950Snate 31731950Snate /* 31831950Snate * How many ticks were we asleep? 31936127Sbde * (stolen from tvtohz()). 32031950Snate */ 32131950Snate 32231950Snate /* Don't do anything */ 32331950Snate if (time_change->tv_sec < 0) 32431950Snate return; 32531950Snate else if (time_change->tv_sec <= LONG_MAX / 1000000) 32631950Snate delta_ticks = (time_change->tv_sec * 1000000 + 32731950Snate time_change->tv_usec + (tick - 1)) / tick + 1; 32831950Snate else if (time_change->tv_sec <= LONG_MAX / hz) 32931950Snate delta_ticks = time_change->tv_sec * hz + 33031950Snate (time_change->tv_usec + (tick - 1)) / tick + 1; 33131950Snate else 33231950Snate delta_ticks = LONG_MAX; 33331950Snate 33431950Snate if (delta_ticks > INT_MAX) 33531950Snate delta_ticks = INT_MAX; 33631950Snate 33731950Snate /* 33831950Snate * Now rip through the timer calltodo list looking for timers 33931950Snate * to expire. 34031950Snate */ 34131950Snate 34231950Snate /* don't collide with softclock() */ 34331950Snate s = splhigh(); 34431950Snate for (p = calltodo.c_next; p != NULL; p = p->c_next) { 34531950Snate p->c_time -= delta_ticks; 34631950Snate 34731950Snate /* Break if the timer had more time on it than delta_ticks */ 34831950Snate if (p->c_time > 0) 34931950Snate break; 35031950Snate 35131950Snate /* take back the ticks the timer didn't use (p->c_time <= 0) */ 35231950Snate delta_ticks = -p->c_time; 35331950Snate } 35431950Snate splx(s); 35531950Snate 35631950Snate return; 35731950Snate} 35831950Snate#endif /* APM_FIXUP_CALLTODO */ 359