21 #include "simulator.h"
22 #include "default-simulator-impl.h"
23 #include "scheduler.h"
24 #include "event-impl.h"
41 NS_OBJECT_ENSURE_REGISTERED (DefaultSimulatorImpl);
44 DefaultSimulatorImpl::GetTypeId (
void)
46 static TypeId tid = TypeId (
"ns3::DefaultSimulatorImpl")
47 .SetParent<SimulatorImpl> ()
48 .AddConstructor<DefaultSimulatorImpl> ()
53 DefaultSimulatorImpl::DefaultSimulatorImpl ()
65 m_currentContext = 0xffffffff;
66 m_unscheduledEvents = 0;
67 m_eventsWithContextEmpty =
true;
71 DefaultSimulatorImpl::~DefaultSimulatorImpl ()
80 while (!m_events->IsEmpty ())
92 while (!m_destroyEvents.empty ())
95 m_destroyEvents.pop_front ();
97 if (!ev->IsCancelled ())
112 while (!m_events->IsEmpty ())
115 scheduler->Insert (next);
118 m_events = scheduler;
129 DefaultSimulatorImpl::ProcessOneEvent (
void)
133 NS_ASSERT (next.key.m_ts >= m_currentTs);
134 m_unscheduledEvents--;
137 m_currentTs = next.key.m_ts;
138 m_currentContext = next.key.m_context;
139 m_currentUid = next.key.m_uid;
143 ProcessEventsWithContext ();
149 return m_events->IsEmpty () || m_stop;
153 DefaultSimulatorImpl::ProcessEventsWithContext (
void)
155 if (m_eventsWithContextEmpty)
161 EventsWithContext eventsWithContext;
163 CriticalSection cs (m_eventsWithContextMutex);
164 m_eventsWithContext.swap(eventsWithContext);
165 m_eventsWithContextEmpty =
true;
167 while (!eventsWithContext.empty ())
169 EventWithContext
event = eventsWithContext.front ();
170 eventsWithContext.pop_front ();
172 ev.impl =
event.event;
173 ev.key.m_ts = m_currentTs +
event.timestamp;
174 ev.key.m_context =
event.context;
175 ev.key.m_uid = m_uid;
177 m_unscheduledEvents++;
178 m_events->Insert (ev);
188 ProcessEventsWithContext ();
191 while (!m_events->IsEmpty () && !m_stop)
198 NS_ASSERT (!m_events->IsEmpty () || m_unscheduledEvents == 0);
224 Time tAbsolute = time + TimeStep (m_currentTs);
227 NS_ASSERT (tAbsolute >= TimeStep (m_currentTs));
232 ev.key.m_uid = m_uid;
234 m_unscheduledEvents++;
235 m_events->Insert (ev);
236 return EventId (event, ev.key.m_ts, ev.key.m_context, ev.key.m_uid);
246 Time tAbsolute = time + TimeStep (m_currentTs);
250 ev.key.m_context = context;
251 ev.key.m_uid = m_uid;
253 m_unscheduledEvents++;
254 m_events->Insert (ev);
259 ev.context = context;
264 m_eventsWithContext.push_back(ev);
265 m_eventsWithContextEmpty =
false;
277 ev.key.m_ts = m_currentTs;
279 ev.key.m_uid = m_uid;
281 m_unscheduledEvents++;
282 m_events->Insert (ev);
283 return EventId (event, ev.key.m_ts, ev.key.m_context, ev.key.m_uid);
292 m_destroyEvents.push_back (
id);
301 return TimeStep (m_currentTs);
313 return TimeStep (
id.GetTs () - m_currentTs);
320 if (
id.GetUid () == 2)
323 for (DestroyEvents::iterator i = m_destroyEvents.begin (); i != m_destroyEvents.end (); i++)
327 m_destroyEvents.erase (i);
338 event.impl =
id.PeekEventImpl ();
339 event.key.m_ts =
id.GetTs ();
340 event.key.m_context =
id.GetContext ();
341 event.key.m_uid =
id.GetUid ();
342 m_events->Remove (event);
343 event.impl->Cancel ();
345 event.impl->Unref ();
347 m_unscheduledEvents--;
355 id.PeekEventImpl ()->Cancel ();
362 if (ev.GetUid () == 2)
364 if (ev.PeekEventImpl () == 0 ||
370 for (DestroyEvents::const_iterator i = m_destroyEvents.begin (); i != m_destroyEvents.end (); i++)
379 if (ev.PeekEventImpl () == 0 ||
380 ev.GetTs () < m_currentTs ||
381 (ev.GetTs () == m_currentTs &&
382 ev.GetUid () <= m_currentUid) ||
398 return TimeStep (0x7fffffffffffffffLL);
404 return m_currentContext;
smart pointer class similar to boost::intrusive_ptr
bool IsPositive(void) const
#define NS_LOG_FUNCTION(parameters)
virtual void DoDispose(void)
virtual Time GetMaximumSimulationTime(void) const
virtual void SetScheduler(ObjectFactory schedulerFactory)
#define NS_ASSERT(condition)
#define NS_LOG_COMPONENT_DEFINE(name)
virtual void DoDispose(void)
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
virtual EventId ScheduleNow(EventImpl *event)
virtual uint32_t GetSystemId(void) const
virtual void Remove(const EventId &ev)
Ptr< Object > Create(void) const
A class which provides a simple way to implement a Critical Section.
virtual Time Now(void) const
#define NS_LOG_LOGIC(msg)
virtual bool IsExpired(const EventId &ev) const
virtual Time GetDelayLeft(const EventId &id) const
virtual EventId ScheduleDestroy(EventImpl *event)
static bool Equals(ThreadId id)
Compares an TharedId with the current ThreadId .
virtual void Cancel(const EventId &ev)
virtual uint32_t GetContext(void) const
int64_t GetTimeStep(void) const
virtual void ScheduleWithContext(uint32_t context, Time const &time, EventImpl *event)
#define NS_ASSERT_MSG(condition, message)
virtual EventId Schedule(Time const &time, EventImpl *event)
instantiate subclasses of ns3::Object.
virtual bool IsFinished(void) const
an identifier for simulation events.
static ThreadId Self(void)
Returns the current thread Id.