1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
.\" $OpenBSD: timeout.9,v 1.55 2022/06/22 14:10:49 visa Exp $
.\"
.\" Copyright (c) 2000 Artur Grabowski <art@openbsd.org>
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\"
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. The name of the author may not be used to endorse or promote products
.\" derived from this software without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd $Mdocdate: June 22 2022 $
.Dt TIMEOUT_SET 9
.Os
.Sh NAME
.Nm timeout_set ,
.Nm timeout_set_flags ,
.Nm timeout_set_proc ,
.Nm timeout_add ,
.Nm timeout_add_sec ,
.Nm timeout_add_msec ,
.Nm timeout_add_nsec ,
.Nm timeout_add_usec ,
.Nm timeout_add_tv ,
.Nm timeout_del ,
.Nm timeout_del_barrier ,
.Nm timeout_barrier ,
.Nm timeout_pending ,
.Nm timeout_initialized ,
.Nm timeout_triggered ,
.Nm TIMEOUT_INITIALIZER ,
.Nm TIMEOUT_INITIALIZER_FLAGS
.Nd execute a function after a specified period of time
.Sh SYNOPSIS
.In sys/types.h
.In sys/timeout.h
.Ft void
.Fn timeout_set "struct timeout *to" "void (*fn)(void *)" "void *arg"
.Ft void
.Fo timeout_set_flags
.Fa "struct timeout *to"
.Fa "void (*fn)(void *)"
.Fa "void *arg"
.Fa "int flags"
.Fc
.Ft void
.Fn timeout_set_proc "struct timeout *to" "void (*fn)(void *)" "void *arg"
.Ft int
.Fn timeout_add "struct timeout *to" "int ticks"
.Ft int
.Fn timeout_del "struct timeout *to"
.Ft int
.Fn timeout_del_barrier "struct timeout *to"
.Ft void
.Fn timeout_barrier "struct timeout *to"
.Ft int
.Fn timeout_pending "struct timeout *to"
.Ft int
.Fn timeout_initialized "struct timeout *to"
.Ft int
.Fn timeout_triggered "struct timeout *to"
.Ft int
.Fn timeout_add_tv "struct timeout *to" "struct timeval *"
.Ft int
.Fn timeout_add_sec "struct timeout *to" "int sec"
.Ft int
.Fn timeout_add_msec "struct timeout *to" "int msec"
.Ft int
.Fn timeout_add_usec "struct timeout *to" "int usec"
.Ft int
.Fn timeout_add_nsec "struct timeout *to" "int nsec"
.Fn TIMEOUT_INITIALIZER "void (*fn)(void *)" "void *arg"
.Fn TIMEOUT_INITIALIZER_FLAGS "void (*fn)(void *)" "void *arg" "int flags"
.Sh DESCRIPTION
The
.Nm timeout
API provides a mechanism to execute a function at a given time.
The granularity of the time is limited by the granularity of the
.Xr hardclock 9
timer which executes
.Xr hz 9
times a second.
.Pp
It is the responsibility of the caller to provide these functions with
pre-allocated timeout structures.
.Pp
The
.Fn timeout_set
function prepares the timeout structure
.Fa to
to be used in future calls to
.Fn timeout_add
and
.Fn timeout_del .
The timeout will be prepared to call the function specified by the
.Fa fn
argument with a
.Fa void *
argument given in the
.Fa arg
argument.
Once initialized, the
.Fa to
structure can be used repeatedly in
.Fn timeout_add
and
.Fn timeout_del
and does not need to be reinitialized unless
the function called and/or its argument must change.
.Pp
The
.Fn timeout_set_flags
function is similar to
.Fn timeout_set
but it additionally accepts the bitwise OR of zero or more of the
following
.Fa flags :
.Bl -tag -width TIMEOUT_PROC -offset indent
.It Dv TIMEOUT_PROC
Runs the timeout in a process context instead of the default
.Dv IPL_SOFTCLOCK
interrupt context.
.El
.Pp
The
.Fn timeout_set_proc
function is similar to
.Fn timeout_set
but it runs the timeout in a process context instead of the default
.Dv IPL_SOFTCLOCK
interrupt context.
.Pp
The function
.Fn timeout_add
schedules the execution of the
.Fa to
timeout in at least
.Fa ticks Ns /hz
seconds.
Negative values of
.Fa ticks
are illegal.
If the value is
.Sq 0
it will, in the current implementation, be treated as
.Sq 1 ,
but in the future it might cause an immediate timeout.
The timeout in the
.Fa to
argument must be already initialized by
.Fn timeout_set ,
.Fn timeout_set_flags ,
or
.Fn timeout_set_proc
and may not be used in calls to
.Fn timeout_set ,
.Fn timeout_set_flags ,
or
.Fn timeout_set_proc
until it has timed out or been removed with
.Fn timeout_del .
If the timeout in the
.Fa to
argument is already scheduled, the old execution time will be
replaced by the new one.
.Pp
The function
.Fn timeout_del
will cancel the timeout in the argument
.Fa to .
If the timeout has already executed or has never been added,
the call will have no effect.
.Pp
.Fn timeout_del_barrier
is like
.Fn timeout_del
but it will wait until any current execution of the timeout has completed.
.Pp
.Fn timeout_barrier
ensures that any current execution of the timeout in the argument
.Fa to
has completed before returning.
.Pp
The caller of
.Fn timeout_barrier
or
.Fn timeout_del_barrier
must not hold locks that can block processing in the timeout's context.
Otherwise, the system will deadlock.
.Pp
The
.Fn timeout_pending
macro can be used to check if a timeout is scheduled to run.
.Pp
The
.Fn timeout_initialized
macro can be used to check if a timeout has been initialized.
.Pp
The
.Fn timeout_triggered
macro can be used to check if a timeout is running or has been run.
The
.Fn timeout_add
and
.Fn timeout_del
functions clear the triggered state for that timeout.
.Pp
When possible, use the
.Fn timeout_add_tv ,
.Fn timeout_add_sec ,
.Fn timeout_add_msec ,
.Fn timeout_add_usec ,
and
.Fn timeout_add_nsec
functions instead of
.Fn timeout_add .
Those functions add a timeout whilst converting the time specified
by the respective types.
They also defer the timeout handler for at least one tick if called
with a positive value.
.Pp
A timeout declaration can be initialised with the
.Fn TIMEOUT_INITIALIZER
macro.
The timeout will be prepared to call the function specified by the
.Fa fn
argument with the
.Fa void *
argument given in
.Fa arg .
.Pp
The
.Fn TIMEOUT_INITIALIZER_FLAGS
macro is similar to
.Fn TIMEOUT_INITIALIZER ,
but it accepts additional flags.
See the
.Fn timeout_set_flags
function for details.
.Sh CONTEXT
.Fn timeout_set ,
.Fn timeout_set_flags ,
and
.Fn timeout_set_proc
can be called during autoconf, from process context, or from interrupt
context.
.Pp
.Fn timeout_add ,
.Fn timeout_add_sec ,
.Fn timeout_add_msec ,
.Fn timeout_add_nsec ,
.Fn timeout_add_usec ,
.Fn timeout_add_tv ,
.Fn timeout_del ,
.Fn timeout_pending ,
.Fn timeout_initialized ,
.Fn timeout_triggered
can be called during autoconf, from process context, or from any
interrupt context at or below
.Dv IPL_CLOCK .
.Pp
.Fn timeout_barrier
and
.Fn timeout_del_barrier
can be called from process context.
.Pp
When the timeout runs, the
.Fa fn
argument to
.Fn timeout_set
or
.Fn timeout_set_flags
will be called in an interrupt context at
.Dv IPL_SOFTCLOCK
or a process context if the
.Dv TIMEOUT_PROC
flag was given at initialization.
The
.Fa fn
argument to
.Fn timeout_set_proc
will be called in a process context.
.Sh RETURN VALUES
.Fn timeout_add ,
.Fn timeout_add_sec ,
.Fn timeout_add_msec ,
.Fn timeout_add_nsec ,
.Fn timeout_add_usec ,
and
.Fn timeout_add_tv
will return 1 if the timeout
.Fa to
was added to the timeout schedule or 0 if it was already queued.
.Pp
.Fn timeout_del
and
.Fn timeout_del_barrier
will return 1 if the timeout
.Fa to
was removed from the pending timeout schedule or 0 if it was not
currently queued.
.Sh CODE REFERENCES
These functions are implemented in the file
.Pa sys/kern/kern_timeout.c .
.Sh SEE ALSO
.Xr hz 9 ,
.Xr splclock 9 ,
.Xr tsleep 9 ,
.Xr tvtohz 9
.Rs
.%A George Varghese
.%A Anthony Lauck
.%B Hashed and hierarchical timing wheels: efficient data structures for \
implementing a timer facility
.%O especially Schemes 6 and 7
.%I IEEE/ACM
.%J Transactions on Networking
.%V vol. 5
.%N no. 6
.%P pp. 824\(en834
.%D December 1997
.Re
|