Home | History | Annotate | Line # | Download | only in kern
kern_timeout.c revision 1.21.4.2
      1  1.21.4.2        ad /*	$NetBSD: kern_timeout.c,v 1.21.4.2 2007/06/16 19:02:55 ad Exp $	*/
      2       1.1   thorpej 
      3       1.1   thorpej /*-
      4  1.21.4.2        ad  * 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.4.2        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.21.4.2        ad __KERNEL_RCSID(0, "$NetBSD: kern_timeout.c,v 1.21.4.2 2007/06/16 19:02:55 ad Exp $");
     70       1.1   thorpej 
     71       1.1   thorpej /*
     72       1.1   thorpej  * Adapted from OpenBSD: kern_timeout.c,v 1.15 2002/12/08 04:21:07 art Exp,
     73       1.1   thorpej  * modified to match NetBSD's pre-existing callout API.
     74       1.1   thorpej  */
     75       1.1   thorpej 
     76       1.1   thorpej #include <sys/param.h>
     77       1.1   thorpej #include <sys/systm.h>
     78       1.1   thorpej #include <sys/kernel.h>
     79       1.1   thorpej #include <sys/lock.h>
     80       1.1   thorpej #include <sys/callout.h>
     81      1.20        ad #include <sys/mutex.h>
     82       1.1   thorpej 
     83       1.1   thorpej #ifdef DDB
     84       1.1   thorpej #include <machine/db_machdep.h>
     85       1.1   thorpej #include <ddb/db_interface.h>
     86       1.1   thorpej #include <ddb/db_access.h>
     87       1.1   thorpej #include <ddb/db_sym.h>
     88       1.1   thorpej #include <ddb/db_output.h>
     89       1.1   thorpej #endif
     90       1.1   thorpej 
     91       1.1   thorpej /*
     92       1.1   thorpej  * Timeouts are kept in a hierarchical timing wheel. The c_time is the value
     93       1.1   thorpej  * of the global variable "hardclock_ticks" when the timeout should be called.
     94       1.1   thorpej  * There are four levels with 256 buckets each. See 'Scheme 7' in
     95       1.1   thorpej  * "Hashed and Hierarchical Timing Wheels: Efficient Data Structures for
     96       1.1   thorpej  * Implementing a Timer Facility" by George Varghese and Tony Lauck.
     97       1.1   thorpej  */
     98       1.1   thorpej #define BUCKETS 1024
     99       1.1   thorpej #define WHEELSIZE 256
    100       1.1   thorpej #define WHEELMASK 255
    101       1.1   thorpej #define WHEELBITS 8
    102       1.1   thorpej 
    103       1.1   thorpej static struct callout_circq timeout_wheel[BUCKETS];	/* Queues of timeouts */
    104       1.1   thorpej static struct callout_circq timeout_todo;		/* Worklist */
    105       1.1   thorpej 
    106       1.1   thorpej #define MASKWHEEL(wheel, time) (((time) >> ((wheel)*WHEELBITS)) & WHEELMASK)
    107       1.1   thorpej 
    108       1.1   thorpej #define BUCKET(rel, abs)						\
    109       1.1   thorpej     (((rel) <= (1 << (2*WHEELBITS)))					\
    110       1.1   thorpej     	? ((rel) <= (1 << WHEELBITS))					\
    111       1.3  drochner             ? &timeout_wheel[MASKWHEEL(0, (abs))]			\
    112       1.3  drochner             : &timeout_wheel[MASKWHEEL(1, (abs)) + WHEELSIZE]		\
    113       1.1   thorpej         : ((rel) <= (1 << (3*WHEELBITS)))				\
    114       1.3  drochner             ? &timeout_wheel[MASKWHEEL(2, (abs)) + 2*WHEELSIZE]		\
    115       1.3  drochner             : &timeout_wheel[MASKWHEEL(3, (abs)) + 3*WHEELSIZE])
    116       1.1   thorpej 
    117       1.1   thorpej #define MOVEBUCKET(wheel, time)						\
    118       1.1   thorpej     CIRCQ_APPEND(&timeout_todo,						\
    119       1.1   thorpej         &timeout_wheel[MASKWHEEL((wheel), (time)) + (wheel)*WHEELSIZE])
    120       1.1   thorpej 
    121       1.1   thorpej /*
    122       1.1   thorpej  * All wheels are locked with the same lock (which must also block out all
    123       1.1   thorpej  * interrupts).
    124       1.1   thorpej  */
    125      1.20        ad kmutex_t callout_mutex;
    126       1.1   thorpej 
    127       1.1   thorpej /*
    128       1.1   thorpej  * Circular queue definitions.
    129       1.1   thorpej  */
    130       1.1   thorpej 
    131      1.11       scw #define CIRCQ_INIT(list)						\
    132       1.1   thorpej do {									\
    133      1.11       scw         (list)->cq_next_l = (list);					\
    134      1.11       scw         (list)->cq_prev_l = (list);					\
    135       1.1   thorpej } while (/*CONSTCOND*/0)
    136       1.1   thorpej 
    137       1.1   thorpej #define CIRCQ_INSERT(elem, list)					\
    138       1.1   thorpej do {									\
    139      1.11       scw         (elem)->cq_prev_e = (list)->cq_prev_e;				\
    140      1.11       scw         (elem)->cq_next_l = (list);					\
    141      1.11       scw         (list)->cq_prev_l->cq_next_l = (elem);				\
    142      1.11       scw         (list)->cq_prev_l = (elem);					\
    143       1.1   thorpej } while (/*CONSTCOND*/0)
    144       1.1   thorpej 
    145       1.1   thorpej #define CIRCQ_APPEND(fst, snd)						\
    146       1.1   thorpej do {									\
    147       1.1   thorpej         if (!CIRCQ_EMPTY(snd)) {					\
    148      1.11       scw                 (fst)->cq_prev_l->cq_next_l = (snd)->cq_next_l;		\
    149      1.11       scw                 (snd)->cq_next_l->cq_prev_l = (fst)->cq_prev_l;		\
    150      1.11       scw                 (snd)->cq_prev_l->cq_next_l = (fst);			\
    151      1.11       scw                 (fst)->cq_prev_l = (snd)->cq_prev_l;			\
    152       1.1   thorpej                 CIRCQ_INIT(snd);					\
    153       1.1   thorpej         }								\
    154       1.1   thorpej } while (/*CONSTCOND*/0)
    155       1.1   thorpej 
    156       1.1   thorpej #define CIRCQ_REMOVE(elem)						\
    157       1.1   thorpej do {									\
    158      1.11       scw         (elem)->cq_next_l->cq_prev_e = (elem)->cq_prev_e;		\
    159      1.11       scw         (elem)->cq_prev_l->cq_next_e = (elem)->cq_next_e;		\
    160       1.1   thorpej } while (/*CONSTCOND*/0)
    161       1.1   thorpej 
    162      1.11       scw #define CIRCQ_FIRST(list)	((list)->cq_next_e)
    163      1.11       scw #define CIRCQ_NEXT(elem)	((elem)->cq_next_e)
    164      1.11       scw #define CIRCQ_LAST(elem,list)	((elem)->cq_next_l == (list))
    165      1.11       scw #define CIRCQ_EMPTY(list)	((list)->cq_next_l == (list))
    166       1.1   thorpej 
    167       1.1   thorpej /*
    168       1.1   thorpej  * Some of the "math" in here is a bit tricky.
    169       1.1   thorpej  *
    170       1.1   thorpej  * We have to beware of wrapping ints.
    171       1.1   thorpej  * We use the fact that any element added to the queue must be added with a
    172       1.1   thorpej  * positive time. That means that any element `to' on the queue cannot be
    173       1.1   thorpej  * scheduled to timeout further in time than INT_MAX, but c->c_time can
    174       1.1   thorpej  * be positive or negative so comparing it with anything is dangerous.
    175       1.1   thorpej  * The only way we can use the c->c_time value in any predictable way
    176       1.6       mjl  * is when we calculate how far in the future `to' will timeout -
    177       1.1   thorpej  * "c->c_time - hardclock_ticks". The result will always be positive for
    178       1.1   thorpej  * future timeouts and 0 or negative for due timeouts.
    179       1.1   thorpej  */
    180       1.1   thorpej 
    181       1.5   thorpej #ifdef CALLOUT_EVENT_COUNTERS
    182       1.5   thorpej static struct evcnt callout_ev_late;
    183       1.5   thorpej #endif
    184       1.5   thorpej 
    185       1.1   thorpej /*
    186      1.20        ad  * callout_barrier:
    187      1.20        ad  *
    188      1.20        ad  *	If the callout is running on another CPU, busy wait until it
    189      1.20        ad  *	completes.
    190      1.20        ad  */
    191      1.20        ad static inline void
    192      1.20        ad callout_barrier(struct callout *c)
    193      1.20        ad {
    194      1.20        ad #ifdef MULTIPROCESSOR
    195      1.20        ad 	struct cpu_info *ci, *ci_cur;
    196      1.20        ad 
    197  1.21.4.1        ad 	KASSERT(mutex_owned(&callout_mutex));
    198      1.20        ad 
    199      1.20        ad 	/*
    200      1.20        ad 	 * The callout may have already been dispatched to run on the
    201      1.20        ad 	 * current CPU.  It's possible for us to arrive here before it
    202      1.20        ad 	 * actually runs because the SPL is dropped from IPL_SCHED in
    203      1.20        ad 	 * softclock(), and IPL_SOFTCLOCK is low priority. We can't deal
    204      1.20        ad 	 * with that race easily, so for now the caller must deal with
    205      1.20        ad 	 * it.
    206      1.20        ad 	 */
    207      1.20        ad #if 1
    208      1.20        ad 	ci_cur = curcpu();	/* XXXgcc get around alpha problem */
    209      1.20        ad 	while ((ci = c->c_oncpu) != NULL && ci != ci_cur &&
    210      1.20        ad 	    ci->ci_data.cpu_callout == c) {
    211      1.20        ad #else
    212      1.20        ad 	while ((ci = c->c_oncpu) != NULL && ci != curcpu() &&
    213      1.20        ad 	    ci->ci_data.cpu_callout == c) {
    214      1.20        ad #endif
    215      1.20        ad 		mutex_spin_exit(&callout_mutex);
    216      1.20        ad 		while (ci->ci_data.cpu_callout == c)
    217      1.20        ad 			;
    218      1.20        ad 		mutex_spin_enter(&callout_mutex);
    219      1.20        ad 	}
    220      1.20        ad 	c->c_oncpu = NULL;
    221      1.20        ad #endif
    222      1.20        ad }
    223      1.20        ad 
    224      1.20        ad /*
    225       1.1   thorpej  * callout_startup:
    226       1.1   thorpej  *
    227       1.1   thorpej  *	Initialize the callout facility, called at system startup time.
    228       1.1   thorpej  */
    229       1.1   thorpej void
    230       1.1   thorpej callout_startup(void)
    231       1.1   thorpej {
    232       1.1   thorpej 	int b;
    233       1.1   thorpej 
    234       1.1   thorpej 	CIRCQ_INIT(&timeout_todo);
    235       1.1   thorpej 	for (b = 0; b < BUCKETS; b++)
    236       1.1   thorpej 		CIRCQ_INIT(&timeout_wheel[b]);
    237      1.20        ad 	mutex_init(&callout_mutex, MUTEX_SPIN, IPL_SCHED);
    238       1.5   thorpej 
    239       1.5   thorpej #ifdef CALLOUT_EVENT_COUNTERS
    240       1.5   thorpej 	evcnt_attach_dynamic(&callout_ev_late, EVCNT_TYPE_MISC,
    241       1.5   thorpej 	    NULL, "callout", "late");
    242       1.5   thorpej #endif
    243       1.1   thorpej }
    244       1.1   thorpej 
    245       1.1   thorpej /*
    246       1.1   thorpej  * callout_init:
    247       1.1   thorpej  *
    248       1.1   thorpej  *	Initialize a callout structure.
    249       1.1   thorpej  */
    250       1.1   thorpej void
    251       1.1   thorpej callout_init(struct callout *c)
    252       1.1   thorpej {
    253       1.1   thorpej 
    254       1.1   thorpej 	memset(c, 0, sizeof(*c));
    255       1.1   thorpej }
    256       1.1   thorpej 
    257       1.1   thorpej /*
    258       1.1   thorpej  * callout_reset:
    259       1.1   thorpej  *
    260       1.1   thorpej  *	Reset a callout structure with a new function and argument, and
    261       1.1   thorpej  *	schedule it to run.
    262       1.1   thorpej  */
    263       1.1   thorpej void
    264       1.1   thorpej callout_reset(struct callout *c, int to_ticks, void (*func)(void *), void *arg)
    265       1.1   thorpej {
    266      1.20        ad 	int old_time;
    267       1.1   thorpej 
    268       1.1   thorpej 	KASSERT(to_ticks >= 0);
    269       1.1   thorpej 
    270      1.20        ad 	mutex_spin_enter(&callout_mutex);
    271      1.20        ad 
    272      1.20        ad 	callout_barrier(c);
    273       1.1   thorpej 
    274       1.1   thorpej 	/* Initialize the time here, it won't change. */
    275       1.1   thorpej 	old_time = c->c_time;
    276       1.1   thorpej 	c->c_time = to_ticks + hardclock_ticks;
    277       1.8        he 	c->c_flags &= ~(CALLOUT_FIRED|CALLOUT_INVOKING);
    278       1.1   thorpej 
    279       1.1   thorpej 	c->c_func = func;
    280       1.1   thorpej 	c->c_arg = arg;
    281       1.1   thorpej 
    282       1.1   thorpej 	/*
    283       1.1   thorpej 	 * If this timeout is already scheduled and now is moved
    284       1.1   thorpej 	 * earlier, reschedule it now. Otherwise leave it in place
    285       1.1   thorpej 	 * and let it be rescheduled later.
    286       1.1   thorpej 	 */
    287  1.21.4.2        ad 	if ((c->c_flags & CALLOUT_PENDING) != 0) {
    288       1.4      yamt 		if (c->c_time - old_time < 0) {
    289       1.1   thorpej 			CIRCQ_REMOVE(&c->c_list);
    290       1.1   thorpej 			CIRCQ_INSERT(&c->c_list, &timeout_todo);
    291       1.1   thorpej 		}
    292       1.1   thorpej 	} else {
    293       1.1   thorpej 		c->c_flags |= CALLOUT_PENDING;
    294       1.1   thorpej 		CIRCQ_INSERT(&c->c_list, &timeout_todo);
    295       1.1   thorpej 	}
    296       1.1   thorpej 
    297      1.20        ad 	mutex_spin_exit(&callout_mutex);
    298       1.1   thorpej }
    299       1.1   thorpej 
    300       1.1   thorpej /*
    301       1.1   thorpej  * callout_schedule:
    302       1.1   thorpej  *
    303       1.1   thorpej  *	Schedule a callout to run.  The function and argument must
    304       1.1   thorpej  *	already be set in the callout structure.
    305       1.1   thorpej  */
    306       1.1   thorpej void
    307       1.1   thorpej callout_schedule(struct callout *c, int to_ticks)
    308       1.1   thorpej {
    309      1.20        ad 	int old_time;
    310       1.1   thorpej 
    311       1.1   thorpej 	KASSERT(to_ticks >= 0);
    312       1.1   thorpej 
    313      1.20        ad 	mutex_spin_enter(&callout_mutex);
    314      1.20        ad 
    315      1.20        ad 	callout_barrier(c);
    316       1.1   thorpej 
    317       1.1   thorpej 	/* Initialize the time here, it won't change. */
    318       1.1   thorpej 	old_time = c->c_time;
    319       1.1   thorpej 	c->c_time = to_ticks + hardclock_ticks;
    320       1.8        he 	c->c_flags &= ~(CALLOUT_FIRED|CALLOUT_INVOKING);
    321       1.1   thorpej 
    322       1.1   thorpej 	/*
    323       1.1   thorpej 	 * If this timeout is already scheduled and now is moved
    324       1.1   thorpej 	 * earlier, reschedule it now. Otherwise leave it in place
    325       1.1   thorpej 	 * and let it be rescheduled later.
    326       1.1   thorpej 	 */
    327  1.21.4.2        ad 	if ((c->c_flags & CALLOUT_PENDING) != 0) {
    328       1.4      yamt 		if (c->c_time - old_time < 0) {
    329       1.1   thorpej 			CIRCQ_REMOVE(&c->c_list);
    330       1.1   thorpej 			CIRCQ_INSERT(&c->c_list, &timeout_todo);
    331       1.1   thorpej 		}
    332       1.1   thorpej 	} else {
    333       1.1   thorpej 		c->c_flags |= CALLOUT_PENDING;
    334       1.1   thorpej 		CIRCQ_INSERT(&c->c_list, &timeout_todo);
    335       1.1   thorpej 	}
    336       1.1   thorpej 
    337      1.20        ad 	mutex_spin_exit(&callout_mutex);
    338       1.1   thorpej }
    339       1.1   thorpej 
    340       1.1   thorpej /*
    341       1.1   thorpej  * callout_stop:
    342       1.1   thorpej  *
    343       1.1   thorpej  *	Cancel a pending callout.
    344       1.1   thorpej  */
    345       1.1   thorpej void
    346       1.1   thorpej callout_stop(struct callout *c)
    347       1.1   thorpej {
    348       1.1   thorpej 
    349      1.20        ad 	mutex_spin_enter(&callout_mutex);
    350      1.20        ad 
    351      1.20        ad 	callout_barrier(c);
    352       1.1   thorpej 
    353  1.21.4.2        ad 	if ((c->c_flags & CALLOUT_PENDING) != 0)
    354       1.1   thorpej 		CIRCQ_REMOVE(&c->c_list);
    355       1.1   thorpej 
    356       1.9        he 	c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED);
    357       1.1   thorpej 
    358      1.20        ad 	mutex_spin_exit(&callout_mutex);
    359       1.1   thorpej }
    360       1.1   thorpej 
    361  1.21.4.2        ad void
    362  1.21.4.2        ad callout_setfunc(struct callout *c, void (*func)(void *), void *arg)
    363  1.21.4.2        ad {
    364  1.21.4.2        ad 
    365  1.21.4.2        ad 	mutex_spin_enter(&callout_mutex);
    366  1.21.4.2        ad 	c->c_func = func;
    367  1.21.4.2        ad 	c->c_arg = arg;
    368  1.21.4.2        ad 	mutex_spin_exit(&callout_mutex);
    369  1.21.4.2        ad }
    370  1.21.4.2        ad 
    371  1.21.4.2        ad bool
    372  1.21.4.2        ad callout_pending(struct callout *c)
    373  1.21.4.2        ad {
    374  1.21.4.2        ad 	bool rv;
    375  1.21.4.2        ad 
    376  1.21.4.2        ad 	mutex_spin_enter(&callout_mutex);
    377  1.21.4.2        ad 	rv = ((c->c_flags & CALLOUT_PENDING) != 0);
    378  1.21.4.2        ad 	mutex_spin_exit(&callout_mutex);
    379  1.21.4.2        ad 
    380  1.21.4.2        ad 	return rv;
    381  1.21.4.2        ad }
    382  1.21.4.2        ad 
    383  1.21.4.2        ad bool
    384  1.21.4.2        ad callout_expired(struct callout *c)
    385  1.21.4.2        ad {
    386  1.21.4.2        ad 	bool rv;
    387  1.21.4.2        ad 
    388  1.21.4.2        ad 	mutex_spin_enter(&callout_mutex);
    389  1.21.4.2        ad 	rv = ((c->c_flags & CALLOUT_FIRED) != 0);
    390  1.21.4.2        ad 	mutex_spin_exit(&callout_mutex);
    391  1.21.4.2        ad 
    392  1.21.4.2        ad 	return rv;
    393  1.21.4.2        ad }
    394  1.21.4.2        ad 
    395  1.21.4.2        ad bool
    396  1.21.4.2        ad callout_active(struct callout *c)
    397  1.21.4.2        ad {
    398  1.21.4.2        ad 	bool rv;
    399  1.21.4.2        ad 
    400  1.21.4.2        ad 	mutex_spin_enter(&callout_mutex);
    401  1.21.4.2        ad 	rv = ((c->c_flags & (CALLOUT_PENDING|CALLOUT_FIRED)) != 0);
    402  1.21.4.2        ad 	mutex_spin_exit(&callout_mutex);
    403  1.21.4.2        ad 
    404  1.21.4.2        ad 	return rv;
    405  1.21.4.2        ad }
    406  1.21.4.2        ad 
    407  1.21.4.2        ad bool
    408  1.21.4.2        ad callout_invoking(struct callout *c)
    409  1.21.4.2        ad {
    410  1.21.4.2        ad 	bool rv;
    411  1.21.4.2        ad 
    412  1.21.4.2        ad 	mutex_spin_enter(&callout_mutex);
    413  1.21.4.2        ad 	rv = ((c->c_flags & CALLOUT_INVOKING) != 0);
    414  1.21.4.2        ad 	mutex_spin_exit(&callout_mutex);
    415  1.21.4.2        ad 
    416  1.21.4.2        ad 	return rv;
    417  1.21.4.2        ad }
    418  1.21.4.2        ad 
    419  1.21.4.2        ad void
    420  1.21.4.2        ad callout_ack(struct callout *c)
    421  1.21.4.2        ad {
    422  1.21.4.2        ad 
    423  1.21.4.2        ad 	mutex_spin_enter(&callout_mutex);
    424  1.21.4.2        ad 	c->c_flags &= ~CALLOUT_INVOKING;
    425  1.21.4.2        ad 	mutex_spin_exit(&callout_mutex);
    426  1.21.4.2        ad }
    427  1.21.4.2        ad 
    428       1.1   thorpej /*
    429       1.1   thorpej  * This is called from hardclock() once every tick.
    430       1.1   thorpej  * We return !0 if we need to schedule a softclock.
    431       1.1   thorpej  */
    432       1.1   thorpej int
    433       1.1   thorpej callout_hardclock(void)
    434       1.1   thorpej {
    435       1.4      yamt 	int needsoftclock;
    436       1.1   thorpej 
    437      1.20        ad 	mutex_spin_enter(&callout_mutex);
    438       1.1   thorpej 
    439       1.1   thorpej 	MOVEBUCKET(0, hardclock_ticks);
    440       1.1   thorpej 	if (MASKWHEEL(0, hardclock_ticks) == 0) {
    441       1.1   thorpej 		MOVEBUCKET(1, hardclock_ticks);
    442       1.1   thorpej 		if (MASKWHEEL(1, hardclock_ticks) == 0) {
    443       1.1   thorpej 			MOVEBUCKET(2, hardclock_ticks);
    444       1.1   thorpej 			if (MASKWHEEL(2, hardclock_ticks) == 0)
    445       1.1   thorpej 				MOVEBUCKET(3, hardclock_ticks);
    446       1.1   thorpej 		}
    447       1.1   thorpej 	}
    448       1.1   thorpej 
    449       1.4      yamt 	needsoftclock = !CIRCQ_EMPTY(&timeout_todo);
    450      1.20        ad 	mutex_spin_exit(&callout_mutex);
    451       1.1   thorpej 
    452       1.4      yamt 	return needsoftclock;
    453       1.1   thorpej }
    454       1.1   thorpej 
    455       1.1   thorpej /* ARGSUSED */
    456       1.1   thorpej void
    457      1.19      yamt softclock(void *v)
    458       1.1   thorpej {
    459      1.20        ad #ifdef MULTIPROCESSOR
    460      1.20        ad 	struct cpu_info *ci = curcpu();
    461      1.20        ad #endif
    462       1.1   thorpej 	struct callout *c;
    463       1.1   thorpej 	void (*func)(void *);
    464       1.1   thorpej 	void *arg;
    465       1.1   thorpej 
    466      1.20        ad 	mutex_spin_enter(&callout_mutex);
    467       1.1   thorpej 
    468       1.1   thorpej 	while (!CIRCQ_EMPTY(&timeout_todo)) {
    469      1.11       scw 		c = CIRCQ_FIRST(&timeout_todo);
    470       1.1   thorpej 		CIRCQ_REMOVE(&c->c_list);
    471       1.1   thorpej 
    472       1.1   thorpej 		/* If due run it, otherwise insert it into the right bucket. */
    473       1.1   thorpej 		if (c->c_time - hardclock_ticks > 0) {
    474       1.1   thorpej 			CIRCQ_INSERT(&c->c_list,
    475       1.3  drochner 			    BUCKET((c->c_time - hardclock_ticks), c->c_time));
    476       1.1   thorpej 		} else {
    477       1.5   thorpej #ifdef CALLOUT_EVENT_COUNTERS
    478       1.1   thorpej 			if (c->c_time - hardclock_ticks < 0)
    479       1.5   thorpej 				callout_ev_late.ev_count++;
    480       1.1   thorpej #endif
    481      1.20        ad 			c->c_flags = (c->c_flags & ~CALLOUT_PENDING) |
    482       1.8        he 			    (CALLOUT_FIRED|CALLOUT_INVOKING);
    483       1.1   thorpej 
    484       1.1   thorpej 			func = c->c_func;
    485       1.1   thorpej 			arg = c->c_arg;
    486       1.1   thorpej 
    487      1.20        ad #ifdef MULTIPROCESSOR
    488      1.20        ad 			c->c_oncpu = ci;
    489      1.20        ad 			ci->ci_data.cpu_callout = c;
    490      1.20        ad #endif
    491      1.20        ad 			mutex_spin_exit(&callout_mutex);
    492  1.21.4.2        ad 			KERNEL_LOCK(1, curlwp);
    493       1.1   thorpej 			(*func)(arg);
    494  1.21.4.2        ad 			KERNEL_UNLOCK_ONE(curlwp);
    495      1.20        ad 			mutex_spin_enter(&callout_mutex);
    496      1.20        ad #ifdef MULTIPROCESSOR
    497      1.20        ad 			ci->ci_data.cpu_callout = NULL;
    498      1.20        ad 			/*
    499      1.20        ad 			 * we can't touch 'c' here because it might be
    500      1.20        ad 			 * freed already.
    501      1.20        ad 			 */
    502      1.20        ad #endif
    503       1.1   thorpej 		}
    504       1.1   thorpej 	}
    505       1.1   thorpej 
    506      1.20        ad 	mutex_spin_exit(&callout_mutex);
    507       1.1   thorpej }
    508       1.1   thorpej 
    509       1.1   thorpej #ifdef DDB
    510       1.1   thorpej static void
    511       1.1   thorpej db_show_callout_bucket(struct callout_circq *bucket)
    512       1.1   thorpej {
    513       1.1   thorpej 	struct callout *c;
    514       1.1   thorpej 	db_expr_t offset;
    515      1.15  christos 	const char *name;
    516      1.15  christos 	static char question[] = "?";
    517       1.1   thorpej 
    518      1.11       scw 	if (CIRCQ_EMPTY(bucket))
    519      1.11       scw 		return;
    520      1.11       scw 
    521      1.11       scw 	for (c = CIRCQ_FIRST(bucket); /*nothing*/; c = CIRCQ_NEXT(&c->c_list)) {
    522      1.10       scw 		db_find_sym_and_offset((db_addr_t)(intptr_t)c->c_func, &name,
    523      1.10       scw 		    &offset);
    524      1.15  christos 		name = name ? name : question;
    525       1.1   thorpej #ifdef _LP64
    526       1.1   thorpej #define	POINTER_WIDTH	"%16lx"
    527       1.1   thorpej #else
    528       1.1   thorpej #define	POINTER_WIDTH	"%8lx"
    529       1.1   thorpej #endif
    530       1.1   thorpej 		db_printf("%9d %2d/%-4d " POINTER_WIDTH "  %s\n",
    531       1.1   thorpej 		    c->c_time - hardclock_ticks,
    532       1.2    martin 		    (int)((bucket - timeout_wheel) / WHEELSIZE),
    533       1.2    martin 		    (int)(bucket - timeout_wheel), (u_long) c->c_arg, name);
    534      1.11       scw 
    535      1.11       scw 		if (CIRCQ_LAST(&c->c_list, bucket))
    536      1.11       scw 			break;
    537       1.1   thorpej 	}
    538       1.1   thorpej }
    539       1.1   thorpej 
    540       1.1   thorpej void
    541      1.21      matt db_show_callout(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    542       1.1   thorpej {
    543       1.1   thorpej 	int b;
    544       1.1   thorpej 
    545       1.1   thorpej 	db_printf("hardclock_ticks now: %d\n", hardclock_ticks);
    546       1.1   thorpej #ifdef _LP64
    547       1.1   thorpej 	db_printf("    ticks  wheel               arg  func\n");
    548       1.1   thorpej #else
    549       1.1   thorpej 	db_printf("    ticks  wheel       arg  func\n");
    550       1.1   thorpej #endif
    551       1.1   thorpej 
    552       1.1   thorpej 	/*
    553       1.1   thorpej 	 * Don't lock the callwheel; all the other CPUs are paused
    554       1.1   thorpej 	 * anyhow, and we might be called in a circumstance where
    555       1.1   thorpej 	 * some other CPU was paused while holding the lock.
    556       1.1   thorpej 	 */
    557       1.1   thorpej 
    558       1.1   thorpej 	db_show_callout_bucket(&timeout_todo);
    559       1.1   thorpej 	for (b = 0; b < BUCKETS; b++)
    560       1.1   thorpej 		db_show_callout_bucket(&timeout_wheel[b]);
    561       1.1   thorpej }
    562       1.1   thorpej #endif /* DDB */
    563