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
This commit is contained in:
William Wilgus 2019-10-09 17:22:48 -05:00
parent 3d6d90382e
commit 1da2708a7c
3 changed files with 21 additions and 4 deletions

View file

@ -30,6 +30,7 @@
#define TIMER_FREQ 1000000 #define TIMER_FREQ 1000000
#endif #endif
/* NOTE: if unreg cb is defined you are in charge of calling timer_unregister() */
bool timer_register(int reg_prio, void (*unregister_callback)(void), bool timer_register(int reg_prio, void (*unregister_callback)(void),
long cycles, void (*timer_callback)(void) long cycles, void (*timer_callback)(void)
IF_COP(,int core)); IF_COP(,int core));
@ -37,6 +38,10 @@ bool timer_set_period(long cycles);
#ifdef CPU_COLDFIRE #ifdef CPU_COLDFIRE
void timers_adjust_prescale(int multiplier, bool enable_irq); void timers_adjust_prescale(int multiplier, bool enable_irq);
#endif #endif
/* NOTE: unregister callbacks are not called by timer_unregister()
* the unregister_callback only gets called when your timer gets
* overwritten by a lower priority timer using timer_register() */
void timer_unregister(void); void timer_unregister(void);
/* target-specific interface */ /* target-specific interface */

View file

@ -113,8 +113,8 @@ void (*global_timer_callback)(void);
static void timer_cb(union sigval arg) static void timer_cb(union sigval arg)
{ {
(void)arg; /* check for spurious callbacks [arg.sival_ptr] */
if (global_timer_callback) if (global_timer_callback && global_timer_callback == arg.sival_ptr)
global_timer_callback(); 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) if (reg_prio <= timer_prio || in_us <= 0)
return false; return false;
if (timer_prio >= 0 && global_unreg_callback) if(timer_prio >= 0)
{
if (global_unreg_callback) /* timer has callback user needs to unreg */
global_unreg_callback(); global_unreg_callback();
else /* no callback -- delete timer */
timer_delete(timer_tid);
}
memset(&sigev, 0, sizeof(sigevent_t)); memset(&sigev, 0, sizeof(sigevent_t));
sigev.sigev_notify = SIGEV_THREAD, sigev.sigev_notify = SIGEV_THREAD,
sigev.sigev_notify_function = timer_cb; 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); div_t q = div(in_us, 1000000);
ts.it_value.tv_sec = ts.it_interval.tv_sec = q.quot; 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_delete(timer_tid);
timer_prio = -1; timer_prio = -1;
global_unreg_callback = NULL;
global_timer_callback = NULL;
} }

View file

@ -42,6 +42,7 @@ bool timer_register(int reg_prio, void (*unregister_callback)(void),
return false; return false;
pfn_timer = timer_callback; pfn_timer = timer_callback;
/* NOTE: if unreg cb is defined you are in charge of calling timer_unregister() */
pfn_unregister = unregister_callback; pfn_unregister = unregister_callback;
timer_prio = reg_prio; timer_prio = reg_prio;
@ -53,6 +54,9 @@ bool timer_set_period(long cycles)
return timer_set(cycles, false); return timer_set(cycles, false);
} }
/* NOTE: unregister callbacks are not called by timer_unregister()
* the unregister_callback only gets called when your timer gets
* overwritten by a lower priority timer using timer_register() */
void timer_unregister(void) void timer_unregister(void)
{ {
timer_stop(); timer_stop();