A Discrete-Event Network Simulator
API
wall-clock-synchronizer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 University of Washington
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 
18 #ifndef WALL_CLOCK_CLOCK_SYNCHRONIZER_H
19 #define WALL_CLOCK_CLOCK_SYNCHRONIZER_H
20 
21 #include "synchronizer.h"
22 
23 #include <condition_variable>
24 #include <mutex>
25 
32 namespace ns3
33 {
34 
69 {
70  public:
75  static TypeId GetTypeId();
76 
80  ~WallClockSynchronizer() override;
81 
83  static const uint64_t US_PER_NS = (uint64_t)1000;
85  static const uint64_t US_PER_SEC = (uint64_t)1000000;
87  static const uint64_t NS_PER_SEC = (uint64_t)1000000000;
88 
89  protected:
102  bool SpinWait(uint64_t ns);
127  bool SleepWait(uint64_t ns);
128 
129  // Inherited from Synchronizer
130  void DoSetOrigin(uint64_t ns) override;
131  bool DoRealtime() override;
132  uint64_t DoGetCurrentRealtime() override;
133  bool DoSynchronize(uint64_t nsCurrent, uint64_t nsDelay) override;
134  void DoSignal() override;
135  void DoSetCondition(bool cond) override;
136  int64_t DoGetDrift(uint64_t ns) override;
137  void DoEventStart() override;
138  uint64_t DoEventEnd() override;
139 
149  uint64_t DriftCorrect(uint64_t nsNow, uint64_t nsDelay);
150 
156  uint64_t GetRealtime();
162  uint64_t GetNormalizedRealtime();
163 
165  uint64_t m_jiffy;
167  uint64_t m_nsEventStart;
168 
170  std::condition_variable m_conditionVariable;
172  std::mutex m_mutex;
175 };
176 
177 } // namespace ns3
178 
179 #endif /* WALL_CLOCK_SYNCHRONIZER_H */
Base class used for synchronizing the simulation events to some real time "wall clock....
Definition: synchronizer.h:53
a unique identifier for an interface.
Definition: type-id.h:59
Class used for synchronizing the simulation events to a real-time "wall clock" using Posix clock func...
uint64_t GetNormalizedRealtime()
Get the current normalized real time, in ns.
static const uint64_t NS_PER_SEC
Conversion constant between ns and s.
int64_t DoGetDrift(uint64_t ns) override
Get the drift between the real time clock used to synchronize the simulation and the current simulati...
bool SleepWait(uint64_t ns)
Put our process to sleep for some number of nanoseconds.
void DoEventStart() override
Record the normalized real time at which the current event is starting execution.
bool DoSynchronize(uint64_t nsCurrent, uint64_t nsDelay) override
Wait until the real time is in sync with the specified simulation time.
void DoSetOrigin(uint64_t ns) override
Establish a correspondence between a simulation time and a wall-clock (real) time.
uint64_t m_jiffy
Size of the system clock tick, as reported by clock_getres, in ns.
void DoSignal() override
Tell a possible simulator thread waiting in the DoSynchronize method that an event has happened which...
uint64_t DoGetCurrentRealtime() override
Retrieve the value of the origin of the underlying normalized wall clock time in Time resolution unit...
static TypeId GetTypeId()
Get the registered TypeId for this class.
bool m_condition
The condition state.
static const uint64_t US_PER_NS
Conversion constant between μs and ns.
void DoSetCondition(bool cond) override
Set the condition variable to tell a possible simulator thread waiting in the Synchronize method that...
uint64_t m_nsEventStart
Time recorded by DoEventStart.
bool SpinWait(uint64_t ns)
Do a busy-wait until the normalized realtime equals the argument or the condition variable becomes tr...
static const uint64_t US_PER_SEC
Conversion constant between μs and seconds.
std::condition_variable m_conditionVariable
Condition variable for thread synchronizer.
uint64_t GetRealtime()
Get the current absolute real time (in ns since the epoch).
~WallClockSynchronizer() override
Destructor.
bool DoRealtime() override
Return true if this synchronizer is actually synchronizing to a realtime clock.
uint64_t DoEventEnd() override
Return the amount of real time elapsed since the last call to EventStart.
uint64_t DriftCorrect(uint64_t nsNow, uint64_t nsDelay)
Compute a correction to the nominal delay to account for realtime drift since the last DoSynchronize.
std::mutex m_mutex
Mutex controlling access to the condition variable.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Synchronizer declaration.