mirror of
https://github.com/espressif/openthread.git
synced 2026-08-03 09:27:47 +00:00
[timer] add RemoveAll() method for use by unit test (#6814)
This commit adds a new static method `Timer::RemoveAll()` which removes all running timers from the `Scheduler`. This method is only intended for use by by `test_timer` unit test, allowing it to remove any timers that may be started from constructors of different objects in an OT instance before starting the unit test. With this change, we can have timers started from constructors. To ensure that the `RemoveAll()` is only used by the unit test, it is defined as a `protected` method which can still be used in the test which defines and uses `TestTimer` as a sub-class of `Timer`.
This commit is contained in:
committed by
Jonathan Hui
parent
6dda0dcd1d
commit
f7fd2b0813
@@ -107,6 +107,11 @@ void TimerMilli::Stop(void)
|
||||
Get<Scheduler>().Remove(*this);
|
||||
}
|
||||
|
||||
void TimerMilli::RemoveAll(Instance &aInstance)
|
||||
{
|
||||
aInstance.Get<Scheduler>().RemoveAll();
|
||||
}
|
||||
|
||||
void Timer::Scheduler::Add(Timer &aTimer, const AlarmApi &aAlarmApi)
|
||||
{
|
||||
Timer *prev = nullptr;
|
||||
@@ -193,6 +198,18 @@ exit:
|
||||
return;
|
||||
}
|
||||
|
||||
void Timer::Scheduler::RemoveAll(const AlarmApi &aAlarmApi)
|
||||
{
|
||||
Timer *timer;
|
||||
|
||||
while ((timer = mTimerList.Pop()) != nullptr)
|
||||
{
|
||||
timer->SetNext(timer);
|
||||
}
|
||||
|
||||
SetAlarm(aAlarmApi);
|
||||
}
|
||||
|
||||
extern "C" void otPlatAlarmMilliFired(otInstance *aInstance)
|
||||
{
|
||||
Instance *instance = static_cast<Instance *>(aInstance);
|
||||
@@ -233,6 +250,11 @@ void TimerMicro::Stop(void)
|
||||
Get<Scheduler>().Remove(*this);
|
||||
}
|
||||
|
||||
void TimerMicro::RemoveAll(Instance &aInstance)
|
||||
{
|
||||
aInstance.Get<Scheduler>().RemoveAll();
|
||||
}
|
||||
|
||||
extern "C" void otPlatAlarmMicroFired(otInstance *aInstance)
|
||||
{
|
||||
Instance *instance = static_cast<Instance *>(aInstance);
|
||||
|
||||
@@ -136,6 +136,7 @@ protected:
|
||||
|
||||
void Add(Timer &aTimer, const AlarmApi &aAlarmApi);
|
||||
void Remove(Timer &aTimer, const AlarmApi &aAlarmApi);
|
||||
void RemoveAll(const AlarmApi &aAlarmApi);
|
||||
void ProcessTimers(const AlarmApi &aAlarmApi);
|
||||
void SetAlarm(const AlarmApi &aAlarmApi);
|
||||
|
||||
@@ -183,6 +184,7 @@ public:
|
||||
private:
|
||||
void Add(TimerMilli &aTimer) { Timer::Scheduler::Add(aTimer, sAlarmMilliApi); }
|
||||
void Remove(TimerMilli &aTimer) { Timer::Scheduler::Remove(aTimer, sAlarmMilliApi); }
|
||||
void RemoveAll(void) { Timer::Scheduler::RemoveAll(sAlarmMilliApi); }
|
||||
void ProcessTimers(void) { Timer::Scheduler::ProcessTimers(sAlarmMilliApi); }
|
||||
|
||||
static const AlarmApi sAlarmMilliApi;
|
||||
@@ -247,6 +249,9 @@ public:
|
||||
*
|
||||
*/
|
||||
static TimeMilli GetNow(void) { return TimeMilli(otPlatAlarmMilliGetNow()); }
|
||||
|
||||
protected:
|
||||
static void RemoveAll(Instance &aInstance);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -322,6 +327,7 @@ public:
|
||||
private:
|
||||
void Add(TimerMicro &aTimer) { Timer::Scheduler::Add(aTimer, sAlarmMicroApi); }
|
||||
void Remove(TimerMicro &aTimer) { Timer::Scheduler::Remove(aTimer, sAlarmMicroApi); }
|
||||
void RemoveAll(void) { Timer::Scheduler::RemoveAll(sAlarmMicroApi); }
|
||||
void ProcessTimers(void) { Timer::Scheduler::ProcessTimers(sAlarmMicroApi); }
|
||||
|
||||
static const AlarmApi sAlarmMicroApi;
|
||||
@@ -377,6 +383,9 @@ public:
|
||||
*
|
||||
*/
|
||||
static TimeMicro GetNow(void) { return Time(otPlatAlarmMicroGetNow()); }
|
||||
|
||||
protected:
|
||||
static void RemoveAll(Instance &aInstance);
|
||||
};
|
||||
#endif // OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
|
||||
|
||||
|
||||
@@ -104,6 +104,8 @@ public:
|
||||
|
||||
void ResetFiredCounter(void) { mFiredCounter = 0; }
|
||||
|
||||
static void RemoveAll(ot::Instance &aInstance) { TimerType::RemoveAll(aInstance); }
|
||||
|
||||
private:
|
||||
uint32_t mFiredCounter; //< Number of times timer has been fired so far
|
||||
};
|
||||
@@ -134,6 +136,7 @@ template <typename TimerType> int TestOneTimer(void)
|
||||
|
||||
// Test one Timer basic operation.
|
||||
|
||||
TestTimer<TimerType>::RemoveAll(*instance);
|
||||
InitTestTimer();
|
||||
InitCounters();
|
||||
|
||||
@@ -259,6 +262,7 @@ template <typename TimerType> int TestTwoTimers(void)
|
||||
TestTimer<TimerType> timer1(*instance);
|
||||
TestTimer<TimerType> timer2(*instance);
|
||||
|
||||
TestTimer<TimerType>::RemoveAll(*instance);
|
||||
InitTestTimer();
|
||||
printf("TestTwoTimers() ");
|
||||
|
||||
@@ -491,6 +495,7 @@ template <typename TimerType> static void TenTimers(uint32_t aTimeShift)
|
||||
|
||||
// Start the Ten timers.
|
||||
|
||||
TestTimer<TimerType>::RemoveAll(*instance);
|
||||
InitTestTimer();
|
||||
InitCounters();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user