Orocos Real-Time Toolkit  2.8.3
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  this->step();
114  }
115 
117  {
118  return false;
119  }
120 
121 
123  {
124  }
125 
127  {
128  if (mmaster && !mmaster->isActive())
129  {
130  Logger::log() << Logger::Error << "Unable to start slave as master activity is not running" << Logger::endl;
131  return false;
132  }
133  if ( active == true )
134  {
135  Logger::log() << Logger::Error << "Unable to start slave as it is already started" << Logger::endl;
136  return false;
137  }
138 
139  active = true;
140 
141  if ( runner ? runner->initialize() : this->initialize() ) {
142  running = this->isPeriodic();
143  } else {
144  active = false;
145  }
146  return active;
147  }
148 
149 
151  {
152  if ( !active )
153  return false;
154 
155  // use breakLoop if not periodic and within loop
156  if ( this->isPeriodic() == false) {
157  if ( running && (runner ? (runner->breakLoop() == false): (this->breakLoop() == false) ) )
158  return false;
159  }
160 
161  running = false;
162  if (runner)
163  runner->finalize();
164  else
165  this->finalize();
166  active = false;
167  return true;
168  }
169 
171  {
172  return running;
173  }
174 
176  {
177  return mperiod != 0.0;
178  }
180  {
181  return active;
182  }
183 
185  {
186  if (mmaster)
187  return mmaster->trigger();
188  return false;
189  }
190 
192  {
193  // non periodic case.
194  if ( mperiod == 0.0 ) {
195  if ( !active || running )
196  return false;
197  running = true;
198  if (runner)
199  runner->loop();
200  else
201  this->loop();
202  running = false;
203  return true;
204  }
205 
206  if ( running ) {
207  if (runner) runner->step(); else this->step();
208  }
209  return running;
210  }
211 
212 
213 }
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.
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 isActive() const =0
Query if the activity is started.
~SlaveActivity()
Cleanup and notify the base::RunnableInterface that we are gone.
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 class is run in a periodic thread...
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 thread.
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:51
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...
base::ActivityInterface * getMaster() const