Skip to content

Commit

Permalink
activity: cleanups + solve double stepping
Browse files Browse the repository at this point in the history
The implementation always did a Trigger before a Timeout,
which caused double cycles.
Renamed mtrigger to mtimeout for consistency
Made the TaskCore counters attributes

Signed-off-by: Peter Soetens <[email protected]>
  • Loading branch information
Peter Soetens committed Apr 12, 2015
1 parent a626eb0 commit 1f1d68b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
46 changes: 26 additions & 20 deletions rtt/Activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ namespace RTT

Activity::Activity(RunnableInterface* _r, const std::string& name )
: ActivityInterface(_r), os::Thread(ORO_SCHED_OTHER, RTT::os::LowestPriority, 0.0, 0, name ),
update_period(0.0), mtrigger(false)
update_period(0.0), mtimeout(false)
{
}

Activity::Activity(int priority, RunnableInterface* r, const std::string& name )
: ActivityInterface(r), os::Thread(ORO_SCHED_RT, priority, 0.0, 0, name ),
update_period(0.0), mtrigger(false)
update_period(0.0), mtimeout(false)
{
}

Activity::Activity(int priority, Seconds period, RunnableInterface* r, const std::string& name )
: ActivityInterface(r), os::Thread(ORO_SCHED_RT, priority, period, 0, name ),
update_period(period), mtrigger(false)
update_period(period), mtimeout(false)
{
// We pass the requested period to the constructor to not confuse users with log messages.
// Then we clear it immediately again in order to force the Thread implementation to
Expand All @@ -82,7 +82,7 @@ namespace RTT

Activity::Activity(int scheduler, int priority, Seconds period, RunnableInterface* r, const std::string& name )
: ActivityInterface(r), os::Thread(scheduler, priority, period, 0, name ),
update_period(period), mtrigger(false)
update_period(period), mtimeout(false)
{
// We pass the requested period to the constructor to not confuse users with log messages.
// Then we clear it immediately again in order to force the Thread implementation to
Expand All @@ -92,7 +92,7 @@ namespace RTT

Activity::Activity(int scheduler, int priority, Seconds period, unsigned cpu_affinity, RunnableInterface* r, const std::string& name )
: ActivityInterface(r), os::Thread(scheduler, priority, period, cpu_affinity, name ),
update_period(period), mtrigger(false)
update_period(period), mtimeout(false)
{
// We pass the requested period to the constructor to not confuse users with log messages.
// Then we clear it immediately again in order to force the Thread implementation to
Expand Down Expand Up @@ -129,6 +129,8 @@ namespace RTT
if (s < 0.0)
return false;
update_period = s;
// we need to trigger internally to get the period started
trigger();
return true;
}

Expand All @@ -155,7 +157,7 @@ namespace RTT
if ( update_period > 0) {
return false;
}
mtrigger = true;
mtimeout = true;
msg_cond.broadcast();
Thread::start();
return true;
Expand All @@ -176,21 +178,25 @@ namespace RTT
wakeup = 0;
}

if ( update_period > 0 ) {
// periodic: we flag mtimeout below; non-periodic: we flag mtimeout in trigger()
if (mtimeout) {
// was a timeout() call, or internally generated after wakeup
mtimeout = false;
this->step();
this->work(base::RunnableInterface::Trigger);
this->work(base::RunnableInterface::TimeOut);
} else {
if (runner) {
runner->loop();
runner->work(base::RunnableInterface::Trigger);
// was a trigger() call
if ( update_period > 0 ) {
this->step();
this->work(base::RunnableInterface::Trigger);
} else {
if (runner) {
runner->loop();
runner->work(base::RunnableInterface::Trigger);
}
}
}

// periodic: we flag mtrigger below; non-periodic: we flag mtrigger in trigger()
if (mtrigger) {
mtrigger = false;
this->step();
this->work(base::RunnableInterface::TimeOut);
// if a timeout() was done during work(), we will re-enter
// loop() due to the Thread::start().
}
// next, sleep/wait
os::MutexLock lock(msg_lock);
Expand All @@ -199,14 +205,14 @@ namespace RTT
return;
} else {
// If periodic, sleep until wakeup time or a message comes in.
// when wakeup time passed, wait_until will return false and we recalculate wakeup + mtrigger
// when wakeup time passed, wait_until will return false and we recalculate wakeup + mtimeout
bool time_elapsed = ! msg_cond.wait_until(msg_lock,wakeup);

if (time_elapsed) {
// calculate next wakeup point, overruns causes skips:
while ( wakeup < os::TimeService::Instance()->getNSecs() )
wakeup = wakeup + Seconds_to_nsecs(update_period);
mtrigger = true;
mtimeout = true;
}
}
if (mstopRequested) {
Expand Down
4 changes: 2 additions & 2 deletions rtt/Activity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ namespace RTT
double update_period;

/**
* When set to true, a next work is Triggered.
* When set to true, a next cycle will be a TimeOut cycle.
*/
bool mtrigger;
bool mtimeout;
bool mstopRequested;
};

Expand Down
4 changes: 4 additions & 0 deletions rtt/TaskContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ namespace RTT
this->addOperation("loadService", &TaskContext::loadService, this, ClientThread).doc("Loads a service known to RTT into this component.").arg("service_name","The name with which the service is registered by in the PluginLoader.");

this->addAttribute("TriggerOnStart",mTriggerOnStart);
this->addAttribute("CycleCounter",mCycleCounter);
this->addAttribute("IOCounter",mIOCounter);
this->addAttribute("TimeOutCounter",mTimeOutCounter);
this->addAttribute("TriggerCounter",mTriggerCounter);
// activity runs from the start.
if (our_act)
our_act->start();
Expand Down

0 comments on commit 1f1d68b

Please sign in to comment.