From 702e41630cd63fcf826553432311d25c9d04c54c Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 1 Sep 2014 16:08:10 +0200 Subject: [PATCH 1/3] Revert "extras/slaveactivity: fix potential segfault if no master activity is set" This reverts commit e1559a65b19cca6a695f33f9c51a4268ac8c864b. --- rtt/extras/SlaveActivity.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/rtt/extras/SlaveActivity.cpp b/rtt/extras/SlaveActivity.cpp index 7143e0d1f..e23de7677 100644 --- a/rtt/extras/SlaveActivity.cpp +++ b/rtt/extras/SlaveActivity.cpp @@ -179,9 +179,7 @@ namespace RTT { bool SlaveActivity::trigger() { - ExecutionEngine *master = 0; - if (mmaster) - master = dynamic_cast( mmaster->getRunner() ); + ExecutionEngine *master = dynamic_cast( mmaster->getRunner() ); ExecutionEngine * r= dynamic_cast( runner ); if(!master || !r){ Logger::log() << Logger::Fatal << " SlaveActivity: cannot push messages to another engine, current engine is unsupported." << Logger::endl; From 12bbd598c2fd8a8d48fdfe4f061398c98f05e194 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 1 Sep 2014 16:08:16 +0200 Subject: [PATCH 2/3] Revert "SlaveActivity" This reverts commit ed1d5f3f35221c3cb412fb1f002e5a3ede34055b. --- rtt/ExecutionEngine.cpp | 11 ----------- rtt/ExecutionEngine.hpp | 7 ------- rtt/base/ActivityInterface.cpp | 4 ---- rtt/base/ActivityInterface.hpp | 6 ------ rtt/extras/SlaveActivity.cpp | 15 +-------------- 5 files changed, 1 insertion(+), 42 deletions(-) diff --git a/rtt/ExecutionEngine.cpp b/rtt/ExecutionEngine.cpp index 5be447ee5..89cf477b0 100644 --- a/rtt/ExecutionEngine.cpp +++ b/rtt/ExecutionEngine.cpp @@ -238,17 +238,6 @@ namespace RTT msg_cond.broadcast(); // required for waitForMessages() (3rd party thread) } - void ExecutionEngine::takeoverMessages( ExecutionEngine *remote ) - { - DisposableInterface* com(0); - { - while ( remote->mqueue->dequeue( com ) ) { - assert( com ); - mqueue->enqueue( com ); - } - } - } - bool ExecutionEngine::process( DisposableInterface* c ) { if ( c && this->getActivity() ) { diff --git a/rtt/ExecutionEngine.hpp b/rtt/ExecutionEngine.hpp index f5cb2cf2d..e55bcd4d5 100644 --- a/rtt/ExecutionEngine.hpp +++ b/rtt/ExecutionEngine.hpp @@ -182,13 +182,6 @@ namespace RTT * Set the 'owner' task in the exception state. */ void setExceptionTask(); - - /** - * this function is mentioned for activietes that are not running by her own - * but need to push messages to this execution engine, like SlaveActivity - */ - void takeoverMessages(ExecutionEngine *remote); - protected: /** * Call this if you wish to block on a message arriving in the Execution Engine. diff --git a/rtt/base/ActivityInterface.cpp b/rtt/base/ActivityInterface.cpp index c63363362..40a11a7f1 100644 --- a/rtt/base/ActivityInterface.cpp +++ b/rtt/base/ActivityInterface.cpp @@ -67,8 +67,4 @@ bool ActivityInterface::run(RunnableInterface* _r) return true; } -RunnableInterface* ActivityInterface::getRunner() -{ - return runner; -} diff --git a/rtt/base/ActivityInterface.hpp b/rtt/base/ActivityInterface.hpp index 6fb3b10d8..ff394a1ce 100644 --- a/rtt/base/ActivityInterface.hpp +++ b/rtt/base/ActivityInterface.hpp @@ -216,12 +216,6 @@ namespace RTT * run this activity. Will not be null. */ virtual os::ThreadInterface* thread() = 0; - - /** - * Returns the current RunnableInterface for this - */ - virtual RunnableInterface* getRunner(); - }; }} diff --git a/rtt/extras/SlaveActivity.cpp b/rtt/extras/SlaveActivity.cpp index e23de7677..0c17f6f91 100644 --- a/rtt/extras/SlaveActivity.cpp +++ b/rtt/extras/SlaveActivity.cpp @@ -39,7 +39,6 @@ #include "SlaveActivity.hpp" #include "../os/MainThread.hpp" #include "Logger.hpp" -#include namespace RTT { using namespace extras; @@ -179,20 +178,8 @@ namespace RTT { bool SlaveActivity::trigger() { - ExecutionEngine *master = dynamic_cast( mmaster->getRunner() ); - ExecutionEngine * r= dynamic_cast( runner ); - if(!master || !r){ - Logger::log() << Logger::Fatal << " SlaveActivity: cannot push messages to another engine, current engine is unsupported." << Logger::endl; - return false; - }else{ - master->takeoverMessages( r ); - } - - if(mmaster) - { + if (mmaster) return mmaster->trigger(); - } - return false; } From 2a012d66432bf2ec4983ece6118fc55d6376bf62 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 1 Sep 2014 16:08:25 +0200 Subject: [PATCH 3/3] ExecutionEngine: forward incoming messages to a master ExecutionEngine if set The master ExecutionEngine is set automatically if the engine runs in a SlaveActivity whose master activity also runs an ExecutionEngine. Signed-off-by: Johannes Meyer --- rtt/ExecutionEngine.cpp | 28 +++++++++++++++++++++++++++- rtt/ExecutionEngine.hpp | 23 +++++++++++++++++++++++ rtt/extras/SlaveActivity.cpp | 5 +++++ rtt/extras/SlaveActivity.hpp | 2 ++ 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/rtt/ExecutionEngine.cpp b/rtt/ExecutionEngine.cpp index 89cf477b0..89f9129b0 100644 --- a/rtt/ExecutionEngine.cpp +++ b/rtt/ExecutionEngine.cpp @@ -45,6 +45,7 @@ #include "internal/MWSRQueue.hpp" #include "TaskContext.hpp" #include "internal/CatchConfig.hpp" +#include "extras/SlaveActivity.hpp" #include #include @@ -71,7 +72,8 @@ namespace RTT ExecutionEngine::ExecutionEngine( TaskCore* owner ) : taskc(owner), mqueue(new MWSRQueue(ORONUM_EE_MQUEUE_SIZE) ), - f_queue( new MWSRQueue(ORONUM_EE_MQUEUE_SIZE) ) + f_queue( new MWSRQueue(ORONUM_EE_MQUEUE_SIZE) ), + mmaster(0) { } @@ -244,6 +246,14 @@ namespace RTT // We only reject running functions when we're in the FatalError state. if (taskc && taskc->mTaskState == TaskCore::FatalError ) return false; + + // forward message to master ExecutionEngine if available + if (mmaster) { + bool result = mmaster->process(c); + msg_cond.broadcast(); + return result; + } + bool result = mqueue->enqueue( c ); this->getActivity()->trigger(); msg_cond.broadcast(); // required for waitAndProcessMessages() (EE thread) @@ -269,6 +279,22 @@ namespace RTT waitForMessagesInternal(pred); // same as for messages. } + void ExecutionEngine::setMaster(ExecutionEngine *master) + { + mmaster = master; + } + + void ExecutionEngine::setActivity( base::ActivityInterface* task ) + { + extras::SlaveActivity *slave_activity = dynamic_cast(task); + if (slave_activity) { + ExecutionEngine *master = dynamic_cast(slave_activity->getMaster()); + setMaster(master); + } else { + setMaster(0); + } + RTT::base::RunnableInterface::setActivity(task); + } void ExecutionEngine::waitForMessagesInternal(boost::function const& pred) { diff --git a/rtt/ExecutionEngine.hpp b/rtt/ExecutionEngine.hpp index e55bcd4d5..849929f90 100644 --- a/rtt/ExecutionEngine.hpp +++ b/rtt/ExecutionEngine.hpp @@ -182,6 +182,23 @@ namespace RTT * Set the 'owner' task in the exception state. */ void setExceptionTask(); + + /** + * Set the master ExecutionEngine. + * If set, all incoming messages are forwarded to the master. + * + * @param master The new master ExecutionEngine. + */ + void setMaster(ExecutionEngine *master); + + /** + * Overwritten version of RTT::base::RunnableInterface::setActivity(). + * This version will also set the master ExecutionEngine if the new activity is a SlaveActivity that runs an ExecutionEngine. + * + * @param task The ActivityInterface running this interface. + */ + virtual void setActivity( base::ActivityInterface* task ); + protected: /** * Call this if you wish to block on a message arriving in the Execution Engine. @@ -246,6 +263,12 @@ namespace RTT os::Mutex msg_lock; os::Condition msg_cond; + /** + * A master ExecutionEngine which should process our messages. + * This is used for ExecutionEngines running in a SlaveActivity which forward incoming messages to their master engine. + */ + ExecutionEngine *mmaster; + void processMessages(); void processFunctions(); void processChildren(); diff --git a/rtt/extras/SlaveActivity.cpp b/rtt/extras/SlaveActivity.cpp index 0c17f6f91..699eea793 100644 --- a/rtt/extras/SlaveActivity.cpp +++ b/rtt/extras/SlaveActivity.cpp @@ -94,6 +94,11 @@ namespace RTT { return mmaster ? mmaster->thread() : os::MainThread::Instance(); } + base::ActivityInterface *SlaveActivity::getMaster() const + { + return mmaster; + } + bool SlaveActivity::initialize() { return true; diff --git a/rtt/extras/SlaveActivity.hpp b/rtt/extras/SlaveActivity.hpp index bb6e871b2..c0512b875 100644 --- a/rtt/extras/SlaveActivity.hpp +++ b/rtt/extras/SlaveActivity.hpp @@ -128,6 +128,8 @@ namespace RTT os::ThreadInterface* thread(); + base::ActivityInterface *getMaster() const; + bool initialize(); void step(); void loop();