From 2b53f35a28b791d945a0d2e170e1dd0a3483b81f Mon Sep 17 00:00:00 2001 From: Jue Date: Thu, 3 Nov 2022 14:25:04 +0100 Subject: [PATCH] ztimer: warn users when calling ztimer_now() on inactive clocks --- sys/include/ztimer.h | 16 ++++++++++++++-- sys/ztimer/core.c | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/sys/include/ztimer.h b/sys/include/ztimer.h index f6ee9c8c7e..3ee7356463 100644 --- a/sys/include/ztimer.h +++ b/sys/include/ztimer.h @@ -554,6 +554,17 @@ int ztimer_msg_receive_timeout(ztimer_clock_t *clock, msg_t *msg, */ ztimer_now_t _ztimer_now_extend(ztimer_clock_t *clock); +/** + * @brief asserts the given clock to be active + * + * @internal + * + * @note This function is internal + * + * @param[in] clock ztimer clock to operate on + */ +void _ztimer_assert_clock_active(ztimer_clock_t *clock); + /** * @brief Get the current time from a clock * @@ -668,9 +679,10 @@ ztimer_now_t _ztimer_now_extend(ztimer_clock_t *clock); */ static inline ztimer_now_t ztimer_now(ztimer_clock_t *clock) { -#if MODULE_ZTIMER_ONDEMAND_STRICT - assert(clock->users > 0); +#if MODULE_ZTIMER_ONDEMAND && DEVELHELP + _ztimer_assert_clock_active(clock); #endif + #if MODULE_ZTIMER_NOW64 if (1) { #elif MODULE_ZTIMER_EXTEND diff --git a/sys/ztimer/core.c b/sys/ztimer/core.c index f8c4e3f9e9..e3f9ab0ff3 100644 --- a/sys/ztimer/core.c +++ b/sys/ztimer/core.c @@ -32,6 +32,7 @@ #include "pm_layered.h" #endif #include "ztimer.h" +#include "log.h" #define ENABLE_DEBUG 0 #include "debug.h" @@ -529,3 +530,17 @@ static void _ztimer_print(const ztimer_clock_t *clock) } while ((entry = entry->next)); puts(""); } + +#if MODULE_ZTIMER_ONDEMAND && DEVELHELP +void _ztimer_assert_clock_active(ztimer_clock_t *clock) +{ + if (clock->users == 0) { + LOG_WARNING("WARNING! You are accessing ztimer_now() on a non-active clock!\n" + " Make sure to call ztimer_acquire() before accessing ztimer_now().\n" + " Once you've finished don't forget to call ztimer_release().\n"); + } +#if MODULE_ZTIMER_ONDEMAND_STRICT + assert(clock->users > 0); +#endif +} +#endif