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_NORESTART
)
41 timer
->function
= NULL
;
44 void hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
45 enum hrtimer_mode mode
)
47 BUG_ON(clock_id
!= CLOCK_MONOTONIC
);
49 memset(timer
, 0, sizeof(struct hrtimer
));
50 timer
->clock_id
= clock_id
;
51 timer
->ht_mode
= mode
;
53 lwkt_token_init(&timer
->timer_token
, "timer token");
54 callout_init_mp(&(timer
)->timer_callout
);
58 hrtimer_start_range_ns(struct hrtimer
*timer
, ktime_t tim
,
59 u64 range_ns
, const enum hrtimer_mode mode
)
61 int expire_ticks
= tim
.tv64
/ (NSEC_PER_SEC
/ hz
);
63 if (mode
== HRTIMER_MODE_ABS
)
64 expire_ticks
-= ticks
;
66 if (expire_ticks
<= 0)
69 lwkt_gettoken(&timer
->timer_token
);
72 callout_reset(&timer
->timer_callout
,
73 expire_ticks
, __hrtimer_function
, timer
);
75 lwkt_reltoken(&timer
->timer_token
);
79 hrtimer_cancel(struct hrtimer
*timer
)
81 return callout_drain(&timer
->timer_callout
) == 0;
84 /* Returns non-zero if the timer is already on the queue */
86 hrtimer_active(const struct hrtimer
*timer
)
88 return callout_pending(&timer
->timer_callout
);