Orocos Real-Time Toolkit  2.8.3
SequentialActivity.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Oct 22 11:59:08 CEST 2009 SequentialActivity.cpp
3 
4  SequentialActivity.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 #include "SequentialActivity.hpp"
40 #include "../os/MainThread.hpp"
41 #include "../os/MutexLock.hpp"
42 
43 namespace RTT {
44  using namespace extras;
45  using namespace base;
46 
48  : ActivityInterface(run), running(false), active(false)
49  {
50  }
51 
53  {
54  stop();
55  }
56 
58  {
59  return 0.0;
60  }
61 
63  if ( s == 0.0)
64  return true;
65  return false;
66  }
67 
69  {
70  return ~0;
71  }
72 
74  {
75  return false;
76  }
77 
79  {
80  return os::MainThread::Instance();
81  }
82 
84  {
85  return true;
86  }
87 
89  {
90  }
91 
93  {
94  return false;
95  }
96 
97 
99  {
100  }
101 
103  {
104  if (active == true )
105  return false;
106 
107  active = true;
108 
109  if ( runner ? runner->initialize() : this->initialize() ) {
110  } else {
111  active = false;
112  }
113  return active;
114  }
115 
116 
118  {
119  if ( !active )
120  return false;
121 
122  if (runner)
123  runner->finalize();
124  else
125  this->finalize();
126  active = false;
127  return true;
128  }
129 
131  {
132  return running;
133  }
134 
136  {
137  return false;
138  }
139 
141  {
142  return active;
143  }
144 
146  {
147  // This function may recurse, in which case it returns true.
148  // We could also rely on the MutexTryLock to fail, but in
149  // case an OS only has recursive mutexes, we'd need to
150  // check running anyway before calling runner->step(). So
151  // we moved that piece of code up front.
152  // The other thread will complete the work. (hasWork).
153  if (running)
154  return true;
155  if ( active ) {
156  bool did_step = false;
157  do {
158  os::MutexTryLock lock(execution_lock);
159  if ( lock.isSuccessful() ) {
160  running = true;
161  if (runner) runner->step(); else this->step();
162  running = false;
163  did_step = true;
164  } else {
165  // nop: step() is already being executed and
166  // should notice that new data is there.
167  // race: step() returns and trigger is called->
168  // trigger is ignored.
169  return true;
170  }
171  // mutex unlocked here.
172  } // if the runner signals work again (ie a race occured),
173  // rety to lock.
174  while ( did_step && runner->hasWork() );
175  return true;
176  }
177  return false;
178  }
179 
181  {
182  return false;
183  }
184 
185 
186 }
bool trigger()
Trigger that work has to be done.
bool isActive() const
Query if the activity is started.
double Seconds
Seconds are stored as a double precision float.
Definition: Time.hpp:53
static ThreadInterface * Instance()
Return an object which represents the main thread.
Definition: MainThread.cpp:58
unsigned getCpuAffinity() const
Get the cpu affinity of this activity.
bool setPeriod(Seconds s)
Set the periodicity of this activity in Seconds.
SequentialActivity(base::RunnableInterface *run=0)
Create an activity which is the Sequential.
A class for running a certain piece of code in a thread.
~SequentialActivity()
Cleanup and notify the base::RunnableInterface that we are gone.
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.
virtual bool hasWork()
This method is for 'intelligent' activity implementations that wish to see if it is required to call ...
Interface to start/stop and query a Activity.
bool isSuccessful()
Return if the locking of the Mutex was succesfull.
Definition: MutexLock.hpp:107
bool isRunning() const
Query if the activity is initialized and executing.
bool execute()
Execute this activity such that it executes a step or loop of the RunnableInterface.
virtual void finalize()=0
The method that will be called after the last periodical execution of step() ( or non periodical exec...
A MutexTryLock tries to lock an Mutex object on construction and if successful, unlocks it on destruc...
Definition: MutexLock.hpp:87
bool stop()
Stop the activity This will stop the activity by removing it from the 'run-queue' of a thread or call...
virtual bool initialize()=0
The method that will be called before the first periodical execution of step() ( or non periodical ex...
bool start()
Start the activity.
bool setCpuAffinity(unsigned cpu)
Set the cpu affinity of this activity.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
bool isPeriodic() const
Inspect if this activity is periodic.
Seconds getPeriod() const
Get the periodicity of this activity in Seconds.
os::ThreadInterface * thread()
Returns a pointer to the thread which will run this activity.