Home | History | Annotate | Line # | Download | only in kern
kern_timeout.c revision 1.16.2.6
      1  1.16.2.6      yamt /*	$NetBSD: kern_timeout.c,v 1.16.2.6 2008/01/21 09:46:15 yamt Exp $	*/
      2       1.1   thorpej 
      3       1.1   thorpej /*-
      4  1.16.2.2      yamt  * Copyright (c) 2003, 2006, 2007 The NetBSD Foundation, Inc.
      5       1.1   thorpej  * All rights reserved.
      6       1.1   thorpej  *
      7       1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.16.2.2      yamt  * by Jason R. Thorpe, and by Andrew Doran.
      9       1.1   thorpej  *
     10       1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     11       1.1   thorpej  * modification, are permitted provided that the following conditions
     12       1.1   thorpej  * are met:
     13       1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     14       1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     15       1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17       1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     18       1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     19       1.1   thorpej  *    must display the following acknowledgement:
     20       1.1   thorpej  *	This product includes software developed by the NetBSD
     21       1.1   thorpej  *	Foundation, Inc. and its contributors.
     22       1.1   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1   thorpej  *    contributors may be used to endorse or promote products derived
     24       1.1   thorpej  *    from this software without specific prior written permission.
     25       1.1   thorpej  *
     26       1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1   thorpej  */
     38       1.1   thorpej 
     39       1.1   thorpej /*
     40       1.1   thorpej  * Copyright (c) 2001 Thomas Nordin <nordin (at) openbsd.org>
     41       1.1   thorpej  * Copyright (c) 2000-2001 Artur Grabowski <art (at) openbsd.org>
     42      1.14     perry  * All rights reserved.
     43      1.14     perry  *
     44      1.14     perry  * Redistribution and use in source and binary forms, with or without
     45      1.14     perry  * modification, are permitted provided that the following conditions
     46      1.14     perry  * are met:
     47       1.1   thorpej  *
     48      1.14     perry  * 1. Redistributions of source code must retain the above copyright
     49      1.14     perry  *    notice, this list of conditions and the following disclaimer.
     50      1.14     perry  * 2. Redistributions in binary form must reproduce the above copyright
     51      1.14     perry  *    notice, this list of conditions and the following disclaimer in the
     52      1.14     perry  *    documentation and/or other materials provided with the distribution.
     53       1.1   thorpej  * 3. The name of the author may not be used to endorse or promote products
     54      1.14     perry  *    derived from this software without specific prior written permission.
     55       1.1   thorpej  *
     56       1.1   thorpej  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     57       1.1   thorpej  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     58       1.1   thorpej  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
     59       1.1   thorpej  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     60       1.1   thorpej  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     61       1.1   thorpej  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     62       1.1   thorpej  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     63       1.1   thorpej  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     64       1.1   thorpej  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     65      1.14     perry  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     66       1.1   thorpej  */
     67       1.7     lukem 
     68       1.7     lukem #include <sys/cdefs.h>
     69  1.16.2.6      yamt __KERNEL_RCSID(0, "$NetBSD: kern_timeout.c,v 1.16.2.6 2008/01/21 09:46:15 yamt Exp $");
     70       1.1   thorpej 
     71       1.1   thorpej /*
     72  1.16.2.2      yamt  * Timeouts are kept in a hierarchical timing wheel.  The c_time is the
     73  1.16.2.2      yamt  * value of the global variable "hardclock_ticks" when the timeout should
     74  1.16.2.2      yamt  * be called.  There are four levels with 256 buckets each. See 'Scheme 7'
     75  1.16.2.2      yamt  * in "Hashed and Hierarchical Timing Wheels: Efficient Data Structures
     76  1.16.2.2      yamt  * for Implementing a Timer Facility" by George Varghese and Tony Lauck.
     77  1.16.2.2      yamt  *
     78  1.16.2.2      yamt  * Some of the "math" in here is a bit tricky.  We have to beware of
     79  1.16.2.2      yamt  * wrapping ints.
     80  1.16.2.2      yamt  *
     81  1.16.2.2      yamt  * We use the fact that any element added to the queue must be added with
     82  1.16.2.2      yamt  * a positive time.  That means that any element `to' on the queue cannot
     83  1.16.2.2      yamt  * be scheduled to timeout further in time than INT_MAX, but c->c_time can
     84  1.16.2.2      yamt  * be positive or negative so comparing it with anything is dangerous.
     85  1.16.2.2      yamt  * The only way we can use the c->c_time value in any predictable way is
     86  1.16.2.2      yamt  * when we calculate how far in the future `to' will timeout - "c->c_time
     87  1.16.2.2      yamt  * - hardclock_ticks".  The result will always be positive for future
     88  1.16.2.2      yamt  * timeouts and 0 or negative for due timeouts.
     89       1.1   thorpej  */
     90       1.1   thorpej 
     91  1.16.2.2      yamt #define	_CALLOUT_PRIVATE
     92  1.16.2.2      yamt 
     93       1.1   thorpej #include <sys/param.h>
     94       1.1   thorpej #include <sys/systm.h>
     95       1.1   thorpej #include <sys/kernel.h>
     96       1.1   thorpej #include <sys/callout.h>
     97  1.16.2.1      yamt #include <sys/mutex.h>
     98  1.16.2.2      yamt #include <sys/proc.h>
     99  1.16.2.2      yamt #include <sys/sleepq.h>
    100  1.16.2.2      yamt #include <sys/syncobj.h>
    101  1.16.2.2      yamt #include <sys/evcnt.h>
    102  1.16.2.3      yamt #include <sys/intr.h>
    103       1.1   thorpej 
    104       1.1   thorpej #ifdef DDB
    105       1.1   thorpej #include <machine/db_machdep.h>
    106       1.1   thorpej #include <ddb/db_interface.h>
    107       1.1   thorpej #include <ddb/db_access.h>
    108       1.1   thorpej #include <ddb/db_sym.h>
    109       1.1   thorpej #include <ddb/db_output.h>
    110       1.1   thorpej #endif
    111       1.1   thorpej 
    112  1.16.2.2      yamt #define BUCKETS		1024
    113  1.16.2.2      yamt #define WHEELSIZE	256
    114  1.16.2.2      yamt #define WHEELMASK	255
    115  1.16.2.2      yamt #define WHEELBITS	8
    116       1.1   thorpej 
    117       1.1   thorpej static struct callout_circq timeout_wheel[BUCKETS];	/* Queues of timeouts */
    118       1.1   thorpej static struct callout_circq timeout_todo;		/* Worklist */
    119       1.1   thorpej 
    120       1.1   thorpej #define MASKWHEEL(wheel, time) (((time) >> ((wheel)*WHEELBITS)) & WHEELMASK)
    121       1.1   thorpej 
    122       1.1   thorpej #define BUCKET(rel, abs)						\
    123       1.1   thorpej     (((rel) <= (1 << (2*WHEELBITS)))					\
    124       1.1   thorpej     	? ((rel) <= (1 << WHEELBITS))					\
    125       1.3  drochner             ? &timeout_wheel[MASKWHEEL(0, (abs))]			\
    126       1.3  drochner             : &timeout_wheel[MASKWHEEL(1, (abs)) + WHEELSIZE]		\
    127       1.1   thorpej         : ((rel) <= (1 << (3*WHEELBITS)))				\
    128       1.3  drochner             ? &timeout_wheel[MASKWHEEL(2, (abs)) + 2*WHEELSIZE]		\
    129       1.3  drochner             : &timeout_wheel[MASKWHEEL(3, (abs)) + 3*WHEELSIZE])
    130       1.1   thorpej 
    131       1.1   thorpej #define MOVEBUCKET(wheel, time)						\
    132       1.1   thorpej     CIRCQ_APPEND(&timeout_todo,						\
    133       1.1   thorpej         &timeout_wheel[MASKWHEEL((wheel), (time)) + (wheel)*WHEELSIZE])
    134       1.1   thorpej 
    135       1.1   thorpej /*
    136       1.1   thorpej  * Circular queue definitions.
    137       1.1   thorpej  */
    138       1.1   thorpej 
    139      1.11       scw #define CIRCQ_INIT(list)						\
    140       1.1   thorpej do {									\
    141      1.11       scw         (list)->cq_next_l = (list);					\
    142      1.11       scw         (list)->cq_prev_l = (list);					\
    143       1.1   thorpej } while (/*CONSTCOND*/0)
    144       1.1   thorpej 
    145       1.1   thorpej #define CIRCQ_INSERT(elem, list)					\
    146       1.1   thorpej do {									\
    147      1.11       scw         (elem)->cq_prev_e = (list)->cq_prev_e;				\
    148      1.11       scw         (elem)->cq_next_l = (list);					\
    149      1.11       scw         (list)->cq_prev_l->cq_next_l = (elem);				\
    150      1.11       scw         (list)->cq_prev_l = (elem);					\
    151       1.1   thorpej } while (/*CONSTCOND*/0)
    152       1.1   thorpej 
    153       1.1   thorpej #define CIRCQ_APPEND(fst, snd)						\
    154       1.1   thorpej do {									\
    155       1.1   thorpej         if (!CIRCQ_EMPTY(snd)) {					\
    156      1.11       scw                 (fst)->cq_prev_l->cq_next_l = (snd)->cq_next_l;		\
    157      1.11       scw                 (snd)->cq_next_l->cq_prev_l = (fst)->cq_prev_l;		\
    158      1.11       scw                 (snd)->cq_prev_l->cq_next_l = (fst);			\
    159      1.11       scw                 (fst)->cq_prev_l = (snd)->cq_prev_l;			\
    160       1.1   thorpej                 CIRCQ_INIT(snd);					\
    161       1.1   thorpej         }								\
    162       1.1   thorpej } while (/*CONSTCOND*/0)
    163       1.1   thorpej 
    164       1.1   thorpej #define CIRCQ_REMOVE(elem)						\
    165       1.1   thorpej do {									\
    166      1.11       scw         (elem)->cq_next_l->cq_prev_e = (elem)->cq_prev_e;		\
    167      1.11       scw         (elem)->cq_prev_l->cq_next_e = (elem)->cq_next_e;		\
    168       1.1   thorpej } while (/*CONSTCOND*/0)
    169       1.1   thorpej 
    170      1.11       scw #define CIRCQ_FIRST(list)	((list)->cq_next_e)
    171      1.11       scw #define CIRCQ_NEXT(elem)	((elem)->cq_next_e)
    172      1.11       scw #define CIRCQ_LAST(elem,list)	((elem)->cq_next_l == (list))
    173      1.11       scw #define CIRCQ_EMPTY(list)	((list)->cq_next_l == (list))
    174       1.1   thorpej 
    175  1.16.2.2      yamt static void	callout_softclock(void *);
    176  1.16.2.2      yamt 
    177       1.1   thorpej /*
    178  1.16.2.2      yamt  * All wheels are locked with the same lock (which must also block out
    179  1.16.2.2      yamt  * all interrupts).  Eventually this should become per-CPU.
    180       1.1   thorpej  */
    181  1.16.2.2      yamt kmutex_t callout_lock;
    182  1.16.2.2      yamt sleepq_t callout_sleepq;
    183  1.16.2.2      yamt void	*callout_si;
    184       1.1   thorpej 
    185       1.5   thorpej static struct evcnt callout_ev_late;
    186  1.16.2.2      yamt static struct evcnt callout_ev_block;
    187       1.5   thorpej 
    188       1.1   thorpej /*
    189  1.16.2.1      yamt  * callout_barrier:
    190  1.16.2.1      yamt  *
    191  1.16.2.2      yamt  *	If the callout is already running, wait until it completes.
    192  1.16.2.2      yamt  *	XXX This should do priority inheritance.
    193  1.16.2.1      yamt  */
    194  1.16.2.2      yamt static void
    195  1.16.2.2      yamt callout_barrier(callout_impl_t *c)
    196  1.16.2.1      yamt {
    197  1.16.2.2      yamt 	extern syncobj_t sleep_syncobj;
    198  1.16.2.2      yamt 	struct cpu_info *ci;
    199  1.16.2.2      yamt 	struct lwp *l;
    200  1.16.2.2      yamt 
    201  1.16.2.2      yamt 	l = curlwp;
    202  1.16.2.2      yamt 
    203  1.16.2.2      yamt 	if ((c->c_flags & CALLOUT_MPSAFE) == 0) {
    204  1.16.2.2      yamt 		/*
    205  1.16.2.2      yamt 		 * Note: we must be called with the kernel lock held,
    206  1.16.2.2      yamt 		 * as we use it to synchronize with callout_softclock().
    207  1.16.2.2      yamt 		 */
    208  1.16.2.2      yamt 		ci = c->c_oncpu;
    209  1.16.2.2      yamt 		ci->ci_data.cpu_callout_cancel = c;
    210  1.16.2.2      yamt 		return;
    211  1.16.2.2      yamt 	}
    212  1.16.2.1      yamt 
    213  1.16.2.2      yamt 	while ((ci = c->c_oncpu) != NULL && ci->ci_data.cpu_callout == c) {
    214  1.16.2.2      yamt 		KASSERT(l->l_wchan == NULL);
    215  1.16.2.1      yamt 
    216  1.16.2.2      yamt 		ci->ci_data.cpu_callout_nwait++;
    217  1.16.2.2      yamt 		callout_ev_block.ev_count++;
    218  1.16.2.2      yamt 
    219  1.16.2.4      yamt 		l->l_kpriority = true;
    220  1.16.2.2      yamt 		sleepq_enter(&callout_sleepq, l);
    221  1.16.2.4      yamt 		sleepq_enqueue(&callout_sleepq, ci, "callout", &sleep_syncobj);
    222  1.16.2.2      yamt 		sleepq_block(0, false);
    223  1.16.2.2      yamt 		mutex_spin_enter(&callout_lock);
    224  1.16.2.1      yamt 	}
    225  1.16.2.2      yamt }
    226  1.16.2.2      yamt 
    227  1.16.2.2      yamt /*
    228  1.16.2.2      yamt  * callout_running:
    229  1.16.2.2      yamt  *
    230  1.16.2.2      yamt  *	Return non-zero if callout 'c' is currently executing.
    231  1.16.2.2      yamt  */
    232  1.16.2.2      yamt static inline bool
    233  1.16.2.2      yamt callout_running(callout_impl_t *c)
    234  1.16.2.2      yamt {
    235  1.16.2.2      yamt 	struct cpu_info *ci;
    236  1.16.2.2      yamt 
    237  1.16.2.2      yamt 	if ((ci = c->c_oncpu) == NULL)
    238  1.16.2.2      yamt 		return false;
    239  1.16.2.2      yamt 	if (ci->ci_data.cpu_callout != c)
    240  1.16.2.2      yamt 		return false;
    241  1.16.2.2      yamt 	if (c->c_onlwp == curlwp)
    242  1.16.2.2      yamt 		return false;
    243  1.16.2.2      yamt 	return true;
    244  1.16.2.1      yamt }
    245  1.16.2.1      yamt 
    246  1.16.2.1      yamt /*
    247       1.1   thorpej  * callout_startup:
    248       1.1   thorpej  *
    249       1.1   thorpej  *	Initialize the callout facility, called at system startup time.
    250       1.1   thorpej  */
    251       1.1   thorpej void
    252       1.1   thorpej callout_startup(void)
    253       1.1   thorpej {
    254       1.1   thorpej 	int b;
    255       1.1   thorpej 
    256  1.16.2.2      yamt 	KASSERT(sizeof(callout_impl_t) <= sizeof(callout_t));
    257  1.16.2.2      yamt 
    258       1.1   thorpej 	CIRCQ_INIT(&timeout_todo);
    259       1.1   thorpej 	for (b = 0; b < BUCKETS; b++)
    260       1.1   thorpej 		CIRCQ_INIT(&timeout_wheel[b]);
    261       1.5   thorpej 
    262  1.16.2.5      yamt 	mutex_init(&callout_lock, MUTEX_DEFAULT, IPL_SCHED);
    263  1.16.2.2      yamt 	sleepq_init(&callout_sleepq, &callout_lock);
    264  1.16.2.2      yamt 
    265       1.5   thorpej 	evcnt_attach_dynamic(&callout_ev_late, EVCNT_TYPE_MISC,
    266       1.5   thorpej 	    NULL, "callout", "late");
    267  1.16.2.2      yamt 	evcnt_attach_dynamic(&callout_ev_block, EVCNT_TYPE_MISC,
    268  1.16.2.2      yamt 	    NULL, "callout", "block waiting");
    269  1.16.2.2      yamt }
    270  1.16.2.2      yamt 
    271  1.16.2.2      yamt /*
    272  1.16.2.2      yamt  * callout_startup2:
    273  1.16.2.2      yamt  *
    274  1.16.2.2      yamt  *	Complete initialization once soft interrupts are available.
    275  1.16.2.2      yamt  */
    276  1.16.2.2      yamt void
    277  1.16.2.2      yamt callout_startup2(void)
    278  1.16.2.2      yamt {
    279  1.16.2.2      yamt 
    280  1.16.2.3      yamt 	callout_si = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    281  1.16.2.2      yamt 	    callout_softclock, NULL);
    282  1.16.2.2      yamt 	if (callout_si == NULL)
    283  1.16.2.2      yamt 		panic("callout_startup2: unable to register softclock intr");
    284       1.1   thorpej }
    285       1.1   thorpej 
    286       1.1   thorpej /*
    287       1.1   thorpej  * callout_init:
    288       1.1   thorpej  *
    289       1.1   thorpej  *	Initialize a callout structure.
    290       1.1   thorpej  */
    291       1.1   thorpej void
    292  1.16.2.2      yamt callout_init(callout_t *cs, u_int flags)
    293       1.1   thorpej {
    294  1.16.2.2      yamt 	callout_impl_t *c = (callout_impl_t *)cs;
    295  1.16.2.2      yamt 
    296  1.16.2.2      yamt 	KASSERT((flags & ~CALLOUT_FLAGMASK) == 0);
    297       1.1   thorpej 
    298       1.1   thorpej 	memset(c, 0, sizeof(*c));
    299  1.16.2.2      yamt 	c->c_flags = flags;
    300  1.16.2.2      yamt 	c->c_magic = CALLOUT_MAGIC;
    301       1.1   thorpej }
    302       1.1   thorpej 
    303       1.1   thorpej /*
    304  1.16.2.2      yamt  * callout_destroy:
    305  1.16.2.2      yamt  *
    306  1.16.2.2      yamt  *	Destroy a callout structure.  The callout must be stopped.
    307  1.16.2.2      yamt  */
    308  1.16.2.2      yamt void
    309  1.16.2.2      yamt callout_destroy(callout_t *cs)
    310  1.16.2.2      yamt {
    311  1.16.2.2      yamt 	callout_impl_t *c = (callout_impl_t *)cs;
    312  1.16.2.2      yamt 
    313  1.16.2.2      yamt 	/*
    314  1.16.2.2      yamt 	 * It's not necessary to lock in order to see the correct value
    315  1.16.2.2      yamt 	 * of c->c_flags.  If the callout could potentially have been
    316  1.16.2.2      yamt 	 * running, the current thread should have stopped it.
    317  1.16.2.2      yamt 	 */
    318  1.16.2.2      yamt 	KASSERT((c->c_flags & CALLOUT_PENDING) == 0);
    319  1.16.2.2      yamt 	if (c->c_oncpu != NULL) {
    320  1.16.2.2      yamt 		KASSERT(
    321  1.16.2.2      yamt 		    ((struct cpu_info *)c->c_oncpu)->ci_data.cpu_callout != c);
    322  1.16.2.2      yamt 	}
    323  1.16.2.2      yamt 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    324  1.16.2.2      yamt 
    325  1.16.2.2      yamt 	c->c_magic = 0;
    326  1.16.2.2      yamt }
    327  1.16.2.2      yamt 
    328  1.16.2.2      yamt /*
    329  1.16.2.5      yamt  * callout_schedule_locked:
    330       1.1   thorpej  *
    331  1.16.2.5      yamt  *	Schedule a callout to run.  The function and argument must
    332  1.16.2.5      yamt  *	already be set in the callout structure.  Must be called with
    333  1.16.2.5      yamt  *	callout_lock.
    334       1.1   thorpej  */
    335  1.16.2.5      yamt static void
    336  1.16.2.5      yamt callout_schedule_locked(callout_impl_t *c, int to_ticks)
    337       1.1   thorpej {
    338  1.16.2.1      yamt 	int old_time;
    339       1.1   thorpej 
    340       1.1   thorpej 	KASSERT(to_ticks >= 0);
    341  1.16.2.5      yamt 	KASSERT(c->c_func != NULL);
    342       1.1   thorpej 
    343       1.1   thorpej 	/* Initialize the time here, it won't change. */
    344       1.1   thorpej 	old_time = c->c_time;
    345       1.1   thorpej 	c->c_time = to_ticks + hardclock_ticks;
    346  1.16.2.2      yamt 	c->c_flags &= ~CALLOUT_FIRED;
    347       1.1   thorpej 
    348       1.1   thorpej 	/*
    349       1.1   thorpej 	 * If this timeout is already scheduled and now is moved
    350       1.1   thorpej 	 * earlier, reschedule it now. Otherwise leave it in place
    351       1.1   thorpej 	 * and let it be rescheduled later.
    352       1.1   thorpej 	 */
    353  1.16.2.2      yamt 	if ((c->c_flags & CALLOUT_PENDING) != 0) {
    354       1.4      yamt 		if (c->c_time - old_time < 0) {
    355       1.1   thorpej 			CIRCQ_REMOVE(&c->c_list);
    356       1.1   thorpej 			CIRCQ_INSERT(&c->c_list, &timeout_todo);
    357       1.1   thorpej 		}
    358       1.1   thorpej 	} else {
    359       1.1   thorpej 		c->c_flags |= CALLOUT_PENDING;
    360       1.1   thorpej 		CIRCQ_INSERT(&c->c_list, &timeout_todo);
    361       1.1   thorpej 	}
    362  1.16.2.5      yamt }
    363  1.16.2.5      yamt 
    364  1.16.2.5      yamt /*
    365  1.16.2.5      yamt  * callout_reset:
    366  1.16.2.5      yamt  *
    367  1.16.2.5      yamt  *	Reset a callout structure with a new function and argument, and
    368  1.16.2.5      yamt  *	schedule it to run.
    369  1.16.2.5      yamt  */
    370  1.16.2.5      yamt void
    371  1.16.2.5      yamt callout_reset(callout_t *cs, int to_ticks, void (*func)(void *), void *arg)
    372  1.16.2.5      yamt {
    373  1.16.2.5      yamt 	callout_impl_t *c = (callout_impl_t *)cs;
    374  1.16.2.5      yamt 
    375  1.16.2.5      yamt 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    376  1.16.2.5      yamt 
    377  1.16.2.5      yamt 	mutex_spin_enter(&callout_lock);
    378  1.16.2.5      yamt 
    379  1.16.2.5      yamt 	c->c_func = func;
    380  1.16.2.5      yamt 	c->c_arg = arg;
    381  1.16.2.5      yamt 
    382  1.16.2.5      yamt 	callout_schedule_locked(c, to_ticks);
    383       1.1   thorpej 
    384  1.16.2.2      yamt 	mutex_spin_exit(&callout_lock);
    385       1.1   thorpej }
    386       1.1   thorpej 
    387       1.1   thorpej /*
    388       1.1   thorpej  * callout_schedule:
    389       1.1   thorpej  *
    390       1.1   thorpej  *	Schedule a callout to run.  The function and argument must
    391       1.1   thorpej  *	already be set in the callout structure.
    392       1.1   thorpej  */
    393       1.1   thorpej void
    394  1.16.2.2      yamt callout_schedule(callout_t *cs, int to_ticks)
    395       1.1   thorpej {
    396  1.16.2.2      yamt 	callout_impl_t *c = (callout_impl_t *)cs;
    397       1.1   thorpej 
    398  1.16.2.2      yamt 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    399       1.1   thorpej 
    400  1.16.2.2      yamt 	mutex_spin_enter(&callout_lock);
    401  1.16.2.5      yamt 	callout_schedule_locked(c, to_ticks);
    402  1.16.2.2      yamt 	mutex_spin_exit(&callout_lock);
    403       1.1   thorpej }
    404       1.1   thorpej 
    405       1.1   thorpej /*
    406       1.1   thorpej  * callout_stop:
    407       1.1   thorpej  *
    408       1.1   thorpej  *	Cancel a pending callout.
    409       1.1   thorpej  */
    410  1.16.2.2      yamt bool
    411  1.16.2.2      yamt callout_stop(callout_t *cs)
    412       1.1   thorpej {
    413  1.16.2.2      yamt 	callout_impl_t *c = (callout_impl_t *)cs;
    414  1.16.2.2      yamt 	bool expired;
    415  1.16.2.2      yamt 
    416  1.16.2.2      yamt 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    417       1.1   thorpej 
    418  1.16.2.2      yamt 	mutex_spin_enter(&callout_lock);
    419  1.16.2.1      yamt 
    420  1.16.2.2      yamt 	if (callout_running(c))
    421  1.16.2.2      yamt 		callout_barrier(c);
    422       1.1   thorpej 
    423  1.16.2.2      yamt 	if ((c->c_flags & CALLOUT_PENDING) != 0)
    424       1.1   thorpej 		CIRCQ_REMOVE(&c->c_list);
    425       1.1   thorpej 
    426  1.16.2.2      yamt 	expired = ((c->c_flags & CALLOUT_FIRED) != 0);
    427       1.9        he 	c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED);
    428       1.1   thorpej 
    429  1.16.2.2      yamt 	mutex_spin_exit(&callout_lock);
    430  1.16.2.2      yamt 
    431  1.16.2.2      yamt 	return expired;
    432  1.16.2.2      yamt }
    433  1.16.2.2      yamt 
    434  1.16.2.2      yamt void
    435  1.16.2.2      yamt callout_setfunc(callout_t *cs, void (*func)(void *), void *arg)
    436  1.16.2.2      yamt {
    437  1.16.2.2      yamt 	callout_impl_t *c = (callout_impl_t *)cs;
    438  1.16.2.2      yamt 
    439  1.16.2.2      yamt 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    440  1.16.2.2      yamt 
    441  1.16.2.2      yamt 	mutex_spin_enter(&callout_lock);
    442  1.16.2.2      yamt 	c->c_func = func;
    443  1.16.2.2      yamt 	c->c_arg = arg;
    444  1.16.2.2      yamt 	mutex_spin_exit(&callout_lock);
    445  1.16.2.2      yamt }
    446  1.16.2.2      yamt 
    447  1.16.2.2      yamt bool
    448  1.16.2.2      yamt callout_expired(callout_t *cs)
    449  1.16.2.2      yamt {
    450  1.16.2.2      yamt 	callout_impl_t *c = (callout_impl_t *)cs;
    451  1.16.2.2      yamt 	bool rv;
    452  1.16.2.2      yamt 
    453  1.16.2.2      yamt 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    454  1.16.2.2      yamt 
    455  1.16.2.2      yamt 	mutex_spin_enter(&callout_lock);
    456  1.16.2.2      yamt 	rv = ((c->c_flags & CALLOUT_FIRED) != 0);
    457  1.16.2.2      yamt 	mutex_spin_exit(&callout_lock);
    458  1.16.2.2      yamt 
    459  1.16.2.2      yamt 	return rv;
    460  1.16.2.2      yamt }
    461  1.16.2.2      yamt 
    462  1.16.2.2      yamt bool
    463  1.16.2.2      yamt callout_active(callout_t *cs)
    464  1.16.2.2      yamt {
    465  1.16.2.2      yamt 	callout_impl_t *c = (callout_impl_t *)cs;
    466  1.16.2.2      yamt 	bool rv;
    467  1.16.2.2      yamt 
    468  1.16.2.2      yamt 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    469  1.16.2.2      yamt 
    470  1.16.2.2      yamt 	mutex_spin_enter(&callout_lock);
    471  1.16.2.2      yamt 	rv = ((c->c_flags & (CALLOUT_PENDING|CALLOUT_FIRED)) != 0);
    472  1.16.2.2      yamt 	mutex_spin_exit(&callout_lock);
    473  1.16.2.2      yamt 
    474  1.16.2.2      yamt 	return rv;
    475  1.16.2.2      yamt }
    476  1.16.2.2      yamt 
    477  1.16.2.2      yamt bool
    478  1.16.2.2      yamt callout_pending(callout_t *cs)
    479  1.16.2.2      yamt {
    480  1.16.2.2      yamt 	callout_impl_t *c = (callout_impl_t *)cs;
    481  1.16.2.2      yamt 	bool rv;
    482  1.16.2.2      yamt 
    483  1.16.2.2      yamt 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    484  1.16.2.2      yamt 
    485  1.16.2.2      yamt 	mutex_spin_enter(&callout_lock);
    486  1.16.2.2      yamt 	rv = ((c->c_flags & CALLOUT_PENDING) != 0);
    487  1.16.2.2      yamt 	mutex_spin_exit(&callout_lock);
    488  1.16.2.2      yamt 
    489  1.16.2.2      yamt 	return rv;
    490  1.16.2.2      yamt }
    491  1.16.2.2      yamt 
    492  1.16.2.2      yamt bool
    493  1.16.2.2      yamt callout_invoking(callout_t *cs)
    494  1.16.2.2      yamt {
    495  1.16.2.2      yamt 	callout_impl_t *c = (callout_impl_t *)cs;
    496  1.16.2.2      yamt 	bool rv;
    497  1.16.2.2      yamt 
    498  1.16.2.2      yamt 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    499  1.16.2.2      yamt 
    500  1.16.2.2      yamt 	mutex_spin_enter(&callout_lock);
    501  1.16.2.2      yamt 	rv = ((c->c_flags & CALLOUT_INVOKING) != 0);
    502  1.16.2.2      yamt 	mutex_spin_exit(&callout_lock);
    503  1.16.2.2      yamt 
    504  1.16.2.2      yamt 	return rv;
    505  1.16.2.2      yamt }
    506  1.16.2.2      yamt 
    507  1.16.2.2      yamt void
    508  1.16.2.2      yamt callout_ack(callout_t *cs)
    509  1.16.2.2      yamt {
    510  1.16.2.2      yamt 	callout_impl_t *c = (callout_impl_t *)cs;
    511  1.16.2.2      yamt 
    512  1.16.2.2      yamt 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    513  1.16.2.2      yamt 
    514  1.16.2.2      yamt 	mutex_spin_enter(&callout_lock);
    515  1.16.2.2      yamt 	c->c_flags &= ~CALLOUT_INVOKING;
    516  1.16.2.2      yamt 	mutex_spin_exit(&callout_lock);
    517       1.1   thorpej }
    518       1.1   thorpej 
    519       1.1   thorpej /*
    520       1.1   thorpej  * This is called from hardclock() once every tick.
    521  1.16.2.2      yamt  * We schedule callout_softclock() if there is work
    522  1.16.2.2      yamt  * to be done.
    523       1.1   thorpej  */
    524  1.16.2.2      yamt void
    525       1.1   thorpej callout_hardclock(void)
    526       1.1   thorpej {
    527       1.4      yamt 	int needsoftclock;
    528       1.1   thorpej 
    529  1.16.2.2      yamt 	mutex_spin_enter(&callout_lock);
    530       1.1   thorpej 
    531       1.1   thorpej 	MOVEBUCKET(0, hardclock_ticks);
    532       1.1   thorpej 	if (MASKWHEEL(0, hardclock_ticks) == 0) {
    533       1.1   thorpej 		MOVEBUCKET(1, hardclock_ticks);
    534       1.1   thorpej 		if (MASKWHEEL(1, hardclock_ticks) == 0) {
    535       1.1   thorpej 			MOVEBUCKET(2, hardclock_ticks);
    536       1.1   thorpej 			if (MASKWHEEL(2, hardclock_ticks) == 0)
    537       1.1   thorpej 				MOVEBUCKET(3, hardclock_ticks);
    538       1.1   thorpej 		}
    539       1.1   thorpej 	}
    540       1.1   thorpej 
    541       1.4      yamt 	needsoftclock = !CIRCQ_EMPTY(&timeout_todo);
    542  1.16.2.2      yamt 	mutex_spin_exit(&callout_lock);
    543       1.1   thorpej 
    544  1.16.2.2      yamt 	if (needsoftclock)
    545  1.16.2.3      yamt 		softint_schedule(callout_si);
    546       1.1   thorpej }
    547       1.1   thorpej 
    548       1.1   thorpej /* ARGSUSED */
    549  1.16.2.2      yamt static void
    550  1.16.2.2      yamt callout_softclock(void *v)
    551       1.1   thorpej {
    552  1.16.2.2      yamt 	callout_impl_t *c;
    553  1.16.2.2      yamt 	struct cpu_info *ci;
    554       1.1   thorpej 	void (*func)(void *);
    555       1.1   thorpej 	void *arg;
    556  1.16.2.2      yamt 	u_int mpsafe, count;
    557  1.16.2.2      yamt 	lwp_t *l;
    558       1.1   thorpej 
    559  1.16.2.2      yamt 	l = curlwp;
    560  1.16.2.2      yamt 	ci = l->l_cpu;
    561  1.16.2.2      yamt 
    562  1.16.2.2      yamt 	mutex_spin_enter(&callout_lock);
    563       1.1   thorpej 
    564       1.1   thorpej 	while (!CIRCQ_EMPTY(&timeout_todo)) {
    565      1.11       scw 		c = CIRCQ_FIRST(&timeout_todo);
    566  1.16.2.2      yamt 		KASSERT(c->c_magic == CALLOUT_MAGIC);
    567  1.16.2.2      yamt 		KASSERT(c->c_func != NULL);
    568  1.16.2.2      yamt 		KASSERT((c->c_flags & CALLOUT_PENDING) != 0);
    569  1.16.2.2      yamt 		KASSERT((c->c_flags & CALLOUT_FIRED) == 0);
    570       1.1   thorpej 		CIRCQ_REMOVE(&c->c_list);
    571       1.1   thorpej 
    572       1.1   thorpej 		/* If due run it, otherwise insert it into the right bucket. */
    573       1.1   thorpej 		if (c->c_time - hardclock_ticks > 0) {
    574       1.1   thorpej 			CIRCQ_INSERT(&c->c_list,
    575       1.3  drochner 			    BUCKET((c->c_time - hardclock_ticks), c->c_time));
    576       1.1   thorpej 		} else {
    577       1.1   thorpej 			if (c->c_time - hardclock_ticks < 0)
    578       1.5   thorpej 				callout_ev_late.ev_count++;
    579       1.1   thorpej 
    580  1.16.2.2      yamt 			c->c_flags ^= (CALLOUT_PENDING | CALLOUT_FIRED);
    581  1.16.2.2      yamt 			mpsafe = (c->c_flags & CALLOUT_MPSAFE);
    582       1.1   thorpej 			func = c->c_func;
    583       1.1   thorpej 			arg = c->c_arg;
    584  1.16.2.1      yamt 			c->c_oncpu = ci;
    585  1.16.2.2      yamt 			c->c_onlwp = l;
    586  1.16.2.2      yamt 
    587  1.16.2.2      yamt 			mutex_spin_exit(&callout_lock);
    588  1.16.2.2      yamt 			if (!mpsafe) {
    589  1.16.2.2      yamt 				KERNEL_LOCK(1, curlwp);
    590  1.16.2.2      yamt 				if (ci->ci_data.cpu_callout_cancel != c)
    591  1.16.2.2      yamt 					(*func)(arg);
    592  1.16.2.2      yamt 				KERNEL_UNLOCK_ONE(curlwp);
    593  1.16.2.2      yamt 			} else
    594  1.16.2.2      yamt 					(*func)(arg);
    595  1.16.2.2      yamt 			mutex_spin_enter(&callout_lock);
    596  1.16.2.2      yamt 
    597  1.16.2.1      yamt 			/*
    598  1.16.2.2      yamt 			 * We can't touch 'c' here because it might be
    599  1.16.2.2      yamt 			 * freed already.  If LWPs waiting for callout
    600  1.16.2.2      yamt 			 * to complete, awaken them.
    601  1.16.2.1      yamt 			 */
    602  1.16.2.2      yamt 			ci->ci_data.cpu_callout_cancel = NULL;
    603  1.16.2.2      yamt 			ci->ci_data.cpu_callout = NULL;
    604  1.16.2.2      yamt 			if ((count = ci->ci_data.cpu_callout_nwait) != 0) {
    605  1.16.2.2      yamt 				ci->ci_data.cpu_callout_nwait = 0;
    606  1.16.2.2      yamt 				/* sleepq_wake() drops the lock. */
    607  1.16.2.2      yamt 				sleepq_wake(&callout_sleepq, ci, count);
    608  1.16.2.2      yamt 				mutex_spin_enter(&callout_lock);
    609  1.16.2.2      yamt 			}
    610       1.1   thorpej 		}
    611       1.1   thorpej 	}
    612       1.1   thorpej 
    613  1.16.2.2      yamt 	mutex_spin_exit(&callout_lock);
    614       1.1   thorpej }
    615       1.1   thorpej 
    616       1.1   thorpej #ifdef DDB
    617       1.1   thorpej static void
    618       1.1   thorpej db_show_callout_bucket(struct callout_circq *bucket)
    619       1.1   thorpej {
    620  1.16.2.2      yamt 	callout_impl_t *c;
    621       1.1   thorpej 	db_expr_t offset;
    622      1.15  christos 	const char *name;
    623      1.15  christos 	static char question[] = "?";
    624       1.1   thorpej 
    625      1.11       scw 	if (CIRCQ_EMPTY(bucket))
    626      1.11       scw 		return;
    627      1.11       scw 
    628      1.11       scw 	for (c = CIRCQ_FIRST(bucket); /*nothing*/; c = CIRCQ_NEXT(&c->c_list)) {
    629      1.10       scw 		db_find_sym_and_offset((db_addr_t)(intptr_t)c->c_func, &name,
    630      1.10       scw 		    &offset);
    631      1.15  christos 		name = name ? name : question;
    632       1.1   thorpej #ifdef _LP64
    633       1.1   thorpej #define	POINTER_WIDTH	"%16lx"
    634       1.1   thorpej #else
    635       1.1   thorpej #define	POINTER_WIDTH	"%8lx"
    636       1.1   thorpej #endif
    637       1.1   thorpej 		db_printf("%9d %2d/%-4d " POINTER_WIDTH "  %s\n",
    638       1.1   thorpej 		    c->c_time - hardclock_ticks,
    639       1.2    martin 		    (int)((bucket - timeout_wheel) / WHEELSIZE),
    640       1.2    martin 		    (int)(bucket - timeout_wheel), (u_long) c->c_arg, name);
    641      1.11       scw 
    642      1.11       scw 		if (CIRCQ_LAST(&c->c_list, bucket))
    643      1.11       scw 			break;
    644       1.1   thorpej 	}
    645       1.1   thorpej }
    646       1.1   thorpej 
    647       1.1   thorpej void
    648  1.16.2.1      yamt db_show_callout(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    649       1.1   thorpej {
    650       1.1   thorpej 	int b;
    651       1.1   thorpej 
    652       1.1   thorpej 	db_printf("hardclock_ticks now: %d\n", hardclock_ticks);
    653       1.1   thorpej #ifdef _LP64
    654       1.1   thorpej 	db_printf("    ticks  wheel               arg  func\n");
    655       1.1   thorpej #else
    656       1.1   thorpej 	db_printf("    ticks  wheel       arg  func\n");
    657       1.1   thorpej #endif
    658       1.1   thorpej 
    659       1.1   thorpej 	/*
    660       1.1   thorpej 	 * Don't lock the callwheel; all the other CPUs are paused
    661       1.1   thorpej 	 * anyhow, and we might be called in a circumstance where
    662       1.1   thorpej 	 * some other CPU was paused while holding the lock.
    663       1.1   thorpej 	 */
    664       1.1   thorpej 
    665       1.1   thorpej 	db_show_callout_bucket(&timeout_todo);
    666       1.1   thorpej 	for (b = 0; b < BUCKETS; b++)
    667       1.1   thorpej 		db_show_callout_bucket(&timeout_wheel[b]);
    668       1.1   thorpej }
    669       1.1   thorpej #endif /* DDB */
    670