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