Timer.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXCore::core
19  * @author Clemens Wallrath ( uagzs at student dot kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
27 #include <IceUtil/Handle.h> // for Handle
28 #include <IceUtil/Shared.h> // for Shared
29 #include <IceUtil/Thread.h> // for Thread
30 #include <IceUtil/Time.h> // for Time
31 #include <IceUtil/Timer.h> // for TimerTaskPtr, TimerPtr
32 
33 #include <condition_variable>
34 #include <mutex>
35 #include <vector> // for vector
36 
37 #include "LocalTimeServer.h" // for CallbackReceiver
38 
39 namespace armarx
40 {
41  typedef struct
42  {
43  IceUtil::TimerTaskPtr task;
45  } ScheduledTask;
46 
47  /**
48  @class Timer
49  * @brief Timer implementation with TimeServer support
50  * @ingroup VirtualTime
51  *
52  * The Timer class provides a timer following the same interface as
53  * IceUtil::Timer, but adding the option to use time from a TimeServer.
54  *
55  * Using system time for scheduling is still possible using forceSystemTime=true
56  * in the constructor.
57  */
58  class Timer :
59  public virtual IceUtil::Shared,
60  private virtual IceUtil::Thread,
61  public CallbackReceiver
62  {
63  public:
64  /**
65  * @brief constructs a new Timer and starts its execution thread.
66  * @param forceSystemTime if set to true, system time will be used even if a TimeServer is available
67  */
68  Timer(bool forceSystemTime = false);
69 
70  ~Timer() override;
71 
72  /**
73  * @brief destroys the Timer and detaches the exection thread if
74  * the calling thread is the timer thread, joins the thread otherwise
75  *
76  * The Timer must not be used to schedule anything new after a call to `destroy()`!
77  */
78  void destroy();
79 
80  /**
81  * @brief schedules a task for execution
82  * @param task the task to execute (an object extending IceUtil::TimerTask
83  * @param interval how long to wait before the task is run
84  */
85  void schedule(const IceUtil::TimerTaskPtr& task, const IceUtil::Time& interval);
86 
87  /**
88  * @brief schedules a task for repeated execution
89  * @param task the task to execute (an object extending IceUtil::TimerTask
90  * @param interval the interval in which the task is executed
91  */
92  void scheduleRepeated(const IceUtil::TimerTaskPtr& task, const IceUtil::Time& interval);
93 
94  /**
95  * @brief cancels a task, returns true if the task was successfully
96  * canceled (i.e. was found in the queue), false otherwise
97  * @param task task to cancel
98  * @return if cancelling was successful
99  */
100  bool cancel(const IceUtil::TimerTaskPtr& task);
101 
102  /**
103  * @brief wakes up the execution thread to check if a task has to run
104  */
105  void call() override;
106 
107  /**
108  * @return true if system time is used, false if VirtualTime is used.
109  */
110  bool getUseSystemTime() const;
111 
112  protected:
113  /**
114  * @brief if we are using the system time (or the TimeServer time)
115  */
117 
118  /**
119  * @brief used for waiting for a callback from the LocalTimeServer
120  */
121  std::mutex callbackWaitMutex;
122 
123  /**
124  * @brief used for waiting for a callback from the LocalTimeServer
125  */
126  std::condition_variable condWait;
127 
128  /**
129  * @brief used for locking scheduledTasks
130  */
132 
133  /**
134  * @brief set to false to stop the execution thread
135  */
136  bool running;
137 
138  /**
139  * @brief if call() has been called. Used together with condWait.
140  */
141  bool called;
142 
143  /**
144  * @brief timer for use in system time mode
145  */
147 
148  /**
149  * @brief list of scheduled tasks
150  */
151  std::vector<ScheduledTask> scheduledTasks;
152 
153  /**
154  * @brief the execution thread main method
155  */
156  void run() override;
157  };
158  /**
159  * @brief smart pointer for armarx::Timer
160  */
162 }
163 
armarx::Timer::destroy
void destroy()
destroys the Timer and detaches the exection thread if the calling thread is the timer thread,...
Definition: Timer.cpp:157
armarx::Timer::scheduledTasksMutex
std::mutex scheduledTasksMutex
used for locking scheduledTasks
Definition: Timer.h:131
armarx::ScheduledTask::task
IceUtil::TimerTaskPtr task
Definition: Timer.h:43
armarx::ScheduledTask
Definition: Timer.h:41
armarx::Timer::iceTimer
IceUtil::TimerPtr iceTimer
timer for use in system time mode
Definition: Timer.h:146
armarx::ScheduledTask::endTime
IceUtil::Time endTime
Definition: Timer.h:44
armarx::Timer::running
bool running
set to false to stop the execution thread
Definition: Timer.h:136
armarx::Timer::run
void run() override
the execution thread main method
Definition: Timer.cpp:105
armarx::Timer::schedule
void schedule(const IceUtil::TimerTaskPtr &task, const IceUtil::Time &interval)
schedules a task for execution
Definition: Timer.cpp:54
LocalTimeServer.h
armarx::Timer::callbackWaitMutex
std::mutex callbackWaitMutex
used for waiting for a callback from the LocalTimeServer
Definition: Timer.h:121
armarx::interval
Interval< T > interval(T lo, T hi)
Definition: OccupancyGrid.h:26
armarx::CallbackReceiver
Used by CallbackWaitLock.
Definition: LocalTimeServer.h:42
armarx::Timer::~Timer
~Timer() override
Definition: Timer.cpp:189
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::Timer::call
void call() override
wakes up the execution thread to check if a task has to run
Definition: Timer.cpp:143
armarx::Timer
Timer implementation with TimeServer support.
Definition: Timer.h:58
IceUtil::Handle< Timer >
armarx::Timer::scheduledTasks
std::vector< ScheduledTask > scheduledTasks
list of scheduled tasks
Definition: Timer.h:151
armarx::Timer::condWait
std::condition_variable condWait
used for waiting for a callback from the LocalTimeServer
Definition: Timer.h:126
armarx::Timer::scheduleRepeated
void scheduleRepeated(const IceUtil::TimerTaskPtr &task, const IceUtil::Time &interval)
schedules a task for repeated execution
Definition: Timer.cpp:72
armarx::Timer::called
bool called
if call() has been called.
Definition: Timer.h:141
armarx::Timer::getUseSystemTime
bool getUseSystemTime() const
Definition: Timer.cpp:152
armarx::Timer::useSystemTime
bool useSystemTime
if we are using the system time (or the TimeServer time)
Definition: Timer.h:116
armarx::Timer::cancel
bool cancel(const IceUtil::TimerTaskPtr &task)
cancels a task, returns true if the task was successfully canceled (i.e.
Definition: Timer.cpp:77
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::Timer::Timer
Timer(bool forceSystemTime=false)
constructs a new Timer and starts its execution thread.
Definition: Timer.cpp:37