1 .\" $NetBSD: timeout.9,v 1.2 1996/06/23 22:32:34 pk Exp $
3 .\" Copyright (c) 1996 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
7 .\" by Paul Kranenburg.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
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 .\" 3. All advertising materials mentioning features or use of this software
18 .\" must display the following acknowledgement:
19 .\" This product includes software developed by the NetBSD
20 .\" Foundation, Inc. and its contributors.
21 .\" 4. Neither the name of The NetBSD Foundation nor the names of its
22 .\" contributors may be used to endorse or promote products derived
23 .\" from this software without specific prior written permission.
25 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
29 .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 .\" POSSIBILITY OF SUCH DAMAGE.
45 .Nm callout_handle_init ,
47 .Nm callout_init_mtx ,
52 .Nm callout_schedule ,
55 .Nm callout_deactivate
56 .Nd execute a function after a specified length of time
62 typedef void timeout_t (void *);
64 .Ft struct callout_handle
65 .Fn timeout "timeout_t *func" "void *arg" "int ticks"
67 .Fn callout_handle_init "struct callout_handle *handle"
70 struct callout_handle handle = CALLOUT_HANDLE_INITIALIZER(&handle)
73 .Fn untimeout "timeout_t *func" "void *arg" "struct callout_handle handle"
75 .Fn callout_init "struct callout *c" "int mpsafe"
77 .Fn callout_init_mtx "struct callout *c" "struct mtx *mtx" "int flags"
79 .Fn callout_init_rw "struct callout *c" "struct rwlock *rw" "int flags"
81 .Fn callout_stop "struct callout *c"
83 .Fn callout_drain "struct callout *c"
85 .Fn callout_reset "struct callout *c" "int ticks" "timeout_t *func" "void *arg"
87 .Fn callout_schedule "struct callout *c" "int ticks"
89 .Fn callout_pending "struct callout *c"
91 .Fn callout_active "struct callout *c"
92 .Fn callout_deactivate "struct callout *c"
96 schedules a call to the function given by the argument
101 Non-positive values of
103 are silently converted to the value
106 should be a pointer to a function that takes a
113 as its only argument.
114 The return value from
117 .Ft struct callout_handle
118 which can be used in conjunction with the
120 function to request that a scheduled timeout be canceled.
123 call is the old style and new code should use the
128 .Fn callout_handle_init
129 can be used to initialize a handle to a state which will cause
132 with that handle to return with no side
135 Assigning a callout handle the value of
136 .Fn CALLOUT_HANDLE_INITIALIZER
137 performs the same function as
138 .Fn callout_handle_init
139 and is provided for use on statically declared or global callout handles.
143 cancels the timeout associated with
149 arguments to validate the handle.
150 If the handle does not correspond to a timeout with
157 must be initialized by a previous call to
159 .Fn callout_handle_init ,
160 or assigned the value of
161 .Fn CALLOUT_HANDLE_INITIALIZER "&handle"
162 before being passed to
164 The behavior of calling
166 with an uninitialized handle
170 call is the old style and new code should use the
174 As handles are recycled by the system, it is possible (although unlikely)
175 that a handle from one invocation of
177 may match the handle of another invocation of
179 if both calls used the same function pointer and argument, and the first
180 timeout is expired or canceled before the second call.
181 The timeout facility offers O(1) running time for
185 Timeouts are executed from
190 Thus they are protected from re-entrancy.
194 .Fn callout_init_mtx ,
195 .Fn callout_init_rw ,
201 are low-level routines for clients who wish to allocate their own
206 initializes a callout so it can be passed to
212 without any side effects.
216 the callout structure is not considered to be
217 .Dq multi-processor safe ;
219 the Giant lock will be acquired before calling the callout function,
220 and released when the callout function returns.
224 function may be used as an alternative to
228 specifies a mutex that is to be acquired by the callout subsystem
229 before calling the callout function, and released when the callout
234 .Bl -tag -width ".Dv CALLOUT_RETURNUNLOCKED"
235 .It Dv CALLOUT_RETURNUNLOCKED
236 The callout function will release
238 itself, so the callout subsystem should not attempt to unlock it
239 after the callout function returns.
244 function serves the need of using rwlocks in conujunction with callouts.
245 The function does basically the same as
247 with the possibility of specifying an extra
250 The usable lock classes are currently limited to mutexes and rwlocks,
251 because callout handlers run in softclock swi, so they cannot sleep nor
252 acquire sleepable locks like sx or lockmgr.
256 .Bl -tag -width ".Dv CALLOUT_SHAREDLOCK"
257 .It Dv CALLOUT_SHAREDLOCK
258 The lock is only acquired in read mode when running the callout handler.
259 It has no effects when used in conjuction with
265 cancels a callout if it is currently pending.
266 If the callout is pending, then
268 will return a non-zero value.
269 If the callout is not set, has already been serviced or is currently
270 being serviced, then zero will be returned.
271 If the callout has an associated mutex, then that mutex must be
272 held when this function is called.
278 except that it will wait for the callout to be completed if it is
280 This function MUST NOT be called while holding any
281 locks on which the callout might block, or deadlock will result.
282 Note that if the callout subsystem has already begun processing this
283 callout, then the callout function may be invoked during the execution of
285 However, the callout subsystem does guarantee that the callout will be
292 first performs the equivalent of
294 to disestablish the callout, and then establishes a new callout in the
297 If there was already a pending callout and it was rescheduled, then
299 will return a non-zero value.
300 If the callout has an associated mutex, then that mutex must be
301 held when this function is called.
304 (re)schedules an existing callout for a new period of time;
305 it is equivalent to calling
311 parameters extracted from the callout structure (though possibly with
315 .Fn callout_pending ,
318 .Fn callout_deactivate
319 provide access to the current state of the callout.
320 Careful use of these macros can avoid many of the race conditions
321 that are inherent in asynchronous timer facilities; see
322 .Sx "Avoiding Race Conditions"
323 below for further details.
326 macro checks whether a callout is
328 a callout is considered
330 when a timeout has been set but the time has not yet arrived.
331 Note that once the timeout time arrives and the callout subsystem
332 starts to process this callout,
336 even though the callout function may not have finished (or even begun)
340 macro checks whether a callout is marked as
343 .Fn callout_deactivate
344 macro clears the callout's
347 The callout subsystem marks a callout as
349 when a timeout is set and it clears the
357 clear it when a callout expires normally via the execution of the
359 .Ss "Avoiding Race Conditions"
360 The callout subsystem invokes callout functions from its own timer
362 Without some kind of synchronization it is possible that a callout
363 function will be invoked concurrently with an attempt to stop or reset
364 the callout by another thread.
365 In particular, since callout functions typically acquire a mutex as
366 their first action, the callout function may have already been invoked,
367 but be blocked waiting for that mutex at the time that another thread
368 tries to reset or stop the callout.
370 The callout subsystem provides a number of mechanisms to address these
371 synchronization concerns:
372 .Bl -enum -offset indent
374 If the callout has an associated mutex that was specified using the
376 function (or implicitly specified as the
384 then this mutex is used to avoid the race conditions.
385 The associated mutex must be acquired by the caller before calling
389 and it is guaranteed that the callout will be correctly stopped
390 or reset as expected.
391 Note that it is still necessary to use
393 before destroying the callout or its associated mutex.
395 The return value from
399 indicates whether or not the callout was removed.
400 If it is known that the callout was set and the callout function has
401 not yet executed, then a return value of
403 indicates that the callout function is about to be called.
405 .Bd -literal -offset indent
406 if (sc->sc_flags & SCFLG_CALLOUT_RUNNING) {
407 if (callout_stop(&sc->sc_callout)) {
408 sc->sc_flags &= ~SCFLG_CALLOUT_RUNNING;
409 /* successfully stopped */
412 * callout has expired and callout
413 * function is about to be executed
420 .Fn callout_pending ,
423 .Fn callout_deactivate
424 macros can be used together to work around the race conditions.
425 When a callout's timeout is set, the callout subsystem marks the
430 When the timeout time arrives, the callout subsystem begins processing
431 the callout by first clearing the
434 It then invokes the callout function without changing the
436 flag, and does not clear the
438 flag even after the callout function returns.
439 The mechanism described here requires the callout function itself to
443 .Fn callout_deactivate
449 functions always clear both the
453 flags before returning.
455 The callout function should first check the
457 flag and return without action if
461 This indicates that the callout was rescheduled using
463 just before the callout function was invoked.
468 then the callout function should also return without action.
469 This indicates that the callout has been stopped.
470 Finally, the callout function should call
471 .Fn callout_deactivate
476 .Bd -literal -offset indent
477 mtx_lock(&sc->sc_mtx);
478 if (callout_pending(&sc->sc_callout)) {
479 /* callout was reset */
480 mtx_unlock(&sc->sc_mtx);
483 if (!callout_active(&sc->sc_callout)) {
484 /* callout was stopped */
485 mtx_unlock(&sc->sc_mtx);
488 callout_deactivate(&sc->sc_callout);
489 /* rest of callout function */
492 Together with appropriate synchronization, such as the mutex used above,
493 this approach permits the
497 functions to be used at any time without races.
499 .Bd -literal -offset indent
500 mtx_lock(&sc->sc_mtx);
501 callout_stop(&sc->sc_callout);
502 /* The callout is effectively stopped now. */
505 If the callout is still pending then these functions operate normally,
506 but if processing of the callout has already begun then the tests in
507 the callout function cause it to return without further action.
508 Synchronization between the callout function and other code ensures that
509 stopping or resetting the callout will never be attempted while the
510 callout function is past the
511 .Fn callout_deactivate
514 The above technique additionally ensures that the
516 flag always reflects whether the callout is effectively enabled or
520 returns false, then the callout is effectively disabled, since even if
521 the callout subsystem is actually just about to invoke the callout
522 function, the callout function will return without action.
525 There is one final race condition that must be considered when a
526 callout is being stopped for the last time.
527 In this case it may not be safe to let the callout function itself
528 detect that the callout was stopped, since it may need to access
529 data objects that have already been destroyed or recycled.
530 To ensure that the callout is completely finished, a call to
537 .Ft struct callout_handle
538 that can be passed to
544 functions return non-zero if the callout was still pending when it was
545 called or zero otherwise.
547 The current timeout and untimeout routines are based on the work of
550 .An George Varghese ,
551 published in a technical report entitled
552 .%T "Redesigning the BSD Callout and Timer Facilities"
553 and modified slightly for inclusion in
556 .An Justin T. Gibbs .
557 The original work on the data structures used in this implementation
563 .%T "Hashed and Hierarchical Timing Wheels: Data Structures for the Efficient Implementation of a Timer Facility"
565 .%B "Proceedings of the 11th ACM Annual Symposium on Operating Systems Principles" .
566 The current implementation replaces the long standing
569 callout mechanism which offered O(n) insertion and removal running time
570 but did not generate or require handles for untimeout operations.