A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
timer.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#include "timer.h"
9
10#include "log.h"
12#include "simulator.h"
13
14/**
15 * \file
16 * \ingroup timer
17 * ns3::Timer implementation.
18 */
19
20namespace ns3
21{
22
24
26 : m_flags(CHECK_ON_DESTROY),
27 m_delay(FemtoSeconds(0)),
28 m_event(),
29 m_impl(nullptr)
30{
31 NS_LOG_FUNCTION(this);
32}
33
35 : m_flags(destroyPolicy),
36 m_delay(FemtoSeconds(0)),
37 m_event(),
38 m_impl(nullptr)
39{
40 NS_LOG_FUNCTION(this << destroyPolicy);
41}
42
44{
45 NS_LOG_FUNCTION(this);
47 {
48 if (m_event.IsPending())
49 {
50 NS_FATAL_ERROR("Event is still running while destroying.");
51 }
52 }
53 else if (m_flags & CANCEL_ON_DESTROY)
54 {
56 }
57 else if (m_flags & REMOVE_ON_DESTROY)
58 {
60 }
61 delete m_impl;
62}
63
64void
66{
67 NS_LOG_FUNCTION(this << time);
68 m_delay = time;
69}
70
71Time
73{
74 NS_LOG_FUNCTION(this);
75 return m_delay;
76}
77
78Time
80{
81 NS_LOG_FUNCTION(this);
82 switch (GetState())
83 {
84 case Timer::RUNNING:
86 case Timer::EXPIRED:
87 return TimeStep(0);
89 return m_delayLeft;
90 default:
91 NS_ASSERT(false);
92 return TimeStep(0);
93 }
94}
95
96void
98{
99 NS_LOG_FUNCTION(this);
100 m_event.Cancel();
101}
102
103void
105{
106 NS_LOG_FUNCTION(this);
107 m_event.Remove();
108}
109
110bool
112{
113 NS_LOG_FUNCTION(this);
114 return !IsSuspended() && m_event.IsExpired();
115}
116
117bool
119{
120 NS_LOG_FUNCTION(this);
121 return !IsSuspended() && m_event.IsPending();
122}
123
124bool
126{
127 NS_LOG_FUNCTION(this);
129}
130
133{
134 NS_LOG_FUNCTION(this);
135 if (IsRunning())
136 {
137 return Timer::RUNNING;
138 }
139 else if (IsExpired())
140 {
141 return Timer::EXPIRED;
142 }
143 else
144 {
146 return Timer::SUSPENDED;
147 }
148}
149
150void
152{
153 NS_LOG_FUNCTION(this);
155}
156
157void
159{
160 NS_LOG_FUNCTION(this << delay);
161 NS_ASSERT(m_impl != nullptr);
162 if (m_event.IsPending())
163 {
164 NS_FATAL_ERROR("Event is still running while re-scheduling.");
165 }
166 m_event = m_impl->Schedule(delay);
167}
168
169void
171{
172 NS_LOG_FUNCTION(this);
176 {
177 m_event.Cancel();
178 }
179 else if (m_flags & REMOVE_ON_DESTROY)
180 {
181 m_event.Remove();
182 }
184}
185
186void
188{
189 NS_LOG_FUNCTION(this);
192 m_flags &= ~TIMER_SUSPENDED;
193}
194
195} // namespace ns3
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition event-id.cc:44
bool IsPending() const
This method is syntactic sugar for !IsExpired().
Definition event-id.cc:65
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition event-id.cc:58
void Remove()
This method is syntactic sugar for the ns3::Simulator::Remove method.
Definition event-id.cc:51
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition simulator.cc:206
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
void SetDelay(const Time &delay)
Definition timer.cc:65
internal::TimerImpl * m_impl
The timer implementation, which contains the bound callback function and arguments.
Definition timer.h:250
bool IsExpired() const
Definition timer.cc:111
Timer()
Create a timer with a default event lifetime management policy:
Definition timer.cc:25
EventId m_event
The future event scheduled to expire the timer.
Definition timer.h:245
Time GetDelayLeft() const
Definition timer.cc:79
Timer::State GetState() const
Definition timer.cc:132
int m_flags
Bitfield for Timer State, DestroyPolicy and InternalSuspended.
Definition timer.h:241
DestroyPolicy
The policy to use to manager the internal timer when an instance of the Timer class is destroyed or s...
Definition timer.h:80
@ CANCEL_ON_DESTROY
This policy cancels the event from the destructor of the Timer or from Suspend().
Definition timer.h:86
@ 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:97
@ REMOVE_ON_DESTROY
This policy removes the event from the simulation event list when the destructor of the Timer is invo...
Definition timer.h:92
State
The possible states of the Timer.
Definition timer.h:102
@ RUNNING
Timer is currently running.
Definition timer.h:103
@ EXPIRED
Timer has already expired.
Definition timer.h:104
@ SUSPENDED
Timer is suspended.
Definition timer.h:105
static constexpr auto TIMER_SUSPENDED
Internal bit marking the suspended timer state.
Definition timer.h:230
void Cancel()
Cancel the currently-running event if there is one.
Definition timer.cc:97
Time GetDelay() const
Definition timer.cc:72
bool IsSuspended() const
Definition timer.cc:125
Time m_delay
The delay configured for this Timer.
Definition timer.h:243
void Remove()
Remove from the simulation event-list the currently-running event if there is one.
Definition timer.cc:104
void Schedule()
Schedule a new event using the currently-configured delay, function, and arguments.
Definition timer.cc:151
Time m_delayLeft
The amount of time left on the Timer while it is suspended.
Definition timer.h:252
void Resume()
Restart the timer to expire within the amount of time left saved during Suspend.
Definition timer.cc:187
bool IsRunning() const
Definition timer.cc:118
void Suspend()
Pause the timer and save the amount of time left until it was set to expire.
Definition timer.cc:170
virtual EventId Schedule(const Time &delay)=0
Schedule the callback for a future time.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Time FemtoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1368
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::SimulationSingleton declaration and template implementation.
ns3::Simulator declaration.
ns3::Timer class declaration.