Skip to content

Commit

Permalink
FuriTimer: Use an event instead of a volatile bool to wait for deleti…
Browse files Browse the repository at this point in the history
…on (#3887)

Co-authored-by: あく <alleteam@gmail.com>
  • Loading branch information
CookiePLMonster and skotopes authored Sep 8, 2024
1 parent 543f605 commit 70d8951
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions furi/core/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "kernel.h"

#include <FreeRTOS.h>
#include <event_groups.h>
#include <timers.h>

struct FuriTimer {
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down

0 comments on commit 70d8951

Please sign in to comment.