A Discrete-Event Network Simulator
API
default-simulator-impl.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005,2006 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 
20 #ifndef DEFAULT_SIMULATOR_IMPL_H
21 #define DEFAULT_SIMULATOR_IMPL_H
22 
23 #include "simulator-impl.h"
24 
25 #include <list>
26 #include <mutex>
27 #include <thread>
28 
35 namespace ns3
36 {
37 
38 // Forward
39 class Scheduler;
40 
47 {
48  public:
53  static TypeId GetTypeId();
54 
58  ~DefaultSimulatorImpl() override;
59 
60  // Inherited
61  void Destroy() override;
62  bool IsFinished() const override;
63  void Stop() override;
64  EventId Stop(const Time& delay) override;
65  EventId Schedule(const Time& delay, EventImpl* event) override;
66  void ScheduleWithContext(uint32_t context, const Time& delay, EventImpl* event) override;
67  EventId ScheduleNow(EventImpl* event) override;
68  EventId ScheduleDestroy(EventImpl* event) override;
69  void Remove(const EventId& id) override;
70  void Cancel(const EventId& id) override;
71  bool IsExpired(const EventId& id) const override;
72  void Run() override;
73  Time Now() const override;
74  Time GetDelayLeft(const EventId& id) const override;
75  Time GetMaximumSimulationTime() const override;
76  void SetScheduler(ObjectFactory schedulerFactory) override;
77  uint32_t GetSystemId() const override;
78  uint32_t GetContext() const override;
79  uint64_t GetEventCount() const override;
80 
81  private:
82  void DoDispose() override;
83 
85  void ProcessOneEvent();
88 
91  {
93  uint32_t context;
95  uint64_t timestamp;
98  };
99 
101  typedef std::list<EventWithContext> EventsWithContext;
111 
113  typedef std::list<EventId> DestroyEvents;
117  bool m_stop;
120 
122  uint32_t m_uid;
124  uint32_t m_currentUid;
126  uint64_t m_currentTs;
130  uint64_t m_eventCount;
136 
138  std::thread::id m_mainThreadId;
139 };
140 
141 } // namespace ns3
142 
143 #endif /* DEFAULT_SIMULATOR_IMPL_H */
The default single process simulator implementation.
bool m_stop
Flag calling for the end of the simulation.
bool m_eventsWithContextEmpty
Flag true if all events with context have been moved to the primary event queue.
uint32_t m_currentContext
Execution context of the current event.
void ProcessEventsWithContext()
Move events from a different context into the main event queue.
static TypeId GetTypeId()
Register this type.
void Run() override
Run the simulation.
uint32_t GetSystemId() const override
Get the system id of this simulator.
DestroyEvents m_destroyEvents
The container of events to run at Destroy.
void ScheduleWithContext(uint32_t context, const Time &delay, EventImpl *event) override
Schedule a future event execution (in a different context).
EventId ScheduleNow(EventImpl *event) override
Schedule an event to run at the current virtual time.
Ptr< Scheduler > m_events
The event priority queue.
void Remove(const EventId &id) override
Remove an event from the event list.
Time GetDelayLeft(const EventId &id) const override
Get the remaining time until this event will execute.
uint64_t m_currentTs
Timestamp of the current event.
uint64_t GetEventCount() const override
Get the number of events executed.
std::list< EventWithContext > EventsWithContext
Container type for the events from a different context.
void Cancel(const EventId &id) override
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
uint32_t GetContext() const override
Get the current simulation context.
Time Now() const override
Return the current simulation virtual time.
uint64_t m_eventCount
The event count.
void DoDispose() override
Destructor implementation.
int m_unscheduledEvents
Number of events that have been inserted but not yet scheduled, not counting the Destroy events; this...
EventId ScheduleDestroy(EventImpl *event) override
Schedule an event to run at the end of the simulation, after the Stop() time or condition has been re...
Time GetMaximumSimulationTime() const override
Get the maximum representable simulation time.
std::list< EventId > DestroyEvents
Container type for the events to run at Simulator::Destroy()
EventId Schedule(const Time &delay, EventImpl *event) override
Schedule a future event execution (in the same context).
void Destroy() override
Execute the events scheduled with ScheduleDestroy().
std::mutex m_eventsWithContextMutex
Mutex to control access to the list of events with context.
uint32_t m_uid
Next event unique id.
uint32_t m_currentUid
Unique id of the current event.
~DefaultSimulatorImpl() override
Destructor.
void Stop() override
Tell the Simulator the calling event should be the last one executed.
bool IsFinished() const override
Check if the simulation should finish.
void ProcessOneEvent()
Process the next event.
bool IsExpired(const EventId &id) const override
Check if an event has already run or been cancelled.
std::thread::id m_mainThreadId
Main execution thread.
void SetScheduler(ObjectFactory schedulerFactory) override
Set the Scheduler to be used to manage the event list.
EventsWithContext m_eventsWithContext
The container of events from a different context.
An identifier for simulation events.
Definition: event-id.h:55
A simulation event.
Definition: event-impl.h:46
Instantiate subclasses of ns3::Object.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
The SimulatorImpl base class.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::SimulatorImpl declaration.
Wrap an event with its execution context.
EventImpl * event
The event implementation.