1 /*
2 * Copyright 2022, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef _OBSD_COMPAT_SYS_TIMEOUT_H_
6 #define _OBSD_COMPAT_SYS_TIMEOUT_H_
7
8
9 #include <sys/callout.h>
10
11
12 struct timeout {
13 struct callout c;
14 };
15
16
17 static inline void
timeout_set(struct timeout * to,void (* fn)(void *),void * arg)18 timeout_set(struct timeout *to, void (*fn)(void *), void *arg)
19 {
20 callout_init_mtx(&to->c, &Giant, 0);
21 callout_reset(&to->c, -1, fn, arg);
22 }
23
24
25 static inline int
timeout_pending(struct timeout * to)26 timeout_pending(struct timeout *to)
27 {
28 return callout_pending(&to->c);
29 }
30
31
32 static inline int
timeout_add_usec(struct timeout * to,int usec)33 timeout_add_usec(struct timeout *to, int usec)
34 {
35 return (callout_schedule(&to->c, USEC_2_TICKS(usec)) ? 0 : 1);
36 }
37
38
39 static inline int
timeout_add_msec(struct timeout * to,int msec)40 timeout_add_msec(struct timeout *to, int msec)
41 {
42 return timeout_add_usec(to, msec * 1000);
43 }
44
45
46 static inline int
timeout_add_sec(struct timeout * to,int sec)47 timeout_add_sec(struct timeout *to, int sec)
48 {
49 return timeout_add_msec(to, sec * 1000);
50 }
51
52
53 static inline int
timeout_del(struct timeout * to)54 timeout_del(struct timeout *to)
55 {
56 return ((callout_stop(&to->c) == 1) ? 1 : 0);
57 }
58
59
60 #endif /* _OBSD_COMPAT_SYS_TIMEOUT_H_ */
61