diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2019-12-27 14:30:27 +0100 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2020-01-10 11:54:34 +0100 |
commit | 1d1c6cef6039effd4c045a76d30414b589336d0e (patch) | |
tree | 3c71bd83eef9745c1c8190278be3504808089702 /afb-timer.c | |
parent | 6d828a1b309685ad7f5d74abc0162c59fbbd3304 (diff) |
timer: Fix stopping a running timericefish_8.99.5icefish/8.99.58.99.5
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 <romain.forlot@iot.bzh>
Diffstat (limited to 'afb-timer.c')
-rw-r--r-- | afb-timer.c | 31 |
1 files 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) { |