RSC  0.16.0
PeriodicTask.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of the RSC project
4  *
5  * Copyright (C) 2010 by Sebastian Wrede <swrede 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 "PeriodicTask.h"
28 
29 using namespace std;
30 
31 namespace rsc {
32 namespace threading {
33 
34 PeriodicTask::PeriodicTask(const unsigned int& ms, bool accountProcTime) :
35  cycleTime(ms), logger(
36  rsc::logging::Logger::getLogger("rsc.threading.PeriodicTask")), fixedScheduling(
37  accountProcTime), nextProcessingStart(0) {
38 }
39 
41  RSCTRACE(logger, "~PeriodicTask() entered");
42 }
43 
45  RSCTRACE(logger, "~PeriodicTask()::continueExec() entered");
46 
47  if (isCancelRequested()) {
48  return false;
49  }
50 
51  if (nextProcessingStart == 0) {
53  return true;
54  }
55 
56  // wait, give others a chance
57  if (cycleTime != 0) {
58  // TODO provide option to interrupt in cancel using boost::this_thread
59  try {
60  RSCTRACE(
61  logger,
62  "PeriodicTask()::continueExec() before thread sleep, sleeping " << cycleTime << " ms");
63  if (fixedScheduling) {
64  boost::this_thread::sleep(
65  boost::posix_time::microseconds(
68  } else {
69  boost::this_thread::sleep(
70  boost::posix_time::microseconds(cycleTime * 1000));
71  }
72  } catch (const boost::thread_interrupted& e) {
73  // TODO handle interruption somewhere directly in task. This is
74  // something all tasks should benefit of, see above TODO.
75  RSCWARN(
76  logger,
77  "PeriodicTask()::continueExec() caught boost::thread_interrupted exception");
78  return false;
79  }RSCTRACE(logger, "PeriodicTask()::continueExec() thread woke up");
80  }RSCTRACE(logger, "PeriodicTask()::continueExec() before lock");
81 
83 
84  return true;
85 }
86 
87 }
88 }
virtual bool continueExec()
Implements a waiting logic for the continuation of the repetitive task.
STL namespace.
virtual bool isCancelRequested()
Tells whether the task was interrupted by a call to cancel.
boost::uint64_t currentTimeMicros()
Returns the current system time as microseconds.
Definition: langutils.cpp:82
#define RSCWARN(logger, msg)
Definition: Logger.h:231
boost::uint64_t nextProcessingStart
Definition: PeriodicTask.h:75
LoggerPtr getLogger()
#define RSCTRACE(logger, msg)
Definition: Logger.h:210
rsc::logging::LoggerPtr logger
Definition: PeriodicTask.h:73