kern_timeout.c revision 50673
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 50673 1999-08-30 21:17:07Z jlemon $ 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 = 12850673Sjlemon (c->c_flags & ~CALLOUT_PENDING); 12944510Swollman } 13029680Sgibbs splx(s); 13129680Sgibbs c_func(c_arg); 13229680Sgibbs s = splhigh(); 13329680Sgibbs steps = 0; 13429680Sgibbs c = nextsoftcheck; 13529680Sgibbs } 13629680Sgibbs } 1371541Srgrimes } 13829680Sgibbs nextsoftcheck = NULL; 1391541Srgrimes splx(s); 1401541Srgrimes} 1411541Srgrimes 1421541Srgrimes/* 1431541Srgrimes * timeout -- 1441541Srgrimes * Execute a function after a specified length of time. 1451541Srgrimes * 1461541Srgrimes * untimeout -- 1471541Srgrimes * Cancel previous timeout function call. 1481541Srgrimes * 14929680Sgibbs * callout_handle_init -- 15029680Sgibbs * Initialize a handle so that using it with untimeout is benign. 15129680Sgibbs * 1521541Srgrimes * See AT&T BCI Driver Reference Manual for specification. This 15329680Sgibbs * implementation differs from that one in that although an 15429680Sgibbs * identification value is returned from timeout, the original 15529680Sgibbs * arguments to timeout as well as the identifier are used to 15629680Sgibbs * identify entries for untimeout. 1571541Srgrimes */ 15829680Sgibbsstruct callout_handle 15929680Sgibbstimeout(ftn, arg, to_ticks) 16033824Sbde timeout_t *ftn; 1611541Srgrimes void *arg; 16229680Sgibbs register int to_ticks; 1631541Srgrimes{ 16429680Sgibbs int s; 16529680Sgibbs struct callout *new; 16629680Sgibbs struct callout_handle handle; 1671541Srgrimes 1681541Srgrimes s = splhigh(); 1691541Srgrimes 1701541Srgrimes /* Fill in the next free callout structure. */ 17129680Sgibbs new = SLIST_FIRST(&callfree); 17229680Sgibbs if (new == NULL) 17329680Sgibbs /* XXX Attempt to malloc first */ 1741541Srgrimes panic("timeout table full"); 17529680Sgibbs SLIST_REMOVE_HEAD(&callfree, c_links.sle); 17644510Swollman 17744510Swollman callout_reset(new, to_ticks, ftn, arg); 1781541Srgrimes 17944510Swollman handle.callout = new; 1801541Srgrimes splx(s); 18129680Sgibbs return (handle); 1821541Srgrimes} 1831541Srgrimes 1841541Srgrimesvoid 18529680Sgibbsuntimeout(ftn, arg, handle) 18633824Sbde timeout_t *ftn; 1871541Srgrimes void *arg; 18829680Sgibbs struct callout_handle handle; 1891541Srgrimes{ 1901541Srgrimes register int s; 1911541Srgrimes 19229680Sgibbs /* 19329680Sgibbs * Check for a handle that was initialized 19429680Sgibbs * by callout_handle_init, but never used 19529680Sgibbs * for a real timeout. 19629680Sgibbs */ 19729680Sgibbs if (handle.callout == NULL) 19829680Sgibbs return; 19929680Sgibbs 2001541Srgrimes s = splhigh(); 20144510Swollman if (handle.callout->c_func == ftn && handle.callout->c_arg == arg) 20244510Swollman callout_stop(handle.callout); 2031541Srgrimes splx(s); 2041541Srgrimes} 2051541Srgrimes 20624101Sbdevoid 20729680Sgibbscallout_handle_init(struct callout_handle *handle) 20829680Sgibbs{ 20929680Sgibbs handle->callout = NULL; 21029680Sgibbs} 21129680Sgibbs 21244510Swollman/* 21344510Swollman * New interface; clients allocate their own callout structures. 21444510Swollman * 21544510Swollman * callout_reset() - establish or change a timeout 21644510Swollman * callout_stop() - disestablish a timeout 21744510Swollman * callout_init() - initialize a callout structure so that it can 21844510Swollman * safely be passed to callout_reset() and callout_stop() 21944510Swollman * 22050673Sjlemon * <sys/callout.h> defines three convenience macros: 22144510Swollman * 22250673Sjlemon * callout_active() - returns truth if callout has not been serviced 22350673Sjlemon * callout_pending() - returns truth if callout is still waiting for timeout 22450673Sjlemon * callout_deactivate() - marks the callout as having been serviced 22544510Swollman */ 22644510Swollmanvoid 22744510Swollmancallout_reset(c, to_ticks, ftn, arg) 22844510Swollman struct callout *c; 22944510Swollman int to_ticks; 23044510Swollman void (*ftn) __P((void *)); 23144510Swollman void *arg; 23244510Swollman{ 23344510Swollman int s; 23444510Swollman 23544510Swollman s = splhigh(); 23644510Swollman if (c->c_flags & CALLOUT_PENDING) 23744510Swollman callout_stop(c); 23844510Swollman 23944510Swollman /* 24044510Swollman * We could spl down here and back up at the TAILQ_INSERT_TAIL, 24144510Swollman * but there's no point since doing this setup doesn't take much 24250673Sjlemon * time. 24344510Swollman */ 24444510Swollman if (to_ticks <= 0) 24544510Swollman to_ticks = 1; 24644510Swollman 24744510Swollman c->c_arg = arg; 24850673Sjlemon c->c_flags |= (CALLOUT_ACTIVE | CALLOUT_PENDING); 24944510Swollman c->c_func = ftn; 25044510Swollman c->c_time = ticks + to_ticks; 25144510Swollman TAILQ_INSERT_TAIL(&callwheel[c->c_time & callwheelmask], 25244510Swollman c, c_links.tqe); 25344510Swollman splx(s); 25444510Swollman 25544510Swollman} 25644510Swollman 25744510Swollmanvoid 25844510Swollmancallout_stop(c) 25944510Swollman struct callout *c; 26044510Swollman{ 26144510Swollman int s; 26244510Swollman 26344510Swollman s = splhigh(); 26444510Swollman /* 26544510Swollman * Don't attempt to delete a callout that's not on the queue. 26644510Swollman */ 26744510Swollman if (!(c->c_flags & CALLOUT_PENDING)) { 26850673Sjlemon c->c_flags &= ~CALLOUT_ACTIVE; 26944510Swollman splx(s); 27044510Swollman return; 27144510Swollman } 27250673Sjlemon c->c_flags &= ~(CALLOUT_ACTIVE | 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