Orocos Real-Time Toolkit  2.9.0
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 #include "../os/MainThread.hpp"
41 #include "Logger.hpp"
42 
43 namespace RTT {
44  using namespace extras;
45  using namespace base;
47  :ActivityInterface(run), mmaster(master), mperiod( master->getPeriod() ), running(false), active(false)
48  {
49  }
50 
52  :ActivityInterface(run), mmaster(0), mperiod(period), running(false), active(false)
53  {
54  }
55 
57  :ActivityInterface(run), mmaster(0), mperiod(0.0), running(false), active(false)
58  {
59  }
60 
62  {
63  stop();
64  }
65 
67  {
68  if (mmaster)
69  return mmaster->getPeriod(); // master can change period.
70  return mperiod;
71  }
72 
74  if (mmaster)
75  return false; // refuse to set period if master is involved.
76  mperiod = s;
77  return true;
78  }
79 
81  {
82  if (mmaster)
83  return mmaster->getCpuAffinity();
84  return ~0;
85  }
86 
87  bool SlaveActivity::setCpuAffinity(unsigned cpu)
88  {
89  return false;
90  }
91 
93  {
94  return mmaster ? mmaster->thread() : os::MainThread::Instance();
95  }
96 
98  {
99  return mmaster;
100  }
101 
103  {
104  return true;
105  }
106 
108  {
109  }
110 
112  {
113  }
114 
116  {
117  this->step();
118  }
119 
121  {
122  return false;
123  }
124 
125 
127  {
128  }
129 
131  {
132  if (mmaster && !mmaster->isActive())
133  {
134  Logger::log() << Logger::Error << "Unable to start slave as master activity is not running" << Logger::endl;
135  return false;
136  }
137  if ( active == true )
138  {
139  Logger::log() << Logger::Error << "Unable to start slave as it is already started" << Logger::endl;
140  return false;
141  }
142 
143  active = true;
144 
145  if ( runner ? runner->initialize() : this->initialize() ) {
146  running = this->isPeriodic();
147  } else {
148  active = false;
149  }
150  return active;
151  }
152 
153 
155  {
156  if ( !active )
157  return false;
158 
159  // use breakLoop if not periodic and within loop
160  if ( this->isPeriodic() == false) {
161  if ( running && (runner ? (runner->breakLoop() == false): (this->breakLoop() == false) ) )
162  return false;
163  }
164 
165  running = false;
166  if (runner)
167  runner->finalize();
168  else
169  this->finalize();
170  active = false;
171  return true;
172  }
173 
175  {
176  return running;
177  }
178 
180  {
181  return mperiod != 0.0;
182  }
184  {
185  return active;
186  }
187 
189  {
190  if (mmaster)
191  return mmaster->trigger();
192  return false;
193  }
194 
196  {
197  if (mmaster)
198  return mmaster->timeout();
199  return false;
200  }
201 
203  {
204  // non periodic case.
205  if ( mperiod == 0.0 ) {
206  if ( !active || running )
207  return false;
208  running = true;
209  // Since we're in execute(), this is semantically forcing a TimeOut towards the runner.
210  if (runner) {
211  runner->loop();
213  } else {
214  this->loop();
216  }
217  running = false;
218  return true;
219  }
220 
221  // executing a slave is semantical identical to a timeout happening:
222  if ( running ) {
223  if (runner) {
224  runner->step();
226  } else {
227  this->step();
229  }
230  }
231  return running;
232  }
233 
234 
235 }
double Seconds
Seconds are stored as a double precision float.
Definition: Time.hpp:53
virtual bool trigger()=0
Trigger that work has to be done.
bool timeout()
Requests this Activity to wakeup and call step() + work(Timeout).
static ThreadInterface * Instance()
Return an object which represents the main thread.
Definition: MainThread.cpp:58
A class for running a certain piece of code in a thread.
virtual bool timeout()=0
Requests this Activity to wakeup and call step() + work(Timeout).
virtual bool isActive() const =0
Query if the activity is started.
~SlaveActivity()
Cleanup and notify the base::RunnableInterface that we are gone.
virtual void work(base::RunnableInterface::WorkReason reason)
bool execute()
Execute this activity such that it executes a step or loop of the RunnableInterface.
virtual void step()=0
The method that will be (periodically) executed when this object is run in an Activity.
bool isRunning() const
Query if the activity is initialized and executing.
virtual unsigned getCpuAffinity() const =0
Get the cpu affinity of this activity.
bool isPeriodic() const
Inspect if this activity is periodic.
virtual bool run(RunnableInterface *r)
Run exclusively this RunnableInterface.
A thread which is being run.
bool trigger()
Trigger that work has to be done.
Interface to start/stop and query a Activity.
static std::ostream & endl(std::ostream &__os)
Definition: Logger.cpp:383
bool isActive() const
Query if the activity is started.
bool start()
Start this slave.
Seconds getPeriod() const
Get the periodicity of this activity in Seconds.
virtual Seconds getPeriod() const =0
Get the periodicity of this activity in Seconds.
bool setPeriod(Seconds s)
Set the periodicity of this activity in Seconds.
unsigned getCpuAffinity() const
Get the cpu affinity of this activity.
virtual void finalize()=0
The method that will be called after the last periodical execution of step() ( or non periodical exec...
bool setCpuAffinity(unsigned cpu)
Set the cpu affinity of this activity.
virtual os::ThreadInterface * thread()=0
Returns a pointer to the thread which will run this activity.
virtual void loop()
The method that will be executed once when this class is run in a non periodic Activity.
static Logger & log()
As Instance(), but more userfriendly.
Definition: Logger.cpp:117
virtual bool initialize()=0
The method that will be called before the first periodical execution of step() ( or non periodical ex...
virtual bool breakLoop()
This method is called by the framework to break out of the loop() method.
os::ThreadInterface * thread()
Returns a pointer to the thread which will run this activity.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
SlaveActivity(base::ActivityInterface *master, base::RunnableInterface *run=0)
Create an activity which is the slave of master.
bool stop()
Stop the activity This will stop the activity by removing it from the &#39;run-queue&#39; of a thread or call...
virtual void work(WorkReason reason)
Identical to step() but gives a reason why the function was called.
base::ActivityInterface * getMaster() const