| Commit | Line | Data |
|---|---|---|
| 92b561b7 MD |
1 | /* |
| 2 | * Copyright (c) 2004 The DragonFly Project. All rights reserved. | |
| 3 | * | |
| 4 | * This code is derived from software contributed to The DragonFly Project | |
| 5 | * by Matthew Dillon <dillon@backplane.com> | |
| 6 | * | |
| 7 | * Redistribution and use in source and binary forms, with or without | |
| 8 | * modification, are permitted provided that the following conditions | |
| 9 | * are met: | |
| 10 | * | |
| 11 | * 1. Redistributions of source code must retain the above copyright | |
| 12 | * notice, this list of conditions and the following disclaimer. | |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 14 | * notice, this list of conditions and the following disclaimer in | |
| 15 | * the documentation and/or other materials provided with the | |
| 16 | * distribution. | |
| 17 | * 3. Neither the name of The DragonFly Project nor the names of its | |
| 18 | * contributors may be used to endorse or promote products derived | |
| 19 | * from this software without specific, prior written permission. | |
| 20 | * | |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 22 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 24 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 25 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 26 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 27 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 28 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
| 29 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| 30 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 31 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 32 | * SUCH DAMAGE. | |
| 33 | */ | |
| 34 | /* | |
| 984263bc MD |
35 | * Copyright (c) 1982, 1986, 1991, 1993 |
| 36 | * The Regents of the University of California. All rights reserved. | |
| 37 | * (c) UNIX System Laboratories, Inc. | |
| 38 | * All or some portions of this file are derived from material licensed | |
| 39 | * to the University of California by American Telephone and Telegraph | |
| 40 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
| 41 | * the permission of UNIX System Laboratories, Inc. | |
| 42 | * | |
| 43 | * Redistribution and use in source and binary forms, with or without | |
| 44 | * modification, are permitted provided that the following conditions | |
| 45 | * are met: | |
| 46 | * 1. Redistributions of source code must retain the above copyright | |
| 47 | * notice, this list of conditions and the following disclaimer. | |
| 48 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 49 | * notice, this list of conditions and the following disclaimer in the | |
| 50 | * documentation and/or other materials provided with the distribution. | |
| 51 | * 3. All advertising materials mentioning features or use of this software | |
| 52 | * must display the following acknowledgement: | |
| 53 | * This product includes software developed by the University of | |
| 54 | * California, Berkeley and its contributors. | |
| 55 | * 4. Neither the name of the University nor the names of its contributors | |
| 56 | * may be used to endorse or promote products derived from this software | |
| 57 | * without specific prior written permission. | |
| 58 | * | |
| 59 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 60 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 61 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 62 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 63 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 64 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 65 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 66 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 67 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 68 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 69 | * SUCH DAMAGE. | |
| 70 | * | |
| 71 | * From: @(#)kern_clock.c 8.5 (Berkeley) 1/21/94 | |
| 72 | * $FreeBSD: src/sys/kern/kern_timeout.c,v 1.59.2.1 2001/11/13 18:24:52 archie Exp $ | |
| bb1a3ed4 | 73 | * $DragonFly: src/sys/kern/kern_timeout.c,v 1.27 2007/11/14 18:27:52 swildner Exp $ |
| 92b561b7 MD |
74 | */ |
| 75 | /* | |
| 76 | * DRAGONFLY BGL STATUS | |
| 77 | * | |
| 78 | * All the API functions should be MP safe. | |
| 79 | * | |
| 80 | * The callback functions will be flagged as being MP safe if the | |
| 81 | * timeout structure is initialized with callout_init_mp() instead of | |
| 82 | * callout_init(). | |
| 83 | * | |
| 84 | * The helper threads cannot be made preempt-capable until after we | |
| 85 | * clean up all the uses of splsoftclock() and related interlocks (which | |
| 86 | * require the related functions to be MP safe as well). | |
| 87 | */ | |
| 88 | /* | |
| 89 | * The callout mechanism is based on the work of Adam M. Costello and | |
| 90 | * George Varghese, published in a technical report entitled "Redesigning | |
| 91 | * the BSD Callout and Timer Facilities" and modified slightly for inclusion | |
| 92 | * in FreeBSD by Justin T. Gibbs. The original work on the data structures | |
| 93 | * used in this implementation was published by G. Varghese and T. Lauck in | |
| 94 | * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for | |
| 95 | * the Efficient Implementation of a Timer Facility" in the Proceedings of | |
| 96 | * the 11th ACM Annual Symposium on Operating Systems Principles, | |
| 97 | * Austin, Texas Nov 1987. | |
| 98 | * | |
| 99 | * The per-cpu augmentation was done by Matthew Dillon. | |
| 984263bc MD |
100 | */ |
| 101 | ||
| 102 | #include <sys/param.h> | |
| 103 | #include <sys/systm.h> | |
| 104 | #include <sys/callout.h> | |
| 105 | #include <sys/kernel.h> | |
| ef0fdad1 | 106 | #include <sys/interrupt.h> |
| 4b5f931b | 107 | #include <sys/thread.h> |
| 684a93c4 | 108 | |
| 88c4d2f6 | 109 | #include <sys/thread2.h> |
| 684a93c4 | 110 | #include <sys/mplock2.h> |
| 92b561b7 MD |
111 | |
| 112 | #ifndef MAX_SOFTCLOCK_STEPS | |
| 113 | #define MAX_SOFTCLOCK_STEPS 100 /* Maximum allowed value of steps. */ | |
| 114 | #endif | |
| 115 | ||
| 116 | ||
| 117 | struct softclock_pcpu { | |
| 92b561b7 MD |
118 | struct callout_tailq *callwheel; |
| 119 | struct callout * volatile next; | |
| b90e37ec | 120 | struct callout *running;/* currently running callout */ |
| 92b561b7 MD |
121 | int softticks; /* softticks index */ |
| 122 | int curticks; /* per-cpu ticks counter */ | |
| 123 | int isrunning; | |
| 124 | struct thread thread; | |
| 125 | ||
| 126 | }; | |
| 127 | ||
| 128 | typedef struct softclock_pcpu *softclock_pcpu_t; | |
| 984263bc MD |
129 | |
| 130 | /* | |
| 131 | * TODO: | |
| 132 | * allocate more timeout table slots when table overflows. | |
| 133 | */ | |
| 92b561b7 MD |
134 | static MALLOC_DEFINE(M_CALLOUT, "callout", "callout structures"); |
| 135 | static int callwheelsize; | |
| 136 | static int callwheelbits; | |
| 137 | static int callwheelmask; | |
| 138 | static struct softclock_pcpu softclock_pcpu_ary[MAXCPU]; | |
| 139 | ||
| 140 | static void softclock_handler(void *arg); | |
| 141 | ||
| 142 | static void | |
| 143 | swi_softclock_setup(void *arg) | |
| 144 | { | |
| 145 | int cpu; | |
| 146 | int i; | |
| 984263bc | 147 | |
| 92b561b7 MD |
148 | /* |
| 149 | * Figure out how large a callwheel we need. It must be a power of 2. | |
| 150 | */ | |
| 151 | callwheelsize = 1; | |
| 152 | callwheelbits = 0; | |
| 153 | while (callwheelsize < ncallout) { | |
| 154 | callwheelsize <<= 1; | |
| 155 | ++callwheelbits; | |
| 156 | } | |
| 157 | callwheelmask = callwheelsize - 1; | |
| 984263bc | 158 | |
| 92b561b7 MD |
159 | /* |
| 160 | * Initialize per-cpu data structures. | |
| 161 | */ | |
| 162 | for (cpu = 0; cpu < ncpus; ++cpu) { | |
| 163 | softclock_pcpu_t sc; | |
| 92b561b7 MD |
164 | |
| 165 | sc = &softclock_pcpu_ary[cpu]; | |
| 166 | ||
| 77652cad | 167 | sc->callwheel = kmalloc(sizeof(*sc->callwheel) * callwheelsize, |
| 92b561b7 MD |
168 | M_CALLOUT, M_WAITOK|M_ZERO); |
| 169 | for (i = 0; i < callwheelsize; ++i) | |
| 170 | TAILQ_INIT(&sc->callwheel[i]); | |
| 171 | ||
| 92b561b7 | 172 | /* |
| c1d0893d MD |
173 | * Mark the softclock handler as being an interrupt thread |
| 174 | * even though it really isn't, but do not allow it to | |
| 175 | * preempt other threads (do not assign td_preemptable). | |
| 176 | * | |
| 177 | * Kernel code now assumes that callouts do not preempt | |
| 178 | * the cpu they were scheduled on. | |
| 92b561b7 MD |
179 | */ |
| 180 | lwkt_create(softclock_handler, sc, NULL, | |
| 4643740a | 181 | &sc->thread, TDF_NOSTART | TDF_INTTHREAD, |
| c9e9fb21 | 182 | cpu, "softclock %d", cpu); |
| 92b561b7 MD |
183 | } |
| 184 | } | |
| 185 | ||
| ed99ec35 MD |
186 | /* |
| 187 | * Must occur after ncpus has been initialized. | |
| 188 | */ | |
| ba39e2e0 MD |
189 | SYSINIT(softclock_setup, SI_BOOT2_SOFTCLOCK, SI_ORDER_SECOND, |
| 190 | swi_softclock_setup, NULL); | |
| 984263bc MD |
191 | |
| 192 | /* | |
| 92b561b7 MD |
193 | * This routine is called from the hardclock() (basically a FASTint/IPI) on |
| 194 | * each cpu in the system. sc->curticks is this cpu's notion of the timebase. | |
| 195 | * It IS NOT NECESSARILY SYNCHRONIZED WITH 'ticks'! sc->softticks is where | |
| 196 | * the callwheel is currently indexed. | |
| 197 | * | |
| 198 | * WARNING! The MP lock is not necessarily held on call, nor can it be | |
| 199 | * safely obtained. | |
| 200 | * | |
| 201 | * sc->softticks is adjusted by either this routine or our helper thread | |
| 202 | * depending on whether the helper thread is running or not. | |
| 984263bc | 203 | */ |
| 92b561b7 MD |
204 | void |
| 205 | hardclock_softtick(globaldata_t gd) | |
| 206 | { | |
| 207 | softclock_pcpu_t sc; | |
| 208 | ||
| 209 | sc = &softclock_pcpu_ary[gd->gd_cpuid]; | |
| 210 | ++sc->curticks; | |
| 211 | if (sc->isrunning) | |
| 212 | return; | |
| 213 | if (sc->softticks == sc->curticks) { | |
| 214 | /* | |
| 215 | * in sync, only wakeup the thread if there is something to | |
| 216 | * do. | |
| 217 | */ | |
| 218 | if (TAILQ_FIRST(&sc->callwheel[sc->softticks & callwheelmask])) | |
| 219 | { | |
| 220 | sc->isrunning = 1; | |
| 221 | lwkt_schedule(&sc->thread); | |
| 222 | } else { | |
| 223 | ++sc->softticks; | |
| 224 | } | |
| 225 | } else { | |
| 226 | /* | |
| 227 | * out of sync, wakeup the thread unconditionally so it can | |
| 228 | * catch up. | |
| 229 | */ | |
| 230 | sc->isrunning = 1; | |
| 231 | lwkt_schedule(&sc->thread); | |
| 232 | } | |
| 233 | } | |
| 984263bc MD |
234 | |
| 235 | /* | |
| 92b561b7 MD |
236 | * This procedure is the main loop of our per-cpu helper thread. The |
| 237 | * sc->isrunning flag prevents us from racing hardclock_softtick() and | |
| 4ac1a4cd MD |
238 | * a critical section is sufficient to interlock sc->curticks and protect |
| 239 | * us from remote IPI's / list removal. | |
| 92b561b7 | 240 | * |
| c9e9fb21 MD |
241 | * The thread starts with the MP lock released and not in a critical |
| 242 | * section. The loop itself is MP safe while individual callbacks | |
| 243 | * may or may not be, so we obtain or release the MP lock as appropriate. | |
| 984263bc | 244 | */ |
| ef0fdad1 | 245 | static void |
| 92b561b7 | 246 | softclock_handler(void *arg) |
| 984263bc | 247 | { |
| 92b561b7 | 248 | softclock_pcpu_t sc; |
| 1fd87d54 RG |
249 | struct callout *c; |
| 250 | struct callout_tailq *bucket; | |
| 92b561b7 MD |
251 | void (*c_func)(void *); |
| 252 | void *c_arg; | |
| 92b561b7 | 253 | #ifdef SMP |
| c9e9fb21 | 254 | int mpsafe = 1; |
| 92b561b7 MD |
255 | #endif |
| 256 | ||
| d2d8515b MD |
257 | /* |
| 258 | * Run the callout thread at the same priority as other kernel | |
| 259 | * threads so it can be round-robined. | |
| 260 | */ | |
| 261 | /*lwkt_setpri_self(TDPRI_SOFT_NORM);*/ | |
| abb6f10a | 262 | |
| 92b561b7 | 263 | sc = arg; |
| 88c4d2f6 | 264 | crit_enter(); |
| 92b561b7 MD |
265 | loop: |
| 266 | while (sc->softticks != (int)(sc->curticks + 1)) { | |
| 267 | bucket = &sc->callwheel[sc->softticks & callwheelmask]; | |
| 268 | ||
| 269 | for (c = TAILQ_FIRST(bucket); c; c = sc->next) { | |
| 4ac1a4cd MD |
270 | if (c->c_time != sc->softticks) { |
| 271 | sc->next = TAILQ_NEXT(c, c_links.tqe); | |
| 92b561b7 | 272 | continue; |
| 4ac1a4cd MD |
273 | } |
| 274 | #ifdef SMP | |
| 275 | if (c->c_flags & CALLOUT_MPSAFE) { | |
| 276 | if (mpsafe == 0) { | |
| 277 | mpsafe = 1; | |
| 278 | rel_mplock(); | |
| 279 | } | |
| 280 | } else { | |
| 281 | /* | |
| 282 | * The request might be removed while we | |
| 283 | * are waiting to get the MP lock. If it | |
| 284 | * was removed sc->next will point to the | |
| 285 | * next valid request or NULL, loop up. | |
| 286 | */ | |
| 287 | if (mpsafe) { | |
| 288 | mpsafe = 0; | |
| 289 | sc->next = c; | |
| 290 | get_mplock(); | |
| 291 | if (c != sc->next) | |
| 292 | continue; | |
| 293 | } | |
| 294 | } | |
| 295 | #endif | |
| 296 | sc->next = TAILQ_NEXT(c, c_links.tqe); | |
| 92b561b7 | 297 | TAILQ_REMOVE(bucket, c, c_links.tqe); |
| 4ac1a4cd | 298 | |
| b90e37ec | 299 | sc->running = c; |
| 92b561b7 MD |
300 | c_func = c->c_func; |
| 301 | c_arg = c->c_arg; | |
| 92b561b7 MD |
302 | c->c_func = NULL; |
| 303 | KKASSERT(c->c_flags & CALLOUT_DID_INIT); | |
| 7eb3b2ae | 304 | c->c_flags &= ~CALLOUT_PENDING; |
| 92b561b7 | 305 | crit_exit(); |
| 92b561b7 MD |
306 | c_func(c_arg); |
| 307 | crit_enter(); | |
| b90e37ec | 308 | sc->running = NULL; |
| 92b561b7 | 309 | /* NOTE: list may have changed */ |
| 984263bc | 310 | } |
| 92b561b7 | 311 | ++sc->softticks; |
| 984263bc | 312 | } |
| 92b561b7 MD |
313 | sc->isrunning = 0; |
| 314 | lwkt_deschedule_self(&sc->thread); /* == curthread */ | |
| 315 | lwkt_switch(); | |
| 316 | goto loop; | |
| 317 | /* NOT REACHED */ | |
| 984263bc MD |
318 | } |
| 319 | ||
| 984263bc MD |
320 | /* |
| 321 | * New interface; clients allocate their own callout structures. | |
| 322 | * | |
| 323 | * callout_reset() - establish or change a timeout | |
| 324 | * callout_stop() - disestablish a timeout | |
| 325 | * callout_init() - initialize a callout structure so that it can | |
| 92b561b7 MD |
326 | * safely be passed to callout_reset() and callout_stop() |
| 327 | * callout_init_mp() - same but any installed functions must be MP safe. | |
| 984263bc MD |
328 | * |
| 329 | * <sys/callout.h> defines three convenience macros: | |
| 330 | * | |
| 331 | * callout_active() - returns truth if callout has not been serviced | |
| 332 | * callout_pending() - returns truth if callout is still waiting for timeout | |
| 333 | * callout_deactivate() - marks the callout as having been serviced | |
| 334 | */ | |
| 92b561b7 MD |
335 | |
| 336 | /* | |
| 337 | * Start or restart a timeout. Install the callout structure in the | |
| 338 | * callwheel. Callers may legally pass any value, even if 0 or negative, | |
| 339 | * but since the sc->curticks index may have already been processed a | |
| 340 | * minimum timeout of 1 tick will be enforced. | |
| 341 | * | |
| 342 | * The callout is installed on and will be processed on the current cpu's | |
| 343 | * callout wheel. | |
| 7ac978db MD |
344 | * |
| 345 | * WARNING! This function may be called from any cpu but the caller must | |
| 346 | * serialize callout_stop() and callout_reset() calls on the passed | |
| 347 | * structure regardless of cpu. | |
| 92b561b7 | 348 | */ |
| 984263bc | 349 | void |
| 92b561b7 MD |
350 | callout_reset(struct callout *c, int to_ticks, void (*ftn)(void *), |
| 351 | void *arg) | |
| 984263bc | 352 | { |
| 92b561b7 MD |
353 | softclock_pcpu_t sc; |
| 354 | globaldata_t gd; | |
| 355 | ||
| 356 | #ifdef INVARIANTS | |
| 357 | if ((c->c_flags & CALLOUT_DID_INIT) == 0) { | |
| 358 | callout_init(c); | |
| 6ea70f76 | 359 | kprintf( |
| 92b561b7 MD |
360 | "callout_reset(%p) from %p: callout was not initialized\n", |
| 361 | c, ((int **)&c)[-1]); | |
| 7ce2998e | 362 | print_backtrace(-1); |
| 92b561b7 MD |
363 | } |
| 364 | #endif | |
| 365 | gd = mycpu; | |
| 366 | sc = &softclock_pcpu_ary[gd->gd_cpuid]; | |
| 367 | crit_enter_gd(gd); | |
| 984263bc | 368 | |
| b90e37ec | 369 | if (c->c_flags & CALLOUT_ACTIVE) |
| 984263bc MD |
370 | callout_stop(c); |
| 371 | ||
| 984263bc MD |
372 | if (to_ticks <= 0) |
| 373 | to_ticks = 1; | |
| 374 | ||
| 375 | c->c_arg = arg; | |
| 376 | c->c_flags |= (CALLOUT_ACTIVE | CALLOUT_PENDING); | |
| 377 | c->c_func = ftn; | |
| 92b561b7 MD |
378 | c->c_time = sc->curticks + to_ticks; |
| 379 | #ifdef SMP | |
| 380 | c->c_gd = gd; | |
| 381 | #endif | |
| 382 | ||
| 383 | TAILQ_INSERT_TAIL(&sc->callwheel[c->c_time & callwheelmask], | |
| 984263bc | 384 | c, c_links.tqe); |
| 92b561b7 | 385 | crit_exit_gd(gd); |
| 984263bc MD |
386 | } |
| 387 | ||
| 92b561b7 MD |
388 | /* |
| 389 | * Stop a running timer. WARNING! If called on a cpu other then the one | |
| 390 | * the callout was started on this function will liveloop on its IPI to | |
| 391 | * the target cpu to process the request. It is possible for the callout | |
| 392 | * to execute in that case. | |
| 393 | * | |
| 7ac978db MD |
394 | * WARNING! This function may be called from any cpu but the caller must |
| 395 | * serialize callout_stop() and callout_reset() calls on the passed | |
| 396 | * structure regardless of cpu. | |
| 397 | * | |
| 92b561b7 | 398 | * WARNING! This routine may be called from an IPI |
| b90e37ec MD |
399 | * |
| 400 | * WARNING! This function can return while it's c_func is still running | |
| 401 | * in the callout thread, a secondary check may be needed. | |
| c5b9d2a5 MD |
402 | * Use callout_stop_sync() to wait for any callout function to |
| 403 | * complete before returning, being sure that no deadlock is | |
| 404 | * possible if you do. | |
| 92b561b7 | 405 | */ |
| 984263bc | 406 | int |
| 216ffe3f | 407 | callout_stop(struct callout *c) |
| 984263bc | 408 | { |
| 92b561b7 MD |
409 | globaldata_t gd = mycpu; |
| 410 | #ifdef SMP | |
| 411 | globaldata_t tgd; | |
| 412 | #endif | |
| 413 | softclock_pcpu_t sc; | |
| 414 | ||
| 415 | #ifdef INVARIANTS | |
| 416 | if ((c->c_flags & CALLOUT_DID_INIT) == 0) { | |
| 417 | callout_init(c); | |
| 6ea70f76 | 418 | kprintf( |
| 634056cf | 419 | "callout_stop(%p) from %p: callout was not initialized\n", |
| 92b561b7 | 420 | c, ((int **)&c)[-1]); |
| 7ce2998e | 421 | print_backtrace(-1); |
| 92b561b7 MD |
422 | } |
| 423 | #endif | |
| 424 | crit_enter_gd(gd); | |
| 984263bc | 425 | |
| 984263bc | 426 | /* |
| 7ac978db MD |
427 | * Don't attempt to delete a callout that's not on the queue. The |
| 428 | * callout may not have a cpu assigned to it. Callers do not have | |
| 429 | * to be on the issuing cpu but must still serialize access to the | |
| 430 | * callout structure. | |
| 431 | * | |
| 432 | * We are not cpu-localized here and cannot safely modify the | |
| 433 | * flags field in the callout structure. Note that most of the | |
| 434 | * time CALLOUT_ACTIVE will be 0 if CALLOUT_PENDING is also 0. | |
| 435 | * | |
| 436 | * If we race another cpu's dispatch of this callout it is possible | |
| 437 | * for CALLOUT_ACTIVE to be set with CALLOUT_PENDING unset. This | |
| 438 | * will cause us to fall through and synchronize with the other | |
| 439 | * cpu. | |
| 984263bc | 440 | */ |
| 92b561b7 | 441 | if ((c->c_flags & CALLOUT_PENDING) == 0) { |
| 7ac978db MD |
442 | #ifdef SMP |
| 443 | if ((c->c_flags & CALLOUT_ACTIVE) == 0) { | |
| 444 | crit_exit_gd(gd); | |
| 445 | return (0); | |
| 446 | } | |
| 447 | if (c->c_gd == NULL || c->c_gd == gd) { | |
| 448 | c->c_flags &= ~CALLOUT_ACTIVE; | |
| 449 | crit_exit_gd(gd); | |
| 450 | return (0); | |
| 451 | } | |
| 452 | /* fall-through to the cpu-localization code. */ | |
| 453 | #else | |
| 984263bc | 454 | c->c_flags &= ~CALLOUT_ACTIVE; |
| 92b561b7 | 455 | crit_exit_gd(gd); |
| 984263bc | 456 | return (0); |
| 7ac978db | 457 | #endif |
| 984263bc | 458 | } |
| 92b561b7 MD |
459 | #ifdef SMP |
| 460 | if ((tgd = c->c_gd) != gd) { | |
| 461 | /* | |
| 462 | * If the callout is owned by a different CPU we have to | |
| 463 | * execute the function synchronously on the target cpu. | |
| 464 | */ | |
| 465 | int seq; | |
| 466 | ||
| 35238fa5 | 467 | cpu_ccfence(); /* don't let tgd alias c_gd */ |
| 92b561b7 MD |
468 | seq = lwkt_send_ipiq(tgd, (void *)callout_stop, c); |
| 469 | lwkt_wait_ipiq(tgd, seq); | |
| 470 | } else | |
| 471 | #endif | |
| 472 | { | |
| 473 | /* | |
| 474 | * If the callout is owned by the same CPU we can | |
| 475 | * process it directly, but if we are racing our helper | |
| 476 | * thread (sc->next), we have to adjust sc->next. The | |
| 477 | * race is interlocked by a critical section. | |
| 478 | */ | |
| 479 | sc = &softclock_pcpu_ary[gd->gd_cpuid]; | |
| 984263bc | 480 | |
| 92b561b7 MD |
481 | c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING); |
| 482 | if (sc->next == c) | |
| 483 | sc->next = TAILQ_NEXT(c, c_links.tqe); | |
| 484 | ||
| 485 | TAILQ_REMOVE(&sc->callwheel[c->c_time & callwheelmask], | |
| 486 | c, c_links.tqe); | |
| 487 | c->c_func = NULL; | |
| 984263bc | 488 | } |
| 92b561b7 | 489 | crit_exit_gd(gd); |
| 984263bc MD |
490 | return (1); |
| 491 | } | |
| 492 | ||
| 92b561b7 | 493 | /* |
| c5b9d2a5 MD |
494 | * Issue a callout_stop() and ensure that any callout race completes |
| 495 | * before returning. Does NOT de-initialized the callout. | |
| 496 | */ | |
| 497 | void | |
| 498 | callout_stop_sync(struct callout *c) | |
| 499 | { | |
| 500 | softclock_pcpu_t sc; | |
| 501 | ||
| 502 | if (c->c_flags & CALLOUT_DID_INIT) { | |
| 503 | callout_stop(c); | |
| 504 | #ifdef SMP | |
| 505 | sc = &softclock_pcpu_ary[c->c_gd->gd_cpuid]; | |
| 506 | #else | |
| 507 | sc = &softclock_pcpu_ary[0]; | |
| 508 | #endif | |
| 509 | if (sc->running == c) { | |
| 510 | while (sc->running == c) | |
| 511 | tsleep(&sc->running, 0, "crace", 1); | |
| 512 | } | |
| 513 | KKASSERT((c->c_flags & (CALLOUT_PENDING|CALLOUT_ACTIVE)) == 0); | |
| 514 | } | |
| 515 | } | |
| 516 | ||
| 517 | /* | |
| b90e37ec MD |
518 | * Terminate a callout |
| 519 | * | |
| 520 | * This function will stop any pending callout and also block while the | |
| 521 | * callout's function is running. It should only be used in cases where | |
| 522 | * no deadlock is possible (due to the callout function acquiring locks | |
| 523 | * that the current caller of callout_terminate() already holds), when | |
| 524 | * the caller is ready to destroy the callout structure. | |
| 525 | * | |
| 526 | * This function clears the CALLOUT_DID_INIT flag. | |
| 527 | * | |
| 528 | * lwkt_token locks are ok. | |
| 529 | */ | |
| 530 | void | |
| 531 | callout_terminate(struct callout *c) | |
| 532 | { | |
| 533 | softclock_pcpu_t sc; | |
| 534 | ||
| 535 | if (c->c_flags & CALLOUT_DID_INIT) { | |
| 536 | callout_stop(c); | |
| 2e393ab4 | 537 | #ifdef SMP |
| b90e37ec | 538 | sc = &softclock_pcpu_ary[c->c_gd->gd_cpuid]; |
| 2e393ab4 SW |
539 | #else |
| 540 | sc = &softclock_pcpu_ary[0]; | |
| 541 | #endif | |
| b90e37ec | 542 | if (sc->running == c) { |
| b90e37ec MD |
543 | while (sc->running == c) |
| 544 | tsleep(&sc->running, 0, "crace", 1); | |
| 545 | } | |
| 546 | KKASSERT((c->c_flags & (CALLOUT_PENDING|CALLOUT_ACTIVE)) == 0); | |
| 547 | c->c_flags &= ~CALLOUT_DID_INIT; | |
| 548 | } | |
| 549 | } | |
| 550 | ||
| 551 | /* | |
| 92b561b7 MD |
552 | * Prepare a callout structure for use by callout_reset() and/or |
| 553 | * callout_stop(). The MP version of this routine requires that the callback | |
| 986d8a07 | 554 | * function installed by callout_reset() be MP safe. |
| 48e7b118 MD |
555 | * |
| 556 | * The init functions can be called from any cpu and do not have to be | |
| 557 | * called from the cpu that the timer will eventually run on. | |
| 92b561b7 | 558 | */ |
| 984263bc | 559 | void |
| 216ffe3f | 560 | callout_init(struct callout *c) |
| 984263bc MD |
561 | { |
| 562 | bzero(c, sizeof *c); | |
| 92b561b7 | 563 | c->c_flags = CALLOUT_DID_INIT; |
| 984263bc MD |
564 | } |
| 565 | ||
| 92b561b7 MD |
566 | void |
| 567 | callout_init_mp(struct callout *c) | |
| ef0fdad1 | 568 | { |
| 92b561b7 MD |
569 | callout_init(c); |
| 570 | c->c_flags |= CALLOUT_MPSAFE; | |
| ef0fdad1 MD |
571 | } |
| 572 | ||
| 92b561b7 MD |
573 | /* What, are you joking? This is nuts! -Matt */ |
| 574 | #if 0 | |
| 984263bc MD |
575 | #ifdef APM_FIXUP_CALLTODO |
| 576 | /* | |
| 577 | * Adjust the kernel calltodo timeout list. This routine is used after | |
| 578 | * an APM resume to recalculate the calltodo timer list values with the | |
| 579 | * number of hz's we have been sleeping. The next hardclock() will detect | |
| 580 | * that there are fired timers and run softclock() to execute them. | |
| 581 | * | |
| 582 | * Please note, I have not done an exhaustive analysis of what code this | |
| 583 | * might break. I am motivated to have my select()'s and alarm()'s that | |
| 584 | * have expired during suspend firing upon resume so that the applications | |
| 585 | * which set the timer can do the maintanence the timer was for as close | |
| 586 | * as possible to the originally intended time. Testing this code for a | |
| 587 | * week showed that resuming from a suspend resulted in 22 to 25 timers | |
| 588 | * firing, which seemed independant on whether the suspend was 2 hours or | |
| 589 | * 2 days. Your milage may vary. - Ken Key <key@cs.utk.edu> | |
| 590 | */ | |
| 591 | void | |
| 216ffe3f | 592 | adjust_timeout_calltodo(struct timeval *time_change) |
| 984263bc | 593 | { |
| 1fd87d54 | 594 | struct callout *p; |
| 984263bc | 595 | unsigned long delta_ticks; |
| 984263bc MD |
596 | |
| 597 | /* | |
| 598 | * How many ticks were we asleep? | |
| 599 | * (stolen from tvtohz()). | |
| 600 | */ | |
| 601 | ||
| 602 | /* Don't do anything */ | |
| 603 | if (time_change->tv_sec < 0) | |
| 604 | return; | |
| 605 | else if (time_change->tv_sec <= LONG_MAX / 1000000) | |
| 606 | delta_ticks = (time_change->tv_sec * 1000000 + | |
| 607 | time_change->tv_usec + (tick - 1)) / tick + 1; | |
| 608 | else if (time_change->tv_sec <= LONG_MAX / hz) | |
| 609 | delta_ticks = time_change->tv_sec * hz + | |
| 610 | (time_change->tv_usec + (tick - 1)) / tick + 1; | |
| 611 | else | |
| 612 | delta_ticks = LONG_MAX; | |
| 613 | ||
| 614 | if (delta_ticks > INT_MAX) | |
| 615 | delta_ticks = INT_MAX; | |
| 616 | ||
| 617 | /* | |
| 618 | * Now rip through the timer calltodo list looking for timers | |
| 619 | * to expire. | |
| 620 | */ | |
| 621 | ||
| 622 | /* don't collide with softclock() */ | |
| e43a034f | 623 | crit_enter(); |
| 984263bc MD |
624 | for (p = calltodo.c_next; p != NULL; p = p->c_next) { |
| 625 | p->c_time -= delta_ticks; | |
| 626 | ||
| 627 | /* Break if the timer had more time on it than delta_ticks */ | |
| 628 | if (p->c_time > 0) | |
| 629 | break; | |
| 630 | ||
| 631 | /* take back the ticks the timer didn't use (p->c_time <= 0) */ | |
| 632 | delta_ticks = -p->c_time; | |
| 633 | } | |
| e43a034f | 634 | crit_exit(); |
| 984263bc MD |
635 | |
| 636 | return; | |
| 637 | } | |
| 638 | #endif /* APM_FIXUP_CALLTODO */ | |
| 92b561b7 MD |
639 | #endif |
| 640 |