RSC
0.9.0
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
RepetitiveTask.cpp
Go to the documentation of this file.
1
/* ============================================================
2
*
3
* This file is a part of the RSC project.
4
*
5
* Copyright (C) 2011 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
6
*
7
* This file may be licensed under the terms of the
8
* GNU Lesser General Public License Version 3 (the ``LGPL''),
9
* or (at your option) any later version.
10
*
11
* Software distributed under the License is distributed
12
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13
* express or implied. See the LGPL for the specific language
14
* governing rights and limitations.
15
*
16
* You should have received a copy of the LGPL along with this
17
* program. If not, go to http://www.gnu.org/licenses/lgpl.html
18
* or write to the Free Software Foundation, Inc.,
19
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
*
21
* The development of this software was supported by:
22
* CoR-Lab, Research Institute for Cognition and Robotics
23
* Bielefeld University
24
*
25
* ============================================================ */
26
27
#include "
RepetitiveTask.h
"
28
29
using namespace
std;
30
31
namespace
rsc {
32
namespace
threading {
33
34
RepetitiveTask::RepetitiveTask() :
35
cancelRequest(false), cancelled(false), done(false), logger(
36
rsc::logging::Logger::
getLogger
(
"rsc.threading.RepetitiveTask"
)) {
37
}
38
39
RepetitiveTask::~RepetitiveTask
() {
40
}
41
42
void
RepetitiveTask::pre
() {
43
timerBeforeCycle
();
44
}
45
46
void
RepetitiveTask::post
() {
47
timerAfterCycle
();
48
}
49
50
void
RepetitiveTask::cancel
() {
51
cancelRequest
=
true
;
52
}
53
54
bool
RepetitiveTask::isCancelRequested
() {
55
return
cancelRequest
;
56
}
57
58
bool
RepetitiveTask::continueExec
() {
59
return
!
isCancelRequested
();
60
}
61
62
void
RepetitiveTask::run
() {
63
64
// ensure that nothing happens in this task if we are canceled before
65
// actually being called
66
if
(
isCancelRequested
()) {
67
markDone
();
68
return
;
69
}
70
71
RSCDEBUG
(
logger
,
"run() entered"
);
72
do
{
73
74
// TODO add exception handling
75
// call pre hook
76
pre
();
77
// call template method
78
execute
();
79
// call post hook
80
post
();
81
82
RSCTRACE
(
logger
,
"run cycle done"
);
83
84
}
while
(
continueExec
());
85
86
markDone
();
87
88
RSCDEBUG
(
logger
,
"task finished"
);
89
90
}
91
92
void
RepetitiveTask::markDone
() {
93
boost::recursive_mutex::scoped_lock lock(
doneMutex
);
94
done
=
true
;
95
this->
doneCondition
.notify_all();
96
}
97
98
void
RepetitiveTask::waitDone
() {
99
RSCDEBUG
(
logger
,
"waitDone() entered"
);
100
boost::recursive_mutex::scoped_lock lock(
doneMutex
);
101
RSCDEBUG
(
logger
,
"waitDone() after lock, before wait"
);
102
while
(!
done
) {
103
this->
doneCondition
.wait(lock);
104
}
105
RSCDEBUG
(
logger
,
"waitDone() finished"
);
106
}
107
108
bool
RepetitiveTask::isDone
() {
109
return
done
;
110
}
111
112
void
RepetitiveTask::timerBeforeCycle
() {
113
timer
.restart();
114
}
115
116
void
RepetitiveTask::timerAfterCycle
() {
117
// calculate processing time for last cycle, last n cycle, variance...
118
RSCTRACE
(
logger
,
"Times (last cycle = "
<<
timer
.elapsed() <<
"s)"
);
119
}
120
121
ostream&
operator<<
(ostream& out,
const
RepetitiveTask
& t) {
122
// TODO missing content to output
123
return
out <<
"RepetitiveTask[cancelRequest="
<< t.
cancelRequest
<<
"]"
;
124
}
125
126
}
127
}
rsc
threading
RepetitiveTask.cpp
Generated on Tue Aug 6 2013 15:04:20 for RSC by
1.8.1.2