xref: /netbsd-src/share/man/man9/callout.9 (revision dd255ccea4286b0c44fa8fd48a9a19a768afe8e1)
1.\"	$NetBSD: callout.9,v 1.26 2013/02/03 08:19:58 jdc Exp $
2.\"
3.\" Copyright (c) 2000, 2003, 2009 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 February 2, 2013
31.Dt CALLOUT 9
32.Os
33.Sh NAME
34.Nm callout_init ,
35.Nm callout_destroy ,
36.Nm callout_halt ,
37.Nm callout_reset ,
38.Nm callout_schedule ,
39.Nm callout_setfunc ,
40.Nm callout_stop ,
41.Nm callout_pending ,
42.Nm callout_expired ,
43.Nm callout_invoking ,
44.Nm callout_ack
45.Nd execute a function after a specified length of time
46.Sh SYNOPSIS
47.In sys/callout.h
48.Ft void
49.Fn "callout_init" "callout_t *c" "u_int flags"
50.Ft void
51.Fn "callout_destroy" "callout_t *c"
52.Ft void
53.Fn "callout_reset" "callout_t *c" "int ticks" \
54    "void (*func)(void *)" "void *arg"
55.Ft void
56.Fn "callout_schedule" "callout_t *c" "int ticks"
57.Ft void
58.Fn "callout_setfunc" "callout_t *c" "void (*func)(void *)" "void *arg"
59.Ft bool
60.Fn "callout_stop" "callout_t *c"
61.Ft bool
62.Fn "callout_halt" "callout_t *c" "kmutex_t *interlock"
63.Ft bool
64.Fn "callout_pending" "callout_t *c"
65.Ft bool
66.Fn "callout_expired" "callout_t *c"
67.Ft bool
68.Fn "callout_active" "callout_t *c"
69.Ft bool
70.Fn "callout_invoking" "callout_t *c"
71.Ft bool
72.Fn "callout_ack" "callout_t *c"
73.Sh DESCRIPTION
74The
75.Nm callout
76facility provides a mechanism to execute a function at a given time.
77The timer is based on the hardclock timer which ticks
78.Dv hz
79times per second.
80The function is called at softclock interrupt level.
81.Pp
82Clients of the
83.Nm callout
84facility are responsible for providing pre-allocated
85callout structures, or
86.Dq handles .
87The
88.Nm callout
89facility replaces the historic
90.Ux
91functions
92.Fn timeout
93and
94.Fn untimeout .
95.Sh FUNCTIONS
96The
97.Fn callout_init
98function initializes the callout handle
99.Fa c
100for use.
101No operations can be performed on the callout before it is initialized.
102If the
103.Fa flags
104argument is
105.Dv CALLOUT_MPSAFE ,
106the handler will be called without getting the global kernel lock.
107In this case it should only use functions that are multiprocessor
108safe.
109.Pp
110.Fn callout_destroy
111destroys the callout, preventing further use.
112It is provided as a diagnostic facility intended to catch bugs.
113To ensure future compatibility,
114.Fn callout_destroy
115should always be called when the callout is no longer required (for instance,
116when a device is being detached).
117The callout should be stopped before
118.Fn callout_destroy
119is called.
120.Pp
121The
122.Fn callout_reset
123function resets and starts the timer associated with the callout handle
124.Fa c .
125When the timer expires after
126.Fa ticks Ns No /hz
127seconds, the function specified by
128.Fa func
129will be called with the argument
130.Fa arg .
131If the timer associated with the callout handle is already running,
132the callout will simply be rescheduled to execute at the newly specified
133time.
134Once the timer is started, the callout handle is marked as
135.Em PENDING .
136Once the timer expires,
137the handle is marked as
138.Em EXPIRED
139and
140.Em INVOKING ,
141and the
142.Em PENDING
143status is cleared.
144.Pp
145The
146.Fn callout_setfunc
147function sets the function and argument of the callout handle
148.Fa c
149to
150.Fa func
151and
152.Fa arg
153respectively.
154The callout handle must already be initialized.
155If a callout will always be used with the same function and argument,
156then
157.Fn callout_setfunc
158used in conjunction with
159.Fn callout_schedule
160is slightly more efficient than using
161.Fn callout_reset .
162.Pp
163The
164.Fn callout_stop
165function requests that the timer associated with the callout handle
166.Fa c
167be stopped.
168The
169.Em PENDING
170and
171.Em EXPIRED
172status for the callout handle is cleared.
173It is safe to call
174.Fn callout_stop
175on a callout handle that is not pending, so long as it is initialized.
176.Fn callout_stop
177will return a non-zero value if the callout was
178.Em EXPIRED .
179Note that
180.Fn callout_stop
181can return while the callout is running on a different CPU or at a
182different interrupt priority level on the current CPU.
183It can only be said to prevent the callout from firing in the future,
184unless explicitly re-scheduled.
185To stop a callout and wait for completion, use
186.Fn callout_halt .
187.Pp
188.Fn callout_halt
189acts much like
190.Fn callout_stop ,
191but waits for the callout to complete if it is currently in-flight.
192.Fn callout_halt
193may not be called from a hard interrupt handler as it will sleep if the
194callout is currently executing.
195If the callout can take locks (such as mutexes or RW locks), the caller of
196.Fn callout_halt
197must not hold any of those locks, otherwise the two could deadlock.
198To facilitate this,
199.Fn callout_halt
200can optionally release a single mutex specified by the
201.Fa interlock
202parameter.
203If
204.Fa interlock
205is not
206.Dv NULL
207and the calling thread must wait for the callout to complete,
208.Fa interlock
209will be released before waiting and re-acquired before returning.
210If no wait is required,
211.Fa interlock
212will not be released.
213However, to avoid race conditions the caller should always assume that
214.Fa interlock
215has been released and reacquired, and act accordingly.
216.Pp
217The
218.Fn callout_pending
219function tests the
220.Em PENDING
221status of the callout handle
222.Fa c .
223A
224.Em PENDING
225callout is one that has been started and whose function has not yet
226been called.
227Note that it is possible for a callout's timer to have expired without
228its function being called if interrupt level has not dropped low enough
229to let softclock interrupts through.
230Note that it is only safe to test
231.Em PENDING
232status when at softclock interrupt level or higher.
233.Pp
234The
235.Fn callout_expired
236function tests to see if the callout's timer has expired and its
237function called.
238.Pp
239The
240.Fn callout_active
241function returns true if a timer has been started but not explicitly stopped,
242even if it has already fired.
243.Fn callout_active foo
244is logically the same as
245.Fn callout_pending foo
246||
247.Fn callout_expired foo ;
248it is implemented as a separate function for compatibility with
249.Fx
250and for the special case of
251.Fn TCP_TIMER_ISARMED .
252Its use is not recommended.
253.Pp
254The
255.Fn callout_invoking
256function tests the
257.Em INVOKING
258status of the callout handle
259.Fa c .
260This flag is set just before a callout's function is being called.
261Since the priority level is lowered prior to invocation of the
262callout function, other pending higher-priority code may run before
263the callout function is allowed to run.
264This may create a race condition if this higher-priority code
265deallocates storage containing one or more callout structures whose
266callout functions are about to be run.
267In such cases, one technique to prevent references to deallocated
268storage would be to test whether any callout functions are in the
269.Em INVOKING
270state using
271.Fn callout_invoking ,
272and if so, to mark the data structure and defer storage
273deallocation until the callout function is allowed to run.
274For this handshake protocol to work, the callout function will
275have to use the
276.Fn callout_ack
277function to clear this flag.
278.Pp
279The
280.Fn callout_ack
281function clears the
282.Em INVOKING
283state in the callout handle
284.Fa c .
285This is used in situations where it is necessary to protect against
286the race condition described under
287.Fn callout_invoking .
288.Sh CONCURRENCY
289The callout facility performs locking internally in order to guarantee the
290atomicity of individual operations performed on callouts.
291It does not provide life cycle management of user-provided callout data
292structures, nor does it ensure that groups of operations (multiple function
293calls) are performed atomically.
294These aspects of callout management are the responsibility of the user of
295the callout facility.
296.Pp
297Scheduled callouts may be active concurrently in a context different to the
298user of the callout facility: on another CPU, or at a different interrupt
299priority level or thread on the current CPU.
300The callout facility provides only one guarantee in this regard: any given
301callout will never have multiple concurrent invocations.
302.Sh SEE ALSO
303.Xr condvar 9 ,
304.Xr hz 9 ,
305.Xr softint 9 ,
306.Xr workqueue 9
307.Sh HISTORY
308The
309.Nm callout
310facility was implemented by Artur Grabowski and Thomas Nordin, based
311on the work of G. Varghese and A. Lauck, described in the paper
312Hashed and Hierarchical Timing Wheels: Data Structures for the
313Efficient Implementation of a Timer Facility
314in the Proceedings of the 11th ACM Annual Symposium on Operating System
315Principles, Austin, Texas, November 1987.
316It was adapted to the
317.Nx
318kernel by Jason R. Thorpe.
319