1/* $OpenBSD: timeout.h,v 1.51 2025/05/23 23:56:14 dlg Exp $ */
2/*
3 * Copyright (c) 2000-2001 Artur Grabowski <art@openbsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef _SYS_TIMEOUT_H_
28#define _SYS_TIMEOUT_H_
29
30#include <sys/time.h>
31
32struct circq {
33 struct circq *next; /* next element */
34 struct circq *prev; /* previous element */
35};
36
37struct timeout {
38 struct circq to_list; /* timeout queue, don't move */
39 struct timespec to_abstime; /* absolute time to run at */
40 void (*to_func)(void *); /* function to call */
41 void *to_arg; /* function argument */
42#if 1 /* NKCOV > 0 */
43 struct process *to_process; /* kcov identifier */
44#endif
45 int to_time; /* ticks on event */
46 int to_flags; /* misc flags */
47 int to_kclock; /* abstime's kernel clock */
48};
49
50/*
51 * flags in the to_flags field.
52 */
53#define TIMEOUT_PROC 0x01 /* needs a process context */
54#define TIMEOUT_ONQUEUE 0x02 /* on any timeout queue */
55#define TIMEOUT_INITIALIZED 0x04 /* initialized */
56#define TIMEOUT_TRIGGERED 0x08 /* running or ran */
57#define TIMEOUT_MPSAFE 0x10 /* run without kernel lock */
58
59struct timeoutstat {
60 uint64_t tos_added; /* timeout_add*(9) calls */
61 uint64_t tos_cancelled; /* dequeued during timeout_del*(9) */
62 uint64_t tos_deleted; /* timeout_del*(9) calls */
63 uint64_t tos_late; /* run after deadline */
64 uint64_t tos_pending; /* number currently ONQUEUE */
65 uint64_t tos_readded; /* timeout_add*(9) + already ONQUEUE */
66 uint64_t tos_rescheduled; /* bucketed + already SCHEDULED */
67 uint64_t tos_run_softclock; /* run from softclock() */
68 uint64_t tos_run_thread; /* run from softclock_thread() */
69 uint64_t tos_scheduled; /* bucketed during softclock() */
70 uint64_t tos_softclocks; /* softclock() calls */
71 uint64_t tos_thread_wakeups; /* wakeups in softclock_thread() */
72};
73
74#ifdef _KERNEL
75int timeout_sysctl(void *, size_t *, void *, size_t);
76
77/*
78 * special macros
79 *
80 * timeout_pending(to) - is this timeout already scheduled to run?
81 * timeout_initialized(to) - is this timeout initialized?
82 */
83#define timeout_pending(to) ((to)->to_flags & TIMEOUT_ONQUEUE)
84#define timeout_initialized(to) ((to)->to_flags & TIMEOUT_INITIALIZED)
85#define timeout_triggered(to) ((to)->to_flags & TIMEOUT_TRIGGERED)
86
87#define KCLOCK_NONE (-1) /* dummy clock for sanity checks */
88#define KCLOCK_UPTIME 0 /* uptime clock; time since boot */
89#define KCLOCK_MAX 1
90
91#define TIMEOUT_INITIALIZER_FLAGS(_fn, _arg, _kclock, _flags) { \
92 .to_list = { NULL, NULL }, \
93 .to_abstime = { .tv_sec = 0, .tv_nsec = 0 }, \
94 .to_func = (_fn), \
95 .to_arg = (_arg), \
96 .to_time = 0, \
97 .to_flags = (_flags) | TIMEOUT_INITIALIZED, \
98 .to_kclock = (_kclock) \
99}
100
101#define TIMEOUT_INITIALIZER(_f, _a) \
102 TIMEOUT_INITIALIZER_FLAGS((_f), (_a), KCLOCK_NONE, 0)
103
104void timeout_set(struct timeout *, void (*)(void *), void *);
105void timeout_set_flags(struct timeout *, void (*)(void *), void *, int, int);
106void timeout_set_proc(struct timeout *, void (*)(void *), void *);
107
108int timeout_add(struct timeout *, int);
109int timeout_add_sec(struct timeout *, int);
110int timeout_add_msec(struct timeout *, uint64_t);
111int timeout_add_usec(struct timeout *, uint64_t);
112int timeout_add_nsec(struct timeout *, uint64_t);
113
114int timeout_abs_ts(struct timeout *, const struct timespec *);
115
116int timeout_del(struct timeout *);
117int timeout_del_barrier(struct timeout *);
118void timeout_barrier(struct timeout *);
119
120void timeout_adjust_ticks(int);
121void timeout_hardclock_update(void);
122void timeout_startup(void);
123
124#endif /* _KERNEL */
125
126#endif /* _SYS_TIMEOUT_H_ */