2 * Copyright (c) 2017 Mark Johnston <markj@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/mutex.h>
36 #include <machine/cpu.h>
38 #include <linux/hrtimer.h>
41 hrtimer_call_handler(void *arg)
43 struct hrtimer *hrtimer;
44 enum hrtimer_restart ret;
47 ret = hrtimer->function(hrtimer);
48 MPASS(ret == HRTIMER_NORESTART);
49 callout_deactivate(&hrtimer->callout);
53 linux_hrtimer_active(struct hrtimer *hrtimer)
57 mtx_lock(&hrtimer->mtx);
58 ret = callout_active(&hrtimer->callout);
59 mtx_unlock(&hrtimer->mtx);
64 * Cancel active hrtimer.
65 * Return 1 if timer was active and cancellation succeeded, or 0 otherwise.
68 linux_hrtimer_cancel(struct hrtimer *hrtimer)
71 return (callout_drain(&hrtimer->callout) > 0);
75 linux_hrtimer_init(struct hrtimer *hrtimer)
78 hrtimer->function = NULL;
79 mtx_init(&hrtimer->mtx, "hrtimer", NULL, MTX_DEF | MTX_RECURSE);
80 callout_init_mtx(&hrtimer->callout, &hrtimer->mtx, 0);
84 linux_hrtimer_set_expires(struct hrtimer *hrtimer __unused,
85 ktime_t time __unused)
90 linux_hrtimer_start(struct hrtimer *hrtimer, ktime_t time)
93 linux_hrtimer_start_range_ns(hrtimer, time, 0);
97 linux_hrtimer_start_range_ns(struct hrtimer *hrtimer, ktime_t time, int64_t nsec)
100 mtx_lock(&hrtimer->mtx);
101 callout_reset_sbt(&hrtimer->callout, nstosbt(time.tv64), nstosbt(nsec),
102 hrtimer_call_handler, hrtimer, 0);
103 mtx_unlock(&hrtimer->mtx);