2008-07-17 23:52:59 -07:00
|
|
|
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2005,2006 INRIA
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
|
* published by the Free Software Foundation;
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*
|
|
|
|
|
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "simulator.h"
|
2008-07-18 21:51:31 -07:00
|
|
|
#include "default-simulator-impl.h"
|
2008-07-17 23:52:59 -07:00
|
|
|
#include "scheduler.h"
|
|
|
|
|
#include "event-impl.h"
|
|
|
|
|
|
|
|
|
|
#include "ns3/ptr.h"
|
|
|
|
|
#include "ns3/pointer.h"
|
|
|
|
|
#include "ns3/assert.h"
|
|
|
|
|
#include "ns3/log.h"
|
|
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
2008-07-18 21:51:31 -07:00
|
|
|
NS_LOG_COMPONENT_DEFINE ("DefaultSimulatorImpl");
|
2008-07-17 23:52:59 -07:00
|
|
|
|
|
|
|
|
namespace ns3 {
|
|
|
|
|
|
2008-07-18 21:51:31 -07:00
|
|
|
NS_OBJECT_ENSURE_REGISTERED (DefaultSimulatorImpl);
|
2008-07-17 23:52:59 -07:00
|
|
|
|
|
|
|
|
TypeId
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::GetTypeId (void)
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
2008-07-18 21:51:31 -07:00
|
|
|
static TypeId tid = TypeId ("ns3::DefaultSimulatorImpl")
|
2008-07-17 23:52:59 -07:00
|
|
|
.SetParent<Object> ()
|
2008-07-18 21:51:31 -07:00
|
|
|
.AddConstructor<DefaultSimulatorImpl> ()
|
2008-07-17 23:52:59 -07:00
|
|
|
;
|
|
|
|
|
return tid;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::DefaultSimulatorImpl ()
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
2008-08-26 15:34:57 -07:00
|
|
|
// No multithreaded stuff here, make sure EventImpl instances don't try and
|
|
|
|
|
// use any stale locking functions.
|
|
|
|
|
EventImpl::SetNoEventLock ();
|
|
|
|
|
|
2008-07-17 23:52:59 -07:00
|
|
|
m_stop = false;
|
|
|
|
|
m_stopAt = 0;
|
|
|
|
|
// uids are allocated from 4.
|
|
|
|
|
// uid 0 is "invalid" events
|
|
|
|
|
// uid 1 is "now" events
|
|
|
|
|
// uid 2 is "destroy" events
|
|
|
|
|
m_uid = 4;
|
|
|
|
|
// before ::Run is entered, the m_currentUid will be zero
|
|
|
|
|
m_currentUid = 0;
|
|
|
|
|
m_currentTs = 0;
|
|
|
|
|
m_unscheduledEvents = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::~DefaultSimulatorImpl ()
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
while (!m_events->IsEmpty ())
|
|
|
|
|
{
|
|
|
|
|
EventId next = m_events->RemoveNext ();
|
|
|
|
|
}
|
|
|
|
|
m_events = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::Destroy ()
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
while (!m_destroyEvents.empty ())
|
|
|
|
|
{
|
|
|
|
|
Ptr<EventImpl> ev = m_destroyEvents.front ().PeekEventImpl ();
|
|
|
|
|
m_destroyEvents.pop_front ();
|
|
|
|
|
NS_LOG_LOGIC ("handle destroy " << ev);
|
|
|
|
|
if (!ev->IsCancelled ())
|
|
|
|
|
{
|
|
|
|
|
ev->Invoke ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::SetScheduler (Ptr<Scheduler> scheduler)
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
if (m_events != 0)
|
|
|
|
|
{
|
|
|
|
|
while (!m_events->IsEmpty ())
|
|
|
|
|
{
|
|
|
|
|
EventId next = m_events->RemoveNext ();
|
|
|
|
|
scheduler->Insert (next);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m_events = scheduler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ptr<Scheduler>
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::GetScheduler (void) const
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
return m_events;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::ProcessOneEvent (void)
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
EventId next = m_events->RemoveNext ();
|
|
|
|
|
|
|
|
|
|
NS_ASSERT (next.GetTs () >= m_currentTs);
|
|
|
|
|
--m_unscheduledEvents;
|
|
|
|
|
|
|
|
|
|
NS_LOG_LOGIC ("handle " << next.GetTs ());
|
|
|
|
|
m_currentTs = next.GetTs ();
|
|
|
|
|
m_currentUid = next.GetUid ();
|
|
|
|
|
EventImpl *event = next.PeekEventImpl ();
|
|
|
|
|
event->Invoke ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::IsFinished (void) const
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
return m_events->IsEmpty ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::NextTs (void) const
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
NS_ASSERT (!m_events->IsEmpty ());
|
|
|
|
|
EventId id = m_events->PeekNext ();
|
|
|
|
|
return id.GetTs ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Time
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::Next (void) const
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
return TimeStep (NextTs ());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::Run (void)
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
while (!m_events->IsEmpty () && !m_stop &&
|
|
|
|
|
(m_stopAt == 0 || m_stopAt > NextTs ()))
|
|
|
|
|
{
|
|
|
|
|
ProcessOneEvent ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the simulator stopped naturally by lack of events, make a
|
|
|
|
|
// consistency test to check that we didn't lose any events along the way.
|
|
|
|
|
NS_ASSERT(!m_events->IsEmpty () || m_unscheduledEvents == 0);
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-06 11:37:52 +01:00
|
|
|
void
|
|
|
|
|
DefaultSimulatorImpl::RunOneEvent (void)
|
|
|
|
|
{
|
|
|
|
|
ProcessOneEvent ();
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-17 23:52:59 -07:00
|
|
|
void
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::Stop (void)
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
m_stop = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::Stop (Time const &time)
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
NS_ASSERT (time.IsPositive ());
|
|
|
|
|
Time absolute = Simulator::Now () + time;
|
|
|
|
|
m_stopAt = absolute.GetTimeStep ();
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-10 15:24:56 -07:00
|
|
|
//
|
|
|
|
|
// Schedule an event for a _relative_ time in the future.
|
|
|
|
|
//
|
2008-07-17 23:52:59 -07:00
|
|
|
EventId
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::Schedule (Time const &time, const Ptr<EventImpl> &event)
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
2008-10-10 15:24:56 -07:00
|
|
|
Time tAbsolute = time + Now();
|
|
|
|
|
|
|
|
|
|
NS_ASSERT (tAbsolute.IsPositive ());
|
|
|
|
|
NS_ASSERT (tAbsolute >= TimeStep (m_currentTs));
|
|
|
|
|
uint64_t ts = (uint64_t) tAbsolute.GetTimeStep ();
|
2008-07-17 23:52:59 -07:00
|
|
|
EventId id (event, ts, m_uid);
|
|
|
|
|
m_uid++;
|
|
|
|
|
++m_unscheduledEvents;
|
|
|
|
|
m_events->Insert (id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EventId
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::ScheduleNow (const Ptr<EventImpl> &event)
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
EventId id (event, m_currentTs, m_uid);
|
|
|
|
|
m_uid++;
|
|
|
|
|
++m_unscheduledEvents;
|
|
|
|
|
m_events->Insert (id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EventId
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::ScheduleDestroy (const Ptr<EventImpl> &event)
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
EventId id (event, m_currentTs, 2);
|
|
|
|
|
m_destroyEvents.push_back (id);
|
|
|
|
|
m_uid++;
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Time
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::Now (void) const
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
return TimeStep (m_currentTs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Time
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::GetDelayLeft (const EventId &id) const
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
if (IsExpired (id))
|
|
|
|
|
{
|
|
|
|
|
return TimeStep (0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return TimeStep (id.GetTs () - m_currentTs);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::Remove (const EventId &ev)
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
if (ev.GetUid () == 2)
|
|
|
|
|
{
|
|
|
|
|
// destroy events.
|
|
|
|
|
for (DestroyEvents::iterator i = m_destroyEvents.begin (); i != m_destroyEvents.end (); i++)
|
|
|
|
|
{
|
|
|
|
|
if (*i == ev)
|
|
|
|
|
{
|
|
|
|
|
m_destroyEvents.erase (i);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (IsExpired (ev))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_events->Remove (ev);
|
|
|
|
|
Cancel (ev);
|
|
|
|
|
|
|
|
|
|
--m_unscheduledEvents;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::Cancel (const EventId &id)
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
if (!IsExpired (id))
|
|
|
|
|
{
|
|
|
|
|
id.PeekEventImpl ()->Cancel ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::IsExpired (const EventId &ev) const
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
if (ev.GetUid () == 2)
|
|
|
|
|
{
|
|
|
|
|
// destroy events.
|
|
|
|
|
for (DestroyEvents::const_iterator i = m_destroyEvents.begin (); i != m_destroyEvents.end (); i++)
|
|
|
|
|
{
|
|
|
|
|
if (*i == ev)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (ev.PeekEventImpl () == 0 ||
|
|
|
|
|
ev.GetTs () < m_currentTs ||
|
|
|
|
|
(ev.GetTs () == m_currentTs &&
|
|
|
|
|
ev.GetUid () <= m_currentUid) ||
|
|
|
|
|
ev.PeekEventImpl ()->IsCancelled ())
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Time
|
2008-07-18 21:51:31 -07:00
|
|
|
DefaultSimulatorImpl::GetMaximumSimulationTime (void) const
|
2008-07-17 23:52:59 -07:00
|
|
|
{
|
|
|
|
|
// XXX: I am fairly certain other compilers use other non-standard
|
|
|
|
|
// post-fixes to indicate 64 bit constants.
|
|
|
|
|
return TimeStep (0x7fffffffffffffffLL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}; // namespace ns3
|
|
|
|
|
|
|
|
|
|
|