From 70d8951fb703d09750972be125a4bc11d2ae3b40 Mon Sep 17 00:00:00 2001 From: Silent Date: Mon, 9 Sep 2024 01:04:56 +0200 Subject: [PATCH] FuriTimer: Use an event instead of a volatile bool to wait for deletion (#3887) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: あく --- furi/core/timer.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/furi/core/timer.c b/furi/core/timer.c index 4ed913958..21952cf12 100644 --- a/furi/core/timer.c +++ b/furi/core/timer.c @@ -3,6 +3,7 @@ #include "kernel.h" #include +#include #include struct FuriTimer { @@ -14,6 +15,8 @@ struct FuriTimer { // IMPORTANT: container MUST be the FIRST struct member static_assert(offsetof(FuriTimer, container) == 0); +#define TIMER_DELETED_EVENT (1U << 0) + static void TimerCallback(TimerHandle_t hTimer) { FuriTimer* instance = pvTimerGetTimerID(hTimer); furi_check(instance); @@ -41,8 +44,8 @@ static void furi_timer_epilogue(void* context, uint32_t arg) { furi_assert(context); UNUSED(arg); - volatile bool* can_be_removed = context; - *can_be_removed = true; + EventGroupHandle_t hEvent = context; + xEventGroupSetBits(hEvent, TIMER_DELETED_EVENT); } void furi_timer_free(FuriTimer* instance) { @@ -52,14 +55,12 @@ void furi_timer_free(FuriTimer* instance) { TimerHandle_t hTimer = (TimerHandle_t)instance; furi_check(xTimerDelete(hTimer, portMAX_DELAY) == pdPASS); - volatile bool can_be_removed = false; - furi_check( - xTimerPendFunctionCall(furi_timer_epilogue, (void*)&can_be_removed, 0, portMAX_DELAY) == - pdPASS); + StaticEventGroup_t event_container; + EventGroupHandle_t hEvent = xEventGroupCreateStatic(&event_container); + furi_check(xTimerPendFunctionCall(furi_timer_epilogue, hEvent, 0, portMAX_DELAY) == pdPASS); - while(!can_be_removed) { - furi_delay_tick(2); - } + xEventGroupWaitBits(hEvent, TIMER_DELETED_EVENT, 0, pdTRUE, portMAX_DELAY); + vEventGroupDelete(hEvent); free(instance); }