1.\" $NetBSD: callout.9,v 1.19 2008/04/30 13:10:58 martin Exp $ 2.\" 3.\" Copyright (c) 2000, 2003 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Jason R. Thorpe. 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 FOUNDATION OR CONTRIBUTORS 22.\" BE 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.Dd December 29, 2007 31.Dt CALLOUT 9 32.Os 33.Sh NAME 34.Nm callout_init , 35.Nm callout_destroy , 36.Nm callout_reset , 37.Nm callout_schedule , 38.Nm callout_setfunc , 39.Nm callout_stop , 40.Nm callout_expired , 41.Nm callout_invoking , 42.Nm callout_ack 43.Nd execute a function after a specified length of time 44.Sh SYNOPSIS 45.In sys/callout.h 46.Ft void 47.Fn "callout_init" "callout_t *c" "u_int flags" 48.Ft void 49.Fn "callout_destroy" "callout_t *c" 50.Ft void 51.Fn "callout_reset" "callout_t *c" "int ticks" \ 52 "void (*func)(void *)" "void *arg" 53.Ft void 54.Fn "callout_schedule" "callout_t *c" "int ticks" 55.Ft void 56.Fn "callout_setfunc" "callout_t *c" "void (*func)(void *)" "void *arg" 57.Ft bool 58.Fn "callout_stop" "callout_t *c" 59.Ft bool 60.Fn "callout_pending" "callout_t *c" 61.Ft bool 62.Fn "callout_expired" "callout_t *c" 63.Ft bool 64.Fn "callout_active" "callout_t *c" 65.Ft bool 66.Fn "callout_invoking" "callout_t *c" 67.Ft bool 68.Fn "callout_ack" "callout_t *c" 69.Sh DESCRIPTION 70The 71.Nm callout 72facility provides a mechanism to execute a function at a given time. 73The timer is based on the hardclock timer which ticks 74.Dv hz 75times per second. 76The function is called at softclock interrupt level. 77.Pp 78Clients of the 79.Nm callout 80facility are responsible for providing pre-allocated 81callout structures, or 82.Dq handles . 83The 84.Nm callout 85facility replaces the historic 86.Ux 87functions 88.Fn timeout 89and 90.Fn untimeout . 91.Pp 92The 93.Fn callout_init 94function initializes the callout handle 95.Fa c 96for use. 97No operations can be performed on the callout before it is initialized. 98If the 99.Fa flags 100argument is 101.Dv CALLOUT_MPSAFE , 102the handler will be called without getting the global kernel lock. 103In this case it should only use functions that are multiprocessor 104safe. 105.Pp 106.Fn callout_destroy 107destroys the callout, preventing further use. 108It is provided as a diagnostic facility intended to catch bugs. 109To ensure future compatibility, 110.Fn callout_destroy 111should always be called when the callout is no longer required (for instance, 112when a device is being detached). 113.Pp 114The 115.Fn callout_reset 116function resets and starts the timer associated with the callout handle 117.Fa c . 118When the timer expires after 119.Fa ticks Ns No /hz 120seconds, the function specified by 121.Fa func 122will be called with the argument 123.Fa arg . 124If the timer associated with the callout handle is already running, 125the callout will simply be rescheduled to execute at the newly specified 126time. 127Once the timer is started, the callout handle is marked as 128.Em PENDING . 129Once the timer expires, 130the handle is marked as 131.Em EXPIRED 132and 133.Em INVOKING , 134and the 135.Em PENDING 136status is cleared. 137.Pp 138The 139.Fn callout_setfunc 140function sets the function and argument of the callout handle 141.Fa c 142to 143.Fa func 144and 145.Fa arg 146respectively. 147The callout handle must already be initialized. 148If a callout will always be used with the same function and argument, 149then 150.Fn callout_setfunc 151used in conjunction with 152.Fn callout_schedule 153is slightly more efficient than using 154.Fn callout_reset . 155.Pp 156The 157.Fn callout_stop 158function stops the timer associated the callout handle 159.Fa c . 160The 161.Em PENDING 162and 163.Em EXPIRED 164status for the callout handle is cleared. 165It is safe to call 166.Fn callout_stop 167on a callout handle that is not pending, so long as it is initialized. 168. Fn callout_stop 169will return a non-zero value if the callout was 170.Em EXPIRED . 171.Pp 172The 173.Fn callout_pending 174function tests the 175.Em PENDING 176status of the callout handle 177.Fa c . 178A 179.Em PENDING 180callout is one that has been started and whose function has not yet 181been called. 182Note that it is possible for a callout's timer to have expired without 183its function being called if interrupt level has not dropped low enough 184to let softclock interrupts through. 185Note that it is only safe to test 186.Em PENDING 187status when at softclock interrupt level or higher. 188.Pp 189The 190.Fn callout_expired 191function tests to see if the callout's timer has expired and its 192function called. 193.Pp 194The 195.Fn callout_active 196function returns true if a timer has been started but not explicitly stopped, 197even if it has already fired. 198.Fn callout_active foo 199is logically the same as 200.Fn callout_pending foo 201|| 202.Fn callout_expired foo ; 203it is implemented as a separate function for compatibility with 204.Fx 205and for the special case of 206.Fn TCP_TIMER_ISARMED . 207Its use is not recommended. 208.Pp 209The 210.Fn callout_invoking 211function tests the 212.Em INVOKING 213status of the callout handle 214.Fa c . 215This flag is set just before a callout's function is being called. 216Since the priority level is lowered prior to invocation of the 217callout function, other pending higher-priority code may run before 218the callout function is allowed to run. 219This may create a race condition if this higher-priority code 220deallocates storage containing one or more callout structures whose 221callout functions are about to be run. 222In such cases, one technique to prevent references to deallocated 223storage would be to test whether any callout functions are in the 224.Em INVOKING 225state using 226.Fn callout_invoking , 227and if so, to mark the data structure and defer storage 228deallocation until the callout function is allowed to run. 229For this handshake protocol to work, the callout function will 230have to use the 231.Fn callout_ack 232function to clear this flag. 233.Pp 234The 235.Fn callout_ack 236function clears the 237.Em INVOKING 238state in the callout handle 239.Fa c . 240This is used in situations where it is necessary to protect against 241the race condition described under 242.Fn callout_invoking . 243.Sh SEE ALSO 244.Xr hz 9 245.Sh HISTORY 246The 247.Nm callout 248facility was implemented by Artur Grabowski and Thomas Nordin, based 249on the work of G. Varghese and A. Lauck, described in the paper 250Hashed and Hierarchical Timing Wheels: Data Structures for the 251Efficient Implementation of a Timer Facility 252in the Proceedings of the 11th ACM Annual Symposium on Operating System 253Principles, Austin, Texas, November 1987. 254It was adapted to the 255.Nx 256kernel by Jason R. Thorpe. 257