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