]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linuxkpi/common/src/linux_hrtimer.c
Define __poll_t type in the LinuxKPI.
[FreeBSD/FreeBSD.git] / sys / compat / linuxkpi / common / src / linux_hrtimer.c
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  * Cancel active hrtimer.
70  * Return 1 if timer was active and cancellation succeeded, or 0 otherwise.
71  */
72 int
73 linux_hrtimer_cancel(struct hrtimer *hrtimer)
74 {
75
76         return (callout_drain(&hrtimer->callout) > 0);
77 }
78
79 void
80 linux_hrtimer_init(struct hrtimer *hrtimer)
81 {
82
83         memset(hrtimer, 0, sizeof(*hrtimer));
84         mtx_init(&hrtimer->mtx, "hrtimer", NULL,
85             MTX_DEF | MTX_RECURSE | MTX_NOWITNESS);
86         callout_init_mtx(&hrtimer->callout, &hrtimer->mtx, 0);
87 }
88
89 void
90 linux_hrtimer_set_expires(struct hrtimer *hrtimer, ktime_t time)
91 {
92         hrtimer->expires = ktime_to_ns(time);
93 }
94
95 void
96 linux_hrtimer_start(struct hrtimer *hrtimer, ktime_t time)
97 {
98
99         linux_hrtimer_start_range_ns(hrtimer, time, 0);
100 }
101
102 void
103 linux_hrtimer_start_range_ns(struct hrtimer *hrtimer, ktime_t time,
104     int64_t nsec)
105 {
106
107         mtx_lock(&hrtimer->mtx);
108         hrtimer->precision = nsec;
109         callout_reset_sbt(&hrtimer->callout, nstosbt(ktime_to_ns(time)),
110             nstosbt(nsec), hrtimer_call_handler, hrtimer, 0);
111         mtx_unlock(&hrtimer->mtx);
112 }
113
114 void
115 linux_hrtimer_forward_now(struct hrtimer *hrtimer, ktime_t interval)
116 {
117
118         mtx_lock(&hrtimer->mtx);
119         callout_reset_sbt(&hrtimer->callout, nstosbt(ktime_to_ns(interval)),
120             nstosbt(hrtimer->precision), hrtimer_call_handler, hrtimer, 0);
121         mtx_unlock(&hrtimer->mtx);
122 }
123