2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * From: @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/kern_timeout.c,v 1.59.2.1 2001/11/13 18:24:52 archie Exp $
40 * $DragonFly: src/sys/kern/kern_timeout.c,v 1.11 2004/09/13 23:18:20 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/callout.h>
46 #include <sys/kernel.h>
47 #include <sys/interrupt.h>
48 #include <sys/thread.h>
49 #include <sys/thread2.h>
50 #include <machine/ipl.h>
54 * allocate more timeout table slots when table overflows.
57 /* Exported to machdep.c and/or kern_clock.c. */
58 struct callout *callout;
59 struct callout_list callfree;
60 int callwheelsize, callwheelbits, callwheelmask;
61 struct callout_tailq *callwheel;
62 int softticks; /* Like ticks, but for softclock(). */
64 static struct callout * volatile nextsoftcheck; /* Next callout to checked. */
67 * The callout mechanism is based on the work of Adam M. Costello and
68 * George Varghese, published in a technical report entitled "Redesigning
69 * the BSD Callout and Timer Facilities" and modified slightly for inclusion
70 * in FreeBSD by Justin T. Gibbs. The original work on the data structures
71 * used in this implementation was published by G. Varghese and T. Lauck in
72 * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
73 * the Efficient Implementation of a Timer Facility" in the Proceedings of
74 * the 11th ACM Annual Symposium on Operating Systems Principles,
75 * Austin, Texas Nov 1987.
79 * Software (low priority) clock interrupt.
80 * Run periodic events from timeout queue.
83 swi_softclock(void *dummy)
86 struct callout_tailq *bucket;
88 int steps; /* #steps since we last allowed interrupts */
90 #ifndef MAX_SOFTCLOCK_STEPS
91 #define MAX_SOFTCLOCK_STEPS 100 /* Maximum allowed value of steps. */
92 #endif /* MAX_SOFTCLOCK_STEPS */
96 while (softticks != ticks) {
99 * softticks may be modified by hard clock, so cache
100 * it while we work on a given bucket.
102 curticks = softticks;
103 bucket = &callwheel[curticks & callwheelmask];
104 c = TAILQ_FIRST(bucket);
106 if (c->c_time != curticks) {
107 c = TAILQ_NEXT(c, c_links.tqe);
109 if (steps >= MAX_SOFTCLOCK_STEPS) {
111 /* Give interrupts a chance. */
118 void (*c_func)(void *);
121 nextsoftcheck = TAILQ_NEXT(c, c_links.tqe);
122 TAILQ_REMOVE(bucket, c, c_links.tqe);
126 if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
127 c->c_flags = CALLOUT_LOCAL_ALLOC;
128 SLIST_INSERT_HEAD(&callfree, c,
132 (c->c_flags & ~CALLOUT_PENDING);
142 nextsoftcheck = NULL;
148 * Execute a function after a specified length of time.
151 * Cancel previous timeout function call.
153 * callout_handle_init --
154 * Initialize a handle so that using it with untimeout is benign.
156 * See AT&T BCI Driver Reference Manual for specification. This
157 * implementation differs from that one in that although an
158 * identification value is returned from timeout, the original
159 * arguments to timeout as well as the identifier are used to
160 * identify entries for untimeout.
162 struct callout_handle
163 timeout(timeout_t *ftn, void *arg, int to_ticks)
167 struct callout_handle handle;
171 /* Fill in the next free callout structure. */
172 new = SLIST_FIRST(&callfree);
174 /* XXX Attempt to malloc first */
175 panic("timeout table full");
176 SLIST_REMOVE_HEAD(&callfree, c_links.sle);
178 callout_reset(new, to_ticks, ftn, arg);
180 handle.callout = new;
186 untimeout(timeout_t *ftn, void *arg, struct callout_handle handle)
191 * Check for a handle that was initialized
192 * by callout_handle_init, but never used
193 * for a real timeout.
195 if (handle.callout == NULL)
199 if (handle.callout->c_func == ftn && handle.callout->c_arg == arg)
200 callout_stop(handle.callout);
205 callout_handle_init(struct callout_handle *handle)
207 handle->callout = NULL;
211 * New interface; clients allocate their own callout structures.
213 * callout_reset() - establish or change a timeout
214 * callout_stop() - disestablish a timeout
215 * callout_init() - initialize a callout structure so that it can
216 * safely be passed to callout_reset() and callout_stop()
218 * <sys/callout.h> defines three convenience macros:
220 * callout_active() - returns truth if callout has not been serviced
221 * callout_pending() - returns truth if callout is still waiting for timeout
222 * callout_deactivate() - marks the callout as having been serviced
225 callout_reset(struct callout *c, int to_ticks,
226 void (*ftn)(void *), void *arg)
231 if (c->c_flags & CALLOUT_PENDING)
235 * We could spl down here and back up at the TAILQ_INSERT_TAIL,
236 * but there's no point since doing this setup doesn't take much
243 c->c_flags |= (CALLOUT_ACTIVE | CALLOUT_PENDING);
245 c->c_time = ticks + to_ticks;
246 TAILQ_INSERT_TAIL(&callwheel[c->c_time & callwheelmask],
253 callout_stop(struct callout *c)
259 * Don't attempt to delete a callout that's not on the queue.
261 if (!(c->c_flags & CALLOUT_PENDING)) {
262 c->c_flags &= ~CALLOUT_ACTIVE;
266 c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING);
268 if (nextsoftcheck == c) {
269 nextsoftcheck = TAILQ_NEXT(c, c_links.tqe);
271 TAILQ_REMOVE(&callwheel[c->c_time & callwheelmask], c, c_links.tqe);
274 if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
275 SLIST_INSERT_HEAD(&callfree, c, c_links.sle);
282 callout_init(struct callout *c)
288 swi_softclock_setup(void *arg)
290 register_swi(SWI_CLOCK, swi_softclock, NULL, "swi_sftclk");
291 swi_setpriority(SWI_CLOCK, TDPRI_SOFT_TIMER);
294 SYSINIT(vm_setup, SI_SUB_CPU, SI_ORDER_ANY, swi_softclock_setup, NULL);
296 #ifdef APM_FIXUP_CALLTODO
298 * Adjust the kernel calltodo timeout list. This routine is used after
299 * an APM resume to recalculate the calltodo timer list values with the
300 * number of hz's we have been sleeping. The next hardclock() will detect
301 * that there are fired timers and run softclock() to execute them.
303 * Please note, I have not done an exhaustive analysis of what code this
304 * might break. I am motivated to have my select()'s and alarm()'s that
305 * have expired during suspend firing upon resume so that the applications
306 * which set the timer can do the maintanence the timer was for as close
307 * as possible to the originally intended time. Testing this code for a
308 * week showed that resuming from a suspend resulted in 22 to 25 timers
309 * firing, which seemed independant on whether the suspend was 2 hours or
310 * 2 days. Your milage may vary. - Ken Key <key@cs.utk.edu>
313 adjust_timeout_calltodo(struct timeval *time_change)
316 unsigned long delta_ticks;
320 * How many ticks were we asleep?
321 * (stolen from tvtohz()).
324 /* Don't do anything */
325 if (time_change->tv_sec < 0)
327 else if (time_change->tv_sec <= LONG_MAX / 1000000)
328 delta_ticks = (time_change->tv_sec * 1000000 +
329 time_change->tv_usec + (tick - 1)) / tick + 1;
330 else if (time_change->tv_sec <= LONG_MAX / hz)
331 delta_ticks = time_change->tv_sec * hz +
332 (time_change->tv_usec + (tick - 1)) / tick + 1;
334 delta_ticks = LONG_MAX;
336 if (delta_ticks > INT_MAX)
337 delta_ticks = INT_MAX;
340 * Now rip through the timer calltodo list looking for timers
344 /* don't collide with softclock() */
346 for (p = calltodo.c_next; p != NULL; p = p->c_next) {
347 p->c_time -= delta_ticks;
349 /* Break if the timer had more time on it than delta_ticks */
353 /* take back the ticks the timer didn't use (p->c_time <= 0) */
354 delta_ticks = -p->c_time;
360 #endif /* APM_FIXUP_CALLTODO */