]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/timeout.9
Merge compiler-rt trunk r366426, resolve conflicts, and add
[FreeBSD/FreeBSD.git] / share / man / man9 / timeout.9
1 .\"     $NetBSD: timeout.9,v 1.2 1996/06/23 22:32:34 pk Exp $
2 .\"
3 .\" Copyright (c) 1996 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
5 .\"
6 .\" This code is derived from software contributed to The NetBSD Foundation
7 .\" by Paul Kranenburg.
8 .\"
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
11 .\" are met:
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\"    notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\"    notice, this list of conditions and the following disclaimer in the
16 .\"    documentation and/or other materials provided with the distribution.
17 .\"
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
22 .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
29 .\"
30 .\" $FreeBSD$
31 .\"
32 .Dd July 27, 2016
33 .Dt TIMEOUT 9
34 .Os
35 .Sh NAME
36 .Nm callout_active ,
37 .Nm callout_deactivate ,
38 .Nm callout_async_drain ,
39 .Nm callout_drain ,
40 .Nm callout_handle_init ,
41 .Nm callout_init ,
42 .Nm callout_init_mtx ,
43 .Nm callout_init_rm ,
44 .Nm callout_init_rw ,
45 .Nm callout_pending ,
46 .Nm callout_reset ,
47 .Nm callout_reset_curcpu ,
48 .Nm callout_reset_on ,
49 .Nm callout_reset_sbt ,
50 .Nm callout_reset_sbt_curcpu ,
51 .Nm callout_reset_sbt_on ,
52 .Nm callout_schedule ,
53 .Nm callout_schedule_curcpu ,
54 .Nm callout_schedule_on ,
55 .Nm callout_schedule_sbt ,
56 .Nm callout_schedule_sbt_curcpu ,
57 .Nm callout_schedule_sbt_on ,
58 .Nm callout_stop ,
59 .Nm callout_when ,
60 .Nm timeout ,
61 .Nm untimeout
62 .Nd execute a function after a specified length of time
63 .Sh SYNOPSIS
64 .In sys/types.h
65 .In sys/systm.h
66 .Bd -literal
67 typedef void timeout_t (void *);
68 .Ed
69 .Ft int
70 .Fn callout_active "struct callout *c"
71 .Ft void
72 .Fn callout_deactivate "struct callout *c"
73 .Ft int
74 .Fn callout_async_drain "struct callout *c" "timeout_t *drain"
75 .Ft int
76 .Fn callout_drain "struct callout *c"
77 .Ft void
78 .Fn callout_handle_init "struct callout_handle *handle"
79 .Bd -literal
80 struct callout_handle handle = CALLOUT_HANDLE_INITIALIZER(&handle);
81 .Ed
82 .Ft void
83 .Fn callout_init "struct callout *c" "int mpsafe"
84 .Ft void
85 .Fn callout_init_mtx "struct callout *c" "struct mtx *mtx" "int flags"
86 .Ft void
87 .Fn callout_init_rm "struct callout *c" "struct rmlock *rm" "int flags"
88 .Ft void
89 .Fn callout_init_rw "struct callout *c" "struct rwlock *rw" "int flags"
90 .Ft int
91 .Fn callout_pending "struct callout *c"
92 .Ft int
93 .Fn callout_reset "struct callout *c" "int ticks" "timeout_t *func" "void *arg"
94 .Ft int
95 .Fo callout_reset_curcpu
96 .Fa "struct callout *c"
97 .Fa "int ticks"
98 .Fa "timeout_t *func"
99 .Fa "void *arg"
100 .Fc
101 .Ft int
102 .Fo callout_reset_on
103 .Fa "struct callout *c"
104 .Fa "int ticks"
105 .Fa "timeout_t *func"
106 .Fa "void *arg"
107 .Fa "int cpu"
108 .Fc
109 .Ft int
110 .Fo callout_reset_sbt
111 .Fa "struct callout *c"
112 .Fa "sbintime_t sbt"
113 .Fa "sbintime_t pr"
114 .Fa "timeout_t *func"
115 .Fa "void *arg"
116 .Fa "int flags"
117 .Fc
118 .Ft int
119 .Fo callout_reset_sbt_curcpu
120 .Fa "struct callout *c"
121 .Fa "sbintime_t sbt"
122 .Fa "sbintime_t pr"
123 .Fa "timeout_t *func"
124 .Fa "void *arg"
125 .Fa "int flags"
126 .Fc
127 .Ft int
128 .Fo callout_reset_sbt_on
129 .Fa "struct callout *c"
130 .Fa "sbintime_t sbt"
131 .Fa "sbintime_t pr"
132 .Fa "timeout_t *func"
133 .Fa "void *arg"
134 .Fa "int cpu"
135 .Fa "int flags"
136 .Fc
137 .Ft int
138 .Fn callout_schedule "struct callout *c" "int ticks"
139 .Ft int
140 .Fn callout_schedule_curcpu "struct callout *c" "int ticks"
141 .Ft int
142 .Fn callout_schedule_on "struct callout *c" "int ticks" "int cpu"
143 .Ft int
144 .Fo callout_schedule_sbt
145 .Fa "struct callout *c"
146 .Fa "sbintime_t sbt"
147 .Fa "sbintime_t pr"
148 .Fa "int flags"
149 .Fc
150 .Ft int
151 .Fo callout_schedule_sbt_curcpu
152 .Fa "struct callout *c"
153 .Fa "sbintime_t sbt"
154 .Fa "sbintime_t pr"
155 .Fa "int flags"
156 .Fc
157 .Ft int
158 .Fo callout_schedule_sbt_on
159 .Fa "struct callout *c"
160 .Fa "sbintime_t sbt"
161 .Fa "sbintime_t pr"
162 .Fa "int cpu"
163 .Fa "int flags"
164 .Fc
165 .Ft int
166 .Fn callout_stop "struct callout *c"
167 .Ft sbintime_t
168 .Fo callout_when
169 .Fa "sbintime_t sbt"
170 .Fa "sbintime_t precision"
171 .Fa "int flags"
172 .Fa "sbintime_t *sbt_res"
173 .Fa "sbintime_t *precision_res"
174 .Fc
175 .Ft struct callout_handle
176 .Fn timeout "timeout_t *func" "void *arg" "int ticks"
177 .Ft void
178 .Fn untimeout "timeout_t *func" "void *arg" "struct callout_handle handle"
179 .Sh DESCRIPTION
180 The
181 .Nm callout
182 API is used to schedule a call to an arbitrary function at a specific
183 time in the future.
184 Consumers of this API are required to allocate a callout structure
185 .Pq struct callout
186 for each pending function invocation.
187 This structure stores state about the pending function invocation including
188 the function to be called and the time at which the function should be invoked.
189 Pending function calls can be cancelled or rescheduled to a different time.
190 In addition,
191 a callout structure may be reused to schedule a new function call after a
192 scheduled call is completed.
193 .Pp
194 Callouts only provide a single-shot mode.
195 If a consumer requires a periodic timer,
196 it must explicitly reschedule each function call.
197 This is normally done by rescheduling the subsequent call within the called
198 function.
199 .Pp
200 Callout functions must not sleep.
201 They may not acquire sleepable locks,
202 wait on condition variables,
203 perform blocking allocation requests,
204 or invoke any other action that might sleep.
205 .Pp
206 Each callout structure must be initialized by
207 .Fn callout_init ,
208 .Fn callout_init_mtx ,
209 .Fn callout_init_rm ,
210 or
211 .Fn callout_init_rw
212 before it is passed to any of the other callout functions.
213 The
214 .Fn callout_init
215 function initializes a callout structure in
216 .Fa c
217 that is not associated with a specific lock.
218 If the
219 .Fa mpsafe
220 argument is zero,
221 the callout structure is not considered to be
222 .Dq multi-processor safe ;
223 and the Giant lock will be acquired before calling the callout function
224 and released when the callout function returns.
225 .Pp
226 The
227 .Fn callout_init_mtx ,
228 .Fn callout_init_rm ,
229 and
230 .Fn callout_init_rw
231 functions initialize a callout structure in
232 .Fa c
233 that is associated with a specific lock.
234 The lock is specified by the
235 .Fa mtx ,
236 .Fa rm ,
237 or
238 .Fa rw
239 parameter.
240 The associated lock must be held while stopping or rescheduling the
241 callout.
242 The callout subsystem acquires the associated lock before calling the
243 callout function and releases it after the function returns.
244 If the callout was cancelled while the callout subsystem waited for the
245 associated lock,
246 the callout function is not called,
247 and the associated lock is released.
248 This ensures that stopping or rescheduling the callout will abort any
249 previously scheduled invocation.
250 .Pp
251 Only regular mutexes may be used with
252 .Fn callout_init_mtx ;
253 spin mutexes are not supported.
254 A sleepable read-mostly lock
255 .Po
256 one initialized with the
257 .Dv RM_SLEEPABLE
258 flag
259 .Pc
260 may not be used with
261 .Fn callout_init_rm .
262 Similarly, other sleepable lock types such as
263 .Xr sx 9
264 and
265 .Xr lockmgr 9
266 cannot be used with callouts because sleeping is not permitted in
267 the callout subsystem.
268 .Pp
269 These
270 .Fa flags
271 may be specified for
272 .Fn callout_init_mtx ,
273 .Fn callout_init_rm ,
274 or
275 .Fn callout_init_rw :
276 .Bl -tag -width ".Dv CALLOUT_RETURNUNLOCKED"
277 .It Dv CALLOUT_RETURNUNLOCKED
278 The callout function will release the associated lock itself,
279 so the callout subsystem should not attempt to unlock it
280 after the callout function returns.
281 .It Dv CALLOUT_SHAREDLOCK
282 The lock is only acquired in read mode when running the callout handler.
283 This flag is ignored by
284 .Fn callout_init_mtx .
285 .El
286 .Pp
287 The function
288 .Fn callout_stop
289 cancels a callout
290 .Fa c
291 if it is currently pending.
292 If the callout is pending and successfully stopped, then
293 .Fn callout_stop
294 returns a value of one.
295 If the callout is not set, or
296 has already been serviced, then
297 negative one is returned.
298 If the callout is currently being serviced and cannot be stopped,
299 then zero will be returned.
300 If the callout is currently being serviced and cannot be stopped, and at the
301 same time a next invocation of the same callout is also scheduled, then
302 .Fn callout_stop
303 unschedules the next run and returns zero.
304 If the callout has an associated lock,
305 then that lock must be held when this function is called.
306 .Pp
307 The function
308 .Fn callout_async_drain
309 is identical to
310 .Fn callout_stop
311 with one difference.
312 When
313 .Fn callout_async_drain
314 returns zero it will arrange for the function
315 .Fa drain
316 to be called using the same argument given to the
317 .Fn callout_reset
318 function.
319 .Fn callout_async_drain
320 If the callout has an associated lock,
321 then that lock must be held when this function is called.
322 Note that when stopping multiple callouts that use the same lock it is possible
323 to get multiple return's of zero and multiple calls to the
324 .Fa drain
325 function, depending upon which CPU's the callouts are running.
326 The
327 .Fa drain
328 function itself is called from the context of the completing callout
329 i.e. softclock or hardclock, just like a callout itself.
330 .Pp
331 The function
332 .Fn callout_drain
333 is identical to
334 .Fn callout_stop
335 except that it will wait for the callout
336 .Fa c
337 to complete if it is already in progress.
338 This function MUST NOT be called while holding any
339 locks on which the callout might block, or deadlock will result.
340 Note that if the callout subsystem has already begun processing this
341 callout, then the callout function may be invoked before
342 .Fn callout_drain
343 returns.
344 However, the callout subsystem does guarantee that the callout will be
345 fully stopped before
346 .Fn callout_drain
347 returns.
348 .Pp
349 The
350 .Fn callout_reset
351 and
352 .Fn callout_schedule
353 function families schedule a future function invocation for callout
354 .Fa c .
355 If
356 .Fa c
357 already has a pending callout,
358 it is cancelled before the new invocation is scheduled.
359 These functions return a value of one if a pending callout was cancelled
360 and zero if there was no pending callout.
361 If the callout has an associated lock,
362 then that lock must be held when any of these functions are called.
363 .Pp
364 The time at which the callout function will be invoked is determined by
365 either the
366 .Fa ticks
367 argument or the
368 .Fa sbt ,
369 .Fa pr ,
370 and
371 .Fa flags
372 arguments.
373 When
374 .Fa ticks
375 is used,
376 the callout is scheduled to execute after
377 .Fa ticks Ns No /hz
378 seconds.
379 Non-positive values of
380 .Fa ticks
381 are silently converted to the value
382 .Sq 1 .
383 .Pp
384 The
385 .Fa sbt ,
386 .Fa pr ,
387 and
388 .Fa flags
389 arguments provide more control over the scheduled time including
390 support for higher resolution times,
391 specifying the precision of the scheduled time,
392 and setting an absolute deadline instead of a relative timeout.
393 The callout is scheduled to execute in a time window which begins at
394 the time specified in
395 .Fa sbt
396 and extends for the amount of time specified in
397 .Fa pr .
398 If
399 .Fa sbt
400 specifies a time in the past,
401 the window is adjusted to start at the current time.
402 A non-zero value for
403 .Fa pr
404 allows the callout subsystem to coalesce callouts scheduled close to each
405 other into fewer timer interrupts,
406 reducing processing overhead and power consumption.
407 These
408 .Fa flags
409 may be specified to adjust the interpretation of
410 .Fa sbt
411 and
412 .Fa pr :
413 .Bl -tag -width ".Dv C_DIRECT_EXEC"
414 .It Dv C_ABSOLUTE
415 Handle the
416 .Fa sbt
417 argument as an absolute time since boot.
418 By default,
419 .Fa sbt
420 is treated as a relative amount of time,
421 similar to
422 .Fa ticks .
423 .It Dv C_DIRECT_EXEC
424 Run the handler directly from hardware interrupt context instead of from the
425 softclock thread.
426 This reduces latency and overhead, but puts more constraints on the callout
427 function.
428 Callout functions run in this context may use only spin mutexes for locking
429 and should be as small as possible because they run with absolute priority.
430 .It Fn C_PREL
431 Specifies relative event time precision as binary logarithm of time interval
432 divided by acceptable time deviation: 1 -- 1/2, 2 -- 1/4, etc.
433 Note that the larger of
434 .Fa pr
435 or this value is used as the length of the time window.
436 Smaller values
437 .Pq which result in larger time intervals
438 allow the callout subsystem to aggregate more events in one timer interrupt.
439 .It Dv C_PRECALC
440 The
441 .Fa sbt
442 argument specifies the absolute time at which the callout should be run,
443 and the
444 .Fa pr
445 argument specifies the requested precision, which will not be
446 adjusted during the scheduling process.
447 The
448 .Fa sbt
449 and
450 .Fa pr
451 values should be calculated by an earlier call to
452 .Fn callout_when
453 which uses the user-supplied
454 .Fa sbt ,
455 .Fa pr ,
456 and
457 .Fa flags
458 values.
459 .It Dv C_HARDCLOCK
460 Align the timeouts to
461 .Fn hardclock
462 calls if possible.
463 .El
464 .Pp
465 The
466 .Fn callout_reset
467 functions accept a
468 .Fa func
469 argument which identifies the function to be called when the time expires.
470 It must be a pointer to a function that takes a single
471 .Fa void *
472 argument.
473 Upon invocation,
474 .Fa func
475 will receive
476 .Fa arg
477 as its only argument.
478 The
479 .Fn callout_schedule
480 functions reuse the
481 .Fa func
482 and
483 .Fa arg
484 arguments from the previous callout.
485 Note that one of the
486 .Fn callout_reset
487 functions must always be called to initialize
488 .Fa func
489 and
490 .Fa arg
491 before one of the
492 .Fn callout_schedule
493 functions can be used.
494 .Pp
495 The callout subsystem provides a softclock thread for each CPU in the system.
496 Callouts are assigned to a single CPU and are executed by the softclock thread
497 for that CPU.
498 Initially,
499 callouts are assigned to CPU 0.
500 The
501 .Fn callout_reset_on ,
502 .Fn callout_reset_sbt_on ,
503 .Fn callout_schedule_on
504 and
505 .Fn callout_schedule_sbt_on
506 functions assign the callout to CPU
507 .Fa cpu .
508 The
509 .Fn callout_reset_curcpu ,
510 .Fn callout_reset_sbt_curpu ,
511 .Fn callout_schedule_curcpu
512 and
513 .Fn callout_schedule_sbt_curcpu
514 functions assign the callout to the current CPU.
515 The
516 .Fn callout_reset ,
517 .Fn callout_reset_sbt ,
518 .Fn callout_schedule
519 and
520 .Fn callout_schedule_sbt
521 functions schedule the callout to execute in the softclock thread of the CPU
522 to which it is currently assigned.
523 .Pp
524 Softclock threads are not pinned to their respective CPUs by default.
525 The softclock thread for CPU 0 can be pinned to CPU 0 by setting the
526 .Va kern.pin_default_swi
527 loader tunable to a non-zero value.
528 Softclock threads for CPUs other than zero can be pinned to their
529 respective CPUs by setting the
530 .Va kern.pin_pcpu_swi
531 loader tunable to a non-zero value.
532 .Pp
533 The macros
534 .Fn callout_pending ,
535 .Fn callout_active
536 and
537 .Fn callout_deactivate
538 provide access to the current state of the callout.
539 The
540 .Fn callout_pending
541 macro checks whether a callout is
542 .Em pending ;
543 a callout is considered
544 .Em pending
545 when a timeout has been set but the time has not yet arrived.
546 Note that once the timeout time arrives and the callout subsystem
547 starts to process this callout,
548 .Fn callout_pending
549 will return
550 .Dv FALSE
551 even though the callout function may not have finished
552 .Pq or even begun
553 executing.
554 The
555 .Fn callout_active
556 macro checks whether a callout is marked as
557 .Em active ,
558 and the
559 .Fn callout_deactivate
560 macro clears the callout's
561 .Em active
562 flag.
563 The callout subsystem marks a callout as
564 .Em active
565 when a timeout is set and it clears the
566 .Em active
567 flag in
568 .Fn callout_stop
569 and
570 .Fn callout_drain ,
571 but it
572 .Em does not
573 clear it when a callout expires normally via the execution of the
574 callout function.
575 .Pp
576 The
577 .Fn callout_when
578 function may be used to pre-calculate the absolute time at which the
579 timeout should be run and the precision of the scheduled run time
580 according to the required time
581 .Fa sbt ,
582 precision
583 .Fa precision ,
584 and additional adjustments requested by the
585 .Fa flags
586 argument.
587 Flags accepted by the
588 .Fn callout_when
589 function are the same as flags for the
590 .Fn callout_reset
591 function.
592 The resulting time is assigned to the variable pointed to by the
593 .Fa sbt_res
594 argument, and the resulting precision is assigned to
595 .Fa *precision_res .
596 When passing the results to
597 .Fa callout_reset ,
598 add the
599 .Va C_PRECALC
600 flag to
601 .Fa flags ,
602 to avoid incorrect re-adjustment.
603 The function is intended for situations where precise time of the callout
604 run should be known in advance, since
605 trying to read this time from the callout structure itself after a
606 .Fn callout_reset
607 call is racy.
608 .Ss "Avoiding Race Conditions"
609 The callout subsystem invokes callout functions from its own thread
610 context.
611 Without some kind of synchronization,
612 it is possible that a callout
613 function will be invoked concurrently with an attempt to stop or reset
614 the callout by another thread.
615 In particular, since callout functions typically acquire a lock as
616 their first action, the callout function may have already been invoked,
617 but is blocked waiting for that lock at the time that another thread
618 tries to reset or stop the callout.
619 .Pp
620 There are three main techniques for addressing these
621 synchronization concerns.
622 The first approach is preferred as it is the simplest:
623 .Bl -enum -offset indent
624 .It
625 Callouts can be associated with a specific lock when they are initialized
626 by
627 .Fn callout_init_mtx ,
628 .Fn callout_init_rm ,
629 or
630 .Fn callout_init_rw .
631 When a callout is associated with a lock,
632 the callout subsystem acquires the lock before the callout function is
633 invoked.
634 This allows the callout subsystem to transparently handle races between
635 callout cancellation,
636 scheduling,
637 and execution.
638 Note that the associated lock must be acquired before calling
639 .Fn callout_stop
640 or one of the
641 .Fn callout_reset
642 or
643 .Fn callout_schedule
644 functions to provide this safety.
645 .Pp
646 A callout initialized via
647 .Fn callout_init
648 with
649 .Fa mpsafe
650 set to zero is implicitly associated with the
651 .Va Giant
652 mutex.
653 If
654 .Va Giant
655 is held when cancelling or rescheduling the callout,
656 then its use will prevent races with the callout function.
657 .It
658 The return value from
659 .Fn callout_stop
660 .Po
661 or the
662 .Fn callout_reset
663 and
664 .Fn callout_schedule
665 function families
666 .Pc
667 indicates whether or not the callout was removed.
668 If it is known that the callout was set and the callout function has
669 not yet executed, then a return value of
670 .Dv FALSE
671 indicates that the callout function is about to be called.
672 For example:
673 .Bd -literal -offset indent
674 if (sc->sc_flags & SCFLG_CALLOUT_RUNNING) {
675         if (callout_stop(&sc->sc_callout)) {
676                 sc->sc_flags &= ~SCFLG_CALLOUT_RUNNING;
677                 /* successfully stopped */
678         } else {
679                 /*
680                  * callout has expired and callout
681                  * function is about to be executed
682                  */
683         }
684 }
685 .Ed
686 .It
687 The
688 .Fn callout_pending ,
689 .Fn callout_active
690 and
691 .Fn callout_deactivate
692 macros can be used together to work around the race conditions.
693 When a callout's timeout is set, the callout subsystem marks the
694 callout as both
695 .Em active
696 and
697 .Em pending .
698 When the timeout time arrives, the callout subsystem begins processing
699 the callout by first clearing the
700 .Em pending
701 flag.
702 It then invokes the callout function without changing the
703 .Em active
704 flag, and does not clear the
705 .Em active
706 flag even after the callout function returns.
707 The mechanism described here requires the callout function itself to
708 clear the
709 .Em active
710 flag using the
711 .Fn callout_deactivate
712 macro.
713 The
714 .Fn callout_stop
715 and
716 .Fn callout_drain
717 functions always clear both the
718 .Em active
719 and
720 .Em pending
721 flags before returning.
722 .Pp
723 The callout function should first check the
724 .Em pending
725 flag and return without action if
726 .Fn callout_pending
727 returns
728 .Dv TRUE .
729 This indicates that the callout was rescheduled using
730 .Fn callout_reset
731 just before the callout function was invoked.
732 If
733 .Fn callout_active
734 returns
735 .Dv FALSE
736 then the callout function should also return without action.
737 This indicates that the callout has been stopped.
738 Finally, the callout function should call
739 .Fn callout_deactivate
740 to clear the
741 .Em active
742 flag.
743 For example:
744 .Bd -literal -offset indent
745 mtx_lock(&sc->sc_mtx);
746 if (callout_pending(&sc->sc_callout)) {
747         /* callout was reset */
748         mtx_unlock(&sc->sc_mtx);
749         return;
750 }
751 if (!callout_active(&sc->sc_callout)) {
752         /* callout was stopped */
753         mtx_unlock(&sc->sc_mtx);
754         return;
755 }
756 callout_deactivate(&sc->sc_callout);
757 /* rest of callout function */
758 .Ed
759 .Pp
760 Together with appropriate synchronization, such as the mutex used above,
761 this approach permits the
762 .Fn callout_stop
763 and
764 .Fn callout_reset
765 functions to be used at any time without races.
766 For example:
767 .Bd -literal -offset indent
768 mtx_lock(&sc->sc_mtx);
769 callout_stop(&sc->sc_callout);
770 /* The callout is effectively stopped now. */
771 .Ed
772 .Pp
773 If the callout is still pending then these functions operate normally,
774 but if processing of the callout has already begun then the tests in
775 the callout function cause it to return without further action.
776 Synchronization between the callout function and other code ensures that
777 stopping or resetting the callout will never be attempted while the
778 callout function is past the
779 .Fn callout_deactivate
780 call.
781 .Pp
782 The above technique additionally ensures that the
783 .Em active
784 flag always reflects whether the callout is effectively enabled or
785 disabled.
786 If
787 .Fn callout_active
788 returns false, then the callout is effectively disabled, since even if
789 the callout subsystem is actually just about to invoke the callout
790 function, the callout function will return without action.
791 .El
792 .Pp
793 There is one final race condition that must be considered when a
794 callout is being stopped for the last time.
795 In this case it may not be safe to let the callout function itself
796 detect that the callout was stopped, since it may need to access
797 data objects that have already been destroyed or recycled.
798 To ensure that the callout is completely finished, a call to
799 .Fn callout_drain
800 should be used.
801 In particular,
802 a callout should always be drained prior to destroying its associated lock
803 or releasing the storage for the callout structure.
804 .Sh LEGACY API
805 .Bf Sy
806 The functions below are a legacy API that will be removed in a future release.
807 New code should not use these routines.
808 .Ef
809 .Pp
810 The function
811 .Fn timeout
812 schedules a call to the function given by the argument
813 .Fa func
814 to take place after
815 .Fa ticks Ns No /hz
816 seconds.
817 Non-positive values of
818 .Fa ticks
819 are silently converted to the value
820 .Sq 1 .
821 .Fa func
822 should be a pointer to a function that takes a
823 .Fa void *
824 argument.
825 Upon invocation,
826 .Fa func
827 will receive
828 .Fa arg
829 as its only argument.
830 The return value from
831 .Fn timeout
832 is a
833 .Ft struct callout_handle
834 which can be used in conjunction with the
835 .Fn untimeout
836 function to request that a scheduled timeout be canceled.
837 .Pp
838 The function
839 .Fn callout_handle_init
840 can be used to initialize a handle to a state which will cause
841 any calls to
842 .Fn untimeout
843 with that handle to return with no side
844 effects.
845 .Pp
846 Assigning a callout handle the value of
847 .Fn CALLOUT_HANDLE_INITIALIZER
848 performs the same function as
849 .Fn callout_handle_init
850 and is provided for use on statically declared or global callout handles.
851 .Pp
852 The function
853 .Fn untimeout
854 cancels the timeout associated with
855 .Fa handle
856 using the
857 .Fa func
858 and
859 .Fa arg
860 arguments to validate the handle.
861 If the handle does not correspond to a timeout with
862 the function
863 .Fa func
864 taking the argument
865 .Fa arg
866 no action is taken.
867 .Fa handle
868 must be initialized by a previous call to
869 .Fn timeout ,
870 .Fn callout_handle_init ,
871 or assigned the value of
872 .Fn CALLOUT_HANDLE_INITIALIZER "&handle"
873 before being passed to
874 .Fn untimeout .
875 The behavior of calling
876 .Fn untimeout
877 with an uninitialized handle
878 is undefined.
879 .Pp
880 As handles are recycled by the system, it is possible (although unlikely)
881 that a handle from one invocation of
882 .Fn timeout
883 may match the handle of another invocation of
884 .Fn timeout
885 if both calls used the same function pointer and argument, and the first
886 timeout is expired or canceled before the second call.
887 The timeout facility offers O(1) running time for
888 .Fn timeout
889 and
890 .Fn untimeout .
891 Timeouts are executed from
892 .Fn softclock
893 with the
894 .Va Giant
895 lock held.
896 Thus they are protected from re-entrancy.
897 .Sh RETURN VALUES
898 The
899 .Fn callout_active
900 macro returns the state of a callout's
901 .Em active
902 flag.
903 .Pp
904 The
905 .Fn callout_pending
906 macro returns the state of a callout's
907 .Em pending
908 flag.
909 .Pp
910 The
911 .Fn callout_reset
912 and
913 .Fn callout_schedule
914 function families return a value of one if the callout was pending before the new
915 function invocation was scheduled.
916 .Pp
917 The
918 .Fn callout_stop
919 and
920 .Fn callout_drain
921 functions return a value of one if the callout was still pending when it was
922 called, a zero if the callout could not be stopped and a negative one is it
923 was either not running or has already completed.
924 The
925 .Fn timeout
926 function returns a
927 .Ft struct callout_handle
928 that can be passed to
929 .Fn untimeout .
930 .Sh HISTORY
931 The current timeout and untimeout routines are based on the work of
932 .An Adam M. Costello
933 and
934 .An George Varghese ,
935 published in a technical report entitled
936 .%T "Redesigning the BSD Callout and Timer Facilities"
937 and modified slightly for inclusion in
938 .Fx
939 by
940 .An Justin T. Gibbs .
941 The original work on the data structures used in this implementation
942 was published by
943 .An G. Varghese
944 and
945 .An A. Lauck
946 in the paper
947 .%T "Hashed and Hierarchical Timing Wheels: Data Structures for the Efficient Implementation of a Timer Facility"
948 in the
949 .%B "Proceedings of the 11th ACM Annual Symposium on Operating Systems Principles" .
950 The current implementation replaces the long standing
951 .Bx
952 linked list
953 callout mechanism which offered O(n) insertion and removal running time
954 but did not generate or require handles for untimeout operations.