]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/timeout.9
Upgrade our Clang in base to r114020, from upstream's release_28 branch.
[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 August 2, 2008
33 .Dt TIMEOUT 9
34 .Os
35 .Sh NAME
36 .Nm timeout ,
37 .Nm untimeout ,
38 .Nm callout_handle_init ,
39 .Nm callout_init ,
40 .Nm callout_init_mtx ,
41 .Nm callout_init_rw ,
42 .Nm callout_stop ,
43 .Nm callout_drain ,
44 .Nm callout_reset ,
45 .Nm callout_schedule ,
46 .Nm callout_pending ,
47 .Nm callout_active ,
48 .Nm callout_deactivate
49 .Nd execute a function after a specified length of time
50 .Sh SYNOPSIS
51 .In sys/types.h
52 .In sys/systm.h
53 .Pp
54 .Bd -literal
55 typedef void timeout_t (void *);
56 .Ed
57 .Ft struct callout_handle
58 .Fn timeout "timeout_t *func" "void *arg" "int ticks"
59 .Ft void
60 .Fn callout_handle_init "struct callout_handle *handle"
61 .Pp
62 .Bd -literal
63 struct callout_handle handle = CALLOUT_HANDLE_INITIALIZER(&handle)
64 .Ed
65 .Ft void
66 .Fn untimeout "timeout_t *func" "void *arg" "struct callout_handle handle"
67 .Ft void
68 .Fn callout_init "struct callout *c" "int mpsafe"
69 .Ft void
70 .Fn callout_init_mtx "struct callout *c" "struct mtx *mtx" "int flags"
71 .Ft void
72 .Fn callout_init_rw "struct callout *c" "struct rwlock *rw" "int flags"
73 .Ft int
74 .Fn callout_stop "struct callout *c"
75 .Ft int
76 .Fn callout_drain "struct callout *c"
77 .Ft int
78 .Fn callout_reset "struct callout *c" "int ticks" "timeout_t *func" "void *arg"
79 .Ft int
80 .Fn callout_schedule "struct callout *c" "int ticks"
81 .Ft int
82 .Fn callout_pending "struct callout *c"
83 .Ft int
84 .Fn callout_active "struct callout *c"
85 .Fn callout_deactivate "struct callout *c"
86 .Sh DESCRIPTION
87 The function
88 .Fn timeout
89 schedules a call to the function given by the argument
90 .Fa func
91 to take place after
92 .Fa ticks Ns No /hz
93 seconds.
94 Non-positive values of
95 .Fa ticks
96 are silently converted to the value
97 .Sq 1 .
98 .Fa func
99 should be a pointer to a function that takes a
100 .Fa void *
101 argument.
102 Upon invocation,
103 .Fa func
104 will receive
105 .Fa arg
106 as its only argument.
107 The return value from
108 .Fn timeout
109 is a
110 .Ft struct callout_handle
111 which can be used in conjunction with the
112 .Fn untimeout
113 function to request that a scheduled timeout be canceled.
114 The
115 .Fn timeout
116 call is the old style and new code should use the
117 .Fn callout_*
118 functions.
119 .Pp
120 The function
121 .Fn callout_handle_init
122 can be used to initialize a handle to a state which will cause
123 any calls to
124 .Fn untimeout
125 with that handle to return with no side
126 effects.
127 .Pp
128 Assigning a callout handle the value of
129 .Fn CALLOUT_HANDLE_INITIALIZER
130 performs the same function as
131 .Fn callout_handle_init
132 and is provided for use on statically declared or global callout handles.
133 .Pp
134 The function
135 .Fn untimeout
136 cancels the timeout associated with
137 .Fa handle
138 using the
139 .Fa func
140 and
141 .Fa arg
142 arguments to validate the handle.
143 If the handle does not correspond to a timeout with
144 the function
145 .Fa func
146 taking the argument
147 .Fa arg
148 no action is taken.
149 .Fa handle
150 must be initialized by a previous call to
151 .Fn timeout ,
152 .Fn callout_handle_init ,
153 or assigned the value of
154 .Fn CALLOUT_HANDLE_INITIALIZER "&handle"
155 before being passed to
156 .Fn untimeout .
157 The behavior of calling
158 .Fn untimeout
159 with an uninitialized handle
160 is undefined.
161 The
162 .Fn untimeout
163 call is the old style and new code should use the
164 .Fn callout_*
165 functions.
166 .Pp
167 As handles are recycled by the system, it is possible (although unlikely)
168 that a handle from one invocation of
169 .Fn timeout
170 may match the handle of another invocation of
171 .Fn timeout
172 if both calls used the same function pointer and argument, and the first
173 timeout is expired or canceled before the second call.
174 The timeout facility offers O(1) running time for
175 .Fn timeout
176 and
177 .Fn untimeout .
178 Timeouts are executed from
179 .Fn softclock
180 with the
181 .Va Giant
182 lock held.
183 Thus they are protected from re-entrancy.
184 .Pp
185 The functions
186 .Fn callout_init ,
187 .Fn callout_init_mtx ,
188 .Fn callout_init_rw ,
189 .Fn callout_stop ,
190 .Fn callout_drain ,
191 .Fn callout_reset
192 and
193 .Fn callout_schedule
194 are low-level routines for clients who wish to allocate their own
195 callout structures.
196 .Pp
197 The function
198 .Fn callout_init
199 initializes a callout so it can be passed to
200 .Fn callout_stop ,
201 .Fn callout_drain ,
202 .Fn callout_reset 
203 or
204 .Fn callout_schedule 
205 without any side effects.
206 If the
207 .Fa mpsafe
208 argument is zero,
209 the callout structure is not considered to be
210 .Dq multi-processor safe ;
211 that is,
212 the Giant lock will be acquired before calling the callout function,
213 and released when the callout function returns.
214 .Pp
215 The
216 .Fn callout_init_mtx
217 function may be used as an alternative to
218 .Fn callout_init .
219 The parameter
220 .Fa mtx
221 specifies a mutex that is to be acquired by the callout subsystem
222 before calling the callout function, and released when the callout
223 function returns.
224 The following
225 .Fa flags
226 may be specified:
227 .Bl -tag -width ".Dv CALLOUT_RETURNUNLOCKED"
228 .It Dv CALLOUT_RETURNUNLOCKED
229 The callout function will release
230 .Fa mtx
231 itself, so the callout subsystem should not attempt to unlock it
232 after the callout function returns.
233 .El
234 .Pp
235 The
236 .Fn callout_init_rw
237 function serves the need of using rwlocks in conjunction with callouts.
238 The function does basically the same as
239 .Fn callout_init_mtx
240 with the possibility of specifying an extra
241 .Fa rw
242 argument.
243 The usable lock classes are currently limited to mutexes and rwlocks,
244 because callout handlers run in softclock swi, so they cannot sleep nor
245 acquire sleepable locks like sx or lockmgr.
246 The following
247 .Fa flags
248 may be specified:
249 .Bl -tag -width ".Dv CALLOUT_SHAREDLOCK"
250 .It Dv CALLOUT_SHAREDLOCK
251 The lock is only acquired in read mode when running the callout handler.
252 It has no effects when used in conjunction with
253 .Fa mtx .
254 .El
255 .Pp
256 The function
257 .Fn callout_stop
258 cancels a callout if it is currently pending.
259 If the callout is pending, then
260 .Fn callout_stop
261 will return a non-zero value.
262 If the callout is not set, has already been serviced or is currently
263 being serviced, then zero will be returned.
264 If the callout has an associated mutex, then that mutex must be
265 held when this function is called.
266 .Pp
267 The function
268 .Fn callout_drain
269 is identical to
270 .Fn callout_stop
271 except that it will wait for the callout to be completed if it is
272 already in progress.
273 This function MUST NOT be called while holding any
274 locks on which the callout might block, or deadlock will result.
275 Note that if the callout subsystem has already begun processing this
276 callout, then the callout function may be invoked during the execution of
277 .Fn callout_drain .
278 However, the callout subsystem does guarantee that the callout will be
279 fully stopped before
280 .Fn callout_drain
281 returns.
282 .Pp
283 The function
284 .Fn callout_reset
285 first performs the equivalent of
286 .Fn callout_stop
287 to disestablish the callout, and then establishes a new callout in the
288 same manner as
289 .Fn timeout .
290 If there was already a pending callout and it was rescheduled, then
291 .Fn callout_reset
292 will return a non-zero value.
293 If the callout has an associated mutex, then that mutex must be
294 held when this function is called.
295 The function
296 .Fn callout_schedule
297 (re)schedules an existing callout for a new period of time;
298 it is equivalent to calling
299 .Fn callout_reset
300 with the
301 .Fa func
302 and
303 .Fa arg
304 parameters extracted from the callout structure (though possibly with
305 lower overhead).
306 .Pp
307 The macros
308 .Fn callout_pending ,
309 .Fn callout_active
310 and
311 .Fn callout_deactivate
312 provide access to the current state of the callout.
313 Careful use of these macros can avoid many of the race conditions
314 that are inherent in asynchronous timer facilities; see
315 .Sx "Avoiding Race Conditions"
316 below for further details.
317 The
318 .Fn callout_pending
319 macro checks whether a callout is
320 .Em pending ;
321 a callout is considered
322 .Em pending
323 when a timeout has been set but the time has not yet arrived.
324 Note that once the timeout time arrives and the callout subsystem
325 starts to process this callout,
326 .Fn callout_pending
327 will return
328 .Dv FALSE
329 even though the callout function may not have finished (or even begun)
330 executing.
331 The
332 .Fn callout_active
333 macro checks whether a callout is marked as
334 .Em active ,
335 and the
336 .Fn callout_deactivate
337 macro clears the callout's
338 .Em active
339 flag.
340 The callout subsystem marks a callout as
341 .Em active
342 when a timeout is set and it clears the
343 .Em active
344 flag in
345 .Fn callout_stop
346 and
347 .Fn callout_drain ,
348 but it
349 .Em does not
350 clear it when a callout expires normally via the execution of the
351 callout function.
352 .Ss "Avoiding Race Conditions"
353 The callout subsystem invokes callout functions from its own timer
354 context.
355 Without some kind of synchronization it is possible that a callout
356 function will be invoked concurrently with an attempt to stop or reset
357 the callout by another thread.
358 In particular, since callout functions typically acquire a mutex as
359 their first action, the callout function may have already been invoked,
360 but be blocked waiting for that mutex at the time that another thread
361 tries to reset or stop the callout.
362 .Pp
363 The callout subsystem provides a number of mechanisms to address these
364 synchronization concerns:
365 .Bl -enum -offset indent
366 .It
367 If the callout has an associated mutex that was specified using the
368 .Fn callout_init_mtx
369 function (or implicitly specified as the
370 .Va Giant
371 mutex using
372 .Fn callout_init
373 with
374 .Fa mpsafe
375 set to
376 .Dv FALSE ) ,
377 then this mutex is used to avoid the race conditions.
378 The associated mutex must be acquired by the caller before calling
379 .Fn callout_stop
380 or
381 .Fn callout_reset
382 and it is guaranteed that the callout will be correctly stopped
383 or reset as expected.
384 Note that it is still necessary to use
385 .Fn callout_drain
386 before destroying the callout or its associated mutex.
387 .It
388 The return value from
389 .Fn callout_stop
390 and
391 .Fn callout_reset
392 indicates whether or not the callout was removed.
393 If it is known that the callout was set and the callout function has
394 not yet executed, then a return value of
395 .Dv FALSE
396 indicates that the callout function is about to be called.
397 For example:
398 .Bd -literal -offset indent
399 if (sc->sc_flags & SCFLG_CALLOUT_RUNNING) {
400         if (callout_stop(&sc->sc_callout)) {
401                 sc->sc_flags &= ~SCFLG_CALLOUT_RUNNING;
402                 /* successfully stopped */
403         } else {
404                 /*
405                  * callout has expired and callout
406                  * function is about to be executed
407                  */
408         }
409 }
410 .Ed
411 .It
412 The
413 .Fn callout_pending ,
414 .Fn callout_active
415 and
416 .Fn callout_deactivate
417 macros can be used together to work around the race conditions.
418 When a callout's timeout is set, the callout subsystem marks the
419 callout as both
420 .Em active
421 and
422 .Em pending .
423 When the timeout time arrives, the callout subsystem begins processing
424 the callout by first clearing the
425 .Em pending
426 flag.
427 It then invokes the callout function without changing the
428 .Em active
429 flag, and does not clear the
430 .Em active
431 flag even after the callout function returns.
432 The mechanism described here requires the callout function itself to
433 clear the
434 .Em active
435 flag using the
436 .Fn callout_deactivate
437 macro.
438 The
439 .Fn callout_stop
440 and
441 .Fn callout_drain
442 functions always clear both the
443 .Em active
444 and
445 .Em pending
446 flags before returning.
447 .Pp
448 The callout function should first check the
449 .Em pending
450 flag and return without action if
451 .Fn callout_pending
452 returns
453 .Dv TRUE .
454 This indicates that the callout was rescheduled using
455 .Fn callout_reset
456 just before the callout function was invoked.
457 If
458 .Fn callout_active
459 returns
460 .Dv FALSE
461 then the callout function should also return without action.
462 This indicates that the callout has been stopped.
463 Finally, the callout function should call
464 .Fn callout_deactivate
465 to clear the
466 .Em active
467 flag.
468 For example:
469 .Bd -literal -offset indent
470 mtx_lock(&sc->sc_mtx);
471 if (callout_pending(&sc->sc_callout)) {
472         /* callout was reset */
473         mtx_unlock(&sc->sc_mtx);
474         return;
475 }
476 if (!callout_active(&sc->sc_callout)) {
477         /* callout was stopped */
478         mtx_unlock(&sc->sc_mtx);
479         return;
480 }
481 callout_deactivate(&sc->sc_callout);
482 /* rest of callout function */
483 .Ed
484 .Pp
485 Together with appropriate synchronization, such as the mutex used above,
486 this approach permits the
487 .Fn callout_stop
488 and
489 .Fn callout_reset
490 functions to be used at any time without races.
491 For example:
492 .Bd -literal -offset indent
493 mtx_lock(&sc->sc_mtx);
494 callout_stop(&sc->sc_callout);
495 /* The callout is effectively stopped now. */
496 .Ed
497 .Pp
498 If the callout is still pending then these functions operate normally,
499 but if processing of the callout has already begun then the tests in
500 the callout function cause it to return without further action.
501 Synchronization between the callout function and other code ensures that
502 stopping or resetting the callout will never be attempted while the
503 callout function is past the
504 .Fn callout_deactivate
505 call.
506 .Pp
507 The above technique additionally ensures that the
508 .Em active
509 flag always reflects whether the callout is effectively enabled or
510 disabled.
511 If
512 .Fn callout_active
513 returns false, then the callout is effectively disabled, since even if
514 the callout subsystem is actually just about to invoke the callout
515 function, the callout function will return without action.
516 .El
517 .Pp
518 There is one final race condition that must be considered when a
519 callout is being stopped for the last time.
520 In this case it may not be safe to let the callout function itself
521 detect that the callout was stopped, since it may need to access
522 data objects that have already been destroyed or recycled.
523 To ensure that the callout is completely finished, a call to
524 .Fn callout_drain
525 should be used.
526 .Sh RETURN VALUES
527 The
528 .Fn timeout
529 function returns a
530 .Ft struct callout_handle
531 that can be passed to
532 .Fn untimeout .
533 The
534 .Fn callout_stop
535 and
536 .Fn callout_drain
537 functions return non-zero if the callout was still pending when it was
538 called or zero otherwise.
539 .Sh HISTORY
540 The current timeout and untimeout routines are based on the work of
541 .An Adam M. Costello
542 and
543 .An George Varghese ,
544 published in a technical report entitled
545 .%T "Redesigning the BSD Callout and Timer Facilities"
546 and modified slightly for inclusion in
547 .Fx
548 by
549 .An Justin T. Gibbs .
550 The original work on the data structures used in this implementation
551 was published by
552 .An G. Varghese
553 and
554 .An A. Lauck
555 in the paper
556 .%T "Hashed and Hierarchical Timing Wheels: Data Structures for the Efficient Implementation of a Timer Facility"
557 in the
558 .%B "Proceedings of the 11th ACM Annual Symposium on Operating Systems Principles" .
559 The current implementation replaces the long standing
560 .Bx
561 linked list
562 callout mechanism which offered O(n) insertion and removal running time
563 but did not generate or require handles for untimeout operations.