1 | /* $NetBSD: timer.h,v 1.5 2014/08/26 17:26:05 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 | |
46 | struct timer_list { |
47 | struct callout tl_callout; |
48 | }; |
49 | |
50 | static inline void |
51 | setup_timer(struct timer_list *timer, void (*fn)(unsigned long), |
52 | unsigned long arg) |
53 | { |
54 | |
55 | callout_init(&timer->tl_callout, 0); |
56 | |
57 | /* XXX Super-sketchy casts! */ |
58 | callout_setfunc(&timer->tl_callout, (void (*)(void *))fn, |
59 | (void *)(uintptr_t)arg); |
60 | } |
61 | |
62 | static inline void |
63 | teardown_timer(struct timer_list *timer) |
64 | { |
65 | |
66 | callout_destroy(&timer->tl_callout); |
67 | } |
68 | |
69 | static inline void |
70 | mod_timer(struct timer_list *timer, unsigned long then) |
71 | { |
72 | const unsigned long now = jiffies; |
73 | |
74 | callout_schedule(&timer->tl_callout, (now < then? (then - now) : 0)); |
75 | } |
76 | |
77 | static inline void |
78 | mod_timer_pinned(struct timer_list *timer, unsigned long then) |
79 | { |
80 | |
81 | /* XXX Stay on the same CPU it was originally on... */ |
82 | mod_timer(timer, then); |
83 | } |
84 | |
85 | static inline void |
86 | del_timer(struct timer_list *timer) |
87 | { |
88 | |
89 | callout_stop(&timer->tl_callout); |
90 | } |
91 | |
92 | static inline void |
93 | del_timer_sync(struct timer_list *timer) |
94 | { |
95 | |
96 | callout_halt(&timer->tl_callout, NULL); |
97 | } |
98 | |
99 | static inline bool |
100 | timer_pending(struct timer_list *timer) |
101 | { |
102 | |
103 | return callout_pending(&timer->tl_callout); |
104 | } |
105 | |
106 | /* |
107 | * XXX This is bogus -- the Linux version does various machinations to |
108 | * give some jitter so that stuff doesn't wake up all at once. |
109 | */ |
110 | |
111 | static inline unsigned long |
112 | round_jiffies_up(unsigned long j) |
113 | { |
114 | return roundup(j, hz); |
115 | } |
116 | |
117 | static inline unsigned long |
118 | round_jiffies_up_relative(unsigned long j) |
119 | { |
120 | return roundup(j, hz); |
121 | } |
122 | |
123 | #endif /* _LINUX_TIMER_H_ */ |
124 | |