1 /*-
2 * Copyright (c) 2017 Mark Johnston <markj@FreeBSD.org>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice unmodified, this list of conditions, and the following
9 * disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/lock.h>
32 #include <sys/mutex.h>
33 #include <sys/time.h>
34
35 #include <machine/cpu.h>
36
37 #include <linux/hrtimer.h>
38
39 static void
40 hrtimer_call_handler(void *arg)
41 {
42 struct hrtimer *hrtimer;
43 enum hrtimer_restart ret;
44
45 hrtimer = arg;
46 ret = hrtimer->function(hrtimer);
47
48 if (ret == HRTIMER_RESTART) {
49 callout_schedule_sbt(&hrtimer->callout,
50 nstosbt(hrtimer->expires), nstosbt(hrtimer->precision), 0);
51 } else {
52 callout_deactivate(&hrtimer->callout);
53 }
54 }
55
56 bool
57 linux_hrtimer_active(struct hrtimer *hrtimer)
58 {
59 bool ret;
60
61 mtx_lock(&hrtimer->mtx);
62 ret = callout_active(&hrtimer->callout);
63 mtx_unlock(&hrtimer->mtx);
64
65 return (ret);
66 }
67
68 /*
69 * Try to cancel active hrtimer.
70 * Return 1 if timer was active and cancellation succeeded, 0 if timer was
71 * inactive, or -1 if the timer is being serviced and can't be cancelled.
72 */
73 int
74 linux_hrtimer_try_to_cancel(struct hrtimer *hrtimer)
75 {
76 int ret;
77
78 mtx_lock(&hrtimer->mtx);
79 ret = callout_stop(&hrtimer->callout);
80 mtx_unlock(&hrtimer->mtx);
81 if (ret > 0) {
82 return (1);
83 } else if (ret < 0) {
84 return (0);
85 } else {
86 return (-1);
87 }
88 }
89
90 /*
91 * Cancel active hrtimer.
92 * Return 1 if timer was active and cancellation succeeded, or 0 otherwise.
93 */
94 int
95 linux_hrtimer_cancel(struct hrtimer *hrtimer)
96 {
97
98 return (callout_drain(&hrtimer->callout) > 0);
99 }
100
101 void
102 linux_hrtimer_init(struct hrtimer *hrtimer)
103 {
104
105 memset(hrtimer, 0, sizeof(*hrtimer));
106 mtx_init(&hrtimer->mtx, "hrtimer", NULL,
107 MTX_DEF | MTX_RECURSE | MTX_NOWITNESS);
108 callout_init_mtx(&hrtimer->callout, &hrtimer->mtx, 0);
109 }
110
111 void
112 linux_hrtimer_set_expires(struct hrtimer *hrtimer, ktime_t time)
113 {
114 hrtimer->expires = ktime_to_ns(time);
115 }
116
117 void
118 linux_hrtimer_start(struct hrtimer *hrtimer, ktime_t time)
119 {
120
121 linux_hrtimer_start_range_ns(hrtimer, time, 0);
122 }
123
124 void
125 linux_hrtimer_start_range_ns(struct hrtimer *hrtimer, ktime_t time,
126 int64_t nsec)
127 {
128
129 mtx_lock(&hrtimer->mtx);
130 hrtimer->precision = nsec;
131 callout_reset_sbt(&hrtimer->callout, nstosbt(ktime_to_ns(time)),
132 nstosbt(nsec), hrtimer_call_handler, hrtimer, 0);
133 mtx_unlock(&hrtimer->mtx);
134 }
135
136 void
137 linux_hrtimer_forward_now(struct hrtimer *hrtimer, ktime_t interval)
138 {
139
140 mtx_lock(&hrtimer->mtx);
141 callout_reset_sbt(&hrtimer->callout, nstosbt(ktime_to_ns(interval)),
142 nstosbt(hrtimer->precision), hrtimer_call_handler, hrtimer, 0);
143 mtx_unlock(&hrtimer->mtx);
144 }
Cache object: dcde9353418b4f971b1fb458fd95bea3
|