ccab213283457666d0279b736c2972de290bb20c
[dragonfly.git] / sys / kern / kern_timeout.c
1 /*-
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.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
25  *
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
36  * SUCH DAMAGE.
37  *
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.2 2003/06/17 04:28:41 dillon Exp $
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/callout.h>
46 #include <sys/kernel.h>
47
48 /*
49  * TODO:
50  *      allocate more timeout table slots when table overflows.
51  */
52
53 /* Exported to machdep.c and/or kern_clock.c.  */
54 struct callout *callout;
55 struct callout_list callfree;
56 int callwheelsize, callwheelbits, callwheelmask;
57 struct callout_tailq *callwheel;
58 int softticks;                  /* Like ticks, but for softclock(). */
59
60 static struct callout *nextsoftcheck;   /* Next callout to be checked. */
61
62 /*
63  * The callout mechanism is based on the work of Adam M. Costello and 
64  * George Varghese, published in a technical report entitled "Redesigning
65  * the BSD Callout and Timer Facilities" and modified slightly for inclusion
66  * in FreeBSD by Justin T. Gibbs.  The original work on the data structures
67  * used in this implementation was published by G.Varghese and A. Lauck in
68  * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
69  * the Efficient Implementation of a Timer Facility" in the Proceedings of
70  * the 11th ACM Annual Symposium on Operating Systems Principles,
71  * Austin, Texas Nov 1987.
72  */
73
74 /*
75  * Software (low priority) clock interrupt.
76  * Run periodic events from timeout queue.
77  */
78 void
79 softclock()
80 {
81         register struct callout *c;
82         register struct callout_tailq *bucket;
83         register int s;
84         register int curticks;
85         register int steps;     /* #steps since we last allowed interrupts */
86
87 #ifndef MAX_SOFTCLOCK_STEPS
88 #define MAX_SOFTCLOCK_STEPS 100 /* Maximum allowed value of steps. */
89 #endif /* MAX_SOFTCLOCK_STEPS */
90
91         steps = 0;
92         s = splhigh();
93         while (softticks != ticks) {
94                 softticks++;
95                 /*
96                  * softticks may be modified by hard clock, so cache
97                  * it while we work on a given bucket.
98                  */
99                 curticks = softticks;
100                 bucket = &callwheel[curticks & callwheelmask];
101                 c = TAILQ_FIRST(bucket);
102                 while (c) {
103                         if (c->c_time != curticks) {
104                                 c = TAILQ_NEXT(c, c_links.tqe);
105                                 ++steps;
106                                 if (steps >= MAX_SOFTCLOCK_STEPS) {
107                                         nextsoftcheck = c;
108                                         /* Give interrupts a chance. */
109                                         splx(s);
110                                         s = splhigh();
111                                         c = nextsoftcheck;
112                                         steps = 0;
113                                 }
114                         } else {
115                                 void (*c_func)(void *);
116                                 void *c_arg;
117
118                                 nextsoftcheck = TAILQ_NEXT(c, c_links.tqe);
119                                 TAILQ_REMOVE(bucket, c, c_links.tqe);
120                                 c_func = c->c_func;
121                                 c_arg = c->c_arg;
122                                 c->c_func = NULL;
123                                 if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
124                                         c->c_flags = CALLOUT_LOCAL_ALLOC;
125                                         SLIST_INSERT_HEAD(&callfree, c,
126                                                           c_links.sle);
127                                 } else {
128                                         c->c_flags =
129                                             (c->c_flags & ~CALLOUT_PENDING);
130                                 }
131                                 splx(s);
132                                 c_func(c_arg);
133                                 s = splhigh();
134                                 steps = 0;
135                                 c = nextsoftcheck;
136                         }
137                 }
138         }
139         nextsoftcheck = NULL;
140         splx(s);
141 }
142
143 /*
144  * timeout --
145  *      Execute a function after a specified length of time.
146  *
147  * untimeout --
148  *      Cancel previous timeout function call.
149  *
150  * callout_handle_init --
151  *      Initialize a handle so that using it with untimeout is benign.
152  *
153  *      See AT&T BCI Driver Reference Manual for specification.  This
154  *      implementation differs from that one in that although an 
155  *      identification value is returned from timeout, the original
156  *      arguments to timeout as well as the identifier are used to
157  *      identify entries for untimeout.
158  */
159 struct callout_handle
160 timeout(ftn, arg, to_ticks)
161         timeout_t *ftn;
162         void *arg;
163         register int to_ticks;
164 {
165         int s;
166         struct callout *new;
167         struct callout_handle handle;
168
169         s = splhigh();
170
171         /* Fill in the next free callout structure. */
172         new = SLIST_FIRST(&callfree);
173         if (new == NULL)
174                 /* XXX Attempt to malloc first */
175                 panic("timeout table full");
176         SLIST_REMOVE_HEAD(&callfree, c_links.sle);
177         
178         callout_reset(new, to_ticks, ftn, arg);
179
180         handle.callout = new;
181         splx(s);
182         return (handle);
183 }
184
185 void
186 untimeout(ftn, arg, handle)
187         timeout_t *ftn;
188         void *arg;
189         struct callout_handle handle;
190 {
191         register int s;
192
193         /*
194          * Check for a handle that was initialized
195          * by callout_handle_init, but never used
196          * for a real timeout.
197          */
198         if (handle.callout == NULL)
199                 return;
200
201         s = splhigh();
202         if (handle.callout->c_func == ftn && handle.callout->c_arg == arg)
203                 callout_stop(handle.callout);
204         splx(s);
205 }
206
207 void
208 callout_handle_init(struct callout_handle *handle)
209 {
210         handle->callout = NULL;
211 }
212
213 /*
214  * New interface; clients allocate their own callout structures.
215  *
216  * callout_reset() - establish or change a timeout
217  * callout_stop() - disestablish a timeout
218  * callout_init() - initialize a callout structure so that it can
219  *      safely be passed to callout_reset() and callout_stop()
220  *
221  * <sys/callout.h> defines three convenience macros:
222  *
223  * callout_active() - returns truth if callout has not been serviced
224  * callout_pending() - returns truth if callout is still waiting for timeout
225  * callout_deactivate() - marks the callout as having been serviced
226  */
227 void
228 callout_reset(c, to_ticks, ftn, arg)
229         struct  callout *c;
230         int     to_ticks;
231         void    (*ftn) __P((void *));
232         void    *arg;
233 {
234         int     s;
235
236         s = splhigh();
237         if (c->c_flags & CALLOUT_PENDING)
238                 callout_stop(c);
239
240         /*
241          * We could spl down here and back up at the TAILQ_INSERT_TAIL,
242          * but there's no point since doing this setup doesn't take much
243          * time.
244          */
245         if (to_ticks <= 0)
246                 to_ticks = 1;
247
248         c->c_arg = arg;
249         c->c_flags |= (CALLOUT_ACTIVE | CALLOUT_PENDING);
250         c->c_func = ftn;
251         c->c_time = ticks + to_ticks;
252         TAILQ_INSERT_TAIL(&callwheel[c->c_time & callwheelmask], 
253                           c, c_links.tqe);
254         splx(s);
255         
256 }
257
258 int
259 callout_stop(c)
260         struct  callout *c;
261 {
262         int     s;
263
264         s = splhigh();
265         /*
266          * Don't attempt to delete a callout that's not on the queue.
267          */
268         if (!(c->c_flags & CALLOUT_PENDING)) {
269                 c->c_flags &= ~CALLOUT_ACTIVE;
270                 splx(s);
271                 return (0);
272         }
273         c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING);
274
275         if (nextsoftcheck == c) {
276                 nextsoftcheck = TAILQ_NEXT(c, c_links.tqe);
277         }
278         TAILQ_REMOVE(&callwheel[c->c_time & callwheelmask], c, c_links.tqe);
279         c->c_func = NULL;
280
281         if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
282                 SLIST_INSERT_HEAD(&callfree, c, c_links.sle);
283         }
284         splx(s);
285         return (1);
286 }
287
288 void
289 callout_init(c)
290         struct  callout *c;
291 {
292         bzero(c, sizeof *c);
293 }
294
295 #ifdef APM_FIXUP_CALLTODO
296 /* 
297  * Adjust the kernel calltodo timeout list.  This routine is used after 
298  * an APM resume to recalculate the calltodo timer list values with the 
299  * number of hz's we have been sleeping.  The next hardclock() will detect 
300  * that there are fired timers and run softclock() to execute them.
301  *
302  * Please note, I have not done an exhaustive analysis of what code this
303  * might break.  I am motivated to have my select()'s and alarm()'s that
304  * have expired during suspend firing upon resume so that the applications
305  * which set the timer can do the maintanence the timer was for as close
306  * as possible to the originally intended time.  Testing this code for a 
307  * week showed that resuming from a suspend resulted in 22 to 25 timers 
308  * firing, which seemed independant on whether the suspend was 2 hours or
309  * 2 days.  Your milage may vary.   - Ken Key <key@cs.utk.edu>
310  */
311 void
312 adjust_timeout_calltodo(time_change)
313     struct timeval *time_change;
314 {
315         register struct callout *p;
316         unsigned long delta_ticks;
317         int s;
318
319         /* 
320          * How many ticks were we asleep?
321          * (stolen from tvtohz()).
322          */
323
324         /* Don't do anything */
325         if (time_change->tv_sec < 0)
326                 return;
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;
333         else
334                 delta_ticks = LONG_MAX;
335
336         if (delta_ticks > INT_MAX)
337                 delta_ticks = INT_MAX;
338
339         /* 
340          * Now rip through the timer calltodo list looking for timers
341          * to expire.
342          */
343
344         /* don't collide with softclock() */
345         s = splhigh(); 
346         for (p = calltodo.c_next; p != NULL; p = p->c_next) {
347                 p->c_time -= delta_ticks;
348
349                 /* Break if the timer had more time on it than delta_ticks */
350                 if (p->c_time > 0)
351                         break;
352
353                 /* take back the ticks the timer didn't use (p->c_time <= 0) */
354                 delta_ticks = -p->c_time;
355         }
356         splx(s);
357
358         return;
359 }
360 #endif /* APM_FIXUP_CALLTODO */