From 991f74a62adc37790c661e7b1d2c30185ef93559 Mon Sep 17 00:00:00 2001 From: chrysn Date: Wed, 15 Dec 2021 14:34:26 +0100 Subject: [PATCH] sys/ztimer: ztimer_remove return its success Co-authored-by: Marian Buschsieweke --- sys/include/ztimer.h | 8 +++++++- sys/ztimer/core.c | 15 +++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/sys/include/ztimer.h b/sys/include/ztimer.h index c7900a3873..70a52ac1e3 100644 --- a/sys/include/ztimer.h +++ b/sys/include/ztimer.h @@ -408,8 +408,14 @@ unsigned ztimer_is_set(const ztimer_clock_t *clock, const ztimer_t *timer); * * @param[in] clock ztimer clock to operate on * @param[in] timer timer entry to remove + * + * @retval true The timer was removed (and thus its callback neither was nor + * will be called by ztimer). + * @retval false The timer fired previously or is not set on the @p clock + * at all. + * */ -void ztimer_remove(ztimer_clock_t *clock, ztimer_t *timer); +bool ztimer_remove(ztimer_clock_t *clock, ztimer_t *timer); /** * @brief Post a message after a delay diff --git a/sys/ztimer/core.c b/sys/ztimer/core.c index 01205f1cf2..fea54c61ad 100644 --- a/sys/ztimer/core.c +++ b/sys/ztimer/core.c @@ -37,7 +37,7 @@ #include "debug.h" static void _add_entry_to_list(ztimer_clock_t *clock, ztimer_base_t *entry); -static void _del_entry_from_list(ztimer_clock_t *clock, ztimer_base_t *entry); +static bool _del_entry_from_list(ztimer_clock_t *clock, ztimer_base_t *entry); static void _ztimer_update(ztimer_clock_t *clock); static void _ztimer_print(const ztimer_clock_t *clock); static uint32_t _ztimer_update_head_offset(ztimer_clock_t *clock); @@ -68,18 +68,20 @@ unsigned ztimer_is_set(const ztimer_clock_t *clock, const ztimer_t *timer) return res; } -void ztimer_remove(ztimer_clock_t *clock, ztimer_t *timer) +bool ztimer_remove(ztimer_clock_t *clock, ztimer_t *timer) { + bool was_removed = false; unsigned state = irq_disable(); if (_is_set(clock, timer)) { _ztimer_update_head_offset(clock); - _del_entry_from_list(clock, &timer->base); + was_removed = _del_entry_from_list(clock, &timer->base); _ztimer_update(clock); } irq_restore(state); + return was_removed; } uint32_t ztimer_set(ztimer_clock_t *clock, ztimer_t *timer, uint32_t val) @@ -232,8 +234,10 @@ static uint32_t _ztimer_update_head_offset(ztimer_clock_t *clock) return now; } -static void _del_entry_from_list(ztimer_clock_t *clock, ztimer_base_t *entry) +static bool _del_entry_from_list(ztimer_clock_t *clock, ztimer_base_t *entry) { + bool was_removed = false; + DEBUG("_del_entry_from_list()\n"); ztimer_base_t *list = &clock->list; @@ -254,6 +258,7 @@ static void _del_entry_from_list(ztimer_clock_t *clock, ztimer_base_t *entry) list_entry->offset += entry->offset; } + was_removed = true; /* reset the entry's next pointer so _is_set() considers it unset */ entry->next = NULL; break; @@ -268,6 +273,8 @@ static void _del_entry_from_list(ztimer_clock_t *clock, ztimer_base_t *entry) pm_unblock(clock->block_pm_mode); } #endif + + return was_removed; } static ztimer_t *_now_next(ztimer_clock_t *clock)