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