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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
.\" $OpenBSD: timeout.9,v 1.60 2025/05/23 23:56:15 dlg Exp $
.\"
.\" Copyright (c) 2000 Artur Grabowski <art@openbsd.org>
.\" Copyright (c) 2021, 2022 Scott Cheloha <cheloha@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: May 23 2025 $
.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_usec ,
.Nm timeout_add_nsec ,
.Nm timeout_abs_ts ,
.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 in the future
.Sh SYNOPSIS
.In sys/types.h
.In sys/timeout.h
.Ft void
.Fo timeout_set
.Fa "struct timeout *to"
.Fa "void (*fn)(void *)"
.Fa "void *arg"
.Fc
.Ft void
.Fo timeout_set_flags
.Fa "struct timeout *to"
.Fa "void (*fn)(void *)"
.Fa "void *arg"
.Fa "int kclock"
.Fa "int flags"
.Fc
.Ft void
.Fo timeout_set_proc
.Fa "struct timeout *to"
.Fa "void (*fn)(void *)"
.Fa "void *arg"
.Fc
.Ft int
.Fo timeout_add
.Fa "struct timeout *to"
.Fa "int nticks"
.Fc
.Ft int
.Fo timeout_add_sec
.Fa "struct timeout *to"
.Fa "int secs"
.Fc
.Ft int
.Fo timeout_add_msec
.Fa "struct timeout *to"
.Fa "uint64_t msecs"
.Fc
.Ft int
.Fo timeout_add_usec
.Fa "struct timeout *to"
.Fa "uint64_t usecs"
.Fc
.Ft int
.Fo timeout_add_nsec
.Fa "struct timeout *to"
.Fa "uint64_t nsecs"
.Fc
.Ft int
.Fo timeout_abs_ts
.Fa "struct timeout *to"
.Fa "const struct timespec *abs"
.Fc
.Ft int
.Fo timeout_del
.Fa "struct timeout *to"
.Fc
.Ft int
.Fo timeout_del_barrier
.Fa "struct timeout *to"
.Fc
.Ft void
.Fo timeout_barrier
.Fa "struct timeout *to"
.Fc
.Ft int
.Fo timeout_pending
.Fa "struct timeout *to"
.Fc
.Ft int
.Fo timeout_initialized
.Fa "struct timeout *to"
.Fc
.Ft int
.Fo timeout_triggered
.Fa "struct timeout *to"
.Fc
.Fo TIMEOUT_INITIALIZER
.Fa "void (*fn)(void *)"
.Fa "void *arg"
.Fc
.Fo TIMEOUT_INITIALIZER_FLAGS
.Fa "void (*fn)(void *)"
.Fa "void *arg"
.Fa "int kclock"
.Fa "int flags"
.Fc
.Sh DESCRIPTION
The
.Nm timeout
subsystem schedules functions for asynchronous execution in the future.
.Pp
All state is encapsulated in a
.Vt struct timeout
allocated by the caller.
A timeout must be initialized before it may be used as input to other
functions in the API.
Once initialized,
a timeout does not need to be reinitialized unless its function or argument
must change.
.Pp
The
.Fn timeout_set
function initializes the timeout
.Fa to .
When the timeout is executed,
the function
.Fa fn
will be called with
.Fa arg
as its sole parameter.
The timeout is implicitly scheduled against the
.Dv KCLOCK_NONE
clock and is not configured with any additional flags.
.Pp
The
.Fn timeout_set_flags
function is similar to
.Fn timeout_set ,
except that it takes two additional parameters:
.Bl -tag -width kclock
.It Fa kclock
The timeout is scheduled against the given
.Fa kclock ,
which must be one of the following:
.Bl -tag -width KCLOCK_UPTIME
.It Dv KCLOCK_NONE
Low resolution tick-based clock.
The granularity of this clock is limited by the
.Xr hardclock 9 ,
which executes roughly
.Xr hz 9
times per second.
.It Dv KCLOCK_UPTIME
The uptime clock.
Counts the time elapsed since the system booted.
.El
.It Fa flags
The timeout's behavior may be configured with the bitwise OR of
zero or more of the following
.Fa flags :
.Bl -tag -width TIMEOUT_MPSAFE
.It Dv TIMEOUT_PROC
Execute the timeout in a process context instead of the default
.Dv IPL_SOFTCLOCK
interrupt context.
.It Dv TIMEOUT_MPSAFE
Execute the timeout without the kernel lock.
Requires the
.Dv TIMEOUT_PROC
flag.
.El
.El
.Pp
The
.Fn timeout_set_proc
function is similar to
.Fn timeout_set ,
except that the given timeout is configured with the
.Dv TIMEOUT_PROC
flag.
.Pp
A timeout may also be initialized statically.
The
.Fn TIMEOUT_INITIALIZER
macro is equivalent to the
.Fn timeout_set
function and the
.Fn TIMEOUT_INITIALIZER_FLAGS
macro is equivalent to the
.Fn timeout_set_flags
function.
.Pp
The interfaces available for scheduling a timeout vary with the
.Fa kclock
set during initialization.
.Pp
.Dv KCLOCK_NONE
timeouts may be scheduled with the function
.Fn timeout_add ,
which schedules the given timeout to execute after at least
.Fa nticks
.Xr hardclock 9
ticks have elapsed.
In practice,
.Fa nticks
ticks will usually elapse in slightly less than
.Pq Fa nticks Cm / Dv hz
seconds.
Negative values of
.Fa nticks
are illegal.
If
.Fa nticks
is zero it will be silently rounded up to one.
.Pp
For convenience,
.Dv KCLOCK_NONE
timeouts may also be scheduled with
.Fn timeout_add_sec ,
.Fn timeout_add_msec ,
.Fn timeout_add_usec ,
or
.Fn timeout_add_nsec .
These wrapper functions convert their input durations to a count of
.Xr hardclock 9
ticks before calling
.Fn timeout_add
to schedule the given timeout.
.Pp
Timeouts for any other
.Fa kclock
may be scheduled with
.Fn timeout_abs_ts ,
which schedules the given timeout to execute at or after the absolute time
.Fa abs
has elapsed on the timeout's
.Fa kclock .
.Pp
Once scheduled,
a timeout is said to be
.Qq pending .
A pending timeout may not be reinitialized with
.Fn timeout_set ,
.Fn timeout_set_flags ,
or
.Fn timeout_set_proc
until it has been executed or it has been cancelled with
.Fn timeout_del
or
.Fn timeout_del_barrier .
A pending timeout may be rescheduled without first cancelling it with
.Fn timeout_del
or
.Fn timeout_del_barrier :
the new expiration time will quietly supersede the original.
.Pp
The function
.Fn timeout_del
cancels any pending execution of the given timeout.
.Pp
The
.Fn timeout_del_barrier
function is similar to
.Fn timeout_del ,
except that it also blocks until any current execution of the given timeout
has completed.
.Pp
The
.Fn timeout_barrier
function blocks until any current execution of the given timeout
has completed.
.Pp
Callers of
.Fn timeout_barrier
and
.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 indicates whether the given timeout is scheduled for execution.
A timeout's pending status is cleared when it executes or is cancelled.
.Pp
The
.Fn timeout_initialized
macro indicates whether the given timeout has been initialized with
.Fn timeout_set
or
.Fn timeout_set_flags .
This macro must not be used unless the memory pointed to by
.Fa to
has been zeroed,
or its return value is meaningless.
.Pp
The
.Fn timeout_triggered
macro indicates whether the given timeout is executing or has finished
executing.
Rescheduling or cancelling a timeout clears its triggered status.
.Sh CONTEXT
.Fn timeout_set ,
.Fn timeout_set_flags ,
.Fn timeout_set_proc ,
.Fn timeout_add ,
.Fn timeout_add_sec ,
.Fn timeout_add_msec ,
.Fn timeout_add_usec ,
.Fn timeout_add_nsec ,
.Fn timeout_abs_ts ,
.Fn timeout_del ,
.Fn timeout_pending ,
.Fn timeout_initialized ,
and
.Fn timeout_triggered
may be called during autoconf,
from process context,
or from any interrupt context.
.Pp
.Fn timeout_barrier
and
.Fn timeout_del_barrier
may only be called from process context.
.Pp
When a timeout is executed,
the function
.Fa fn
set during initialization is called from the
.Dv IPL_SOFTCLOCK
interrupt context,
or a process context if the timeout was configured with the
.Dv TIMEOUT_PROC
flag.
The function
.Fa fn
must not block and must be safe to execute on any CPU in the system.
.Pp
Timeouts without the
.Dv TIMEOUT_MPSAFE
flag are executed under the kernel lock.
.Sh RETURN VALUES
.Fn timeout_add ,
.Fn timeout_add_sec ,
.Fn timeout_add_msec ,
.Fn timeout_add_usec ,
.Fn timeout_add_nsec ,
and
.Fn timeout_abs_ts
return 1 if the timeout
.Fa to
is newly scheduled,
or zero if the timeout was already pending.
.Pp
.Fn timeout_del
and
.Fn timeout_del_barrier
return 1 if the timeout
.Fa to
was pending,
or zero otherwise.
.Pp
.Fn timeout_pending ,
.Fn timeout_initialized ,
and
.Fn timeout_triggered
return non-zero if the corresponding condition is true,
or zero otherwise.
.Sh CODE REFERENCES
.Pa sys/kern/kern_timeout.c
.Sh SEE ALSO
.Xr hardclock 9 ,
.Xr hz 9 ,
.Xr microtime 9 ,
.Xr splclock 9 ,
.Xr task_add 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
|