Orocos Real-Time Toolkit  2.8.3
PeriodicActivity.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon May 10 19:10:29 CEST 2004 PeriodicActivity.cxx
3 
4  PeriodicActivity.cxx - description
5  -------------------
6  begin : Mon May 10 2004
7  copyright : (C) 2004 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.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 #ifdef ORO_PRAGMA_INTERFACE
39 #pragma implementation
40 #endif
41 #include "../Time.hpp"
42 #include "PeriodicActivity.hpp"
43 #include "../os/MutexLock.hpp"
44 #include "../Logger.hpp"
45 #include "TimerThread.hpp"
46 #include <cmath>
47 
48 namespace RTT {
49  using namespace extras;
50  using namespace base;
51 
53  : ActivityInterface(r), running(false), active(false),
54  thread_( TimerThread::Instance(priority,period) )
55  {
56  this->init();
57  }
58 
59  PeriodicActivity::PeriodicActivity(int scheduler, int priority, Seconds period, RunnableInterface* r )
60  : ActivityInterface(r), running(false), active(false),
61  thread_( TimerThread::Instance(scheduler, priority,period) )
62  {
63  this->init();
64  }
65 
66  PeriodicActivity::PeriodicActivity(int scheduler, int priority, Seconds period, unsigned cpu_affinity, RunnableInterface* r )
67  : ActivityInterface(r), running(false), active(false),
68  thread_( TimerThread::Instance(scheduler, priority, period, cpu_affinity) )
69  {
70  this->init();
71  }
72 
74  : ActivityInterface(r), running(false), active(false),
75  thread_( thread )
76  {
77  this->init();
78  }
79 
81  : ActivityInterface(r), running(false), active(false),
82  thread_(thread)
83  {
84  this->init();
85  }
86 
88  : ActivityInterface(r),
89  running(false), active(false),
90  thread_(thread)
91  {
92  this->init();
93  }
94 
96  {
97  stop();
98  }
99 
101  }
102 
104  {
105  if ( isActive() || !thread_ ) {
106  //Logger::log() << Logger::Error << "PeriodicActivity : already active or thread not running." << Logger::endl;
107  return false;
108  }
109  // If thread is not yet running, try to start it.
110  if ( !thread_->isRunning() && thread_->start() == false )
111  return false;
112 
113  active = true;
114  bool inError = !this->initialize();
115  if ( inError ) {
116  //Logger::log() << Logger::Error << "PeriodicActivity : initialize() returned false " << Logger::endl;
117  active = false;
118  return false;
119  }
120 
121  bool res;
122  res = thread_->addActivity( this );
123  if ( res == false ) {
124  //Logger::log() << Logger::Error << "PeriodicActivity : addActivity() returned false " << Logger::endl;
125  this->finalize();
126  active = false;
127  return false;
128  }
129 
130  running = true;
131  return true;
132  }
133 
135  {
136  if ( !isActive() ) return false;
137 
138  // since removeActivity synchronises, we do not need to mutex-lock
139  // stop()
140  if ( thread_->removeActivity( this ) ) {
141  running = false;
142  this->finalize();
143  active = false;
144  return true;
145  }
146  return false;
147  }
148 
150  {
151  return running;
152  }
153 
155  {
156  return active;
157  }
158 
160  {
161  return thread_->getPeriod();
162  }
163 
165  return false;
166  }
167 
169  {
170  return thread_->getCpuAffinity();
171  }
172 
174  {
175  return thread_->setCpuAffinity(cpu);
176  }
177 
179  if (runner != 0)
180  return runner->initialize();
181  else
182  return true;
183  }
184 
186  {
187  return false;
188  }
189 
191  {
192  return false;
193  }
194 
196  {
197  // override this method to avoid running runner.
198  if (runner != 0)
199  runner->step();
200  }
201 
203  if (runner != 0)
204  runner->finalize();
205  }
206 
208 
210  return true;
211  }
212 }
virtual Seconds getPeriod() const
Get the periodicity of this activity in Seconds.
virtual bool setPeriod(Seconds s)
Set the periodicity of this activity in Seconds.
double Seconds
Seconds are stored as a double precision float.
Definition: Time.hpp:53
This Periodic Thread is meant for executing a PeriodicActivity object periodically.
Definition: TimerThread.hpp:69
virtual bool isRunning() const
Query if the activity is initialized and executing.
virtual bool start()
Start the activity.
virtual bool setCpuAffinity(unsigned cpu)
Set the cpu affinity of this activity.
virtual 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 bool isActive() const
Query if the activity is started.
A class for running a certain piece of code in a thread.
virtual bool execute()
Execute this activity such that it executes a step or loop of the RunnableInterface.
long secs
seconds as a signed long.
Definition: Time.hpp:57
boost::shared_ptr< TimerThread > TimerThreadPtr
TimerThread objects are reference counted such that when the last PeriodicActivity which uses it is d...
Definition: TimerThread.hpp:61
TimerThreadPtr thread_
The thread which runs this activity.
virtual void step()=0
The method that will be periodically executed when this class is run in a periodic thread...
A thread which is being run.
PeriodicActivity(int priority, Seconds period, base::RunnableInterface *r=0)
Create a Periodic Activity with a given priority and period.
Interface to start/stop and query a Activity.
virtual os::ThreadInterface * thread()
Returns a pointer to the thread which will run this activity.
virtual unsigned getCpuAffinity() const
Get the cpu affinity of this activity.
virtual bool trigger()
Trigger that work has to be done.
virtual ~PeriodicActivity()
Stops and terminates a PeriodicActivity.
virtual void finalize()=0
The method that will be called after the last periodical execution of step() ( or non periodical exec...
virtual bool initialize()=0
The method that will be called before the first periodical execution of step() ( or non periodical ex...
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
long long nsecs
nanoseconds as a signed long long.
Definition: Time.hpp:69
virtual bool isPeriodic() const
Inspect if this activity is periodic.