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

SlaveActivity without master ExecutionEngine (pull request) #60

Merged
merged 3 commits into from
Oct 14, 2014
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
39 changes: 27 additions & 12 deletions rtt/ExecutionEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "internal/MWSRQueue.hpp"
#include "TaskContext.hpp"
#include "internal/CatchConfig.hpp"
#include "extras/SlaveActivity.hpp"

#include <boost/bind.hpp>
#include <boost/ref.hpp>
Expand All @@ -71,7 +72,8 @@ namespace RTT
ExecutionEngine::ExecutionEngine( TaskCore* owner )
: taskc(owner),
mqueue(new MWSRQueue<DisposableInterface*>(ORONUM_EE_MQUEUE_SIZE) ),
f_queue( new MWSRQueue<ExecutableInterface*>(ORONUM_EE_MQUEUE_SIZE) )
f_queue( new MWSRQueue<ExecutableInterface*>(ORONUM_EE_MQUEUE_SIZE) ),
mmaster(0)
{
}

Expand Down Expand Up @@ -238,23 +240,20 @@ 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() ) {
// 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)
Expand All @@ -280,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<extras::SlaveActivity *>(task);
if (slave_activity) {
ExecutionEngine *master = dynamic_cast<ExecutionEngine *>(slave_activity->getMaster());
setMaster(master);
} else {
setMaster(0);
}
RTT::base::RunnableInterface::setActivity(task);
}

void ExecutionEngine::waitForMessagesInternal(boost::function<bool(void)> const& pred)
{
Expand Down
22 changes: 19 additions & 3 deletions rtt/ExecutionEngine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,20 @@ namespace RTT
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
* 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.
*/
void takeoverMessages(ExecutionEngine *remote);
virtual void setActivity( base::ActivityInterface* task );

protected:
/**
Expand Down Expand Up @@ -253,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();
Expand Down
4 changes: 0 additions & 4 deletions rtt/base/ActivityInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,4 @@ bool ActivityInterface::run(RunnableInterface* _r)
return true;
}

RunnableInterface* ActivityInterface::getRunner()
{
return runner;
}

6 changes: 0 additions & 6 deletions rtt/base/ActivityInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

};

}}
Expand Down
20 changes: 5 additions & 15 deletions rtt/extras/SlaveActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include "SlaveActivity.hpp"
#include "../os/MainThread.hpp"
#include "Logger.hpp"
#include <rtt/ExecutionEngine.hpp>

namespace RTT {
using namespace extras;
Expand Down Expand Up @@ -95,6 +94,11 @@ namespace RTT {
return mmaster ? mmaster->thread() : os::MainThread::Instance();
}

base::ActivityInterface *SlaveActivity::getMaster() const
{
return mmaster;
}

bool SlaveActivity::initialize()
{
return true;
Expand Down Expand Up @@ -179,22 +183,8 @@ namespace RTT {

bool SlaveActivity::trigger()
{
ExecutionEngine *master = 0;
if (mmaster)
master = dynamic_cast<ExecutionEngine*>( mmaster->getRunner() );
ExecutionEngine * r= dynamic_cast<ExecutionEngine*>( 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)
{
return mmaster->trigger();
}

return false;
}

Expand Down
2 changes: 2 additions & 0 deletions rtt/extras/SlaveActivity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ namespace RTT

os::ThreadInterface* thread();

base::ActivityInterface *getMaster() const;

bool initialize();
void step();
void loop();
Expand Down