A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
timer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#ifndef TIMER_H
20#define TIMER_H
21
22#include "event-id.h"
23#include "fatal-error.h"
24#include "nstime.h"
25
26/**
27 * \file
28 * \ingroup timer
29 * ns3::Timer class declaration.
30 */
31
32namespace ns3
33{
34
35/**
36 * \ingroup core
37 * \defgroup timer Virtual Time Timer and Watchdog
38 *
39 * The Timer and Watchdog objects both facilitate scheduling functions
40 * to execute a specified virtual time in the future.
41 *
42 * A Watchdog timer cannot be paused or cancelled once it has been started,
43 * however it can be lengthened (delayed). A Watchdog takes no action
44 * when it is destroyed.
45 *
46 * A Timer can be suspended, resumed, cancelled and queried for time left,
47 * but it can't be extended (except by suspending and resuming).
48 * In addition, it can be configured to take different actions when the
49 * Timer is destroyed.
50 */
51
52namespace internal
53{
54
55class TimerImpl;
56
57} // namespace internal
58
59/**
60 * \ingroup timer
61 * \brief A simple virtual Timer class
62 *
63 * A (virtual time) timer is used to hold together a delay, a function to invoke
64 * when the delay expires, and a set of arguments to pass to the function
65 * when the delay expires.
66 *
67 * A Timer can be suspended, resumed, cancelled and queried for the
68 * time left, but it can't be extended (except by suspending and
69 * resuming).
70 *
71 * A timer can also be used to enforce a set of predefined event lifetime
72 * management policies. These policies are specified at construction time
73 * and cannot be changed after.
74 *
75 * \see Watchdog for a simpler interface for a watchdog timer.
76 */
77class Timer
78{
79 public:
80 /**
81 * The policy to use to manager the internal timer when an
82 * instance of the Timer class is destroyed or suspended.
83 *
84 * In the case of suspension, only `CANCEL_ON_DESTROY` and
85 * `REMOVE_ON_DESTROY` apply.
86 *
87 * These symbols have "Destroy" in their names
88 * for historical reasons.
89 */
91 {
92 /**
93 * This policy cancels the event from the destructor of the Timer
94 * or from Suspend(). This is typically faster than `REMOVE_ON_DESTROY`
95 * but uses more memory.
96 */
98 /**
99 * This policy removes the event from the simulation event list
100 * when the destructor of the Timer is invoked, or the Timer is
101 * suspended. This is typically slower than Cancel, but frees memory.
102 */
104 /**
105 * This policy enforces a check from the destructor of the Timer
106 * to verify that the timer has already expired.
107 */
108 CHECK_ON_DESTROY = (1 << 5)
109 };
110
111 /** The possible states of the Timer. */
112 enum State
113 {
114 RUNNING, /**< Timer is currently running. */
115 EXPIRED, /**< Timer has already expired. */
116 SUSPENDED, /**< Timer is suspended. */
117 };
118
119 /**
120 * Create a timer with a default event lifetime management policy:
121 * - CHECK_ON_DESTROY
122 */
123 Timer();
124 /**
125 * \param [in] destroyPolicy the event lifetime management policies
126 * to use for destroy events
127 */
128 Timer(DestroyPolicy destroyPolicy);
129 ~Timer();
130
131 /**
132 * \tparam FN \deduced The type of the function.
133 * \param [in] fn the function
134 *
135 * Store this function in this Timer for later use by Timer::Schedule.
136 */
137 template <typename FN>
138 void SetFunction(FN fn);
139
140 /**
141 * \tparam MEM_PTR \deduced The type of the class member function.
142 * \tparam OBJ_PTR \deduced The type of the class instance pointer.
143 * \param [in] memPtr the member function pointer
144 * \param [in] objPtr the pointer to object
145 *
146 * Store this function and object in this Timer for later use by
147 * Timer::Schedule.
148 */
149 template <typename MEM_PTR, typename OBJ_PTR>
150 void SetFunction(MEM_PTR memPtr, OBJ_PTR objPtr);
151
152 /**
153 * \tparam Ts \deduced Argument types
154 * \param [in] args arguments
155 *
156 * Store these arguments in this Timer for later use by Timer::Schedule.
157 */
158 template <typename... Ts>
159 void SetArguments(Ts... args);
160
161 /**
162 * \param [in] delay The delay
163 *
164 * The next call to Schedule will schedule the timer with this delay.
165 */
166 void SetDelay(const Time& delay);
167 /**
168 * \returns The currently-configured delay for the next Schedule.
169 */
170 Time GetDelay() const;
171 /**
172 * \returns The amount of time left until this timer expires.
173 *
174 * This method returns zero if the timer is in EXPIRED state.
175 */
176 Time GetDelayLeft() const;
177 /**
178 * Cancel the currently-running event if there is one. Do nothing
179 * otherwise.
180 */
181 void Cancel();
182 /**
183 * Remove from the simulation event-list the currently-running event
184 * if there is one. Do nothing otherwise.
185 */
186 void Remove();
187 /**
188 * \return \c true if there is no currently-running event,
189 * \c false otherwise.
190 */
191 bool IsExpired() const;
192 /**
193 * \return \c true if there is a currently-running event,
194 * \c false otherwise.
195 */
196 bool IsRunning() const;
197 /**
198 * \returns \c true if this timer was suspended and not yet resumed,
199 * \c false otherwise.
200 */
201 bool IsSuspended() const;
202 /**
203 * \returns The current state of the timer.
204 */
205 Timer::State GetState() const;
206 /**
207 * Schedule a new event using the currently-configured delay, function,
208 * and arguments.
209 */
210 void Schedule();
211 /**
212 * \param [in] delay the delay to use
213 *
214 * Schedule a new event using the specified delay (ignore the delay set by
215 * Timer::SetDelay), function, and arguments.
216 */
217 void Schedule(Time delay);
218
219 /**
220 * Pause the timer and save the amount of time left until it was
221 * set to expire.
222 *
223 * Subsequently calling Resume() will restart the Timer with the
224 * remaining time.
225 *
226 * The DestroyPolicy set at construction determines
227 * whether the underlying Simulator::Event is cancelled or removed.
228 *
229 * Calling Suspend on a non-running timer is an error.
230 */
231 void Suspend();
232 /**
233 * Restart the timer to expire within the amount of time left saved
234 * during Suspend.
235 * Calling Resume without a prior call to Suspend is an error.
236 */
237 void Resume();
238
239 private:
240 /** Internal bit marking the suspended timer state */
241 static constexpr auto TIMER_SUSPENDED{1 << 7};
242
243 /**
244 * Bitfield for Timer State, DestroyPolicy and InternalSuspended.
245 *
246 * \internal
247 * The DestroyPolicy, State and InternalSuspended state are stored
248 * in this single bitfield. The State uses the low-order bits,
249 * so the other users of the bitfield have to be careful in defining
250 * their bits to avoid the State.
251 */
253 /** The delay configured for this Timer. */
255 /** The future event scheduled to expire the timer. */
257 /**
258 * The timer implementation, which contains the bound callback
259 * function and arguments.
260 */
262 /** The amount of time left on the Timer while it is suspended. */
264};
265
266} // namespace ns3
267
268/********************************************************************
269 * Implementation of the templates declared above.
270 ********************************************************************/
271
272#include "timer-impl.h"
273
274namespace ns3
275{
276
277template <typename FN>
278void
280{
281 delete m_impl;
283}
284
285template <typename MEM_PTR, typename OBJ_PTR>
286void
287Timer::SetFunction(MEM_PTR memPtr, OBJ_PTR objPtr)
288{
289 delete m_impl;
290 m_impl = internal::MakeTimerImpl(memPtr, objPtr);
291}
292
293template <typename... Ts>
294void
296{
297 if (m_impl == nullptr)
298 {
299 NS_FATAL_ERROR("You cannot set the arguments of a Timer before setting its function.");
300 return;
301 }
302 m_impl->SetArgs(args...);
303}
304
305} // namespace ns3
306
307#endif /* TIMER_H */
An identifier for simulation events.
Definition: event-id.h:55
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
A simple virtual Timer class.
Definition: timer.h:78
void SetDelay(const Time &delay)
Definition: timer.cc:76
void SetFunction(FN fn)
Definition: timer.h:279
internal::TimerImpl * m_impl
The timer implementation, which contains the bound callback function and arguments.
Definition: timer.h:261
~Timer()
Definition: timer.cc:54
bool IsExpired() const
Definition: timer.cc:122
Timer()
Create a timer with a default event lifetime management policy:
Definition: timer.cc:36
EventId m_event
The future event scheduled to expire the timer.
Definition: timer.h:256
Time GetDelayLeft() const
Definition: timer.cc:90
Timer::State GetState() const
Definition: timer.cc:143
void SetArguments(Ts... args)
Definition: timer.h:295
int m_flags
Bitfield for Timer State, DestroyPolicy and InternalSuspended.
Definition: timer.h:252
DestroyPolicy
The policy to use to manager the internal timer when an instance of the Timer class is destroyed or s...
Definition: timer.h:91
@ CANCEL_ON_DESTROY
This policy cancels the event from the destructor of the Timer or from Suspend().
Definition: timer.h:97
@ CHECK_ON_DESTROY
This policy enforces a check from the destructor of the Timer to verify that the timer has already ex...
Definition: timer.h:108
@ REMOVE_ON_DESTROY
This policy removes the event from the simulation event list when the destructor of the Timer is invo...
Definition: timer.h:103
State
The possible states of the Timer.
Definition: timer.h:113
@ RUNNING
Timer is currently running.
Definition: timer.h:114
@ EXPIRED
Timer has already expired.
Definition: timer.h:115
@ SUSPENDED
Timer is suspended.
Definition: timer.h:116
static constexpr auto TIMER_SUSPENDED
Internal bit marking the suspended timer state.
Definition: timer.h:241
void Cancel()
Cancel the currently-running event if there is one.
Definition: timer.cc:108
Time GetDelay() const
Definition: timer.cc:83
bool IsSuspended() const
Definition: timer.cc:136
Time m_delay
The delay configured for this Timer.
Definition: timer.h:254
void Remove()
Remove from the simulation event-list the currently-running event if there is one.
Definition: timer.cc:115
void Schedule()
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:162
Time m_delayLeft
The amount of time left on the Timer while it is suspended.
Definition: timer.h:263
void Resume()
Restart the timer to expire within the amount of time left saved during Suspend.
Definition: timer.cc:198
bool IsRunning() const
Definition: timer.cc:129
void Suspend()
Pause the timer and save the amount of time left until it was set to expire.
Definition: timer.cc:181
The timer implementation underlying Timer and Watchdog.
Definition: timer-impl.h:46
void SetArgs(Args... args)
Set the arguments to be used when invoking the expire function.
Definition: timer-impl.h:193
ns3::EventId declarations.
NS_FATAL_x macro definitions.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
TimerImpl * MakeTimerImpl(U(fn)(Ts...))
Make a TimerImpl from a function pointer taking varying numbers of arguments.
Definition: timer-impl.h:103
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes.
ns3::TimerImpl declaration and implementation.