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