diff --git a/sys/include/ztimer.h b/sys/include/ztimer.h index 156e2277a3..23a107922a 100644 --- a/sys/include/ztimer.h +++ b/sys/include/ztimer.h @@ -365,6 +365,9 @@ struct ztimer_clock { /** * @brief main ztimer callback handler + * + * This gets called by clock implementations, and must only be called by them + * with interrupts disabled. */ void ztimer_handler(ztimer_clock_t *clock); @@ -408,8 +411,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)