2 * Copyright (c) 2017 François Tigeot
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 <linux/hrtimer.h>
28 #include <linux/bug.h>
31 __hrtimer_function(void *arg
)
33 struct hrtimer
*timer
= arg
;
34 enum hrtimer_restart restart
= HRTIMER_RESTART
;
36 if (timer
->function
) {
37 restart
= timer
->function(timer
);
40 if (restart
== HRTIMER_RESTART
) {
42 * XXX Not implemented yet, we would need to store the
43 * expiration period, to do the callout_reset here.
46 timer
->active
= false;
50 void hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
51 enum hrtimer_mode mode
)
53 BUG_ON(clock_id
!= CLOCK_MONOTONIC
);
55 memset(timer
, 0, sizeof(struct hrtimer
));
56 timer
->clock_id
= clock_id
;
57 timer
->ht_mode
= mode
;
59 lwkt_token_init(&timer
->timer_token
, "timer token");
60 callout_init_mp(&(timer
)->timer_callout
);
64 hrtimer_start_range_ns(struct hrtimer
*timer
, ktime_t tim
,
65 u64 range_ns
, const enum hrtimer_mode mode
)
67 int expire_ticks
= tim
.tv64
/ (NSEC_PER_SEC
/ hz
);
69 if (mode
== HRTIMER_MODE_ABS
)
70 expire_ticks
-= ticks
;
72 if (expire_ticks
<= 0)
75 lwkt_gettoken(&timer
->timer_token
);
78 callout_reset(&timer
->timer_callout
,
79 expire_ticks
, __hrtimer_function
, timer
);
81 lwkt_reltoken(&timer
->timer_token
);
85 hrtimer_cancel(struct hrtimer
*timer
)
87 return callout_drain(&timer
->timer_callout
) == 0;
90 /* Returns non-zero if the timer is already on the queue */
92 hrtimer_active(const struct hrtimer
*timer
)
94 return callout_pending(&timer
->timer_callout
);