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
46struct timer_list {
47 struct callout tl_callout;
48};
49
50static inline void
51setup_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
62static inline void
63teardown_timer(struct timer_list *timer)
64{
65
66 callout_destroy(&timer->tl_callout);
67}
68
69static inline void
70mod_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
77static inline void
78mod_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
85static inline void
86del_timer(struct timer_list *timer)
87{
88
89 callout_stop(&timer->tl_callout);
90}
91
92static inline void
93del_timer_sync(struct timer_list *timer)
94{
95
96 callout_halt(&timer->tl_callout, NULL);
97}
98
99static inline bool
100timer_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
111static inline unsigned long
112round_jiffies_up(unsigned long j)
113{
114 return roundup(j, hz);
115}
116
117static inline unsigned long
118round_jiffies_up_relative(unsigned long j)
119{
120 return roundup(j, hz);
121}
122
123#endif /* _LINUX_TIMER_H_ */
124