From 1da2708a7c48c144605d84da94d0184209713683 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Wed, 9 Oct 2019 17:22:48 -0500 Subject: Fix timer Agptek Rocker (other hosted players) on timer_unregister callbacks are not removed It seems (at least on the Rocker) timers continue to fire (for a bit??) Now we store the registered callback in the sigev structure and check that the callback matches the one registered when the timer is created. This should stop the possible case of a new timer getting spurious callbacks We also now NULL the callbacks on un-register which should stop the segfaults Added some notes to timer.c and timer.h Change-Id: Ia155c3a4e4af89f474d55ed845560ccc1fab85aa --- firmware/target/hosted/kernel-unix.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'firmware/target/hosted') diff --git a/firmware/target/hosted/kernel-unix.c b/firmware/target/hosted/kernel-unix.c index e3c492a8cc..ed1f83cd8c 100644 --- a/firmware/target/hosted/kernel-unix.c +++ b/firmware/target/hosted/kernel-unix.c @@ -113,8 +113,8 @@ void (*global_timer_callback)(void); static void timer_cb(union sigval arg) { - (void)arg; - if (global_timer_callback) + /* check for spurious callbacks [arg.sival_ptr] */ + if (global_timer_callback && global_timer_callback == arg.sival_ptr) global_timer_callback(); } @@ -129,12 +129,18 @@ bool timer_register(int reg_prio, void (*unregister_callback)(void), if (reg_prio <= timer_prio || in_us <= 0) return false; - if (timer_prio >= 0 && global_unreg_callback) - global_unreg_callback(); + if(timer_prio >= 0) + { + if (global_unreg_callback) /* timer has callback user needs to unreg */ + global_unreg_callback(); + else /* no callback -- delete timer */ + timer_delete(timer_tid); + } memset(&sigev, 0, sizeof(sigevent_t)); sigev.sigev_notify = SIGEV_THREAD, sigev.sigev_notify_function = timer_cb; + sigev.sigev_value.sival_ptr = timer_callback; /* store cb to check later */ div_t q = div(in_us, 1000000); ts.it_value.tv_sec = ts.it_interval.tv_sec = q.quot; @@ -166,4 +172,6 @@ void timer_unregister(void) { timer_delete(timer_tid); timer_prio = -1; + global_unreg_callback = NULL; + global_timer_callback = NULL; } -- cgit v1.2.3