From cee252174b472713cb259fd21c8a36ea35a705da Mon Sep 17 00:00:00 2001 From: yinshengkai Date: Wed, 28 Feb 2024 11:29:02 +0800 Subject: [PATCH] clock/perf: add critical section protection Signed-off-by: yinshengkai --- sched/clock/clock_perf.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sched/clock/clock_perf.c b/sched/clock/clock_perf.c index 18393d74d1..8eac83ba69 100644 --- a/sched/clock/clock_perf.c +++ b/sched/clock/clock_perf.c @@ -26,8 +26,9 @@ #include -#include #include +#include +#include #include #if defined(CONFIG_PERF_OVERFLOW_CORRECTION) && ULONG_MAX != UINT64_MAX @@ -39,6 +40,7 @@ struct perf_s { struct wdog_s wdog; + spinlock_t lock; unsigned long last; unsigned long overflow; }; @@ -93,6 +95,8 @@ clock_t perf_gettime(void) { FAR struct perf_s *perf = &g_perf; unsigned long now = up_perf_gettime(); + irqstate_t flags = spin_lock_irqsave(&perf->lock); + clock_t result; /* Check if overflow */ @@ -102,7 +106,9 @@ clock_t perf_gettime(void) } perf->last = now; - return (clock_t)now | (clock_t)perf->overflow << 32; + result = (clock_t)now | (clock_t)perf->overflow << 32; + spin_unlock_irqrestore(&perf->lock, flags); + return result; } /****************************************************************************