14b28d96eSJohn Baldwin.\" $NetBSD: timeout.9,v 1.2 1996/06/23 22:32:34 pk Exp $ 24b28d96eSJohn Baldwin.\" 34b28d96eSJohn Baldwin.\" Copyright (c) 1996 The NetBSD Foundation, Inc. 44b28d96eSJohn Baldwin.\" All rights reserved. 54b28d96eSJohn Baldwin.\" 64b28d96eSJohn Baldwin.\" This code is derived from software contributed to The NetBSD Foundation 74b28d96eSJohn Baldwin.\" by Paul Kranenburg. 84b28d96eSJohn Baldwin.\" 94b28d96eSJohn Baldwin.\" Redistribution and use in source and binary forms, with or without 104b28d96eSJohn Baldwin.\" modification, are permitted provided that the following conditions 114b28d96eSJohn Baldwin.\" are met: 124b28d96eSJohn Baldwin.\" 1. Redistributions of source code must retain the above copyright 134b28d96eSJohn Baldwin.\" notice, this list of conditions and the following disclaimer. 144b28d96eSJohn Baldwin.\" 2. Redistributions in binary form must reproduce the above copyright 154b28d96eSJohn Baldwin.\" notice, this list of conditions and the following disclaimer in the 164b28d96eSJohn Baldwin.\" documentation and/or other materials provided with the distribution. 174b28d96eSJohn Baldwin.\" 184b28d96eSJohn Baldwin.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 194b28d96eSJohn Baldwin.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 204b28d96eSJohn Baldwin.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 214b28d96eSJohn Baldwin.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 224b28d96eSJohn Baldwin.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 234b28d96eSJohn Baldwin.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 244b28d96eSJohn Baldwin.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 254b28d96eSJohn Baldwin.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 264b28d96eSJohn Baldwin.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 274b28d96eSJohn Baldwin.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 284b28d96eSJohn Baldwin.\" POSSIBILITY OF SUCH DAMAGE. 294b28d96eSJohn Baldwin.\" 30*5fc3104aSGleb Smirnoff.Dd January 22, 2024 314b28d96eSJohn Baldwin.Dt CALLOUT 9 324b28d96eSJohn Baldwin.Os 334b28d96eSJohn Baldwin.Sh NAME 344b28d96eSJohn Baldwin.Nm callout_active , 354b28d96eSJohn Baldwin.Nm callout_deactivate , 364b28d96eSJohn Baldwin.Nm callout_drain , 374b28d96eSJohn Baldwin.Nm callout_init , 384b28d96eSJohn Baldwin.Nm callout_init_mtx , 394b28d96eSJohn Baldwin.Nm callout_init_rm , 404b28d96eSJohn Baldwin.Nm callout_init_rw , 414b28d96eSJohn Baldwin.Nm callout_pending , 424b28d96eSJohn Baldwin.Nm callout_reset , 434b28d96eSJohn Baldwin.Nm callout_reset_curcpu , 444b28d96eSJohn Baldwin.Nm callout_reset_on , 454b28d96eSJohn Baldwin.Nm callout_reset_sbt , 464b28d96eSJohn Baldwin.Nm callout_reset_sbt_curcpu , 474b28d96eSJohn Baldwin.Nm callout_reset_sbt_on , 484b28d96eSJohn Baldwin.Nm callout_schedule , 494b28d96eSJohn Baldwin.Nm callout_schedule_curcpu , 504b28d96eSJohn Baldwin.Nm callout_schedule_on , 514b28d96eSJohn Baldwin.Nm callout_schedule_sbt , 524b28d96eSJohn Baldwin.Nm callout_schedule_sbt_curcpu , 534b28d96eSJohn Baldwin.Nm callout_schedule_sbt_on , 544b28d96eSJohn Baldwin.Nm callout_stop , 554b28d96eSJohn Baldwin.Nm callout_when 564b28d96eSJohn Baldwin.Nd execute a function after a specified length of time 574b28d96eSJohn Baldwin.Sh SYNOPSIS 584b28d96eSJohn Baldwin.In sys/types.h 594b28d96eSJohn Baldwin.In sys/callout.h 604b28d96eSJohn Baldwin.Bd -literal 614b28d96eSJohn Baldwintypedef void callout_func_t (void *); 624b28d96eSJohn Baldwin.Ed 634b28d96eSJohn Baldwin.Ft int 644b28d96eSJohn Baldwin.Fn callout_active "struct callout *c" 654b28d96eSJohn Baldwin.Ft void 664b28d96eSJohn Baldwin.Fn callout_deactivate "struct callout *c" 674b28d96eSJohn Baldwin.Ft int 684b28d96eSJohn Baldwin.Fn callout_drain "struct callout *c" 694b28d96eSJohn Baldwin.Ft void 704b28d96eSJohn Baldwin.Fn callout_init "struct callout *c" "int mpsafe" 714b28d96eSJohn Baldwin.Ft void 724b28d96eSJohn Baldwin.Fn callout_init_mtx "struct callout *c" "struct mtx *mtx" "int flags" 734b28d96eSJohn Baldwin.Ft void 744b28d96eSJohn Baldwin.Fn callout_init_rm "struct callout *c" "struct rmlock *rm" "int flags" 754b28d96eSJohn Baldwin.Ft void 764b28d96eSJohn Baldwin.Fn callout_init_rw "struct callout *c" "struct rwlock *rw" "int flags" 774b28d96eSJohn Baldwin.Ft int 784b28d96eSJohn Baldwin.Fn callout_pending "struct callout *c" 794b28d96eSJohn Baldwin.Ft int 804b28d96eSJohn Baldwin.Fo callout_reset 814b28d96eSJohn Baldwin.Fa "struct callout *c" 824b28d96eSJohn Baldwin.Fa "int ticks" 834b28d96eSJohn Baldwin.Fa "callout_func_t *func" 844b28d96eSJohn Baldwin.Fa "void *arg" 854b28d96eSJohn Baldwin.Fc 864b28d96eSJohn Baldwin.Ft int 874b28d96eSJohn Baldwin.Fo callout_reset_curcpu 884b28d96eSJohn Baldwin.Fa "struct callout *c" 894b28d96eSJohn Baldwin.Fa "int ticks" 904b28d96eSJohn Baldwin.Fa "callout_func_t *func" 914b28d96eSJohn Baldwin.Fa "void *arg" 924b28d96eSJohn Baldwin.Fc 934b28d96eSJohn Baldwin.Ft int 944b28d96eSJohn Baldwin.Fo callout_reset_on 954b28d96eSJohn Baldwin.Fa "struct callout *c" 964b28d96eSJohn Baldwin.Fa "int ticks" 974b28d96eSJohn Baldwin.Fa "callout_func_t *func" 984b28d96eSJohn Baldwin.Fa "void *arg" 994b28d96eSJohn Baldwin.Fa "int cpu" 1004b28d96eSJohn Baldwin.Fc 1014b28d96eSJohn Baldwin.Ft int 1024b28d96eSJohn Baldwin.Fo callout_reset_sbt 1034b28d96eSJohn Baldwin.Fa "struct callout *c" 1044b28d96eSJohn Baldwin.Fa "sbintime_t sbt" 1054b28d96eSJohn Baldwin.Fa "sbintime_t pr" 1064b28d96eSJohn Baldwin.Fa "callout_func_t *func" 1074b28d96eSJohn Baldwin.Fa "void *arg" 1084b28d96eSJohn Baldwin.Fa "int flags" 1094b28d96eSJohn Baldwin.Fc 1104b28d96eSJohn Baldwin.Ft int 1114b28d96eSJohn Baldwin.Fo callout_reset_sbt_curcpu 1124b28d96eSJohn Baldwin.Fa "struct callout *c" 1134b28d96eSJohn Baldwin.Fa "sbintime_t sbt" 1144b28d96eSJohn Baldwin.Fa "sbintime_t pr" 1154b28d96eSJohn Baldwin.Fa "callout_func_t *func" 1164b28d96eSJohn Baldwin.Fa "void *arg" 1174b28d96eSJohn Baldwin.Fa "int flags" 1184b28d96eSJohn Baldwin.Fc 1194b28d96eSJohn Baldwin.Ft int 1204b28d96eSJohn Baldwin.Fo callout_reset_sbt_on 1214b28d96eSJohn Baldwin.Fa "struct callout *c" 1224b28d96eSJohn Baldwin.Fa "sbintime_t sbt" 1234b28d96eSJohn Baldwin.Fa "sbintime_t pr" 1244b28d96eSJohn Baldwin.Fa "callout_func_t *func" 1254b28d96eSJohn Baldwin.Fa "void *arg" 1264b28d96eSJohn Baldwin.Fa "int cpu" 1274b28d96eSJohn Baldwin.Fa "int flags" 1284b28d96eSJohn Baldwin.Fc 1294b28d96eSJohn Baldwin.Ft int 1304b28d96eSJohn Baldwin.Fn callout_schedule "struct callout *c" "int ticks" 1314b28d96eSJohn Baldwin.Ft int 1324b28d96eSJohn Baldwin.Fn callout_schedule_curcpu "struct callout *c" "int ticks" 1334b28d96eSJohn Baldwin.Ft int 1344b28d96eSJohn Baldwin.Fn callout_schedule_on "struct callout *c" "int ticks" "int cpu" 1354b28d96eSJohn Baldwin.Ft int 1364b28d96eSJohn Baldwin.Fo callout_schedule_sbt 1374b28d96eSJohn Baldwin.Fa "struct callout *c" 1384b28d96eSJohn Baldwin.Fa "sbintime_t sbt" 1394b28d96eSJohn Baldwin.Fa "sbintime_t pr" 1404b28d96eSJohn Baldwin.Fa "int flags" 1414b28d96eSJohn Baldwin.Fc 1424b28d96eSJohn Baldwin.Ft int 1434b28d96eSJohn Baldwin.Fo callout_schedule_sbt_curcpu 1444b28d96eSJohn Baldwin.Fa "struct callout *c" 1454b28d96eSJohn Baldwin.Fa "sbintime_t sbt" 1464b28d96eSJohn Baldwin.Fa "sbintime_t pr" 1474b28d96eSJohn Baldwin.Fa "int flags" 1484b28d96eSJohn Baldwin.Fc 1494b28d96eSJohn Baldwin.Ft int 1504b28d96eSJohn Baldwin.Fo callout_schedule_sbt_on 1514b28d96eSJohn Baldwin.Fa "struct callout *c" 1524b28d96eSJohn Baldwin.Fa "sbintime_t sbt" 1534b28d96eSJohn Baldwin.Fa "sbintime_t pr" 1544b28d96eSJohn Baldwin.Fa "int cpu" 1554b28d96eSJohn Baldwin.Fa "int flags" 1564b28d96eSJohn Baldwin.Fc 1574b28d96eSJohn Baldwin.Ft int 1584b28d96eSJohn Baldwin.Fn callout_stop "struct callout *c" 1594b28d96eSJohn Baldwin.Ft sbintime_t 1604b28d96eSJohn Baldwin.Fo callout_when 1614b28d96eSJohn Baldwin.Fa "sbintime_t sbt" 1624b28d96eSJohn Baldwin.Fa "sbintime_t precision" 1634b28d96eSJohn Baldwin.Fa "int flags" 1644b28d96eSJohn Baldwin.Fa "sbintime_t *sbt_res" 1654b28d96eSJohn Baldwin.Fa "sbintime_t *precision_res" 1664b28d96eSJohn Baldwin.Fc 1674b28d96eSJohn Baldwin.Sh DESCRIPTION 1684b28d96eSJohn BaldwinThe 1694b28d96eSJohn Baldwin.Nm callout 1704b28d96eSJohn BaldwinAPI is used to schedule a call to an arbitrary function at a specific 1714b28d96eSJohn Baldwintime in the future. 1724b28d96eSJohn BaldwinConsumers of this API are required to allocate a callout structure 1734b28d96eSJohn Baldwin.Pq struct callout 1744b28d96eSJohn Baldwinfor each pending function invocation. 1754b28d96eSJohn BaldwinThis structure stores state about the pending function invocation including 1764b28d96eSJohn Baldwinthe function to be called and the time at which the function should be invoked. 1774b28d96eSJohn BaldwinPending function calls can be cancelled or rescheduled to a different time. 1784b28d96eSJohn BaldwinIn addition, 1794b28d96eSJohn Baldwina callout structure may be reused to schedule a new function call after a 1804b28d96eSJohn Baldwinscheduled call is completed. 1814b28d96eSJohn Baldwin.Pp 1824b28d96eSJohn BaldwinCallouts only provide a single-shot mode. 1834b28d96eSJohn BaldwinIf a consumer requires a periodic timer, 1844b28d96eSJohn Baldwinit must explicitly reschedule each function call. 1854b28d96eSJohn BaldwinThis is normally done by rescheduling the subsequent call within the called 1864b28d96eSJohn Baldwinfunction. 1874b28d96eSJohn Baldwin.Pp 1884b28d96eSJohn BaldwinCallout functions must not sleep. 1894b28d96eSJohn BaldwinThey may not acquire sleepable locks, 1904b28d96eSJohn Baldwinwait on condition variables, 1914b28d96eSJohn Baldwinperform blocking allocation requests, 1924b28d96eSJohn Baldwinor invoke any other action that might sleep. 1934b28d96eSJohn Baldwin.Pp 1944b28d96eSJohn BaldwinEach callout structure must be initialized by 1954b28d96eSJohn Baldwin.Fn callout_init , 1964b28d96eSJohn Baldwin.Fn callout_init_mtx , 1974b28d96eSJohn Baldwin.Fn callout_init_rm , 1984b28d96eSJohn Baldwinor 1994b28d96eSJohn Baldwin.Fn callout_init_rw 2004b28d96eSJohn Baldwinbefore it is passed to any of the other callout functions. 2014b28d96eSJohn BaldwinThe 2024b28d96eSJohn Baldwin.Fn callout_init 2034b28d96eSJohn Baldwinfunction initializes a callout structure in 2044b28d96eSJohn Baldwin.Fa c 2054b28d96eSJohn Baldwinthat is not associated with a specific lock. 2064b28d96eSJohn BaldwinIf the 2074b28d96eSJohn Baldwin.Fa mpsafe 2084b28d96eSJohn Baldwinargument is zero, 2094b28d96eSJohn Baldwinthe callout structure is not considered to be 2104b28d96eSJohn Baldwin.Dq multi-processor safe ; 2114b28d96eSJohn Baldwinand the Giant lock will be acquired before calling the callout function 2124b28d96eSJohn Baldwinand released when the callout function returns. 2134b28d96eSJohn Baldwin.Pp 2144b28d96eSJohn BaldwinThe 2154b28d96eSJohn Baldwin.Fn callout_init_mtx , 2164b28d96eSJohn Baldwin.Fn callout_init_rm , 2174b28d96eSJohn Baldwinand 2184b28d96eSJohn Baldwin.Fn callout_init_rw 2194b28d96eSJohn Baldwinfunctions initialize a callout structure in 2204b28d96eSJohn Baldwin.Fa c 2214b28d96eSJohn Baldwinthat is associated with a specific lock. 2224b28d96eSJohn BaldwinThe lock is specified by the 2234b28d96eSJohn Baldwin.Fa mtx , 2244b28d96eSJohn Baldwin.Fa rm , 2254b28d96eSJohn Baldwinor 2264b28d96eSJohn Baldwin.Fa rw 2274b28d96eSJohn Baldwinparameter. 2284b28d96eSJohn BaldwinThe associated lock must be held while stopping or rescheduling the 2294b28d96eSJohn Baldwincallout. 2304b28d96eSJohn BaldwinThe callout subsystem acquires the associated lock before calling the 2314b28d96eSJohn Baldwincallout function and releases it after the function returns. 2324b28d96eSJohn BaldwinIf the callout was cancelled while the callout subsystem waited for the 2334b28d96eSJohn Baldwinassociated lock, 2344b28d96eSJohn Baldwinthe callout function is not called, 2354b28d96eSJohn Baldwinand the associated lock is released. 2364b28d96eSJohn BaldwinThis ensures that stopping or rescheduling the callout will abort any 2374b28d96eSJohn Baldwinpreviously scheduled invocation. 2384b28d96eSJohn Baldwin.Pp 2394b28d96eSJohn BaldwinA sleepable read-mostly lock 2404b28d96eSJohn Baldwin.Po 2414b28d96eSJohn Baldwinone initialized with the 2424b28d96eSJohn Baldwin.Dv RM_SLEEPABLE 2434b28d96eSJohn Baldwinflag 2444b28d96eSJohn Baldwin.Pc 2454b28d96eSJohn Baldwinmay not be used with 2464b28d96eSJohn Baldwin.Fn callout_init_rm . 2474b28d96eSJohn BaldwinSimilarly, other sleepable lock types such as 2484b28d96eSJohn Baldwin.Xr sx 9 2494b28d96eSJohn Baldwinand 2504b28d96eSJohn Baldwin.Xr lockmgr 9 2514b28d96eSJohn Baldwincannot be used with callouts because sleeping is not permitted in 2524b28d96eSJohn Baldwinthe callout subsystem. 2534b28d96eSJohn Baldwin.Pp 2544b28d96eSJohn BaldwinThese 2554b28d96eSJohn Baldwin.Fa flags 2564b28d96eSJohn Baldwinmay be specified for 2574b28d96eSJohn Baldwin.Fn callout_init_mtx , 2584b28d96eSJohn Baldwin.Fn callout_init_rm , 2594b28d96eSJohn Baldwinor 2604b28d96eSJohn Baldwin.Fn callout_init_rw : 2614b28d96eSJohn Baldwin.Bl -tag -width ".Dv CALLOUT_RETURNUNLOCKED" 2624b28d96eSJohn Baldwin.It Dv CALLOUT_RETURNUNLOCKED 2634b28d96eSJohn BaldwinThe callout function will release the associated lock itself, 2644b28d96eSJohn Baldwinso the callout subsystem should not attempt to unlock it 2654b28d96eSJohn Baldwinafter the callout function returns. 2664b28d96eSJohn Baldwin.It Dv CALLOUT_SHAREDLOCK 2674b28d96eSJohn BaldwinThe lock is only acquired in read mode when running the callout handler. 2684b28d96eSJohn BaldwinThis flag is ignored by 2694b28d96eSJohn Baldwin.Fn callout_init_mtx . 2704b28d96eSJohn Baldwin.El 2714b28d96eSJohn Baldwin.Pp 2724b28d96eSJohn BaldwinThe function 2734b28d96eSJohn Baldwin.Fn callout_stop 2744b28d96eSJohn Baldwincancels a callout 2754b28d96eSJohn Baldwin.Fa c 2764b28d96eSJohn Baldwinif it is currently pending. 2774b28d96eSJohn BaldwinIf the callout is pending and successfully stopped, then 2784b28d96eSJohn Baldwin.Fn callout_stop 2794b28d96eSJohn Baldwinreturns a value of one. 2804b28d96eSJohn BaldwinIf the callout is not set, or 2814b28d96eSJohn Baldwinhas already been serviced, then 2824b28d96eSJohn Baldwinnegative one is returned. 2834b28d96eSJohn BaldwinIf the callout is currently being serviced and cannot be stopped, 2844b28d96eSJohn Baldwinthen zero will be returned. 2854b28d96eSJohn BaldwinIf the callout is currently being serviced and cannot be stopped, and at the 2864b28d96eSJohn Baldwinsame time a next invocation of the same callout is also scheduled, then 2874b28d96eSJohn Baldwin.Fn callout_stop 2884b28d96eSJohn Baldwinunschedules the next run and returns zero. 2894b28d96eSJohn BaldwinIf the callout has an associated lock, 2904b28d96eSJohn Baldwinthen that lock must be held when this function is called. 2914b28d96eSJohn Baldwin.Pp 2924b28d96eSJohn BaldwinThe function 2934b28d96eSJohn Baldwin.Fn callout_drain 2944b28d96eSJohn Baldwinis identical to 2954b28d96eSJohn Baldwin.Fn callout_stop 2964b28d96eSJohn Baldwinexcept that it will wait for the callout 2974b28d96eSJohn Baldwin.Fa c 2984b28d96eSJohn Baldwinto complete if it is already in progress. 2994b28d96eSJohn BaldwinThis function MUST NOT be called while holding any 3004b28d96eSJohn Baldwinlocks on which the callout might block, or deadlock will result. 3014b28d96eSJohn BaldwinNote that if the callout subsystem has already begun processing this 3024b28d96eSJohn Baldwincallout, then the callout function may be invoked before 3034b28d96eSJohn Baldwin.Fn callout_drain 3044b28d96eSJohn Baldwinreturns. 3054b28d96eSJohn BaldwinHowever, the callout subsystem does guarantee that the callout will be 3064b28d96eSJohn Baldwinfully stopped before 3074b28d96eSJohn Baldwin.Fn callout_drain 3084b28d96eSJohn Baldwinreturns. 3094b28d96eSJohn Baldwin.Pp 3104b28d96eSJohn BaldwinThe 3114b28d96eSJohn Baldwin.Fn callout_reset 3124b28d96eSJohn Baldwinand 3134b28d96eSJohn Baldwin.Fn callout_schedule 3144b28d96eSJohn Baldwinfunction families schedule a future function invocation for callout 3154b28d96eSJohn Baldwin.Fa c . 3164b28d96eSJohn BaldwinIf 3174b28d96eSJohn Baldwin.Fa c 3184b28d96eSJohn Baldwinalready has a pending callout, 3194b28d96eSJohn Baldwinit is cancelled before the new invocation is scheduled. 3204b28d96eSJohn BaldwinThese functions return a value of one if a pending callout was cancelled 3214b28d96eSJohn Baldwinand zero if there was no pending callout. 3224b28d96eSJohn BaldwinIf the callout has an associated lock, 3234b28d96eSJohn Baldwinthen that lock must be held when any of these functions are called. 3244b28d96eSJohn Baldwin.Pp 3254b28d96eSJohn BaldwinThe time at which the callout function will be invoked is determined by 3264b28d96eSJohn Baldwineither the 3274b28d96eSJohn Baldwin.Fa ticks 3284b28d96eSJohn Baldwinargument or the 3294b28d96eSJohn Baldwin.Fa sbt , 3304b28d96eSJohn Baldwin.Fa pr , 3314b28d96eSJohn Baldwinand 3324b28d96eSJohn Baldwin.Fa flags 3334b28d96eSJohn Baldwinarguments. 3344b28d96eSJohn BaldwinWhen 3354b28d96eSJohn Baldwin.Fa ticks 3364b28d96eSJohn Baldwinis used, 3374b28d96eSJohn Baldwinthe callout is scheduled to execute after 3384b28d96eSJohn Baldwin.Fa ticks Ns No /hz 3394b28d96eSJohn Baldwinseconds. 3404b28d96eSJohn BaldwinNon-positive values of 3414b28d96eSJohn Baldwin.Fa ticks 3424b28d96eSJohn Baldwinare silently converted to the value 3434b28d96eSJohn Baldwin.Sq 1 . 3444b28d96eSJohn Baldwin.Pp 3454b28d96eSJohn BaldwinThe 3464b28d96eSJohn Baldwin.Fa sbt , 3474b28d96eSJohn Baldwin.Fa pr , 3484b28d96eSJohn Baldwinand 3494b28d96eSJohn Baldwin.Fa flags 3504b28d96eSJohn Baldwinarguments provide more control over the scheduled time including 3514b28d96eSJohn Baldwinsupport for higher resolution times, 3524b28d96eSJohn Baldwinspecifying the precision of the scheduled time, 3534b28d96eSJohn Baldwinand setting an absolute deadline instead of a relative timeout. 3544b28d96eSJohn BaldwinThe callout is scheduled to execute in a time window which begins at 3554b28d96eSJohn Baldwinthe time specified in 3564b28d96eSJohn Baldwin.Fa sbt 3574b28d96eSJohn Baldwinand extends for the amount of time specified in 3584b28d96eSJohn Baldwin.Fa pr . 3594b28d96eSJohn BaldwinIf 3604b28d96eSJohn Baldwin.Fa sbt 3614b28d96eSJohn Baldwinspecifies a time in the past, 3624b28d96eSJohn Baldwinthe window is adjusted to start at the current time. 3634b28d96eSJohn BaldwinA non-zero value for 3644b28d96eSJohn Baldwin.Fa pr 3654b28d96eSJohn Baldwinallows the callout subsystem to coalesce callouts scheduled close to each 3664b28d96eSJohn Baldwinother into fewer timer interrupts, 3674b28d96eSJohn Baldwinreducing processing overhead and power consumption. 3684b28d96eSJohn BaldwinThese 3694b28d96eSJohn Baldwin.Fa flags 3704b28d96eSJohn Baldwinmay be specified to adjust the interpretation of 3714b28d96eSJohn Baldwin.Fa sbt 3724b28d96eSJohn Baldwinand 3734b28d96eSJohn Baldwin.Fa pr : 3744b28d96eSJohn Baldwin.Bl -tag -width ".Dv C_DIRECT_EXEC" 3754b28d96eSJohn Baldwin.It Dv C_ABSOLUTE 3764b28d96eSJohn BaldwinHandle the 3774b28d96eSJohn Baldwin.Fa sbt 3784b28d96eSJohn Baldwinargument as an absolute time since boot. 3794b28d96eSJohn BaldwinBy default, 3804b28d96eSJohn Baldwin.Fa sbt 3814b28d96eSJohn Baldwinis treated as a relative amount of time, 3824b28d96eSJohn Baldwinsimilar to 3834b28d96eSJohn Baldwin.Fa ticks . 3844b28d96eSJohn Baldwin.It Dv C_DIRECT_EXEC 3854b28d96eSJohn BaldwinRun the handler directly from hardware interrupt context instead of from the 3864b28d96eSJohn Baldwinsoftclock thread. 3874b28d96eSJohn BaldwinThis reduces latency and overhead, but puts more constraints on the callout 3884b28d96eSJohn Baldwinfunction. 3894b28d96eSJohn BaldwinCallout functions run in this context may use only spin mutexes for locking 3904b28d96eSJohn Baldwinand should be as small as possible because they run with absolute priority. 3914b28d96eSJohn Baldwin.It Fn C_PREL 3924b28d96eSJohn BaldwinSpecifies relative event time precision as binary logarithm of time interval 3934b28d96eSJohn Baldwindivided by acceptable time deviation: 1 -- 1/2, 2 -- 1/4, etc. 3944b28d96eSJohn BaldwinNote that the larger of 3954b28d96eSJohn Baldwin.Fa pr 3964b28d96eSJohn Baldwinor this value is used as the length of the time window. 3974b28d96eSJohn BaldwinSmaller values 3984b28d96eSJohn Baldwin.Pq which result in larger time intervals 3994b28d96eSJohn Baldwinallow the callout subsystem to aggregate more events in one timer interrupt. 4004b28d96eSJohn Baldwin.It Dv C_PRECALC 4014b28d96eSJohn BaldwinThe 4024b28d96eSJohn Baldwin.Fa sbt 4034b28d96eSJohn Baldwinargument specifies the absolute time at which the callout should be run, 4044b28d96eSJohn Baldwinand the 4054b28d96eSJohn Baldwin.Fa pr 4064b28d96eSJohn Baldwinargument specifies the requested precision, which will not be 4074b28d96eSJohn Baldwinadjusted during the scheduling process. 4084b28d96eSJohn BaldwinThe 4094b28d96eSJohn Baldwin.Fa sbt 4104b28d96eSJohn Baldwinand 4114b28d96eSJohn Baldwin.Fa pr 4124b28d96eSJohn Baldwinvalues should be calculated by an earlier call to 4134b28d96eSJohn Baldwin.Fn callout_when 4144b28d96eSJohn Baldwinwhich uses the user-supplied 4154b28d96eSJohn Baldwin.Fa sbt , 4164b28d96eSJohn Baldwin.Fa pr , 4174b28d96eSJohn Baldwinand 4184b28d96eSJohn Baldwin.Fa flags 4194b28d96eSJohn Baldwinvalues. 4204b28d96eSJohn Baldwin.It Dv C_HARDCLOCK 4214b28d96eSJohn BaldwinAlign the timeouts to 4224b28d96eSJohn Baldwin.Fn hardclock 4234b28d96eSJohn Baldwincalls if possible. 4244b28d96eSJohn Baldwin.El 4254b28d96eSJohn Baldwin.Pp 4264b28d96eSJohn BaldwinThe 4274b28d96eSJohn Baldwin.Fn callout_reset 4284b28d96eSJohn Baldwinfunctions accept a 4294b28d96eSJohn Baldwin.Fa func 4304b28d96eSJohn Baldwinargument which identifies the function to be called when the time expires. 4314b28d96eSJohn BaldwinIt must be a pointer to a function that takes a single 4324b28d96eSJohn Baldwin.Fa void * 4334b28d96eSJohn Baldwinargument. 4344b28d96eSJohn BaldwinUpon invocation, 4354b28d96eSJohn Baldwin.Fa func 4364b28d96eSJohn Baldwinwill receive 4374b28d96eSJohn Baldwin.Fa arg 4384b28d96eSJohn Baldwinas its only argument. 4394b28d96eSJohn BaldwinThe 4404b28d96eSJohn Baldwin.Fn callout_schedule 4414b28d96eSJohn Baldwinfunctions reuse the 4424b28d96eSJohn Baldwin.Fa func 4434b28d96eSJohn Baldwinand 4444b28d96eSJohn Baldwin.Fa arg 4454b28d96eSJohn Baldwinarguments from the previous callout. 4464b28d96eSJohn BaldwinNote that one of the 4474b28d96eSJohn Baldwin.Fn callout_reset 4484b28d96eSJohn Baldwinfunctions must always be called to initialize 4494b28d96eSJohn Baldwin.Fa func 4504b28d96eSJohn Baldwinand 4514b28d96eSJohn Baldwin.Fa arg 4524b28d96eSJohn Baldwinbefore one of the 4534b28d96eSJohn Baldwin.Fn callout_schedule 4544b28d96eSJohn Baldwinfunctions can be used. 4554b28d96eSJohn Baldwin.Pp 4564b28d96eSJohn BaldwinThe callout subsystem provides a softclock thread for each CPU in the system. 4574b28d96eSJohn BaldwinCallouts are assigned to a single CPU and are executed by the softclock thread 4584b28d96eSJohn Baldwinfor that CPU. 4594b28d96eSJohn BaldwinInitially, 4604b28d96eSJohn Baldwincallouts are assigned to CPU 0. 4614b28d96eSJohn BaldwinThe 4624b28d96eSJohn Baldwin.Fn callout_reset_on , 4634b28d96eSJohn Baldwin.Fn callout_reset_sbt_on , 4644b28d96eSJohn Baldwin.Fn callout_schedule_on 4654b28d96eSJohn Baldwinand 4664b28d96eSJohn Baldwin.Fn callout_schedule_sbt_on 4674b28d96eSJohn Baldwinfunctions assign the callout to CPU 4684b28d96eSJohn Baldwin.Fa cpu . 4694b28d96eSJohn BaldwinThe 4704b28d96eSJohn Baldwin.Fn callout_reset_curcpu , 4714b28d96eSJohn Baldwin.Fn callout_reset_sbt_curpu , 4724b28d96eSJohn Baldwin.Fn callout_schedule_curcpu 4734b28d96eSJohn Baldwinand 4744b28d96eSJohn Baldwin.Fn callout_schedule_sbt_curcpu 4754b28d96eSJohn Baldwinfunctions assign the callout to the current CPU. 4764b28d96eSJohn BaldwinThe 4774b28d96eSJohn Baldwin.Fn callout_reset , 4784b28d96eSJohn Baldwin.Fn callout_reset_sbt , 4794b28d96eSJohn Baldwin.Fn callout_schedule 4804b28d96eSJohn Baldwinand 4814b28d96eSJohn Baldwin.Fn callout_schedule_sbt 4824b28d96eSJohn Baldwinfunctions schedule the callout to execute in the softclock thread of the CPU 4834b28d96eSJohn Baldwinto which it is currently assigned. 4844b28d96eSJohn Baldwin.Pp 4854b28d96eSJohn BaldwinSoftclock threads are not pinned to their respective CPUs by default. 4864b28d96eSJohn BaldwinThe softclock thread for CPU 0 can be pinned to CPU 0 by setting the 4874b28d96eSJohn Baldwin.Va kern.pin_default_swi 4884b28d96eSJohn Baldwinloader tunable to a non-zero value. 4894b28d96eSJohn BaldwinSoftclock threads for CPUs other than zero can be pinned to their 4904b28d96eSJohn Baldwinrespective CPUs by setting the 4914b28d96eSJohn Baldwin.Va kern.pin_pcpu_swi 4924b28d96eSJohn Baldwinloader tunable to a non-zero value. 4934b28d96eSJohn Baldwin.Pp 4944b28d96eSJohn BaldwinThe macros 4954b28d96eSJohn Baldwin.Fn callout_pending , 4964b28d96eSJohn Baldwin.Fn callout_active 4974b28d96eSJohn Baldwinand 4984b28d96eSJohn Baldwin.Fn callout_deactivate 4994b28d96eSJohn Baldwinprovide access to the current state of the callout. 5004b28d96eSJohn BaldwinThe 5014b28d96eSJohn Baldwin.Fn callout_pending 5024b28d96eSJohn Baldwinmacro checks whether a callout is 5034b28d96eSJohn Baldwin.Em pending ; 5044b28d96eSJohn Baldwina callout is considered 5054b28d96eSJohn Baldwin.Em pending 5064b28d96eSJohn Baldwinwhen a timeout has been set but the time has not yet arrived. 5074b28d96eSJohn BaldwinNote that once the timeout time arrives and the callout subsystem 5084b28d96eSJohn Baldwinstarts to process this callout, 5094b28d96eSJohn Baldwin.Fn callout_pending 5104b28d96eSJohn Baldwinwill return 5114b28d96eSJohn Baldwin.Dv FALSE 5124b28d96eSJohn Baldwineven though the callout function may not have finished 5134b28d96eSJohn Baldwin.Pq or even begun 5144b28d96eSJohn Baldwinexecuting. 5154b28d96eSJohn BaldwinThe 5164b28d96eSJohn Baldwin.Fn callout_active 5174b28d96eSJohn Baldwinmacro checks whether a callout is marked as 5184b28d96eSJohn Baldwin.Em active , 5194b28d96eSJohn Baldwinand the 5204b28d96eSJohn Baldwin.Fn callout_deactivate 5214b28d96eSJohn Baldwinmacro clears the callout's 5224b28d96eSJohn Baldwin.Em active 5234b28d96eSJohn Baldwinflag. 5244b28d96eSJohn BaldwinThe callout subsystem marks a callout as 5254b28d96eSJohn Baldwin.Em active 5264b28d96eSJohn Baldwinwhen a timeout is set and it clears the 5274b28d96eSJohn Baldwin.Em active 5284b28d96eSJohn Baldwinflag in 5294b28d96eSJohn Baldwin.Fn callout_stop 5304b28d96eSJohn Baldwinand 5314b28d96eSJohn Baldwin.Fn callout_drain , 5324b28d96eSJohn Baldwinbut it 5334b28d96eSJohn Baldwin.Em does not 5344b28d96eSJohn Baldwinclear it when a callout expires normally via the execution of the 5354b28d96eSJohn Baldwincallout function. 5364b28d96eSJohn Baldwin.Pp 5374b28d96eSJohn BaldwinThe 5384b28d96eSJohn Baldwin.Fn callout_when 5394b28d96eSJohn Baldwinfunction may be used to pre-calculate the absolute time at which the 5404b28d96eSJohn Baldwintimeout should be run and the precision of the scheduled run time 5414b28d96eSJohn Baldwinaccording to the required time 5424b28d96eSJohn Baldwin.Fa sbt , 5434b28d96eSJohn Baldwinprecision 5444b28d96eSJohn Baldwin.Fa precision , 5454b28d96eSJohn Baldwinand additional adjustments requested by the 5464b28d96eSJohn Baldwin.Fa flags 5474b28d96eSJohn Baldwinargument. 5484b28d96eSJohn BaldwinFlags accepted by the 5494b28d96eSJohn Baldwin.Fn callout_when 5504b28d96eSJohn Baldwinfunction are the same as flags for the 5514b28d96eSJohn Baldwin.Fn callout_reset 5524b28d96eSJohn Baldwinfunction. 5534b28d96eSJohn BaldwinThe resulting time is assigned to the variable pointed to by the 5544b28d96eSJohn Baldwin.Fa sbt_res 5554b28d96eSJohn Baldwinargument, and the resulting precision is assigned to 5564b28d96eSJohn Baldwin.Fa *precision_res . 5574b28d96eSJohn BaldwinWhen passing the results to 5584b28d96eSJohn Baldwin.Fa callout_reset , 5594b28d96eSJohn Baldwinadd the 5604b28d96eSJohn Baldwin.Va C_PRECALC 5614b28d96eSJohn Baldwinflag to 5624b28d96eSJohn Baldwin.Fa flags , 5634b28d96eSJohn Baldwinto avoid incorrect re-adjustment. 5644b28d96eSJohn BaldwinThe function is intended for situations where precise time of the callout 5654b28d96eSJohn Baldwinrun should be known in advance, since 5664b28d96eSJohn Baldwintrying to read this time from the callout structure itself after a 5674b28d96eSJohn Baldwin.Fn callout_reset 5684b28d96eSJohn Baldwincall is racy. 5694b28d96eSJohn Baldwin.Ss "Avoiding Race Conditions" 5704b28d96eSJohn BaldwinThe callout subsystem invokes callout functions from its own thread 5714b28d96eSJohn Baldwincontext. 5724b28d96eSJohn BaldwinWithout some kind of synchronization, 5734b28d96eSJohn Baldwinit is possible that a callout 5744b28d96eSJohn Baldwinfunction will be invoked concurrently with an attempt to stop or reset 5754b28d96eSJohn Baldwinthe callout by another thread. 5764b28d96eSJohn BaldwinIn particular, since callout functions typically acquire a lock as 5774b28d96eSJohn Baldwintheir first action, the callout function may have already been invoked, 5784b28d96eSJohn Baldwinbut is blocked waiting for that lock at the time that another thread 5794b28d96eSJohn Baldwintries to reset or stop the callout. 5804b28d96eSJohn Baldwin.Pp 5814b28d96eSJohn BaldwinThere are three main techniques for addressing these 5824b28d96eSJohn Baldwinsynchronization concerns. 5834b28d96eSJohn BaldwinThe first approach is preferred as it is the simplest: 5844b28d96eSJohn Baldwin.Bl -enum -offset indent 5854b28d96eSJohn Baldwin.It 5864b28d96eSJohn BaldwinCallouts can be associated with a specific lock when they are initialized 5874b28d96eSJohn Baldwinby 5884b28d96eSJohn Baldwin.Fn callout_init_mtx , 5894b28d96eSJohn Baldwin.Fn callout_init_rm , 5904b28d96eSJohn Baldwinor 5914b28d96eSJohn Baldwin.Fn callout_init_rw . 5924b28d96eSJohn BaldwinWhen a callout is associated with a lock, 5934b28d96eSJohn Baldwinthe callout subsystem acquires the lock before the callout function is 5944b28d96eSJohn Baldwininvoked. 5954b28d96eSJohn BaldwinThis allows the callout subsystem to transparently handle races between 5964b28d96eSJohn Baldwincallout cancellation, 5974b28d96eSJohn Baldwinscheduling, 5984b28d96eSJohn Baldwinand execution. 5994b28d96eSJohn BaldwinNote that the associated lock must be acquired before calling 6004b28d96eSJohn Baldwin.Fn callout_stop 6014b28d96eSJohn Baldwinor one of the 6024b28d96eSJohn Baldwin.Fn callout_reset 6034b28d96eSJohn Baldwinor 6044b28d96eSJohn Baldwin.Fn callout_schedule 6054b28d96eSJohn Baldwinfunctions to provide this safety. 6064b28d96eSJohn Baldwin.Pp 6074b28d96eSJohn BaldwinA callout initialized via 6084b28d96eSJohn Baldwin.Fn callout_init 6094b28d96eSJohn Baldwinwith 6104b28d96eSJohn Baldwin.Fa mpsafe 6114b28d96eSJohn Baldwinset to zero is implicitly associated with the 6124b28d96eSJohn Baldwin.Va Giant 6134b28d96eSJohn Baldwinmutex. 6144b28d96eSJohn BaldwinIf 6154b28d96eSJohn Baldwin.Va Giant 6164b28d96eSJohn Baldwinis held when cancelling or rescheduling the callout, 6174b28d96eSJohn Baldwinthen its use will prevent races with the callout function. 6184b28d96eSJohn Baldwin.It 6194b28d96eSJohn BaldwinThe return value from 6204b28d96eSJohn Baldwin.Fn callout_stop 6214b28d96eSJohn Baldwin.Po 6224b28d96eSJohn Baldwinor the 6234b28d96eSJohn Baldwin.Fn callout_reset 6244b28d96eSJohn Baldwinand 6254b28d96eSJohn Baldwin.Fn callout_schedule 6264b28d96eSJohn Baldwinfunction families 6274b28d96eSJohn Baldwin.Pc 6284b28d96eSJohn Baldwinindicates whether or not the callout was removed. 6294b28d96eSJohn BaldwinIf it is known that the callout was set and the callout function has 6304b28d96eSJohn Baldwinnot yet executed, then a return value of 6314b28d96eSJohn Baldwin.Dv FALSE 6324b28d96eSJohn Baldwinindicates that the callout function is about to be called. 6334b28d96eSJohn BaldwinFor example: 6344b28d96eSJohn Baldwin.Bd -literal -offset indent 6354b28d96eSJohn Baldwinif (sc->sc_flags & SCFLG_CALLOUT_RUNNING) { 6364b28d96eSJohn Baldwin if (callout_stop(&sc->sc_callout)) { 6374b28d96eSJohn Baldwin sc->sc_flags &= ~SCFLG_CALLOUT_RUNNING; 6384b28d96eSJohn Baldwin /* successfully stopped */ 6394b28d96eSJohn Baldwin } else { 6404b28d96eSJohn Baldwin /* 6414b28d96eSJohn Baldwin * callout has expired and callout 6424b28d96eSJohn Baldwin * function is about to be executed 6434b28d96eSJohn Baldwin */ 6444b28d96eSJohn Baldwin } 6454b28d96eSJohn Baldwin} 6464b28d96eSJohn Baldwin.Ed 6474b28d96eSJohn Baldwin.It 6484b28d96eSJohn BaldwinThe 6494b28d96eSJohn Baldwin.Fn callout_pending , 6504b28d96eSJohn Baldwin.Fn callout_active 6514b28d96eSJohn Baldwinand 6524b28d96eSJohn Baldwin.Fn callout_deactivate 6534b28d96eSJohn Baldwinmacros can be used together to work around the race conditions. 6544b28d96eSJohn BaldwinWhen a callout's timeout is set, the callout subsystem marks the 6554b28d96eSJohn Baldwincallout as both 6564b28d96eSJohn Baldwin.Em active 6574b28d96eSJohn Baldwinand 6584b28d96eSJohn Baldwin.Em pending . 6594b28d96eSJohn BaldwinWhen the timeout time arrives, the callout subsystem begins processing 6604b28d96eSJohn Baldwinthe callout by first clearing the 6614b28d96eSJohn Baldwin.Em pending 6624b28d96eSJohn Baldwinflag. 6634b28d96eSJohn BaldwinIt then invokes the callout function without changing the 6644b28d96eSJohn Baldwin.Em active 6654b28d96eSJohn Baldwinflag, and does not clear the 6664b28d96eSJohn Baldwin.Em active 6674b28d96eSJohn Baldwinflag even after the callout function returns. 6684b28d96eSJohn BaldwinThe mechanism described here requires the callout function itself to 6694b28d96eSJohn Baldwinclear the 6704b28d96eSJohn Baldwin.Em active 6714b28d96eSJohn Baldwinflag using the 6724b28d96eSJohn Baldwin.Fn callout_deactivate 6734b28d96eSJohn Baldwinmacro. 6744b28d96eSJohn BaldwinThe 6754b28d96eSJohn Baldwin.Fn callout_stop 6764b28d96eSJohn Baldwinand 6774b28d96eSJohn Baldwin.Fn callout_drain 6784b28d96eSJohn Baldwinfunctions always clear both the 6794b28d96eSJohn Baldwin.Em active 6804b28d96eSJohn Baldwinand 6814b28d96eSJohn Baldwin.Em pending 6824b28d96eSJohn Baldwinflags before returning. 6834b28d96eSJohn Baldwin.Pp 6844b28d96eSJohn BaldwinThe callout function should first check the 6854b28d96eSJohn Baldwin.Em pending 6864b28d96eSJohn Baldwinflag and return without action if 6874b28d96eSJohn Baldwin.Fn callout_pending 6884b28d96eSJohn Baldwinreturns 6894b28d96eSJohn Baldwin.Dv TRUE . 6904b28d96eSJohn BaldwinThis indicates that the callout was rescheduled using 6914b28d96eSJohn Baldwin.Fn callout_reset 6924b28d96eSJohn Baldwinjust before the callout function was invoked. 6934b28d96eSJohn BaldwinIf 6944b28d96eSJohn Baldwin.Fn callout_active 6954b28d96eSJohn Baldwinreturns 6964b28d96eSJohn Baldwin.Dv FALSE 6974b28d96eSJohn Baldwinthen the callout function should also return without action. 6984b28d96eSJohn BaldwinThis indicates that the callout has been stopped. 6994b28d96eSJohn BaldwinFinally, the callout function should call 7004b28d96eSJohn Baldwin.Fn callout_deactivate 7014b28d96eSJohn Baldwinto clear the 7024b28d96eSJohn Baldwin.Em active 7034b28d96eSJohn Baldwinflag. 7044b28d96eSJohn BaldwinFor example: 7054b28d96eSJohn Baldwin.Bd -literal -offset indent 7064b28d96eSJohn Baldwinmtx_lock(&sc->sc_mtx); 7074b28d96eSJohn Baldwinif (callout_pending(&sc->sc_callout)) { 7084b28d96eSJohn Baldwin /* callout was reset */ 7094b28d96eSJohn Baldwin mtx_unlock(&sc->sc_mtx); 7104b28d96eSJohn Baldwin return; 7114b28d96eSJohn Baldwin} 7124b28d96eSJohn Baldwinif (!callout_active(&sc->sc_callout)) { 7134b28d96eSJohn Baldwin /* callout was stopped */ 7144b28d96eSJohn Baldwin mtx_unlock(&sc->sc_mtx); 7154b28d96eSJohn Baldwin return; 7164b28d96eSJohn Baldwin} 7174b28d96eSJohn Baldwincallout_deactivate(&sc->sc_callout); 7184b28d96eSJohn Baldwin/* rest of callout function */ 7194b28d96eSJohn Baldwin.Ed 7204b28d96eSJohn Baldwin.Pp 7214b28d96eSJohn BaldwinTogether with appropriate synchronization, such as the mutex used above, 7224b28d96eSJohn Baldwinthis approach permits the 7234b28d96eSJohn Baldwin.Fn callout_stop 7244b28d96eSJohn Baldwinand 7254b28d96eSJohn Baldwin.Fn callout_reset 7264b28d96eSJohn Baldwinfunctions to be used at any time without races. 7274b28d96eSJohn BaldwinFor example: 7284b28d96eSJohn Baldwin.Bd -literal -offset indent 7294b28d96eSJohn Baldwinmtx_lock(&sc->sc_mtx); 7304b28d96eSJohn Baldwincallout_stop(&sc->sc_callout); 7314b28d96eSJohn Baldwin/* The callout is effectively stopped now. */ 7324b28d96eSJohn Baldwin.Ed 7334b28d96eSJohn Baldwin.Pp 7344b28d96eSJohn BaldwinIf the callout is still pending then these functions operate normally, 7354b28d96eSJohn Baldwinbut if processing of the callout has already begun then the tests in 7364b28d96eSJohn Baldwinthe callout function cause it to return without further action. 7374b28d96eSJohn BaldwinSynchronization between the callout function and other code ensures that 7384b28d96eSJohn Baldwinstopping or resetting the callout will never be attempted while the 7394b28d96eSJohn Baldwincallout function is past the 7404b28d96eSJohn Baldwin.Fn callout_deactivate 7414b28d96eSJohn Baldwincall. 7424b28d96eSJohn Baldwin.Pp 7434b28d96eSJohn BaldwinThe above technique additionally ensures that the 7444b28d96eSJohn Baldwin.Em active 7454b28d96eSJohn Baldwinflag always reflects whether the callout is effectively enabled or 7464b28d96eSJohn Baldwindisabled. 7474b28d96eSJohn BaldwinIf 7484b28d96eSJohn Baldwin.Fn callout_active 7494b28d96eSJohn Baldwinreturns false, then the callout is effectively disabled, since even if 7504b28d96eSJohn Baldwinthe callout subsystem is actually just about to invoke the callout 7514b28d96eSJohn Baldwinfunction, the callout function will return without action. 7524b28d96eSJohn Baldwin.El 7534b28d96eSJohn Baldwin.Pp 7544b28d96eSJohn BaldwinThere is one final race condition that must be considered when a 7554b28d96eSJohn Baldwincallout is being stopped for the last time. 7564b28d96eSJohn BaldwinIn this case it may not be safe to let the callout function itself 7574b28d96eSJohn Baldwindetect that the callout was stopped, since it may need to access 7584b28d96eSJohn Baldwindata objects that have already been destroyed or recycled. 7594b28d96eSJohn BaldwinTo ensure that the callout is completely finished, a call to 7604b28d96eSJohn Baldwin.Fn callout_drain 7614b28d96eSJohn Baldwinshould be used. 7624b28d96eSJohn BaldwinIn particular, 7634b28d96eSJohn Baldwina callout should always be drained prior to destroying its associated lock 7644b28d96eSJohn Baldwinor releasing the storage for the callout structure. 7654b28d96eSJohn Baldwin.Sh RETURN VALUES 7664b28d96eSJohn BaldwinThe 7674b28d96eSJohn Baldwin.Fn callout_active 7684b28d96eSJohn Baldwinmacro returns the state of a callout's 7694b28d96eSJohn Baldwin.Em active 7704b28d96eSJohn Baldwinflag. 7714b28d96eSJohn Baldwin.Pp 7724b28d96eSJohn BaldwinThe 7734b28d96eSJohn Baldwin.Fn callout_pending 7744b28d96eSJohn Baldwinmacro returns the state of a callout's 7754b28d96eSJohn Baldwin.Em pending 7764b28d96eSJohn Baldwinflag. 7774b28d96eSJohn Baldwin.Pp 7784b28d96eSJohn BaldwinThe 7794b28d96eSJohn Baldwin.Fn callout_reset 7804b28d96eSJohn Baldwinand 7814b28d96eSJohn Baldwin.Fn callout_schedule 7824b28d96eSJohn Baldwinfunction families return a value of one if the callout was pending before the new 7834b28d96eSJohn Baldwinfunction invocation was scheduled. 7844b28d96eSJohn Baldwin.Pp 7854b28d96eSJohn BaldwinThe 7864b28d96eSJohn Baldwin.Fn callout_stop 7874b28d96eSJohn Baldwinand 7884b28d96eSJohn Baldwin.Fn callout_drain 7894b28d96eSJohn Baldwinfunctions return a value of one if the callout was still pending when it was 7904b28d96eSJohn Baldwincalled, a zero if the callout could not be stopped and a negative one is it 7914b28d96eSJohn Baldwinwas either not running or has already completed. 7924b28d96eSJohn Baldwin.Sh HISTORY 7934b28d96eSJohn Baldwin.Fx 7944b28d96eSJohn Baldwininitially used the long standing 7954b28d96eSJohn Baldwin.Bx 7964b28d96eSJohn Baldwinlinked list 7974b28d96eSJohn Baldwincallout mechanism which offered O(n) insertion and removal running time 7984b28d96eSJohn Baldwinbut did not generate or require handles for untimeout operations. 7994b28d96eSJohn Baldwin.Pp 8004b28d96eSJohn Baldwin.Fx 3.0 8014b28d96eSJohn Baldwinintroduced a new set of timeout and untimeout routines from 8024b28d96eSJohn Baldwin.Nx 8034b28d96eSJohn Baldwinbased on the work of 8044b28d96eSJohn Baldwin.An Adam M. Costello 8054b28d96eSJohn Baldwinand 8064b28d96eSJohn Baldwin.An George Varghese , 8074b28d96eSJohn Baldwinpublished in a technical report entitled 8084b28d96eSJohn Baldwin.%T "Redesigning the BSD Callout and Timer Facilities" 8094b28d96eSJohn Baldwinand modified for inclusion in 8104b28d96eSJohn Baldwin.Fx 8114b28d96eSJohn Baldwinby 8124b28d96eSJohn Baldwin.An Justin T. Gibbs . 8134b28d96eSJohn BaldwinThe original work on the data structures used in that implementation 8144b28d96eSJohn Baldwinwas published by 8154b28d96eSJohn Baldwin.An G. Varghese 8164b28d96eSJohn Baldwinand 8174b28d96eSJohn Baldwin.An A. Lauck 8184b28d96eSJohn Baldwinin the paper 8194b28d96eSJohn Baldwin.%T "Hashed and Hierarchical Timing Wheels: Data Structures for the Efficient Implementation of a Timer Facility" 8204b28d96eSJohn Baldwinin the 8214b28d96eSJohn Baldwin.%B "Proceedings of the 11th ACM Annual Symposium on Operating Systems Principles" . 8224b28d96eSJohn Baldwin.Pp 8234b28d96eSJohn Baldwin.Fx 3.3 8244b28d96eSJohn Baldwinintroduced the first implementations of 8254b28d96eSJohn Baldwin.Fn callout_init , 8264b28d96eSJohn Baldwin.Fn callout_reset , 8274b28d96eSJohn Baldwinand 8284b28d96eSJohn Baldwin.Fn callout_stop 8294b28d96eSJohn Baldwinwhich permitted callers to allocate dedicated storage for callouts. 8304b28d96eSJohn BaldwinThis ensured that a callout would always fire unlike 8314b28d96eSJohn Baldwin.Fn timeout 8324b28d96eSJohn Baldwinwhich would silently fail if it was unable to allocate a callout. 8334b28d96eSJohn Baldwin.Pp 8344b28d96eSJohn Baldwin.Fx 5.0 8354b28d96eSJohn Baldwinpermitted callout handlers to be tagged as MPSAFE via 8364b28d96eSJohn Baldwin.Fn callout_init . 8374b28d96eSJohn Baldwin.Pp 8384b28d96eSJohn Baldwin.Fx 5.3 8394b28d96eSJohn Baldwinintroduced 8404b28d96eSJohn Baldwin.Fn callout_drain . 8414b28d96eSJohn Baldwin.Pp 8424b28d96eSJohn Baldwin.Fx 6.0 8434b28d96eSJohn Baldwinintroduced 8444b28d96eSJohn Baldwin.Fn callout_init_mtx . 8454b28d96eSJohn Baldwin.Pp 8464b28d96eSJohn Baldwin.Fx 8.0 8474b28d96eSJohn Baldwinintroduced per-CPU callout wheels, 8484b28d96eSJohn Baldwin.Fn callout_init_rw , 8494b28d96eSJohn Baldwinand 8504b28d96eSJohn Baldwin.Fn callout_schedule . 8514b28d96eSJohn Baldwin.Pp 8524b28d96eSJohn Baldwin.Fx 9.0 8534b28d96eSJohn Baldwinchanged the underlying timer interrupts used to drive callouts to prefer 8544b28d96eSJohn Baldwinone-shot event timers instead of a periodic timer interrupt. 8554b28d96eSJohn Baldwin.Pp 8564b28d96eSJohn Baldwin.Fx 10.0 8574b28d96eSJohn Baldwinswitched the callout wheel to support tickless operation. 8584b28d96eSJohn BaldwinThese changes introduced 8594b28d96eSJohn Baldwin.Vt sbintime_t 8604b28d96eSJohn Baldwinand the 8614b28d96eSJohn Baldwin.Fn callout_reset_sbt* 8624b28d96eSJohn Baldwinfamily of functions. 8634b28d96eSJohn Baldwin.Fx 10.0 also added 8644b28d96eSJohn Baldwin.Dv C_DIRECT_EXEC 8654b28d96eSJohn Baldwinand 8664b28d96eSJohn Baldwin.Fn callout_init_rm . 8674b28d96eSJohn Baldwin.Pp 8684b28d96eSJohn Baldwin.Fx 10.2 8694b28d96eSJohn Baldwinintroduced the 8704b28d96eSJohn Baldwin.Fn callout_schedule_sbt* 8714b28d96eSJohn Baldwinfamily of functions. 8724b28d96eSJohn Baldwin.Pp 8734b28d96eSJohn Baldwin.Fx 11.0 8744b28d96eSJohn Baldwinintroduced 8754b28d96eSJohn Baldwin.Fn callout_async_drain . 8764b28d96eSJohn Baldwin.Fx 11.1 8774b28d96eSJohn Baldwinintroduced 8784b28d96eSJohn Baldwin.Fn callout_when . 8794b28d96eSJohn Baldwin.Fx 13.0 8804b28d96eSJohn Baldwinremoved 8814b28d96eSJohn Baldwin.Vt timeout_t , 8824b28d96eSJohn Baldwin.Fn timeout , 8834b28d96eSJohn Baldwinand 8844b28d96eSJohn Baldwin.Fn untimeout . 885