From: idgay Date: Fri, 11 Aug 2006 21:27:59 +0000 (+0000) Subject: separate firing and from-timer update logic (similar to VirtualizeAlarmC, X-Git-Tag: tinyos/2.0.1~285 X-Git-Url: https://oss.titaniummirror.com/gitweb/?a=commitdiff_plain;h=5049546204a2f459a828dc67a6534fa6f8b85faf;p=tinyos-2.x.git separate firing and from-timer update logic (similar to VirtualizeAlarmC, though doesn't have to worry about concurrent changes) --- diff --git a/tos/lib/timer/VirtualizeTimerC.nc b/tos/lib/timer/VirtualizeTimerC.nc index b36e75a8..3c26c371 100644 --- a/tos/lib/timer/VirtualizeTimerC.nc +++ b/tos/lib/timer/VirtualizeTimerC.nc @@ -40,10 +40,10 @@ generic module VirtualizeTimerC(typedef precision_tag, int max_timers) implementation { enum - { - NUM_TIMERS = max_timers, - END_OF_LIST = 255, - }; + { + NUM_TIMERS = max_timers, + END_OF_LIST = 255, + }; typedef struct { @@ -57,9 +57,35 @@ implementation Timer_t m_timers[NUM_TIMERS]; bool m_timers_changed; - task void executeTimersNow(); + task void updateFromTimer(); + + void fireTimers(uint32_t now) + { + uint8_t num; + + for (num=0; numisrunning) + { + uint32_t elapsed = now - timer->t0; + + if (elapsed >= timer->dt) + { + if (timer->isoneshot) + timer->isrunning = FALSE; + else // Update timer for next event + timer->t0 += timer->dt; - void executeTimers() + signal Timer.fired[num](); + } + } + } + post updateFromTimer(); + } + + task void updateFromTimer() { /* This code supports a maximum dt of MAXINT. If min_remaining and remaining were switched to uint32_t, and the logic changed a @@ -71,70 +97,36 @@ implementation uint8_t num; call TimerFrom.stop(); - m_timers_changed = FALSE; for (num=0; numisrunning) { - uint32_t elapsed = now - timer->t0; + Timer_t* timer = &m_timers[num]; - if (elapsed >= timer->dt) - { - if (timer->isoneshot) - { - timer->isrunning = FALSE; - } - else + if (timer->isrunning) { - // Update timer for next event - timer->t0 += timer->dt; - // Update elapsed so we compute the time of the next event - elapsed -= timer->dt; + uint32_t elapsed = now - timer->t0; + int32_t remaining = timer->dt - elapsed; + + if (remaining < min_remaining) + { + min_remaining = remaining; + min_remaining_isset = TRUE; + } } - - signal Timer.fired[num](); - } - - // Check isrunning in case the timer was stopped in the fired - // event or this was a one shot timer; note that a one shot - // timer that was restarted in its fired event will push us - // through here with a value of remaining <= 0. But we're - // already scheduled an executeTimersNow in that case (and - // suppressed setting TimerFrom) - if (timer->isrunning) - { - int32_t remaining = timer->dt - elapsed; - - if (remaining < min_remaining) - { - min_remaining = remaining; - min_remaining_isset = TRUE; - } - } } - } - if (!m_timers_changed && min_remaining_isset) - { - if (min_remaining <= 0) - post executeTimersNow(); - else - call TimerFrom.startOneShotAt(now, min_remaining); - } + if (min_remaining_isset) + { + if (min_remaining <= 0) + fireTimers(now); + else + call TimerFrom.startOneShotAt(now, min_remaining); + } } - event void TimerFrom.fired() { - executeTimers(); - } - - task void executeTimersNow() - { - executeTimers(); + fireTimers(call TimerFrom.getNow()); } void startTimer(uint8_t num, uint32_t t0, uint32_t dt, bool isoneshot) @@ -144,8 +136,7 @@ implementation timer->dt = dt; timer->isoneshot = isoneshot; timer->isrunning = TRUE; - m_timers_changed = TRUE; - post executeTimersNow(); + post updateFromTimer(); } command void Timer.startPeriodic[uint8_t num](uint32_t dt)