Timer reschedule if earlier timer added

This commit is contained in:
Michael Davidsaver
2018-10-18 14:42:30 -07:00
parent f0fa8a2481
commit e400d9f5fd
2 changed files with 11 additions and 6 deletions

View File

@@ -126,6 +126,7 @@ private:
mutable Mutex mutex;
queue_t queue;
Event waitForWork;
bool waiting;
bool alive;
Thread thread;
};

View File

@@ -28,9 +28,10 @@ TimerCallback::TimerCallback()
}
Timer::Timer(string threadName,ThreadPriority priority)
: waitForWork(false),
alive(true),
thread(threadName,priority,this)
:waitForWork(false)
,waiting(false)
,alive(true)
,thread(threadName,priority,this)
{}
struct TimerCallback::IncreasingTime {
@@ -90,6 +91,7 @@ void Timer::run()
if(queue.empty()) {
// no jobs, just go to sleep
waiting = true;
epicsGuardRelease<epicsMutex> U(G);
waitForWork.wait();
@@ -117,12 +119,14 @@ void Timer::run()
// don't update 'now' until all expired jobs run
} else {
waiting = true;
// wait for first un-expired
epicsGuardRelease<epicsMutex> U(G);
waitForWork.wait(waitfor);
now = epicsTime::getCurrent();
}
waiting = false;
}
}
@@ -164,7 +168,7 @@ void Timer::schedulePeriodic(
{
epicsTime now(epicsTime::getCurrent());
bool wasempty;
bool wakeup;
{
Lock xx(mutex);
if(timerCallback->onList) {
@@ -180,10 +184,10 @@ void Timer::schedulePeriodic(
timerCallback->timeToRun = now + delay;
timerCallback->period = period;
wasempty = queue.empty();
wakeup = waiting && (queue.empty() || queue.front()->timeToRun > timerCallback->timeToRun);
addElement(timerCallback);
}
if(wasempty) waitForWork.signal();
if(wakeup) waitForWork.signal();
}
void Timer::dump(std::ostream& o) const