TaskCore.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: FMTC Tue Mar 11 21:49:25 CET 2008 TaskCore.cpp
3 
4  TaskCore.cpp - description
5  -------------------
6  begin : Tue March 11 2008
7  copyright : (C) 2008 FMTC
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 
40 #include "TaskCore.hpp"
41 #include "../ExecutionEngine.hpp"
42 #include "ActivityInterface.hpp"
43 #include "Logger.hpp"
44 #include "internal/CatchConfig.hpp"
45 #include <rtt/os/traces.h>
46 #include <cstring>
47 
48 namespace RTT {
49  using namespace detail;
50 
51  using namespace std;
52 
53  TaskCore::TaskCore(TaskState initial_state /*= Stopped*/, const std::string& name /* = std::string() */ )
54  : ee( new ExecutionEngine(this) )
55  ,mTaskState(initial_state)
56  ,mInitialState(initial_state)
57  ,mTargetState(initial_state)
58  ,mTriggerOnStart(true)
59  ,mCycleCounter(0)
60  ,mIOCounter(0)
61  ,mTimeOutCounter(0)
62  ,mTriggerCounter(0)
63  ,mName(name)
64  {
65  }
66 
68  {
69  if ( ee->getParent() == this ) {
70  delete ee;
71  }
72  // Note: calling cleanup() here has no use or even dangerous, as
73  // cleanupHook() is a virtual function and the user code is already
74  // destroyed. The user's subclass is responsible to make this state
75  // transition in its destructor if required.
76  }
77 
79  return mTaskState;
80  }
81 
83  return mTargetState;
84  }
85 
87  {
88  return this->engine()->getActivity() && this->engine()->getActivity()->execute();
89  }
90 
92  {
93  return this->engine()->getActivity() && this->engine()->getActivity()->timeout();
94  }
95 
98  TRY(
100  bool successful;
101  { tracepoint_context(orocos_rtt, TaskContext_configureHook, mName.c_str());
102  successful = configureHook(); }
103  if (successful) {
104  if (mTaskState != Stopped && (mTaskState == mTargetState)) {
105  log(Error) << "in configure(): state has been changed inside the configureHook" << endlog();
106  log(Error) << " but configureHook returned true. Bailing out." << endlog();
107  exception();
108  return false;
109  }
110  else {
112  return true;
113  }
114  } else {
116  return false;
117  }
118  ) CATCH(std::exception const& e,
119  log(Error) << "in configure(): switching to exception state because of unhandled exception" << endlog();
120  log(Error) << " " << e.what() << endlog();
121  exception();
122  ) CATCH_ALL(
123  log(Error) << "in configure(): switching to exception state because of unhandled exception" << endlog();
124  exception();
125  )
126  }
127  return false; // no configure when running.
128  }
129 
131  if ( mTaskState == Stopped ) {
132  TRY(
134  { tracepoint_context(orocos_rtt, TaskContext_cleanupHook, mName.c_str());
135  cleanupHook(); }
136  if (mTaskState == Stopped)
138  return true;
139  ) CATCH(std::exception const& e,
140  log(Error) << "in cleanup(): switching to exception state because of unhandled exception" << endlog();
141  log(Error) << " " << e.what() << endlog();
142  exception();
143  ) CATCH_ALL (
144  log(Error) << "in cleanup(): switching to exception state because of unhandled exception" << endlog();
145  exception();
146  )
147  }
148  return false; // no cleanup when running or not configured.
149  }
150 
153  this->engine()->getActivity() && engine()->getActivity()->stop();
154  }
155 
157  // detects error() from within start():
158  if (mTargetState < Running)
159  return;
161  }
162 
164  //log(Error) <<"Exception happend in TaskCore."<<endlog();
165  TaskState copy = mTaskState;
167  TRY (
168  if ( copy >= Running ) {
169  { tracepoint_context(orocos_rtt, TaskContext_stopHook, mName.c_str());
170  stopHook(); }
171  }
172  if ( copy >= Stopped && mInitialState == PreOperational ) {
173  { tracepoint_context(orocos_rtt, TaskContext_cleanupHook, mName.c_str());
174  cleanupHook(); }
175  }
176  exceptionHook();
177  ) CATCH(std::exception const& e,
178  log(RTT::Error) << "stopHook(), cleanupHook() and/or exceptionHook() raised " << e.what() << ", going into Fatal" << endlog();
179  fatal();
180  ) CATCH_ALL (
181  log(Error) << "stopHook(), cleanupHook() and/or exceptionHook() raised an exception, going into Fatal" << endlog();
182  fatal();
183  )
184  }
185 
187  if ( mTaskState == Exception ) {
189  return true;
190  }
193  return true;
194  }
195  return false;
196  }
197 
199  if ( mTaskState == Stopped ) {
200  TRY (
202  bool successful;
203  { tracepoint_context(orocos_rtt, TaskContext_startHook, mName.c_str());
204  successful = startHook(); }
205  if (successful) {
206  if (mTaskState != Running && (mTargetState == mTaskState)) {
207  log(Error) << "in start(): state has been changed inside the startHook" << endlog();
208  log(Error) << " but startHook returned true. Bailing out." << endlog();
209  exception();
210  return false;
211  }
212  else {
214  if ( mTriggerOnStart )
215  trigger(); // triggers updateHook() in case of non periodic!
216  return true;
217  }
218  }
220  ) CATCH(std::exception const& e,
221  log(Error) << "in start(): switching to exception state because of unhandled exception" << endlog();
222  log(Error) << " " << e.what() << endlog();
223  exception();
224  ) CATCH_ALL (
225  log(Error) << "in start(): switching to exception state because of unhandled exception" << endlog();
226  exception();
227  )
228  }
229  return false;
230  }
231 
232  bool TaskCore::stop() {
233  TaskState orig = mTaskState;
234  if ( mTaskState >= Running ) {
235  TRY(
237  if ( engine()->stopTask(this) ) {
238  { tracepoint_context(orocos_rtt, TaskContext_stopHook, mName.c_str());
239  stopHook(); }
241  return true;
242  } else {
243  mTaskState = orig;
244  mTargetState = orig;
245  }
246  ) CATCH(std::exception const& e,
247  log(Error) << "in stop(): switching to exception state because of unhandled exception" << endlog();
248  log(Error) << " " << e.what() << endlog();
249  exception();
250  ) CATCH_ALL (
251  log(Error) << "in stop(): switching to exception state because of unhandled exception" << endlog();
252  exception();
253  )
254  }
255  return false;
256  }
257 
259  this->engine()->getActivity() && this->engine()->getActivity()->start();
260  return isActive();
261  }
262 
264  }
265 
266  bool TaskCore::isRunning() const {
267  return mTaskState >= Running;
268  }
269 
270  bool TaskCore::isConfigured() const {
271  return mTaskState >= Stopped;
272  }
273 
274  bool TaskCore::inFatalError() const {
275  return mTaskState == FatalError;
276  }
277 
278  bool TaskCore::inException() const {
279  return mTaskState == Exception;
280  }
281 
283  return mTaskState == RunTimeError;
284  }
285 
286  bool TaskCore::isActive() const
287  {
288  return this->engine()->getActivity() && this->engine()->getActivity()->isActive();
289  }
290 
292  {
293  return this->engine()->getActivity() ? this->engine()->getActivity()->getPeriod() : -1.0;
294  }
295 
297  {
298  return this->engine()->getActivity() && this->engine()->getActivity()->setPeriod(s);
299  }
300 
301  unsigned TaskCore::getCpuAffinity() const
302  {
303  return this->engine()->getActivity() ? this->engine()->getActivity()->getCpuAffinity() : ~0;
304  }
305 
306  bool TaskCore::setCpuAffinity(unsigned cpu)
307  {
308  return this->engine()->getActivity() && this->engine()->getActivity()->setCpuAffinity(cpu);
309  }
310 
312  return true;
313  }
314 
316  {
317  return true;
318  }
319 
321  }
322 
324  {
325  }
326 
328  {
329  return true;
330  }
331 
333  }
334 
336  {
337  }
338 }
virtual bool setPeriod(Seconds s)=0
double Seconds
Definition: os/Time.hpp:53
#define TRY(C)
Contains static global configuration variables and cached entries.
Definition: CatchConfig.hpp:56
The state indicating the component encountered a C++ exception.
Definition: TaskCore.hpp:103
ActivityInterface * getActivity() const
Query for the task this interface is run in.
virtual bool trigger()
Definition: TaskCore.cpp:91
virtual bool configureHook()
Definition: TaskCore.cpp:311
virtual void updateHook()
Definition: TaskCore.cpp:323
TaskState const mInitialState
Definition: TaskCore.hpp:453
virtual bool recover()
Definition: TaskCore.cpp:186
Definition: mystd.hpp:163
virtual TaskState getTargetState() const
Definition: TaskCore.cpp:82
virtual bool startHook()
Definition: TaskCore.cpp:315
virtual bool isActive() const =0
TaskState mTaskState
Definition: TaskCore.hpp:446
The state indicating that a run-time error has occured [red] and needs attention. ...
Definition: TaskCore.hpp:106
virtual bool isConfigured() const
Definition: TaskCore.cpp:270
virtual bool activate()
Definition: TaskCore.cpp:258
virtual bool setCpuAffinity(unsigned cpu)
Definition: TaskCore.cpp:306
virtual void stopHook()
Definition: TaskCore.cpp:335
virtual bool isRunning() const
Definition: TaskCore.cpp:266
virtual bool configure()
Definition: TaskCore.cpp:96
virtual unsigned getCpuAffinity() const =0
The state indicating additional configuration is required.
Definition: TaskCore.hpp:101
virtual void error()
Definition: TaskCore.cpp:156
virtual Seconds getPeriod() const
Definition: TaskCore.cpp:291
virtual bool inException() const
Definition: TaskCore.cpp:278
#define CATCH(T, C)
Definition: CatchConfig.hpp:57
virtual bool inFatalError() const
Definition: TaskCore.cpp:274
virtual void errorHook()
Definition: TaskCore.cpp:320
TaskCore(TaskState initial_state=Stopped, const std::string &name=std::string())
Definition: TaskCore.cpp:53
virtual bool update()
Definition: TaskCore.cpp:86
virtual bool setCpuAffinity(unsigned cpu)=0
The state indicating the component encountered a fatal error and is unable to execute.
Definition: TaskCore.hpp:102
virtual void exceptionHook()
Definition: TaskCore.cpp:332
virtual void fatal()
Definition: TaskCore.cpp:151
virtual void exception()
Definition: TaskCore.cpp:163
std::string mName
Definition: TaskCore.hpp:493
#define CATCH_ALL(C)
Definition: CatchConfig.hpp:58
base::TaskCore * getParent()
virtual Seconds getPeriod() const =0
virtual bool breakUpdateHook()
Definition: TaskCore.cpp:327
virtual TaskState getTaskState() const
Definition: TaskCore.cpp:78
virtual bool isActive() const
Definition: TaskCore.cpp:286
virtual void cleanupHook()
Definition: TaskCore.cpp:263
virtual bool inRunTimeError() const
Definition: TaskCore.cpp:282
The state indicating the component is running [green].
Definition: TaskCore.hpp:105
virtual ~TaskCore()
Definition: TaskCore.cpp:67
virtual unsigned getCpuAffinity() const
Definition: TaskCore.cpp:301
#define tracepoint_context(provider, event, name)
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
The state indicating the component is ready to run.
Definition: TaskCore.hpp:104
virtual bool stop()
Definition: TaskCore.cpp:232
TaskState mTargetState
Definition: TaskCore.hpp:458
ExecutionEngine * ee
Definition: TaskCore.hpp:444
static Logger & log()
Definition: Logger.hpp:350
virtual bool setPeriod(Seconds s)
Definition: TaskCore.cpp:296
const ExecutionEngine * engine() const
Definition: TaskCore.hpp:306
static Logger::LogFunction endlog()
Definition: Logger.hpp:362
virtual bool cleanup()
Definition: TaskCore.cpp:130
virtual bool start()
Definition: TaskCore.cpp:198


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:37