Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
rtt
extras
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
47
SequentialActivity::SequentialActivity
(
RunnableInterface
* run
/*= 0*/
)
48
:
ActivityInterface
(run), running(false), active(false)
49
{
50
}
51
52
SequentialActivity::~SequentialActivity
()
53
{
54
stop
();
55
}
56
57
Seconds
SequentialActivity::getPeriod
()
const
58
{
59
return
0.0;
60
}
61
62
bool
SequentialActivity::setPeriod
(
Seconds
s) {
63
if
( s == 0.0)
64
return
true
;
65
return
false
;
66
}
67
68
unsigned
SequentialActivity::getCpuAffinity
()
const
69
{
70
return
~0;
71
}
72
73
bool
SequentialActivity::setCpuAffinity
(
unsigned
cpu)
74
{
75
return
false
;
76
}
77
78
os::ThreadInterface
*
SequentialActivity::thread
()
79
{
80
return
os::MainThread::Instance
();
81
}
82
83
bool
SequentialActivity::initialize
()
84
{
85
return
true
;
86
}
87
88
void
SequentialActivity::step
()
89
{
90
}
91
92
bool
SequentialActivity::breakLoop
()
93
{
94
return
false
;
95
}
96
97
98
void
SequentialActivity::finalize
()
99
{
100
}
101
102
bool
SequentialActivity::start
()
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
117
bool
SequentialActivity::stop
()
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
130
bool
SequentialActivity::isRunning
()
const
131
{
132
return
running
;
133
}
134
135
bool
SequentialActivity::isPeriodic
()
const
136
{
137
return
false
;
138
}
139
140
bool
SequentialActivity::isActive
()
const
141
{
142
return
active
;
143
}
144
145
bool
SequentialActivity::trigger
()
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
) {
162
runner
->
step
();
163
runner
->
work
(
RunnableInterface::TimeOut
);
// for sequentials, every trigger is also a TimeOut...
164
}
else
{
165
this->
step
();
166
}
167
running
=
false
;
168
did_step =
true
;
169
}
else
{
170
// nop: step() is already being executed and
171
// should notice that new data is there.
172
// race: step() returns and trigger is called->
173
// trigger is ignored.
174
return
true
;
175
}
176
// mutex unlocked here.
177
}
// if the runner signals work again (ie a race occured),
178
// rety to lock.
179
while
( did_step &&
runner
->
hasWork
() );
180
return
true
;
181
}
182
return
false
;
183
}
184
185
bool
SequentialActivity::timeout
()
186
{
187
return
trigger
();
188
}
189
190
bool
SequentialActivity::execute
()
191
{
192
return
false
;
193
}
194
195
196
}
RTT::base::RunnableInterface::work
virtual void work(WorkReason reason)
Definition:
CoreRunnableInterface.cpp:71
RTT::extras::SequentialActivity::trigger
bool trigger()
Definition:
SequentialActivity.cpp:145
RTT::extras::SequentialActivity::isActive
bool isActive() const
Definition:
SequentialActivity.cpp:140
RTT::Seconds
double Seconds
Definition:
os/Time.hpp:53
RTT::os::MainThread::Instance
static ThreadInterface * Instance()
Definition:
MainThread.cpp:58
RTT::extras::SequentialActivity::getCpuAffinity
unsigned getCpuAffinity() const
Definition:
SequentialActivity.cpp:68
RTT::extras::SequentialActivity::setPeriod
bool setPeriod(Seconds s)
Definition:
SequentialActivity.cpp:62
SequentialActivity.hpp
RTT::extras::SequentialActivity::timeout
bool timeout()
Definition:
SequentialActivity.cpp:185
RTT::extras::SequentialActivity::SequentialActivity
SequentialActivity(base::RunnableInterface *run=0)
Definition:
SequentialActivity.cpp:47
RTT::base::RunnableInterface
A class for running a certain piece of code in a thread.
Definition:
RunnableInterface.hpp:69
RTT::extras::SequentialActivity::~SequentialActivity
~SequentialActivity()
Definition:
SequentialActivity.cpp:52
RTT::extras::SequentialActivity::finalize
void finalize()
Definition:
SequentialActivity.cpp:98
RTT::base::RunnableInterface::step
virtual void step()=0
RTT::extras::SequentialActivity::step
void step()
Definition:
SequentialActivity.cpp:88
RTT::base::ActivityInterface::runner
RunnableInterface * runner
Definition:
ActivityInterface.hpp:65
RTT::os::ThreadInterface
Definition:
ThreadInterface.hpp:56
RTT::extras::SequentialActivity::active
bool active
Definition:
SequentialActivity.hpp:119
RTT::extras::SequentialActivity::running
bool running
Definition:
SequentialActivity.hpp:118
RTT::extras::SequentialActivity::execution_lock
os::Mutex execution_lock
Definition:
SequentialActivity.hpp:120
RTT::base::ActivityInterface
Interface to start/stop and query a Activity.
Definition:
ActivityInterface.hpp:62
RTT::os::MutexTryLock::isSuccessful
bool isSuccessful()
Definition:
MutexLock.hpp:107
RTT::extras::SequentialActivity::isRunning
bool isRunning() const
Definition:
SequentialActivity.cpp:130
base
RTT::extras::SequentialActivity::initialize
bool initialize()
Definition:
SequentialActivity.cpp:83
RTT::extras::SequentialActivity::execute
bool execute()
Definition:
SequentialActivity.cpp:190
RTT::base::RunnableInterface::finalize
virtual void finalize()=0
RTT::os::MutexTryLock
A MutexTryLock tries to lock an Mutex object on construction and if successful, unlocks it on destruc...
Definition:
MutexLock.hpp:87
RTT::extras::SequentialActivity::stop
bool stop()
Definition:
SequentialActivity.cpp:117
RTT::base::RunnableInterface::initialize
virtual bool initialize()=0
RTT::extras::SequentialActivity::breakLoop
bool breakLoop()
Definition:
SequentialActivity.cpp:92
RTT::extras::SequentialActivity::start
bool start()
Definition:
SequentialActivity.cpp:102
RTT::extras::SequentialActivity::setCpuAffinity
bool setCpuAffinity(unsigned cpu)
Definition:
SequentialActivity.cpp:73
RTT::base::RunnableInterface::TimeOut
Definition:
RunnableInterface.hpp:76
RTT
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition:
Activity.cpp:53
RTT::extras::SequentialActivity::isPeriodic
bool isPeriodic() const
Definition:
SequentialActivity.cpp:135
RTT::extras::SequentialActivity::getPeriod
Seconds getPeriod() const
Definition:
SequentialActivity.cpp:57
RTT::base::RunnableInterface::hasWork
virtual bool hasWork()
Definition:
CoreRunnableInterface.cpp:67
RTT::extras::SequentialActivity::thread
os::ThreadInterface * thread()
Definition:
SequentialActivity.cpp:78
rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:35