From bb651c77f5f71a38dd90b2eee214e7a2224421a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20P=C3=A9dron?= Date: Wed, 11 Jan 2023 02:00:57 +0100 Subject: [PATCH] linuxkpi: Define `hrtimer_try_to_cancel()` It is the same as callout_stop(9) but the return values are different. Reviewed by: hselasky Approved by: hselasky Differential Revision: https://reviews.freebsd.org/D38081 --- .../linuxkpi/common/include/linux/hrtimer.h | 2 ++ .../linuxkpi/common/src/linux_hrtimer.c | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/sys/compat/linuxkpi/common/include/linux/hrtimer.h b/sys/compat/linuxkpi/common/include/linux/hrtimer.h index 23e707d906b4..4a63db80cc12 100644 --- a/sys/compat/linuxkpi/common/include/linux/hrtimer.h +++ b/sys/compat/linuxkpi/common/include/linux/hrtimer.h @@ -53,6 +53,7 @@ struct hrtimer { }; #define hrtimer_active(hrtimer) linux_hrtimer_active(hrtimer) +#define hrtimer_try_to_cancel(hrtimer) linux_hrtimer_try_to_cancel(hrtimer) #define hrtimer_cancel(hrtimer) linux_hrtimer_cancel(hrtimer) #define hrtimer_init(hrtimer, clock, mode) do { \ @@ -79,6 +80,7 @@ struct hrtimer { } while (0) bool linux_hrtimer_active(struct hrtimer *); +int linux_hrtimer_try_to_cancel(struct hrtimer *); int linux_hrtimer_cancel(struct hrtimer *); void linux_hrtimer_init(struct hrtimer *); void linux_hrtimer_set_expires(struct hrtimer *, ktime_t); diff --git a/sys/compat/linuxkpi/common/src/linux_hrtimer.c b/sys/compat/linuxkpi/common/src/linux_hrtimer.c index a56485512a14..5d9b376f9561 100644 --- a/sys/compat/linuxkpi/common/src/linux_hrtimer.c +++ b/sys/compat/linuxkpi/common/src/linux_hrtimer.c @@ -65,6 +65,28 @@ linux_hrtimer_active(struct hrtimer *hrtimer) return (ret); } +/* + * Try to cancel active hrtimer. + * Return 1 if timer was active and cancellation succeeded, 0 if timer was + * inactive, or -1 if the timer is being serviced and can't be cancelled. + */ +int +linux_hrtimer_try_to_cancel(struct hrtimer *hrtimer) +{ + int ret; + + mtx_lock(&hrtimer->mtx); + ret = callout_stop(&hrtimer->callout); + mtx_unlock(&hrtimer->mtx); + if (ret > 0) { + return (1); + } else if (ret < 0) { + return (0); + } else { + return (-1); + } +} + /* * Cancel active hrtimer. * Return 1 if timer was active and cancellation succeeded, or 0 otherwise.