timer.h revision 1.13 1 /* $NetBSD: timer.h,v 1.13 2021/12/19 11:49:12 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Notes on porting:
34 *
35 * - Linux does not have teardown_timer. You must add it yourself in
36 * the appropriate place.
37 */
38
39 #ifndef _LINUX_TIMER_H_
40 #define _LINUX_TIMER_H_
41
42 #include <sys/callout.h>
43
44 #include <linux/jiffies.h>
45 #include <linux/ktime.h>
46
47 struct timer_list {
48 struct callout tl_callout;
49 };
50
51 #define from_timer(V, T, F) container_of(T, __typeof__(*(V)), F)
52
53 #define TIMER_IRQSAFE __BIT(0)
54
55 static inline void
56 timer_setup(struct timer_list *timer, void (*fn)(struct timer_list *),
57 int flags)
58 {
59
60 callout_init(&timer->tl_callout, 0);
61
62 /* XXX Super-sketchy casts! */
63 callout_setfunc(&timer->tl_callout, (void (*)(void *))(void *)fn,
64 (void *)timer);
65 }
66
67 static inline void
68 teardown_timer(struct timer_list *timer)
69 {
70
71 callout_destroy(&timer->tl_callout);
72 }
73
74 static inline int
75 mod_timer(struct timer_list *timer, unsigned long then)
76 {
77 const unsigned long now = jiffies;
78 int pending;
79
80 pending = callout_pending(&timer->tl_callout);
81 callout_schedule(&timer->tl_callout, (now < then? (then - now) : 0));
82 return pending;
83 }
84
85 static inline void
86 mod_timer_pinned(struct timer_list *timer, unsigned long then)
87 {
88
89 /* XXX Stay on the same CPU it was originally on... */
90 mod_timer(timer, then);
91 }
92
93 static inline void
94 del_timer(struct timer_list *timer)
95 {
96
97 callout_stop(&timer->tl_callout);
98 }
99
100 static inline bool
101 del_timer_sync(struct timer_list *timer)
102 {
103
104 /* XXX return values? */
105 return callout_halt(&timer->tl_callout, NULL);
106 }
107
108 static inline bool
109 timer_pending(struct timer_list *timer)
110 {
111
112 return callout_pending(&timer->tl_callout);
113 }
114
115 /*
116 * XXX This is bogus -- the Linux version does various machinations to
117 * give some jitter so that stuff doesn't wake up all at once.
118 */
119
120 static inline unsigned long
121 round_jiffies_up(unsigned long j)
122 {
123 return roundup(j, hz);
124 }
125
126 static inline unsigned long
127 round_jiffies_up_relative(unsigned long j)
128 {
129 return roundup(j, hz);
130 }
131
132 #endif /* _LINUX_TIMER_H_ */
133