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  return false;
198  }
199 
201  {
202  // override this method to avoid running runner.
203  if (runner != 0)
204  runner->step();
205  }
206 
208  {
209  // override this method to avoid running runner.
210  if (runner != 0)
211  runner->work(reason);
212  }
213 
215  if (runner != 0)
216  runner->finalize();
217  }
218 
220 
222  return true;
223  }
224 }
virtual void work(WorkReason reason)
double Seconds
Definition: os/Time.hpp:53
virtual bool setPeriod(Seconds s)
virtual bool isPeriodic() const
virtual os::ThreadInterface * thread()
A class for running a certain piece of code in a thread.
PeriodicActivity(int priority, Seconds period, base::RunnableInterface *r=0)
Create a Periodic Activity with a given priority and period. The default scheduler for PeriodicActivi...
long secs
Definition: os/Time.hpp:57
virtual void work(base::RunnableInterface::WorkReason reason)
boost::shared_ptr< TimerThread > TimerThreadPtr
Definition: TimerThread.hpp:61
virtual unsigned getCpuAffinity() const
Interface to start/stop and query a Activity.
virtual bool setCpuAffinity(unsigned cpu)
virtual bool isRunning() const
virtual bool initialize()=0
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
long long nsecs
Definition: os/Time.hpp:69
virtual bool isActive() const
virtual Seconds getPeriod() const


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