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