kernel: Make SMP support default (and non-optional).
[dragonfly.git] / sys / kern / kern_timeout.c
CommitLineData
92b561b7
MD
1/*
2 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34/*
984263bc
MD
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
37 * (c) UNIX System Laboratories, Inc.
38 * All or some portions of this file are derived from material licensed
39 * to the University of California by American Telephone and Telegraph
40 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
41 * the permission of UNIX System Laboratories, Inc.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * From: @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
72 * $FreeBSD: src/sys/kern/kern_timeout.c,v 1.59.2.1 2001/11/13 18:24:52 archie Exp $
92b561b7
MD
73 */
74/*
75 * DRAGONFLY BGL STATUS
76 *
77 * All the API functions should be MP safe.
78 *
79 * The callback functions will be flagged as being MP safe if the
80 * timeout structure is initialized with callout_init_mp() instead of
81 * callout_init().
82 *
83 * The helper threads cannot be made preempt-capable until after we
84 * clean up all the uses of splsoftclock() and related interlocks (which
85 * require the related functions to be MP safe as well).
86 */
87/*
88 * The callout mechanism is based on the work of Adam M. Costello and
89 * George Varghese, published in a technical report entitled "Redesigning
90 * the BSD Callout and Timer Facilities" and modified slightly for inclusion
91 * in FreeBSD by Justin T. Gibbs. The original work on the data structures
92 * used in this implementation was published by G. Varghese and T. Lauck in
93 * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
94 * the Efficient Implementation of a Timer Facility" in the Proceedings of
95 * the 11th ACM Annual Symposium on Operating Systems Principles,
96 * Austin, Texas Nov 1987.
97 *
98 * The per-cpu augmentation was done by Matthew Dillon.
984263bc
MD
99 */
100
101#include <sys/param.h>
102#include <sys/systm.h>
103#include <sys/callout.h>
104#include <sys/kernel.h>
ef0fdad1 105#include <sys/interrupt.h>
4b5f931b 106#include <sys/thread.h>
684a93c4 107
88c4d2f6 108#include <sys/thread2.h>
684a93c4 109#include <sys/mplock2.h>
92b561b7
MD
110
111#ifndef MAX_SOFTCLOCK_STEPS
112#define MAX_SOFTCLOCK_STEPS 100 /* Maximum allowed value of steps. */
113#endif
114
115
116struct softclock_pcpu {
92b561b7
MD
117 struct callout_tailq *callwheel;
118 struct callout * volatile next;
b90e37ec 119 struct callout *running;/* currently running callout */
92b561b7
MD
120 int softticks; /* softticks index */
121 int curticks; /* per-cpu ticks counter */
122 int isrunning;
123 struct thread thread;
124
125};
126
127typedef struct softclock_pcpu *softclock_pcpu_t;
984263bc
MD
128
129/*
130 * TODO:
131 * allocate more timeout table slots when table overflows.
132 */
92b561b7
MD
133static MALLOC_DEFINE(M_CALLOUT, "callout", "callout structures");
134static int callwheelsize;
92b561b7
MD
135static int callwheelmask;
136static struct softclock_pcpu softclock_pcpu_ary[MAXCPU];
137
138static void softclock_handler(void *arg);
139
140static void
141swi_softclock_setup(void *arg)
142{
143 int cpu;
144 int i;
984263bc 145
92b561b7
MD
146 /*
147 * Figure out how large a callwheel we need. It must be a power of 2.
148 */
149 callwheelsize = 1;
8c7a5b07 150 while (callwheelsize < ncallout)
92b561b7 151 callwheelsize <<= 1;
92b561b7 152 callwheelmask = callwheelsize - 1;
984263bc 153
92b561b7
MD
154 /*
155 * Initialize per-cpu data structures.
156 */
157 for (cpu = 0; cpu < ncpus; ++cpu) {
158 softclock_pcpu_t sc;
92b561b7
MD
159
160 sc = &softclock_pcpu_ary[cpu];
161
77652cad 162 sc->callwheel = kmalloc(sizeof(*sc->callwheel) * callwheelsize,
92b561b7
MD
163 M_CALLOUT, M_WAITOK|M_ZERO);
164 for (i = 0; i < callwheelsize; ++i)
165 TAILQ_INIT(&sc->callwheel[i]);
166
92b561b7 167 /*
c1d0893d
MD
168 * Mark the softclock handler as being an interrupt thread
169 * even though it really isn't, but do not allow it to
170 * preempt other threads (do not assign td_preemptable).
171 *
172 * Kernel code now assumes that callouts do not preempt
173 * the cpu they were scheduled on.
92b561b7
MD
174 */
175 lwkt_create(softclock_handler, sc, NULL,
4643740a 176 &sc->thread, TDF_NOSTART | TDF_INTTHREAD,
c9e9fb21 177 cpu, "softclock %d", cpu);
92b561b7
MD
178 }
179}
180
ed99ec35
MD
181/*
182 * Must occur after ncpus has been initialized.
183 */
ba39e2e0
MD
184SYSINIT(softclock_setup, SI_BOOT2_SOFTCLOCK, SI_ORDER_SECOND,
185 swi_softclock_setup, NULL);
984263bc
MD
186
187/*
92b561b7
MD
188 * This routine is called from the hardclock() (basically a FASTint/IPI) on
189 * each cpu in the system. sc->curticks is this cpu's notion of the timebase.
190 * It IS NOT NECESSARILY SYNCHRONIZED WITH 'ticks'! sc->softticks is where
191 * the callwheel is currently indexed.
192 *
193 * WARNING! The MP lock is not necessarily held on call, nor can it be
194 * safely obtained.
195 *
196 * sc->softticks is adjusted by either this routine or our helper thread
197 * depending on whether the helper thread is running or not.
984263bc 198 */
92b561b7
MD
199void
200hardclock_softtick(globaldata_t gd)
201{
202 softclock_pcpu_t sc;
203
204 sc = &softclock_pcpu_ary[gd->gd_cpuid];
205 ++sc->curticks;
206 if (sc->isrunning)
207 return;
208 if (sc->softticks == sc->curticks) {
209 /*
210 * in sync, only wakeup the thread if there is something to
211 * do.
212 */
213 if (TAILQ_FIRST(&sc->callwheel[sc->softticks & callwheelmask]))
214 {
215 sc->isrunning = 1;
216 lwkt_schedule(&sc->thread);
217 } else {
218 ++sc->softticks;
219 }
220 } else {
221 /*
222 * out of sync, wakeup the thread unconditionally so it can
223 * catch up.
224 */
225 sc->isrunning = 1;
226 lwkt_schedule(&sc->thread);
227 }
228}
984263bc
MD
229
230/*
92b561b7
MD
231 * This procedure is the main loop of our per-cpu helper thread. The
232 * sc->isrunning flag prevents us from racing hardclock_softtick() and
4ac1a4cd
MD
233 * a critical section is sufficient to interlock sc->curticks and protect
234 * us from remote IPI's / list removal.
92b561b7 235 *
c9e9fb21
MD
236 * The thread starts with the MP lock released and not in a critical
237 * section. The loop itself is MP safe while individual callbacks
238 * may or may not be, so we obtain or release the MP lock as appropriate.
984263bc 239 */
ef0fdad1 240static void
92b561b7 241softclock_handler(void *arg)
984263bc 242{
92b561b7 243 softclock_pcpu_t sc;
1fd87d54
RG
244 struct callout *c;
245 struct callout_tailq *bucket;
92b561b7
MD
246 void (*c_func)(void *);
247 void *c_arg;
c9e9fb21 248 int mpsafe = 1;
92b561b7 249
d2d8515b
MD
250 /*
251 * Run the callout thread at the same priority as other kernel
252 * threads so it can be round-robined.
253 */
254 /*lwkt_setpri_self(TDPRI_SOFT_NORM);*/
abb6f10a 255
92b561b7 256 sc = arg;
88c4d2f6 257 crit_enter();
92b561b7
MD
258loop:
259 while (sc->softticks != (int)(sc->curticks + 1)) {
260 bucket = &sc->callwheel[sc->softticks & callwheelmask];
261
262 for (c = TAILQ_FIRST(bucket); c; c = sc->next) {
4ac1a4cd
MD
263 if (c->c_time != sc->softticks) {
264 sc->next = TAILQ_NEXT(c, c_links.tqe);
92b561b7 265 continue;
4ac1a4cd 266 }
4ac1a4cd
MD
267 if (c->c_flags & CALLOUT_MPSAFE) {
268 if (mpsafe == 0) {
269 mpsafe = 1;
270 rel_mplock();
271 }
272 } else {
273 /*
274 * The request might be removed while we
275 * are waiting to get the MP lock. If it
276 * was removed sc->next will point to the
277 * next valid request or NULL, loop up.
278 */
279 if (mpsafe) {
280 mpsafe = 0;
281 sc->next = c;
282 get_mplock();
283 if (c != sc->next)
284 continue;
285 }
286 }
4ac1a4cd 287 sc->next = TAILQ_NEXT(c, c_links.tqe);
92b561b7 288 TAILQ_REMOVE(bucket, c, c_links.tqe);
4ac1a4cd 289
b90e37ec 290 sc->running = c;
92b561b7
MD
291 c_func = c->c_func;
292 c_arg = c->c_arg;
92b561b7
MD
293 c->c_func = NULL;
294 KKASSERT(c->c_flags & CALLOUT_DID_INIT);
7eb3b2ae 295 c->c_flags &= ~CALLOUT_PENDING;
92b561b7 296 crit_exit();
92b561b7
MD
297 c_func(c_arg);
298 crit_enter();
b90e37ec 299 sc->running = NULL;
92b561b7 300 /* NOTE: list may have changed */
984263bc 301 }
92b561b7 302 ++sc->softticks;
984263bc 303 }
92b561b7
MD
304 sc->isrunning = 0;
305 lwkt_deschedule_self(&sc->thread); /* == curthread */
306 lwkt_switch();
307 goto loop;
308 /* NOT REACHED */
984263bc
MD
309}
310
984263bc
MD
311/*
312 * New interface; clients allocate their own callout structures.
313 *
314 * callout_reset() - establish or change a timeout
315 * callout_stop() - disestablish a timeout
316 * callout_init() - initialize a callout structure so that it can
92b561b7
MD
317 * safely be passed to callout_reset() and callout_stop()
318 * callout_init_mp() - same but any installed functions must be MP safe.
984263bc
MD
319 *
320 * <sys/callout.h> defines three convenience macros:
321 *
322 * callout_active() - returns truth if callout has not been serviced
323 * callout_pending() - returns truth if callout is still waiting for timeout
324 * callout_deactivate() - marks the callout as having been serviced
325 */
92b561b7
MD
326
327/*
328 * Start or restart a timeout. Install the callout structure in the
329 * callwheel. Callers may legally pass any value, even if 0 or negative,
330 * but since the sc->curticks index may have already been processed a
331 * minimum timeout of 1 tick will be enforced.
332 *
333 * The callout is installed on and will be processed on the current cpu's
334 * callout wheel.
7ac978db
MD
335 *
336 * WARNING! This function may be called from any cpu but the caller must
337 * serialize callout_stop() and callout_reset() calls on the passed
338 * structure regardless of cpu.
92b561b7 339 */
984263bc 340void
92b561b7
MD
341callout_reset(struct callout *c, int to_ticks, void (*ftn)(void *),
342 void *arg)
984263bc 343{
92b561b7
MD
344 softclock_pcpu_t sc;
345 globaldata_t gd;
346
347#ifdef INVARIANTS
348 if ((c->c_flags & CALLOUT_DID_INIT) == 0) {
349 callout_init(c);
6ea70f76 350 kprintf(
92b561b7
MD
351 "callout_reset(%p) from %p: callout was not initialized\n",
352 c, ((int **)&c)[-1]);
7ce2998e 353 print_backtrace(-1);
92b561b7
MD
354 }
355#endif
356 gd = mycpu;
357 sc = &softclock_pcpu_ary[gd->gd_cpuid];
358 crit_enter_gd(gd);
984263bc 359
b90e37ec 360 if (c->c_flags & CALLOUT_ACTIVE)
984263bc
MD
361 callout_stop(c);
362
984263bc
MD
363 if (to_ticks <= 0)
364 to_ticks = 1;
365
366 c->c_arg = arg;
367 c->c_flags |= (CALLOUT_ACTIVE | CALLOUT_PENDING);
368 c->c_func = ftn;
92b561b7 369 c->c_time = sc->curticks + to_ticks;
92b561b7 370 c->c_gd = gd;
92b561b7
MD
371
372 TAILQ_INSERT_TAIL(&sc->callwheel[c->c_time & callwheelmask],
984263bc 373 c, c_links.tqe);
92b561b7 374 crit_exit_gd(gd);
984263bc
MD
375}
376
ffdce284
SZ
377struct callout_remote_arg {
378 struct callout *c;
379 void (*ftn)(void *);
380 void *arg;
381 int to_ticks;
382};
383
384static void
385callout_reset_ipi(void *arg)
386{
387 struct callout_remote_arg *rmt = arg;
388
389 callout_reset(rmt->c, rmt->to_ticks, rmt->ftn, rmt->arg);
390}
391
ffdce284
SZ
392void
393callout_reset_bycpu(struct callout *c, int to_ticks, void (*ftn)(void *),
394 void *arg, int cpuid)
395{
396 KASSERT(cpuid >= 0 && cpuid < ncpus, ("invalid cpuid %d", cpuid));
397
ffdce284
SZ
398 if (cpuid == mycpuid) {
399 callout_reset(c, to_ticks, ftn, arg);
400 } else {
401 struct globaldata *target_gd;
402 struct callout_remote_arg rmt;
403 int seq;
404
405 rmt.c = c;
406 rmt.ftn = ftn;
407 rmt.arg = arg;
408 rmt.to_ticks = to_ticks;
409
410 target_gd = globaldata_find(cpuid);
411
412 seq = lwkt_send_ipiq(target_gd, callout_reset_ipi, &rmt);
413 lwkt_wait_ipiq(target_gd, seq);
414 }
ffdce284
SZ
415}
416
92b561b7
MD
417/*
418 * Stop a running timer. WARNING! If called on a cpu other then the one
419 * the callout was started on this function will liveloop on its IPI to
420 * the target cpu to process the request. It is possible for the callout
421 * to execute in that case.
422 *
7ac978db
MD
423 * WARNING! This function may be called from any cpu but the caller must
424 * serialize callout_stop() and callout_reset() calls on the passed
425 * structure regardless of cpu.
426 *
92b561b7 427 * WARNING! This routine may be called from an IPI
b90e37ec
MD
428 *
429 * WARNING! This function can return while it's c_func is still running
430 * in the callout thread, a secondary check may be needed.
c5b9d2a5
MD
431 * Use callout_stop_sync() to wait for any callout function to
432 * complete before returning, being sure that no deadlock is
433 * possible if you do.
92b561b7 434 */
984263bc 435int
216ffe3f 436callout_stop(struct callout *c)
984263bc 437{
92b561b7 438 globaldata_t gd = mycpu;
92b561b7 439 globaldata_t tgd;
92b561b7
MD
440 softclock_pcpu_t sc;
441
442#ifdef INVARIANTS
443 if ((c->c_flags & CALLOUT_DID_INIT) == 0) {
444 callout_init(c);
6ea70f76 445 kprintf(
634056cf 446 "callout_stop(%p) from %p: callout was not initialized\n",
92b561b7 447 c, ((int **)&c)[-1]);
7ce2998e 448 print_backtrace(-1);
92b561b7
MD
449 }
450#endif
451 crit_enter_gd(gd);
984263bc 452
984263bc 453 /*
7ac978db
MD
454 * Don't attempt to delete a callout that's not on the queue. The
455 * callout may not have a cpu assigned to it. Callers do not have
456 * to be on the issuing cpu but must still serialize access to the
457 * callout structure.
458 *
459 * We are not cpu-localized here and cannot safely modify the
460 * flags field in the callout structure. Note that most of the
461 * time CALLOUT_ACTIVE will be 0 if CALLOUT_PENDING is also 0.
462 *
463 * If we race another cpu's dispatch of this callout it is possible
464 * for CALLOUT_ACTIVE to be set with CALLOUT_PENDING unset. This
465 * will cause us to fall through and synchronize with the other
466 * cpu.
984263bc 467 */
92b561b7 468 if ((c->c_flags & CALLOUT_PENDING) == 0) {
7ac978db
MD
469 if ((c->c_flags & CALLOUT_ACTIVE) == 0) {
470 crit_exit_gd(gd);
471 return (0);
472 }
473 if (c->c_gd == NULL || c->c_gd == gd) {
474 c->c_flags &= ~CALLOUT_ACTIVE;
475 crit_exit_gd(gd);
476 return (0);
477 }
984263bc 478 }
92b561b7
MD
479 if ((tgd = c->c_gd) != gd) {
480 /*
481 * If the callout is owned by a different CPU we have to
482 * execute the function synchronously on the target cpu.
483 */
484 int seq;
485
35238fa5 486 cpu_ccfence(); /* don't let tgd alias c_gd */
92b561b7
MD
487 seq = lwkt_send_ipiq(tgd, (void *)callout_stop, c);
488 lwkt_wait_ipiq(tgd, seq);
1918fc5c 489 } else {
92b561b7
MD
490 /*
491 * If the callout is owned by the same CPU we can
492 * process it directly, but if we are racing our helper
493 * thread (sc->next), we have to adjust sc->next. The
494 * race is interlocked by a critical section.
495 */
496 sc = &softclock_pcpu_ary[gd->gd_cpuid];
984263bc 497
92b561b7
MD
498 c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING);
499 if (sc->next == c)
500 sc->next = TAILQ_NEXT(c, c_links.tqe);
501
502 TAILQ_REMOVE(&sc->callwheel[c->c_time & callwheelmask],
503 c, c_links.tqe);
504 c->c_func = NULL;
984263bc 505 }
92b561b7 506 crit_exit_gd(gd);
984263bc
MD
507 return (1);
508}
509
c5b9d2a5
MD
510/*
511 * Issue a callout_stop() and ensure that any callout race completes
512 * before returning. Does NOT de-initialized the callout.
513 */
514void
515callout_stop_sync(struct callout *c)
516{
517 softclock_pcpu_t sc;
518
7881367c 519 while (c->c_flags & CALLOUT_DID_INIT) {
c5b9d2a5 520 callout_stop(c);
a471eac5
MD
521 if (c->c_gd) {
522 sc = &softclock_pcpu_ary[c->c_gd->gd_cpuid];
523 if (sc->running == c) {
524 while (sc->running == c)
525 tsleep(&sc->running, 0, "crace", 1);
526 }
527 }
7881367c
MD
528 if ((c->c_flags & (CALLOUT_PENDING | CALLOUT_ACTIVE)) == 0)
529 break;
530 kprintf("Warning: %s: callout race\n", curthread->td_comm);
c5b9d2a5
MD
531 }
532}
533
b90e37ec
MD
534/*
535 * Terminate a callout
536 *
537 * This function will stop any pending callout and also block while the
538 * callout's function is running. It should only be used in cases where
539 * no deadlock is possible (due to the callout function acquiring locks
540 * that the current caller of callout_terminate() already holds), when
541 * the caller is ready to destroy the callout structure.
542 *
543 * This function clears the CALLOUT_DID_INIT flag.
544 *
545 * lwkt_token locks are ok.
546 */
547void
548callout_terminate(struct callout *c)
549{
550 softclock_pcpu_t sc;
551
552 if (c->c_flags & CALLOUT_DID_INIT) {
553 callout_stop(c);
554 sc = &softclock_pcpu_ary[c->c_gd->gd_cpuid];
555 if (sc->running == c) {
b90e37ec
MD
556 while (sc->running == c)
557 tsleep(&sc->running, 0, "crace", 1);
558 }
559 KKASSERT((c->c_flags & (CALLOUT_PENDING|CALLOUT_ACTIVE)) == 0);
560 c->c_flags &= ~CALLOUT_DID_INIT;
561 }
562}
563
92b561b7
MD
564/*
565 * Prepare a callout structure for use by callout_reset() and/or
566 * callout_stop(). The MP version of this routine requires that the callback
986d8a07 567 * function installed by callout_reset() be MP safe.
48e7b118
MD
568 *
569 * The init functions can be called from any cpu and do not have to be
570 * called from the cpu that the timer will eventually run on.
92b561b7 571 */
984263bc 572void
216ffe3f 573callout_init(struct callout *c)
984263bc
MD
574{
575 bzero(c, sizeof *c);
92b561b7 576 c->c_flags = CALLOUT_DID_INIT;
984263bc
MD
577}
578
92b561b7
MD
579void
580callout_init_mp(struct callout *c)
ef0fdad1 581{
92b561b7
MD
582 callout_init(c);
583 c->c_flags |= CALLOUT_MPSAFE;
ef0fdad1
MD
584}
585
92b561b7
MD
586/* What, are you joking? This is nuts! -Matt */
587#if 0
984263bc
MD
588#ifdef APM_FIXUP_CALLTODO
589/*
590 * Adjust the kernel calltodo timeout list. This routine is used after
591 * an APM resume to recalculate the calltodo timer list values with the
592 * number of hz's we have been sleeping. The next hardclock() will detect
593 * that there are fired timers and run softclock() to execute them.
594 *
595 * Please note, I have not done an exhaustive analysis of what code this
596 * might break. I am motivated to have my select()'s and alarm()'s that
597 * have expired during suspend firing upon resume so that the applications
598 * which set the timer can do the maintanence the timer was for as close
599 * as possible to the originally intended time. Testing this code for a
600 * week showed that resuming from a suspend resulted in 22 to 25 timers
601 * firing, which seemed independant on whether the suspend was 2 hours or
602 * 2 days. Your milage may vary. - Ken Key <key@cs.utk.edu>
603 */
604void
216ffe3f 605adjust_timeout_calltodo(struct timeval *time_change)
984263bc 606{
1fd87d54 607 struct callout *p;
984263bc 608 unsigned long delta_ticks;
984263bc
MD
609
610 /*
611 * How many ticks were we asleep?
612 * (stolen from tvtohz()).
613 */
614
615 /* Don't do anything */
616 if (time_change->tv_sec < 0)
617 return;
618 else if (time_change->tv_sec <= LONG_MAX / 1000000)
619 delta_ticks = (time_change->tv_sec * 1000000 +
620 time_change->tv_usec + (tick - 1)) / tick + 1;
621 else if (time_change->tv_sec <= LONG_MAX / hz)
622 delta_ticks = time_change->tv_sec * hz +
623 (time_change->tv_usec + (tick - 1)) / tick + 1;
624 else
625 delta_ticks = LONG_MAX;
626
627 if (delta_ticks > INT_MAX)
628 delta_ticks = INT_MAX;
629
630 /*
631 * Now rip through the timer calltodo list looking for timers
632 * to expire.
633 */
634
635 /* don't collide with softclock() */
e43a034f 636 crit_enter();
984263bc
MD
637 for (p = calltodo.c_next; p != NULL; p = p->c_next) {
638 p->c_time -= delta_ticks;
639
640 /* Break if the timer had more time on it than delta_ticks */
641 if (p->c_time > 0)
642 break;
643
644 /* take back the ticks the timer didn't use (p->c_time <= 0) */
645 delta_ticks = -p->c_time;
646 }
e43a034f 647 crit_exit();
984263bc
MD
648
649 return;
650}
651#endif /* APM_FIXUP_CALLTODO */
92b561b7
MD
652#endif
653