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 <condition_variable>
28#include <mutex>
29#include <vector> // for vector
30
31#include <IceUtil/Handle.h> // for Handle
32#include <IceUtil/Shared.h> // for Shared
33#include <IceUtil/Thread.h> // for Thread
34#include <IceUtil/Time.h> // for Time
35#include <IceUtil/Timer.h> // for TimerTaskPtr, TimerPtr
36
37#include "LocalTimeServer.h" // for CallbackReceiver
38
39namespace armarx
40{
41 typedef struct
42 {
43 IceUtil::TimerTaskPtr task;
44 IceUtil::Time endTime;
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 */
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 */
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 /**
160 * @brief smart pointer for armarx::Timer
161 */
163} // namespace armarx
Used by CallbackWaitLock.
Timer(bool forceSystemTime=false)
constructs a new Timer and starts its execution thread.
Definition Timer.cpp:39
std::vector< ScheduledTask > scheduledTasks
list of scheduled tasks
Definition Timer.h:151
~Timer() override
Definition Timer.cpp:202
bool useSystemTime
if we are using the system time (or the TimeServer time)
Definition Timer.h:116
std::condition_variable condWait
used for waiting for a callback from the LocalTimeServer
Definition Timer.h:126
std::mutex scheduledTasksMutex
used for locking scheduledTasks
Definition Timer.h:131
bool running
set to false to stop the execution thread
Definition Timer.h:136
void destroy()
destroys the Timer and detaches the exection thread if the calling thread is the timer thread,...
Definition Timer.cpp:170
bool called
if call() has been called.
Definition Timer.h:141
bool cancel(const IceUtil::TimerTaskPtr &task)
cancels a task, returns true if the task was successfully canceled (i.e.
Definition Timer.cpp:82
bool getUseSystemTime() const
Definition Timer.cpp:164
void call() override
wakes up the execution thread to check if a task has to run
Definition Timer.cpp:154
void run() override
the execution thread main method
Definition Timer.cpp:112
IceUtil::TimerPtr iceTimer
timer for use in system time mode
Definition Timer.h:146
std::mutex callbackWaitMutex
used for waiting for a callback from the LocalTimeServer
Definition Timer.h:121
void scheduleRepeated(const IceUtil::TimerTaskPtr &task, const IceUtil::Time &interval)
schedules a task for repeated execution
Definition Timer.cpp:76
void schedule(const IceUtil::TimerTaskPtr &task, const IceUtil::Time &interval)
schedules a task for execution
Definition Timer.cpp:57
IceUtil::Handle< Timer > TimerPtr
Definition Instance.h:25
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< Timer > TimerPtr
smart pointer for armarx::Timer
Definition Timer.h:162
Interval< T > interval(T lo, T hi)
IceUtil::Time endTime
Definition Timer.h:44
IceUtil::TimerTaskPtr task
Definition Timer.h:43