Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip task/timer updated if late for more than a few periods #129

Merged
merged 1 commit into from
Jan 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion rtt/os/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ namespace RTT {

// Wait
int ret = 0;
if ( wake_up_time > rtos_get_time_ns() )
Time now = rtos_get_time_ns();
if ( wake_up_time > now )
ret = msem.waitUntil( wake_up_time ); // case of no timers or running timers
else
ret = -1; // case of timer overrun.
Expand All @@ -99,6 +100,11 @@ namespace RTT {
TimerIds::iterator tim = mtimers.begin() + next_timer_id;
if ( tim->period ) {
// periodic timer
// if late by more than 4 periods, skip late updates
int maxDelayInPeriods = 4;
if (now - tim->expires > tim->period*maxDelayInPeriods) {
tim->expires += tim->period*((now - tim->expires) / tim->period);
}
tim->expires += tim->period;
} else {
// aperiodic timer
Expand Down
9 changes: 8 additions & 1 deletion rtt/os/gnulinux/fosi_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,16 @@ namespace RTT

if (task->wait_policy == ORO_WAIT_ABS)
{
// in the case of overrun by more than 4 periods,
// skip all the updates before now, with the next update aligned to period
int maxDelayInPeriods = 4;
NANO_TIME period = task->period;
if (now - wake > maxDelayInPeriods*period) {
period = period * ((now - wake) / period);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this should have been

period += period * ((now - wake) / period);

because we always want to add at least one period (line 260) plus eventually the additional ones, like in Timer.cpp:108.

}
// program next period:
// 1. convert period to timespec
TIME_SPEC ts = ticks2timespec( nano2ticks( task->period) );
TIME_SPEC ts = ticks2timespec( nano2ticks(period) );
// 2. Add ts to periodMark (danger: tn guards for overflows!)
NANO_TIME tn = (task->periodMark.tv_nsec + ts.tv_nsec);
task->periodMark.tv_nsec = tn % 1000000000LL;
Expand Down