From 1d1c6cef6039effd4c045a76d30414b589336d0e Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Fri, 27 Dec 2019 14:30:27 +0100 Subject: timer: Fix stopping a running timer This fix a bug where using 1 don't stop the timer to trigger and now TimerStop really stop the timer... sd_event_source_unref only decrement the ref counter of the sd_event_source. If we want to stop returning -1 in the callback will make sure we effectively stop the timer instead of only decrement the ref counter. Bug-AGL: SPEC-3073 Change-Id: I4619ff1695c398a7c9683b58bd193b8c21b9abb1 Signed-off-by: Romain Forlot --- afb-timer.c | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/afb-timer.c b/afb-timer.c index 4afdb0e..33d1b0a 100644 --- a/afb-timer.c +++ b/afb-timer.c @@ -36,34 +36,29 @@ static int TimerNext (sd_event_source* source, uint64_t timer, void* handle) { int done; uint64_t usec; + // Stop Timer when count is null + if (timerHandle->count-- == 0) { + if (timerHandle->freeCB) timerHandle->freeCB(timerHandle->context); + free (timerHandle); + return -1; + } + done= timerHandle->callback(timerHandle); if (!done) { AFB_API_WARNING(timerHandle->api, "TimerNext Callback Fail Tag=%s", timerHandle->uid); return -1; } - // Rearm timer if needed - timerHandle->count --; - if (timerHandle->count == 0) { - sd_event_source_unref(source); - if (timerHandle->freeCB) timerHandle->freeCB(timerHandle->context); - free (handle); - return 0; - } - else { - // otherwise validate timer for a new run - sd_event_now(afb_api_get_event_loop(timerHandle->api), CLOCK_MONOTONIC, &usec); - sd_event_source_set_enabled(source, SD_EVENT_ONESHOT); - sd_event_source_set_time(source, usec + timerHandle->delay*1000); - } + // otherwise validate timer for a new run + sd_event_now(afb_api_get_event_loop(timerHandle->api), CLOCK_MONOTONIC, &usec); + sd_event_source_set_time(source, usec + timerHandle->delay*1000); + sd_event_source_set_enabled(source, SD_EVENT_ONESHOT); return 0; } void TimerEvtStop(TimerHandleT *timerHandle) { - sd_event_source_unref(timerHandle->evtSource); - if (timerHandle->freeCB) timerHandle->freeCB(timerHandle->context); - free (timerHandle); + timerHandle->count = 0; // Force timer to close } void TimerEvtStart(afb_api_t api, TimerHandleT *timerHandle, timerCallbackT callback, void *context) { @@ -76,7 +71,7 @@ void TimerEvtStart(afb_api_t api, TimerHandleT *timerHandle, timerCallbackT call // set a timer with ~250us accuracy sd_event_now(afb_api_get_event_loop(api), CLOCK_MONOTONIC, &usec); - sd_event_add_time(afb_api_get_event_loop(api), &timerHandle->evtSource, CLOCK_MONOTONIC, usec+timerHandle->delay*1000, 250, TimerNext, timerHandle); + sd_event_add_time(afb_api_get_event_loop(api), &timerHandle->evtSource, CLOCK_MONOTONIC, usec+timerHandle->delay*1000, 0, TimerNext, timerHandle); } uint64_t LockWait(afb_api_t api, uint64_t utimeout) { -- cgit 1.2.3-korg