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"
11#include "simulator.h"
12
13/**
14 * @file
15 * @ingroup timer
16 * ns3::Timer implementation.
17 */
18
19namespace ns3
20{
21
23
26 m_delay(),
27 m_event(),
28 m_impl(nullptr)
29{
30 NS_LOG_FUNCTION(this);
31}
32
34 : m_flags(destroyPolicy),
35 m_delay(),
36 m_event(),
37 m_impl(nullptr)
38{
39 NS_LOG_FUNCTION(this << destroyPolicy);
40}
41
43{
44 NS_LOG_FUNCTION(this);
46 {
47 if (m_event.IsPending())
48 {
49 NS_FATAL_ERROR("Event is still running while destroying.");
50 }
51 }
52 else if (m_flags & CANCEL_ON_DESTROY)
53 {
54 m_event.Cancel();
55 }
56 else if (m_flags & REMOVE_ON_DESTROY)
57 {
58 m_event.Remove();
59 }
60 delete m_impl;
61}
62
63void
65{
66 NS_LOG_FUNCTION(this << time);
67 m_delay = time;
68}
69
70Time
72{
73 NS_LOG_FUNCTION(this);
74 return m_delay;
75}
76
77Time
79{
80 NS_LOG_FUNCTION(this);
81 switch (GetState())
82 {
83 case Timer::RUNNING:
85 case Timer::EXPIRED:
86 return TimeStep(0);
88 return m_delayLeft;
89 default:
90 NS_ASSERT(false);
91 return TimeStep(0);
92 }
93}
94
95void
97{
98 NS_LOG_FUNCTION(this);
99 m_event.Cancel();
100}
101
102void
104{
105 NS_LOG_FUNCTION(this);
106 m_event.Remove();
107}
108
109bool
111{
112 NS_LOG_FUNCTION(this);
113 return !IsSuspended() && m_event.IsExpired();
114}
115
116bool
118{
119 NS_LOG_FUNCTION(this);
120 return !IsSuspended() && m_event.IsPending();
121}
122
123bool
125{
126 NS_LOG_FUNCTION(this);
128}
129
132{
133 NS_LOG_FUNCTION(this);
134 if (IsRunning())
135 {
136 return Timer::RUNNING;
137 }
138 else if (IsExpired())
139 {
140 return Timer::EXPIRED;
141 }
142 else
143 {
145 return Timer::SUSPENDED;
146 }
147}
148
149void
151{
152 NS_LOG_FUNCTION(this);
154}
155
156void
158{
159 NS_LOG_FUNCTION(this << delay);
160 NS_ASSERT(m_impl != nullptr);
161 if (m_event.IsPending())
162 {
163 NS_FATAL_ERROR("Event is still running while re-scheduling.");
164 }
165 m_event = m_impl->Schedule(delay);
166}
167
168void
170{
171 NS_LOG_FUNCTION(this);
175 {
176 m_event.Cancel();
177 }
178 else if (m_flags & REMOVE_ON_DESTROY)
179 {
180 m_event.Remove();
181 }
183}
184
185void
193
194} // namespace ns3
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition simulator.cc:200
Simulation virtual time values and global simulation resolution.
Definition nstime.h:96
Time TimeStep(uint64_t ts)
Scheduler interface.
Definition nstime.h:1478
void SetDelay(const Time &delay)
Definition timer.cc:64
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:110
Timer()
Create a timer with a default event lifetime management policy:
Definition timer.cc:24
EventId m_event
The future event scheduled to expire the timer.
Definition timer.h:245
Time GetDelayLeft() const
Definition timer.cc:78
Timer::State GetState() const
Definition timer.cc:131
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:96
Time GetDelay() const
Definition timer.cc:71
bool IsSuspended() const
Definition timer.cc:124
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:103
void Schedule()
Schedule a new event using the currently-configured delay, function, and arguments.
Definition timer.cc:150
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:186
bool IsRunning() const
Definition timer.cc:117
void Suspend()
Pause the timer and save the amount of time left until it was set to expire.
Definition timer.cc:169
#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:194
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Simulator declaration.
ns3::Timer class declaration.