A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
default-simulator-impl.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005,2006 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8
9#ifndef DEFAULT_SIMULATOR_IMPL_H
10#define DEFAULT_SIMULATOR_IMPL_H
11
12#include "simulator-impl.h"
13
14#include <list>
15#include <mutex>
16#include <thread>
17
18/**
19 * \file
20 * \ingroup simulator
21 * ns3::DefaultSimulatorImpl declaration.
22 */
23
24namespace ns3
25{
26
27// Forward
28class Scheduler;
29
30/**
31 * \ingroup simulator
32 *
33 * The default single process simulator implementation.
34 */
36{
37 public:
38 /**
39 * Register this type.
40 * \return The object TypeId.
41 */
42 static TypeId GetTypeId();
43
44 /** Constructor. */
46 /** Destructor. */
47 ~DefaultSimulatorImpl() override;
48
49 // Inherited
50 void Destroy() override;
51 bool IsFinished() const override;
52 void Stop() override;
53 EventId Stop(const Time& delay) override;
54 EventId Schedule(const Time& delay, EventImpl* event) override;
55 void ScheduleWithContext(uint32_t context, const Time& delay, EventImpl* event) override;
56 EventId ScheduleNow(EventImpl* event) override;
57 EventId ScheduleDestroy(EventImpl* event) override;
58 void Remove(const EventId& id) override;
59 void Cancel(const EventId& id) override;
60 bool IsExpired(const EventId& id) const override;
61 void Run() override;
62 Time Now() const override;
63 Time GetDelayLeft(const EventId& id) const override;
64 Time GetMaximumSimulationTime() const override;
65 void SetScheduler(ObjectFactory schedulerFactory) override;
66 uint32_t GetSystemId() const override;
67 uint32_t GetContext() const override;
68 uint64_t GetEventCount() const override;
69
70 private:
71 void DoDispose() override;
72
73 /** Process the next event. */
74 void ProcessOneEvent();
75 /** Move events from a different context into the main event queue. */
77
78 /** Wrap an event with its execution context. */
80 {
81 /** The event context. */
83 /** Event timestamp. */
84 uint64_t timestamp;
85 /** The event implementation. */
87 };
88
89 /** Container type for the events from a different context. */
90 typedef std::list<EventWithContext> EventsWithContext;
91 /** The container of events from a different context. */
93 /**
94 * Flag \c true if all events with context have been moved to the
95 * primary event queue.
96 */
98 /** Mutex to control access to the list of events with context. */
100
101 /** Container type for the events to run at Simulator::Destroy() */
102 typedef std::list<EventId> DestroyEvents;
103 /** The container of events to run at Destroy. */
105 /** Flag calling for the end of the simulation. */
106 bool m_stop;
107 /** The event priority queue. */
109
110 /** Next event unique id. */
112 /** Unique id of the current event. */
114 /** Timestamp of the current event. */
115 uint64_t m_currentTs;
116 /** Execution context of the current event. */
118 /** The event count. */
119 uint64_t m_eventCount;
120 /**
121 * Number of events that have been inserted but not yet scheduled,
122 * not counting the Destroy events; this is used for validation
123 */
125
126 /** Main execution thread. */
127 std::thread::id m_mainThreadId;
128};
129
130} // namespace ns3
131
132#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:45
A simulation event.
Definition event-impl.h:35
Instantiate subclasses of ns3::Object.
Smart pointer class similar to boost::intrusive_ptr.
The SimulatorImpl base class.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
a unique identifier for an interface.
Definition type-id.h:48
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.