Timer: Update how we detect if a timer is running (#1547)

This commit makes some changes in the `Timer` implementation to
simplify how we detect if a timer is running.

On an unscheduled timer, we ensure to set the `mNext` to point back
to the `Timer` instance itself. In a scheduled/running timer, the
timer is included in a linked-list where `mNext` is used to point
to next timer in the list (or `NULL` if at the end of list). The
`mNext` value is then used to quickly determine if a timer is
scheduled/running or not.

This commit also removes the now unused `TimerScheduler::IsAdded()`
method.
This commit is contained in:
Abtin Keshavarzian
2017-04-03 14:24:04 -07:00
committed by Jonathan Hui
parent 70553d51bd
commit c187c1ff81
2 changed files with 9 additions and 27 deletions
+7 -16
View File
@@ -56,6 +56,7 @@ void TimerScheduler::Add(Timer &aTimer)
if (mHead == NULL)
{
mHead = &aTimer;
aTimer.mNext = NULL;
SetAlarm();
}
else
@@ -88,16 +89,18 @@ void TimerScheduler::Add(Timer &aTimer)
if (cur == NULL)
{
prev->mNext = &aTimer;
aTimer.mNext = NULL;
}
}
}
void TimerScheduler::Remove(Timer &aTimer)
{
VerifyOrExit(aTimer.mNext != &aTimer, ;);
if (mHead == &aTimer)
{
mHead = aTimer.mNext;
aTimer.mNext = NULL;
SetAlarm();
}
else
@@ -107,27 +110,15 @@ void TimerScheduler::Remove(Timer &aTimer)
if (cur->mNext == &aTimer)
{
cur->mNext = aTimer.mNext;
aTimer.mNext = NULL;
break;
}
}
}
}
bool TimerScheduler::IsAdded(const Timer &aTimer)
{
bool rval = false;
for (Timer *cur = mHead; cur; cur = cur->mNext)
{
if (cur == &aTimer)
{
ExitNow(rval = true);
}
}
aTimer.mNext = &aTimer;
exit:
return rval;
return;
}
void TimerScheduler::SetAlarm(void)
@@ -156,7 +147,7 @@ extern "C" void otPlatAlarmFired(otInstance *aInstance)
otLogFuncExit();
}
void TimerScheduler::FireTimers()
void TimerScheduler::FireTimers(void)
{
uint32_t now = otPlatAlarmGetNow();
uint32_t elapsed;
+2 -11
View File
@@ -89,15 +89,6 @@ public:
*/
void Remove(Timer &aTimer);
/**
* This method returns whether or not the timer instance is already added.
*
* @retval TRUE If the timer instance is already added.
* @retval FALSE If the timer instance is not added.
*
*/
bool IsAdded(const Timer &aTimer);
/**
* This method processes all running timers.
*
@@ -162,7 +153,7 @@ public:
mContext(aContext),
mT0(0),
mDt(0),
mNext(NULL) {
mNext(this) {
}
/**
@@ -187,7 +178,7 @@ public:
* @retval TRUE If the timer is running.
* @retval FALSE If the timer is not running.
*/
bool IsRunning(void) const { return mScheduler.IsAdded(*this); }
bool IsRunning(void) const { return (mNext != this); }
/**
* This method schedules the timer to fire a @p dt milliseconds from now.