Orocos Real-Time Toolkit  2.8.3
Activity.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Oct 22 11:59:07 CEST 2009 Activity.cpp
3 
4  Activity.cpp - description
5  -------------------
6  begin : Thu October 22 2009
7  copyright : (C) 2009 Peter Soetens
8  email : peter@thesourcworks.com
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 #ifdef ORO_PRAGMA_INTERFACE
41 #pragma implementation
42 #endif
43 #include "Time.hpp"
44 #include "Activity.hpp"
45 #include "os/MutexLock.hpp"
46 #include "Logger.hpp"
47 #include "rtt-fwd.hpp"
48 
49 #include <cmath>
50 
51 namespace RTT
52 {
53  using namespace detail;
54 
55  Activity::Activity(RunnableInterface* _r, const std::string& name )
56  : ActivityInterface(_r), os::Thread(ORO_SCHED_OTHER, RTT::os::LowestPriority, 0.0, 0, name )
57  {
58  }
59 
60  Activity::Activity(int priority, RunnableInterface* r, const std::string& name )
61  : ActivityInterface(r), os::Thread(ORO_SCHED_RT, priority, 0.0, 0, name )
62  {
63  }
64 
65  Activity::Activity(int priority, Seconds period, RunnableInterface* r, const std::string& name )
66  : ActivityInterface(r), os::Thread(ORO_SCHED_RT, priority, period, 0, name )
67  {
68  }
69 
70  Activity::Activity(int scheduler, int priority, RunnableInterface* r, const std::string& name )
71  : ActivityInterface(r), os::Thread(scheduler, priority, 0.0, 0, name )
72  {
73  }
74 
75  Activity::Activity(int scheduler, int priority, Seconds period, RunnableInterface* r, const std::string& name )
76  : ActivityInterface(r), os::Thread(scheduler, priority, period, 0, name )
77  {
78  }
79 
80  Activity::Activity(int scheduler, int priority, Seconds period, unsigned cpu_affinity, RunnableInterface* r, const std::string& name )
81  : ActivityInterface(r), os::Thread(scheduler, priority, period, cpu_affinity, name )
82  {
83  }
84 
86  {
87  stop();
88  }
89 
91  return this;
92  }
93 
95  if ( runner )
96  return runner->initialize();
97  return true;
98  }
99 
100  void Activity::step() {
101  if ( runner )
102  runner->step();
103  }
104 
105  void Activity::loop() {
106  if ( runner )
107  runner->loop();
108  else
109  this->step();
110  }
111 
113  if ( runner )
114  return runner->breakLoop();
115  return false;
116  }
117 
119  if ( runner )
120  runner->finalize();
121  }
122 
123 
125  return Thread::start();
126  }
127 
128  bool Activity::stop() {
129  return Thread::stop();
130  }
131 
133  return Thread::isActive() ? Thread::start() : false;
134  }
135 
137  return false;
138  }
139 
140  bool Activity::isRunning() const {
141  return Thread::isRunning();
142  }
143 
144  bool Activity::isActive() const {
145  return Thread::isActive();
146  }
147 
149  {
150  return Thread::getPeriod();
151  }
152 
154  {
155  return Thread::setPeriod(period);
156  }
157 
158 
159  bool Activity::isPeriodic() const {
160  return Thread::isPeriodic();
161  }
162 
163  unsigned Activity::getCpuAffinity() const
164  {
165  return Thread::getCpuAffinity();
166  }
167 
168  bool Activity::setCpuAffinity(unsigned cpu)
169  {
170  return Thread::setCpuAffinity(cpu);
171  }
172 
173 }
virtual void loop()
Definition: Activity.cpp:105
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...
Definition: Activity.cpp:128
double Seconds
Seconds are stored as a double precision float.
Definition: Time.hpp:53
virtual Seconds getPeriod() const
Get the periodicity of this activity in Seconds.
Definition: Activity.cpp:148
virtual bool isPeriodic() const
Inspect if this activity is periodic.
Definition: Activity.cpp:159
virtual bool initialize()
Definition: Activity.cpp:94
A Thread object executes user code in its own thread.
Definition: Thread.hpp:109
virtual void finalize()
Definition: Activity.cpp:118
Activity(base::RunnableInterface *r=0, const std::string &name="Activity")
Create a not real-time Activity.
Definition: Activity.cpp:55
A class for running a certain piece of code in a thread.
virtual bool setCpuAffinity(unsigned cpu_affinity)
Set cpu affinity for this thread.
Definition: Thread.cpp:601
virtual void step()=0
The method that will be periodically executed when this class is run in a periodic thread...
virtual bool setPeriod(Seconds period)
Set the periodicity of this activity in Seconds.
Definition: Activity.cpp:153
virtual unsigned getCpuAffinity() const
Get the cpu affinity of this activity.
Definition: Activity.cpp:163
A thread which is being run.
virtual bool breakLoop()
Definition: Activity.cpp:112
virtual void step()
Definition: Activity.cpp:100
virtual bool isActive() const
Returns whether the thread is active.
Definition: Thread.cpp:464
Interface to start/stop and query a Activity.
virtual bool execute()
Execute this activity such that it executes a step or loop of the RunnableInterface.
Definition: Activity.cpp:136
virtual unsigned getCpuAffinity() const
Definition: Thread.cpp:606
virtual bool isRunning() const
Query if the activity is initialized and executing.
Definition: Activity.cpp:140
virtual void finalize()=0
The method that will be called after the last periodical execution of step() ( or non periodical exec...
virtual ~Activity()
Stops and terminates a Activity.
Definition: Activity.cpp:85
bool setPeriod(Seconds s)
Set the periodicity in Seconds.
Definition: Thread.cpp:536
virtual void loop()
The method that will be executed once when this class is run in a non periodic thread.
virtual bool initialize()=0
The method that will be called before the first periodical execution of step() ( or non periodical ex...
virtual bool start()
Start the Thread.
Definition: Thread.cpp:338
const int LowestPriority
An integer denoting the lowest priority of the selected OS.
Definition: ecosthreads.cpp:44
#define ORO_SCHED_RT
Definition: fosi.h:49
virtual bool breakLoop()
This method is called by the framework to break out of the loop() method.
virtual bool stop()
Stop the Thread.
Definition: Thread.cpp:417
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
virtual bool start()
Start the activity.
Definition: Activity.cpp:124
virtual bool setCpuAffinity(unsigned cpu)
Set the cpu affinity of this activity.
Definition: Activity.cpp:168
virtual bool trigger()
Trigger that work has to be done.
Definition: Activity.cpp:132
virtual Seconds getPeriod() const
Get the periodicity in Seconds.
Definition: Thread.cpp:591
virtual os::ThreadInterface * thread()
Returns a pointer to the thread which will run this activity.
Definition: Activity.cpp:90
#define ORO_SCHED_OTHER
Definition: fosi.h:50
virtual bool isPeriodic() const
Definition: Thread.cpp:581
virtual bool isRunning() const
Returns whether the thread is running.
Definition: Thread.cpp:459
virtual bool isActive() const
Query if the activity is started.
Definition: Activity.cpp:144