SlaveActivity.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jun 26 13:25:56 CEST 2006 SlaveActivity.cxx
3 
4  SlaveActivity.cxx - description
5  -------------------
6  begin : Mon June 26 2006
7  copyright : (C) 2006 Peter Soetens
8  email : peter.soetens@fmtc.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 #include "SlaveActivity.hpp"
40 
41 #include "../base/DisposableInterface.hpp"
42 #include "../os/MainThread.hpp"
43 #include "ExecutionEngine.hpp"
44 #include "Logger.hpp"
45 
46 namespace RTT {
47  using namespace extras;
48  using namespace base;
49 
51  public:
53  TriggerSlaveActivity(SlaveActivity* act) : mslave(act) {}
54  virtual void executeAndDispose() {
55  base::RunnableInterface *runner = mslave->getRunner();
56  if (runner) {
58  } else {
60  }
61  }
62  virtual void dispose() {}
63  };
64 
66  :ActivityInterface(run), mmaster(master), mperiod( master->getPeriod() ), running(false), active(false),
67  mtrigger(new TriggerSlaveActivity(this))
68  {
69  }
70 
72  :ActivityInterface(run), mmaster(0), mperiod(period), running(false), active(false),
74  {
75  }
76 
78  :ActivityInterface(run), mmaster(0), mperiod(0.0), running(false), active(false),
80  {
81  }
82 
84  {
85  stop();
86  delete mtrigger;
87  }
88 
90  {
91  if (mmaster)
92  return mmaster->getPeriod(); // master can change period.
93  return mperiod;
94  }
95 
97  if (mmaster)
98  return false; // refuse to set period if master is involved.
99  mperiod = s;
100  return true;
101  }
102 
104  {
105  if (mmaster)
106  return mmaster->getCpuAffinity();
107  return ~0;
108  }
109 
110  bool SlaveActivity::setCpuAffinity(unsigned cpu)
111  {
112  return false;
113  }
114 
116  {
118  }
119 
121  {
122  return true;
123  }
124 
126  {
127  }
128 
130  {
131  }
132 
134  {
135  this->step();
136  }
137 
139  {
140  return false;
141  }
142 
143 
145  {
146  }
147 
149  {
150  if (mmaster && !mmaster->isActive())
151  {
152  Logger::log() << Logger::Error << "Unable to start slave as master activity is not running" << Logger::endl;
153  return false;
154  }
155  if ( active == true )
156  {
157  Logger::log() << Logger::Error << "Unable to start slave as it is already started" << Logger::endl;
158  return false;
159  }
160 
161  active = true;
162 
163  if ( runner ? runner->initialize() : this->initialize() ) {
164  running = this->isPeriodic();
165  } else {
166  active = false;
167  }
168  return active;
169  }
170 
171 
173  {
174  if ( !active )
175  return false;
176 
177  // use breakLoop if not periodic and within loop
178  if ( this->isPeriodic() == false) {
179  if ( running && (runner ? (runner->breakLoop() == false): (this->breakLoop() == false) ) )
180  return false;
181  }
182 
183  running = false;
184  if (runner)
185  runner->finalize();
186  else
187  this->finalize();
188  active = false;
189  return true;
190  }
191 
193  {
194  return running;
195  }
196 
198  {
199  return mperiod != 0.0;
200  }
202  {
203  return active;
204  }
205 
207  {
208  if (!mmaster) { return false; }
209 
210  ExecutionEngine *engine = dynamic_cast<ExecutionEngine *>(mmaster->getRunner());
211  if (!engine) { return false; }
212  return engine->process(mtrigger);
213  }
214 
216  {
217  if (!mmaster) { return false; }
218  return mmaster->timeout();
219  }
220 
222  {
223  // non periodic case.
224  if ( mperiod == 0.0 ) {
225  if ( !active || running )
226  return false;
227  running = true;
228  // Since we're in execute(), this is semantically forcing a TimeOut towards the runner.
229  if (runner) {
230  runner->loop();
232  } else {
233  this->loop();
235  }
236  running = false;
237  return true;
238  }
239 
240  // executing a slave is semantical identical to a timeout happening:
241  if ( running ) {
242  if (runner) {
243  runner->step();
245  } else {
246  this->step();
248  }
249  }
250  return running;
251  }
252 
253 }
virtual void work(WorkReason reason)
double Seconds
Definition: os/Time.hpp:53
base::ActivityInterface * mmaster
static ThreadInterface * Instance()
Definition: MainThread.cpp:58
SlaveActivity(base::ActivityInterface *master, base::RunnableInterface *run=0)
virtual void executeAndDispose()
os::ThreadInterface * thread()
A class for running a certain piece of code in a thread.
virtual bool isActive() const =0
TriggerSlaveActivity(SlaveActivity *act)
virtual unsigned getCpuAffinity() const =0
virtual void work(base::RunnableInterface::WorkReason reason)
virtual bool run(RunnableInterface *r)
Interface to start/stop and query a Activity.
static std::ostream & endl(std::ostream &__os)
Definition: Logger.cpp:383
base::DisposableInterface * mtrigger
virtual Seconds getPeriod() const =0
An object that is executable and is freed after execution.
unsigned getCpuAffinity() const
virtual os::ThreadInterface * thread()=0
static Logger & log()
Definition: Logger.cpp:117
virtual bool initialize()=0
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
bool setCpuAffinity(unsigned cpu)
virtual bool process(base::DisposableInterface *c)
An base::ActivityInterface implementation which executes &#39;step&#39; upon the invocation of &#39;execute()&#39;...
Seconds getPeriod() const
virtual RunnableInterface * getRunner() const


rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:44