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