A Discrete-Event Network Simulator
API
wall-clock-synchronizer.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #ifndef WALL_CLOCK_CLOCK_SYNCHRONIZER_H
20 #define WALL_CLOCK_CLOCK_SYNCHRONIZER_H
21 
22 #include "synchronizer.h"
23 
24 #include <condition_variable>
25 #include <mutex>
26 
33 namespace ns3 {
34 
73 {
74 public:
79  static TypeId GetTypeId (void);
80 
84  virtual ~WallClockSynchronizer ();
85 
87  static const uint64_t US_PER_NS = (uint64_t)1000;
89  static const uint64_t US_PER_SEC = (uint64_t)1000000;
91  static const uint64_t NS_PER_SEC = (uint64_t)1000000000;
92 
93 protected:
106  bool SpinWait (uint64_t ns);
131  bool SleepWait (uint64_t ns);
132 
133  // Inherited from Synchronizer
134  virtual void DoSetOrigin (uint64_t ns);
135  virtual bool DoRealtime (void);
136  virtual uint64_t DoGetCurrentRealtime (void);
137  virtual bool DoSynchronize (uint64_t nsCurrent, uint64_t nsDelay);
138  virtual void DoSignal (void);
139  virtual void DoSetCondition (bool cond);
140  virtual int64_t DoGetDrift (uint64_t ns);
141  virtual void DoEventStart (void);
142  virtual uint64_t DoEventEnd (void);
143 
153  uint64_t DriftCorrect (uint64_t nsNow, uint64_t nsDelay);
154 
160  uint64_t GetRealtime (void);
166  uint64_t GetNormalizedRealtime (void);
167 
174  void NsToTimeval (int64_t ns, struct timeval *tv);
181  uint64_t TimevalToNs (struct timeval *tv);
182 
190  void TimevalAdd (
191  struct timeval *tv1,
192  struct timeval *tv2,
193  struct timeval *result);
194 
196  uint64_t m_jiffy;
198  uint64_t m_nsEventStart;
199 
201  std::condition_variable m_conditionVariable;
203  std::mutex m_mutex;
206 };
207 
208 } // namespace ns3
209 
210 #endif /* WALL_CLOCK_SYNCHRONIZER_H */
Base class used for synchronizing the simulation events to some real time "wall clock....
Definition: synchronizer.h:52
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...
virtual void DoSetOrigin(uint64_t ns)
Establish a correspondence between a simulation time and a wall-clock (real) time.
static const uint64_t NS_PER_SEC
Conversion constant between ns and s.
uint64_t GetNormalizedRealtime(void)
Get the current normalized real time, in ns.
bool SleepWait(uint64_t ns)
Put our process to sleep for some number of nanoseconds.
virtual ~WallClockSynchronizer()
Destructor.
void NsToTimeval(int64_t ns, struct timeval *tv)
Convert an absolute time in ns to a timeval.
virtual bool DoSynchronize(uint64_t nsCurrent, uint64_t nsDelay)
Wait until the real time is in sync with the specified simulation time.
uint64_t m_jiffy
Size of the system clock tick, as reported by clock_getres, in ns.
virtual bool DoRealtime(void)
Return true if this synchronizer is actually synchronizing to a realtime clock.
uint64_t TimevalToNs(struct timeval *tv)
Convert a timeval to absolute time, in ns.
bool m_condition
The condition state.
static const uint64_t US_PER_NS
Conversion constant between μs and ns.
virtual void DoSignal(void)
Tell a possible simulator thread waiting in the DoSynchronize method that an event has happened which...
static TypeId GetTypeId(void)
Get the registered TypeId for this class.
virtual uint64_t DoGetCurrentRealtime(void)
Retrieve the value of the origin of the underlying normalized wall clock time in Time resolution unit...
virtual int64_t DoGetDrift(uint64_t ns)
Get the drift between the real time clock used to synchronize the simulation and the current simulati...
uint64_t GetRealtime(void)
Get the current absolute real time (in ns since the epoch).
uint64_t m_nsEventStart
Time recorded by DoEventStart.
void TimevalAdd(struct timeval *tv1, struct timeval *tv2, struct timeval *result)
Add two timeval.
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.
virtual void DoEventStart(void)
Record the normalized real time at which the current event is starting execution.
virtual void DoSetCondition(bool cond)
Set the condition variable to tell a possible simulator thread waiting in the Synchronize method that...
virtual uint64_t DoEventEnd(void)
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.