1df8bae1dSRodney W. Grimes /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 19df8bae1dSRodney W. Grimes * must display the following acknowledgement: 20df8bae1dSRodney W. Grimes * This product includes software developed by the University of 21df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 22df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 23df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 24df8bae1dSRodney W. Grimes * without specific prior written permission. 25df8bae1dSRodney W. Grimes * 26df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36df8bae1dSRodney W. Grimes * SUCH DAMAGE. 37df8bae1dSRodney W. Grimes * 38acc8326dSGarrett Wollman * From: @(#)kern_clock.c 8.5 (Berkeley) 1/21/94 39c3aac50fSPeter Wemm * $FreeBSD$ 40df8bae1dSRodney W. Grimes */ 41df8bae1dSRodney W. Grimes 42df8bae1dSRodney W. Grimes #include <sys/param.h> 43df8bae1dSRodney W. Grimes #include <sys/systm.h> 4415b7a470SPoul-Henning Kamp #include <sys/callout.h> 45df8bae1dSRodney W. Grimes #include <sys/kernel.h> 46f34fa851SJohn Baldwin #include <sys/lock.h> 47cb799bfeSJohn Baldwin #include <sys/mutex.h> 48df8bae1dSRodney W. Grimes 4915b7a470SPoul-Henning Kamp /* 5015b7a470SPoul-Henning Kamp * TODO: 5115b7a470SPoul-Henning Kamp * allocate more timeout table slots when table overflows. 5215b7a470SPoul-Henning Kamp */ 5315b7a470SPoul-Henning Kamp 5415b7a470SPoul-Henning Kamp /* Exported to machdep.c and/or kern_clock.c. */ 55ab36c067SJustin T. Gibbs struct callout *callout; 56ab36c067SJustin T. Gibbs struct callout_list callfree; 57ab36c067SJustin T. Gibbs int callwheelsize, callwheelbits, callwheelmask; 58ab36c067SJustin T. Gibbs struct callout_tailq *callwheel; 5915b7a470SPoul-Henning Kamp int softticks; /* Like ticks, but for softclock(). */ 60fa2fbc3dSJake Burkholder struct mtx callout_lock; 61f23b4c91SGarrett Wollman 62ab36c067SJustin T. Gibbs static struct callout *nextsoftcheck; /* Next callout to be checked. */ 63df8bae1dSRodney W. Grimes 64df8bae1dSRodney W. Grimes /* 65219d632cSMatthew Dillon * kern_timeout_callwheel_alloc() - kernel low level callwheel initialization 66219d632cSMatthew Dillon * 67219d632cSMatthew Dillon * This code is called very early in the kernel initialization sequence, 68219d632cSMatthew Dillon * and may be called more then once. 69219d632cSMatthew Dillon */ 70219d632cSMatthew Dillon caddr_t 71219d632cSMatthew Dillon kern_timeout_callwheel_alloc(caddr_t v) 72219d632cSMatthew Dillon { 73219d632cSMatthew Dillon /* 74219d632cSMatthew Dillon * Calculate callout wheel size 75219d632cSMatthew Dillon */ 76219d632cSMatthew Dillon for (callwheelsize = 1, callwheelbits = 0; 77219d632cSMatthew Dillon callwheelsize < ncallout; 78219d632cSMatthew Dillon callwheelsize <<= 1, ++callwheelbits) 79219d632cSMatthew Dillon ; 80219d632cSMatthew Dillon callwheelmask = callwheelsize - 1; 81219d632cSMatthew Dillon 82219d632cSMatthew Dillon callout = (struct callout *)v; 83219d632cSMatthew Dillon v = (caddr_t)(callout + ncallout); 84219d632cSMatthew Dillon callwheel = (struct callout_tailq *)v; 85219d632cSMatthew Dillon v = (caddr_t)(callwheel + callwheelsize); 86219d632cSMatthew Dillon return(v); 87219d632cSMatthew Dillon } 88219d632cSMatthew Dillon 89219d632cSMatthew Dillon /* 90219d632cSMatthew Dillon * kern_timeout_callwheel_init() - initialize previously reserved callwheel 91219d632cSMatthew Dillon * space. 92219d632cSMatthew Dillon * 93219d632cSMatthew Dillon * This code is called just once, after the space reserved for the 94219d632cSMatthew Dillon * callout wheel has been finalized. 95219d632cSMatthew Dillon */ 96219d632cSMatthew Dillon void 97219d632cSMatthew Dillon kern_timeout_callwheel_init(void) 98219d632cSMatthew Dillon { 99219d632cSMatthew Dillon int i; 100219d632cSMatthew Dillon 101219d632cSMatthew Dillon SLIST_INIT(&callfree); 102219d632cSMatthew Dillon for (i = 0; i < ncallout; i++) { 103219d632cSMatthew Dillon callout_init(&callout[i], 0); 104219d632cSMatthew Dillon callout[i].c_flags = CALLOUT_LOCAL_ALLOC; 105219d632cSMatthew Dillon SLIST_INSERT_HEAD(&callfree, &callout[i], c_links.sle); 106219d632cSMatthew Dillon } 107219d632cSMatthew Dillon for (i = 0; i < callwheelsize; i++) { 108219d632cSMatthew Dillon TAILQ_INIT(&callwheel[i]); 109219d632cSMatthew Dillon } 110219d632cSMatthew Dillon mtx_init(&callout_lock, "callout", MTX_SPIN | MTX_RECURSE); 111219d632cSMatthew Dillon } 112219d632cSMatthew Dillon 113219d632cSMatthew Dillon /* 114ab36c067SJustin T. Gibbs * The callout mechanism is based on the work of Adam M. Costello and 115ab36c067SJustin T. Gibbs * George Varghese, published in a technical report entitled "Redesigning 116ab36c067SJustin T. Gibbs * the BSD Callout and Timer Facilities" and modified slightly for inclusion 117ab36c067SJustin T. Gibbs * in FreeBSD by Justin T. Gibbs. The original work on the data structures 118ab36c067SJustin T. Gibbs * used in this implementation was published by G.Varghese and A. Lauck in 119ab36c067SJustin T. Gibbs * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for 120ab36c067SJustin T. Gibbs * the Efficient Implementation of a Timer Facility" in the Proceedings of 121ab36c067SJustin T. Gibbs * the 11th ACM Annual Symposium on Operating Systems Principles, 122ab36c067SJustin T. Gibbs * Austin, Texas Nov 1987. 123ab36c067SJustin T. Gibbs */ 124a50ec505SPoul-Henning Kamp 125ab36c067SJustin T. Gibbs /* 126df8bae1dSRodney W. Grimes * Software (low priority) clock interrupt. 127df8bae1dSRodney W. Grimes * Run periodic events from timeout queue. 128df8bae1dSRodney W. Grimes */ 129df8bae1dSRodney W. Grimes void 1308088699fSJohn Baldwin softclock(void *dummy) 131df8bae1dSRodney W. Grimes { 132df8bae1dSRodney W. Grimes register struct callout *c; 13345327611SJustin T. Gibbs register struct callout_tailq *bucket; 13445327611SJustin T. Gibbs register int curticks; 13515b7a470SPoul-Henning Kamp register int steps; /* #steps since we last allowed interrupts */ 136df8bae1dSRodney W. Grimes 13715b7a470SPoul-Henning Kamp #ifndef MAX_SOFTCLOCK_STEPS 13815b7a470SPoul-Henning Kamp #define MAX_SOFTCLOCK_STEPS 100 /* Maximum allowed value of steps. */ 13915b7a470SPoul-Henning Kamp #endif /* MAX_SOFTCLOCK_STEPS */ 140ab36c067SJustin T. Gibbs 141ab36c067SJustin T. Gibbs steps = 0; 1429ed346baSBosko Milekic mtx_lock_spin(&callout_lock); 143ab36c067SJustin T. Gibbs while (softticks != ticks) { 14445327611SJustin T. Gibbs softticks++; 14545327611SJustin T. Gibbs /* 14645327611SJustin T. Gibbs * softticks may be modified by hard clock, so cache 14745327611SJustin T. Gibbs * it while we work on a given bucket. 14845327611SJustin T. Gibbs */ 14945327611SJustin T. Gibbs curticks = softticks; 15045327611SJustin T. Gibbs bucket = &callwheel[curticks & callwheelmask]; 15145327611SJustin T. Gibbs c = TAILQ_FIRST(bucket); 152ab36c067SJustin T. Gibbs while (c) { 15345327611SJustin T. Gibbs if (c->c_time != curticks) { 154ab36c067SJustin T. Gibbs c = TAILQ_NEXT(c, c_links.tqe); 155ab36c067SJustin T. Gibbs ++steps; 156ab36c067SJustin T. Gibbs if (steps >= MAX_SOFTCLOCK_STEPS) { 157ab36c067SJustin T. Gibbs nextsoftcheck = c; 15845327611SJustin T. Gibbs /* Give interrupts a chance. */ 1599ed346baSBosko Milekic mtx_unlock_spin(&callout_lock); 160ab32297dSJohn Baldwin ; /* nothing */ 1619ed346baSBosko Milekic mtx_lock_spin(&callout_lock); 162ab36c067SJustin T. Gibbs c = nextsoftcheck; 163ab36c067SJustin T. Gibbs steps = 0; 164df8bae1dSRodney W. Grimes } 165ab36c067SJustin T. Gibbs } else { 166ab36c067SJustin T. Gibbs void (*c_func)(void *); 167ab36c067SJustin T. Gibbs void *c_arg; 168fa2fbc3dSJake Burkholder int c_flags; 169ab36c067SJustin T. Gibbs 170ab36c067SJustin T. Gibbs nextsoftcheck = TAILQ_NEXT(c, c_links.tqe); 17145327611SJustin T. Gibbs TAILQ_REMOVE(bucket, c, c_links.tqe); 172ab36c067SJustin T. Gibbs c_func = c->c_func; 173ab36c067SJustin T. Gibbs c_arg = c->c_arg; 174fa2fbc3dSJake Burkholder c_flags = c->c_flags; 175ab36c067SJustin T. Gibbs c->c_func = NULL; 176acc8326dSGarrett Wollman if (c->c_flags & CALLOUT_LOCAL_ALLOC) { 177acc8326dSGarrett Wollman c->c_flags = CALLOUT_LOCAL_ALLOC; 178acc8326dSGarrett Wollman SLIST_INSERT_HEAD(&callfree, c, 179acc8326dSGarrett Wollman c_links.sle); 180acc8326dSGarrett Wollman } else { 181acc8326dSGarrett Wollman c->c_flags = 1829b8b58e0SJonathan Lemon (c->c_flags & ~CALLOUT_PENDING); 183acc8326dSGarrett Wollman } 1849ed346baSBosko Milekic mtx_unlock_spin(&callout_lock); 185fa2fbc3dSJake Burkholder if (!(c_flags & CALLOUT_MPSAFE)) 1869ed346baSBosko Milekic mtx_lock(&Giant); 187ab36c067SJustin T. Gibbs c_func(c_arg); 188fa2fbc3dSJake Burkholder if (!(c_flags & CALLOUT_MPSAFE)) 1899ed346baSBosko Milekic mtx_unlock(&Giant); 1909ed346baSBosko Milekic mtx_lock_spin(&callout_lock); 191ab36c067SJustin T. Gibbs steps = 0; 192ab36c067SJustin T. Gibbs c = nextsoftcheck; 193ab36c067SJustin T. Gibbs } 194ab36c067SJustin T. Gibbs } 195ab36c067SJustin T. Gibbs } 196ab36c067SJustin T. Gibbs nextsoftcheck = NULL; 1979ed346baSBosko Milekic mtx_unlock_spin(&callout_lock); 198df8bae1dSRodney W. Grimes } 199df8bae1dSRodney W. Grimes 200df8bae1dSRodney W. Grimes /* 201df8bae1dSRodney W. Grimes * timeout -- 202df8bae1dSRodney W. Grimes * Execute a function after a specified length of time. 203df8bae1dSRodney W. Grimes * 204df8bae1dSRodney W. Grimes * untimeout -- 205df8bae1dSRodney W. Grimes * Cancel previous timeout function call. 206df8bae1dSRodney W. Grimes * 207ab36c067SJustin T. Gibbs * callout_handle_init -- 208ab36c067SJustin T. Gibbs * Initialize a handle so that using it with untimeout is benign. 209ab36c067SJustin T. Gibbs * 210df8bae1dSRodney W. Grimes * See AT&T BCI Driver Reference Manual for specification. This 211ab36c067SJustin T. Gibbs * implementation differs from that one in that although an 212ab36c067SJustin T. Gibbs * identification value is returned from timeout, the original 213ab36c067SJustin T. Gibbs * arguments to timeout as well as the identifier are used to 214ab36c067SJustin T. Gibbs * identify entries for untimeout. 215df8bae1dSRodney W. Grimes */ 216ab36c067SJustin T. Gibbs struct callout_handle 217ab36c067SJustin T. Gibbs timeout(ftn, arg, to_ticks) 2188f03c6f1SBruce Evans timeout_t *ftn; 219df8bae1dSRodney W. Grimes void *arg; 220e82ac18eSJonathan Lemon int to_ticks; 221df8bae1dSRodney W. Grimes { 222ab36c067SJustin T. Gibbs struct callout *new; 223ab36c067SJustin T. Gibbs struct callout_handle handle; 224df8bae1dSRodney W. Grimes 2259ed346baSBosko Milekic mtx_lock_spin(&callout_lock); 226df8bae1dSRodney W. Grimes 227df8bae1dSRodney W. Grimes /* Fill in the next free callout structure. */ 228ab36c067SJustin T. Gibbs new = SLIST_FIRST(&callfree); 229ab36c067SJustin T. Gibbs if (new == NULL) 230ab36c067SJustin T. Gibbs /* XXX Attempt to malloc first */ 231df8bae1dSRodney W. Grimes panic("timeout table full"); 232ab36c067SJustin T. Gibbs SLIST_REMOVE_HEAD(&callfree, c_links.sle); 233df8bae1dSRodney W. Grimes 234acc8326dSGarrett Wollman callout_reset(new, to_ticks, ftn, arg); 235acc8326dSGarrett Wollman 236ab36c067SJustin T. Gibbs handle.callout = new; 2379ed346baSBosko Milekic mtx_unlock_spin(&callout_lock); 238ab36c067SJustin T. Gibbs return (handle); 239df8bae1dSRodney W. Grimes } 240df8bae1dSRodney W. Grimes 241df8bae1dSRodney W. Grimes void 242ab36c067SJustin T. Gibbs untimeout(ftn, arg, handle) 2438f03c6f1SBruce Evans timeout_t *ftn; 244df8bae1dSRodney W. Grimes void *arg; 245ab36c067SJustin T. Gibbs struct callout_handle handle; 246df8bae1dSRodney W. Grimes { 247df8bae1dSRodney W. Grimes 248ab36c067SJustin T. Gibbs /* 249ab36c067SJustin T. Gibbs * Check for a handle that was initialized 250ab36c067SJustin T. Gibbs * by callout_handle_init, but never used 251ab36c067SJustin T. Gibbs * for a real timeout. 252ab36c067SJustin T. Gibbs */ 253ab36c067SJustin T. Gibbs if (handle.callout == NULL) 254ab36c067SJustin T. Gibbs return; 255df8bae1dSRodney W. Grimes 2569ed346baSBosko Milekic mtx_lock_spin(&callout_lock); 257acc8326dSGarrett Wollman if (handle.callout->c_func == ftn && handle.callout->c_arg == arg) 258acc8326dSGarrett Wollman callout_stop(handle.callout); 2599ed346baSBosko Milekic mtx_unlock_spin(&callout_lock); 260df8bae1dSRodney W. Grimes } 261df8bae1dSRodney W. Grimes 2623c816944SBruce Evans void 263ab36c067SJustin T. Gibbs callout_handle_init(struct callout_handle *handle) 264ab36c067SJustin T. Gibbs { 265ab36c067SJustin T. Gibbs handle->callout = NULL; 266ab36c067SJustin T. Gibbs } 267ab36c067SJustin T. Gibbs 268acc8326dSGarrett Wollman /* 269acc8326dSGarrett Wollman * New interface; clients allocate their own callout structures. 270acc8326dSGarrett Wollman * 271acc8326dSGarrett Wollman * callout_reset() - establish or change a timeout 272acc8326dSGarrett Wollman * callout_stop() - disestablish a timeout 273acc8326dSGarrett Wollman * callout_init() - initialize a callout structure so that it can 274acc8326dSGarrett Wollman * safely be passed to callout_reset() and callout_stop() 275acc8326dSGarrett Wollman * 2769b8b58e0SJonathan Lemon * <sys/callout.h> defines three convenience macros: 277acc8326dSGarrett Wollman * 2789b8b58e0SJonathan Lemon * callout_active() - returns truth if callout has not been serviced 2799b8b58e0SJonathan Lemon * callout_pending() - returns truth if callout is still waiting for timeout 2809b8b58e0SJonathan Lemon * callout_deactivate() - marks the callout as having been serviced 281acc8326dSGarrett Wollman */ 282acc8326dSGarrett Wollman void 283e82ac18eSJonathan Lemon callout_reset(c, to_ticks, ftn, arg) 284acc8326dSGarrett Wollman struct callout *c; 285acc8326dSGarrett Wollman int to_ticks; 286acc8326dSGarrett Wollman void (*ftn) __P((void *)); 287acc8326dSGarrett Wollman void *arg; 288acc8326dSGarrett Wollman { 289acc8326dSGarrett Wollman 2909ed346baSBosko Milekic mtx_lock_spin(&callout_lock); 291acc8326dSGarrett Wollman if (c->c_flags & CALLOUT_PENDING) 292acc8326dSGarrett Wollman callout_stop(c); 293acc8326dSGarrett Wollman 294acc8326dSGarrett Wollman /* 295ab32297dSJohn Baldwin * We could unlock callout_lock here and lock it again before the 296ab32297dSJohn Baldwin * TAILQ_INSERT_TAIL, but there's no point since doing this setup 297ab32297dSJohn Baldwin * doesn't take much time. 298acc8326dSGarrett Wollman */ 299acc8326dSGarrett Wollman if (to_ticks <= 0) 300acc8326dSGarrett Wollman to_ticks = 1; 301acc8326dSGarrett Wollman 302acc8326dSGarrett Wollman c->c_arg = arg; 303e82ac18eSJonathan Lemon c->c_flags |= (CALLOUT_ACTIVE | CALLOUT_PENDING); 304acc8326dSGarrett Wollman c->c_func = ftn; 305acc8326dSGarrett Wollman c->c_time = ticks + to_ticks; 306acc8326dSGarrett Wollman TAILQ_INSERT_TAIL(&callwheel[c->c_time & callwheelmask], 307acc8326dSGarrett Wollman c, c_links.tqe); 3089ed346baSBosko Milekic mtx_unlock_spin(&callout_lock); 309acc8326dSGarrett Wollman } 310acc8326dSGarrett Wollman 311a45982d2SJohn Baldwin int 312acc8326dSGarrett Wollman callout_stop(c) 313acc8326dSGarrett Wollman struct callout *c; 314acc8326dSGarrett Wollman { 315acc8326dSGarrett Wollman 3169ed346baSBosko Milekic mtx_lock_spin(&callout_lock); 317acc8326dSGarrett Wollman /* 318acc8326dSGarrett Wollman * Don't attempt to delete a callout that's not on the queue. 319acc8326dSGarrett Wollman */ 320acc8326dSGarrett Wollman if (!(c->c_flags & CALLOUT_PENDING)) { 3219b8b58e0SJonathan Lemon c->c_flags &= ~CALLOUT_ACTIVE; 3229ed346baSBosko Milekic mtx_unlock_spin(&callout_lock); 323a45982d2SJohn Baldwin return (0); 324acc8326dSGarrett Wollman } 3259b8b58e0SJonathan Lemon c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING); 326acc8326dSGarrett Wollman 327acc8326dSGarrett Wollman if (nextsoftcheck == c) { 328acc8326dSGarrett Wollman nextsoftcheck = TAILQ_NEXT(c, c_links.tqe); 329acc8326dSGarrett Wollman } 330acc8326dSGarrett Wollman TAILQ_REMOVE(&callwheel[c->c_time & callwheelmask], c, c_links.tqe); 331acc8326dSGarrett Wollman c->c_func = NULL; 332acc8326dSGarrett Wollman 333acc8326dSGarrett Wollman if (c->c_flags & CALLOUT_LOCAL_ALLOC) { 334acc8326dSGarrett Wollman SLIST_INSERT_HEAD(&callfree, c, c_links.sle); 335acc8326dSGarrett Wollman } 3369ed346baSBosko Milekic mtx_unlock_spin(&callout_lock); 337a45982d2SJohn Baldwin return (1); 338acc8326dSGarrett Wollman } 339acc8326dSGarrett Wollman 340acc8326dSGarrett Wollman void 341e82ac18eSJonathan Lemon callout_init(c, mpsafe) 342acc8326dSGarrett Wollman struct callout *c; 343e82ac18eSJonathan Lemon int mpsafe; 344acc8326dSGarrett Wollman { 3457347e1c6SGarrett Wollman bzero(c, sizeof *c); 346e82ac18eSJonathan Lemon if (mpsafe) 347e82ac18eSJonathan Lemon c->c_flags |= CALLOUT_MPSAFE; 348acc8326dSGarrett Wollman } 349acc8326dSGarrett Wollman 350e1d6dc65SNate Williams #ifdef APM_FIXUP_CALLTODO 351e1d6dc65SNate Williams /* 352e1d6dc65SNate Williams * Adjust the kernel calltodo timeout list. This routine is used after 353e1d6dc65SNate Williams * an APM resume to recalculate the calltodo timer list values with the 354e1d6dc65SNate Williams * number of hz's we have been sleeping. The next hardclock() will detect 355e1d6dc65SNate Williams * that there are fired timers and run softclock() to execute them. 356e1d6dc65SNate Williams * 357e1d6dc65SNate Williams * Please note, I have not done an exhaustive analysis of what code this 358e1d6dc65SNate Williams * might break. I am motivated to have my select()'s and alarm()'s that 359e1d6dc65SNate Williams * have expired during suspend firing upon resume so that the applications 360e1d6dc65SNate Williams * which set the timer can do the maintanence the timer was for as close 361e1d6dc65SNate Williams * as possible to the originally intended time. Testing this code for a 362e1d6dc65SNate Williams * week showed that resuming from a suspend resulted in 22 to 25 timers 363e1d6dc65SNate Williams * firing, which seemed independant on whether the suspend was 2 hours or 364e1d6dc65SNate Williams * 2 days. Your milage may vary. - Ken Key <key@cs.utk.edu> 365e1d6dc65SNate Williams */ 366e1d6dc65SNate Williams void 367e1d6dc65SNate Williams adjust_timeout_calltodo(time_change) 368e1d6dc65SNate Williams struct timeval *time_change; 369e1d6dc65SNate Williams { 370e1d6dc65SNate Williams register struct callout *p; 371e1d6dc65SNate Williams unsigned long delta_ticks; 372e1d6dc65SNate Williams 373e1d6dc65SNate Williams /* 374e1d6dc65SNate Williams * How many ticks were we asleep? 375c8b47828SBruce Evans * (stolen from tvtohz()). 376e1d6dc65SNate Williams */ 377e1d6dc65SNate Williams 378e1d6dc65SNate Williams /* Don't do anything */ 379e1d6dc65SNate Williams if (time_change->tv_sec < 0) 380e1d6dc65SNate Williams return; 381e1d6dc65SNate Williams else if (time_change->tv_sec <= LONG_MAX / 1000000) 382e1d6dc65SNate Williams delta_ticks = (time_change->tv_sec * 1000000 + 383e1d6dc65SNate Williams time_change->tv_usec + (tick - 1)) / tick + 1; 384e1d6dc65SNate Williams else if (time_change->tv_sec <= LONG_MAX / hz) 385e1d6dc65SNate Williams delta_ticks = time_change->tv_sec * hz + 386e1d6dc65SNate Williams (time_change->tv_usec + (tick - 1)) / tick + 1; 387e1d6dc65SNate Williams else 388e1d6dc65SNate Williams delta_ticks = LONG_MAX; 389e1d6dc65SNate Williams 390e1d6dc65SNate Williams if (delta_ticks > INT_MAX) 391e1d6dc65SNate Williams delta_ticks = INT_MAX; 392e1d6dc65SNate Williams 393e1d6dc65SNate Williams /* 394e1d6dc65SNate Williams * Now rip through the timer calltodo list looking for timers 395e1d6dc65SNate Williams * to expire. 396e1d6dc65SNate Williams */ 397e1d6dc65SNate Williams 398e1d6dc65SNate Williams /* don't collide with softclock() */ 3999ed346baSBosko Milekic mtx_lock_spin(&callout_lock); 400e1d6dc65SNate Williams for (p = calltodo.c_next; p != NULL; p = p->c_next) { 401e1d6dc65SNate Williams p->c_time -= delta_ticks; 402e1d6dc65SNate Williams 403e1d6dc65SNate Williams /* Break if the timer had more time on it than delta_ticks */ 404e1d6dc65SNate Williams if (p->c_time > 0) 405e1d6dc65SNate Williams break; 406e1d6dc65SNate Williams 407e1d6dc65SNate Williams /* take back the ticks the timer didn't use (p->c_time <= 0) */ 408e1d6dc65SNate Williams delta_ticks = -p->c_time; 409e1d6dc65SNate Williams } 4109ed346baSBosko Milekic mtx_unlock_spin(&callout_lock); 411e1d6dc65SNate Williams 412e1d6dc65SNate Williams return; 413e1d6dc65SNate Williams } 414e1d6dc65SNate Williams #endif /* APM_FIXUP_CALLTODO */ 415