RSC  0.16.0
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), done(false), logger(
36  rsc::logging::Logger::getLogger("rsc.threading.RepetitiveTask")) {
37 }
38 
40 }
41 
44 }
45 
48 }
49 
51  cancelRequest = true;
52 }
53 
55  return cancelRequest;
56 }
57 
59  return !isCancelRequested();
60 }
61 
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 
93  boost::recursive_mutex::scoped_lock lock(doneMutex);
94  done = true;
95  this->doneCondition.notify_all();
96 }
97 
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 
109  return done;
110 }
111 
113  timer.restart();
114 }
115 
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 }
#define RSCDEBUG(logger, msg)
Definition: Logger.h:217
boost::recursive_mutex doneMutex
virtual void pre()
A method called before each iteration of the task.
STL namespace.
void run()
Performs the real task work orchestrating the iterative loop with the different hook methods and so o...
friend std::ostream & operator<<(std::ostream &out, const RepetitiveTask &t)
virtual void waitDone()
Waits for the execution of this task to finish.
virtual void post()
A method called after each iteration of the task.
virtual bool isCancelRequested()
Tells whether the task was interrupted by a call to cancel.
virtual bool isDone()
Indicates whether the task finished execution, either through being canceled or through successfully ...
A task that already provides a base frame for common logic of interruptible tasks.
rsc::logging::LoggerPtr logger
virtual void cancel()
Interrupts the task.
LoggerPtr getLogger()
#define RSCTRACE(logger, msg)
Definition: Logger.h:210
virtual bool continueExec()
Tells whether the #exec method shall be called another time.
virtual void execute()=0
This method is invoked to perform the real work of the task.