src/core/model/realtime-simulator-impl.cc
author Peter D. Barnes, Jr. <barnes26@llnl.gov>
Thu, 18 Dec 2014 15:12:35 -0800
changeset 11131 6a448ac28669
parent 11091 4bbd7b9a2889
child 11246 9a95c9e7c9b4
permissions -rw-r--r--
[Doxygen] Various in src/core
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     2
/*
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     3
 * Copyright (c) 2008 University of Washington
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     4
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License version 2 as
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     7
 * published by the Free Software Foundation;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     8
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    12
 * GNU General Public License for more details.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    13
 *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    17
 */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    18
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    19
#include "simulator.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    20
#include "realtime-simulator-impl.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    21
#include "wall-clock-synchronizer.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    22
#include "scheduler.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    23
#include "event-impl.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    24
#include "synchronizer.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    25
7383
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    26
#include "ptr.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    27
#include "pointer.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    28
#include "assert.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    29
#include "fatal-error.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    30
#include "log.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    31
#include "system-mutex.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    32
#include "boolean.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    33
#include "enum.h"
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    34
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    35
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7810
diff changeset
    36
#include <cmath>
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
10968
2d29fee2b7b8 [Bug 1551] Redux: NS_LOG_COMPONENT_DEFINE inside or outside of ns3 namespace?
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10652
diff changeset
    38
11131
6a448ac28669 [Doxygen] Various in src/core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11091
diff changeset
    39
/**
6a448ac28669 [Doxygen] Various in src/core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11091
diff changeset
    40
 * \file
6a448ac28669 [Doxygen] Various in src/core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11091
diff changeset
    41
 * \ingroup realtime
6a448ac28669 [Doxygen] Various in src/core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11091
diff changeset
    42
 * ns3::RealTimeSimulatorImpl implementation.
6a448ac28669 [Doxygen] Various in src/core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11091
diff changeset
    43
 */
6a448ac28669 [Doxygen] Various in src/core
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 11091
diff changeset
    44
10968
2d29fee2b7b8 [Bug 1551] Redux: NS_LOG_COMPONENT_DEFINE inside or outside of ns3 namespace?
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10652
diff changeset
    45
namespace ns3 {
2d29fee2b7b8 [Bug 1551] Redux: NS_LOG_COMPONENT_DEFINE inside or outside of ns3 namespace?
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10652
diff changeset
    46
9191
f094300690db bug 1549: remove logging statements causing stack overflows
Tom Henderson <tomh@tomh.org>
parents: 9134
diff changeset
    47
// Note:  Logging in this file is largely avoided due to the
f094300690db bug 1549: remove logging statements causing stack overflows
Tom Henderson <tomh@tomh.org>
parents: 9134
diff changeset
    48
// number of calls that are made to these functions and the possibility
f094300690db bug 1549: remove logging statements causing stack overflows
Tom Henderson <tomh@tomh.org>
parents: 9134
diff changeset
    49
// of causing recursions leading to stack overflow
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
NS_LOG_COMPONENT_DEFINE ("RealtimeSimulatorImpl");
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    51
10652
dc18deba4502 [doxygen] Revert r10410, r10411, r10412
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10410
diff changeset
    52
NS_OBJECT_ENSURE_REGISTERED (RealtimeSimulatorImpl);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    54
TypeId
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
RealtimeSimulatorImpl::GetTypeId (void)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    56
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
  static TypeId tid = TypeId ("ns3::RealtimeSimulatorImpl")
6492
4e65e9ebad53 use proper parent class
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6273
diff changeset
    58
    .SetParent<SimulatorImpl> ()
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    59
    .AddConstructor<RealtimeSimulatorImpl> ()
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    60
    .AddAttribute ("SynchronizationMode", 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    61
                   "What to do if the simulation cannot keep up with real time.",
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    62
                   EnumValue (SYNC_BEST_EFFORT),
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    63
                   MakeEnumAccessor (&RealtimeSimulatorImpl::SetSynchronizationMode),
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    64
                   MakeEnumChecker (SYNC_BEST_EFFORT, "BestEffort",
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    65
                                    SYNC_HARD_LIMIT, "HardLimit"))
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    66
    .AddAttribute ("HardLimit", 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    67
                   "Maximum acceptable real-time jitter (used in conjunction with SynchronizationMode=HardLimit)",
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    68
                   TimeValue (Seconds (0.1)),
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    69
                   MakeTimeAccessor (&RealtimeSimulatorImpl::m_hardLimit),
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    70
                   MakeTimeChecker ())
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
    71
  ;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    72
  return tid;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    73
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    74
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    75
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    76
RealtimeSimulatorImpl::RealtimeSimulatorImpl ()
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    77
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
    78
  NS_LOG_FUNCTION (this);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    79
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    80
  m_stop = false;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    81
  m_running = false;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    82
  // uids are allocated from 4.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    83
  // uid 0 is "invalid" events
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    84
  // uid 1 is "now" events
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    85
  // uid 2 is "destroy" events
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
  m_uid = 4; 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
  // before ::Run is entered, the m_currentUid will be zero
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
  m_currentUid = 0;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
  m_currentTs = 0;
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
    90
  m_currentContext = 0xffffffff;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    91
  m_unscheduledEvents = 0;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    92
7780
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
    93
  m_main = SystemThread::Self();
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
    94
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
  // Be very careful not to do anything that would cause a change or assignment
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
  // of the underlying reference counts of m_synchronizer or you will be sorry.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    97
  m_synchronizer = CreateObject<WallClockSynchronizer> ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    98
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    99
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   100
RealtimeSimulatorImpl::~RealtimeSimulatorImpl ()
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   101
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   102
  NS_LOG_FUNCTION (this);
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   103
}
5872
188ecd74fcb4 It is not legal to call Dispose from within a destructor
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 5822
diff changeset
   104
188ecd74fcb4 It is not legal to call Dispose from within a destructor
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 5822
diff changeset
   105
void
188ecd74fcb4 It is not legal to call Dispose from within a destructor
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 5822
diff changeset
   106
RealtimeSimulatorImpl::DoDispose (void)
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   108
  NS_LOG_FUNCTION (this);
7810
b2802209f236 bools are bools
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7796
diff changeset
   109
  while (!m_events->IsEmpty ())
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   110
    {
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   111
      Scheduler::Event next = m_events->RemoveNext ();
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   112
      next.impl->Unref ();
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   113
    }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   114
  m_events = 0;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   115
  m_synchronizer = 0;
5872
188ecd74fcb4 It is not legal to call Dispose from within a destructor
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 5822
diff changeset
   116
  SimulatorImpl::DoDispose ();
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   117
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   118
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   119
void
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
RealtimeSimulatorImpl::Destroy ()
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   121
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   122
  NS_LOG_FUNCTION (this);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   123
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   124
  //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
  // This function is only called with the private version "disconnected" from
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   126
  // the main simulator functions.  We rely on the user not calling 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   127
  // Simulator::Destroy while there is a chance that a worker thread could be
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   128
  // accessing the current instance of the private object.  In practice this
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   129
  // means shutting down the workers and doing a Join() before calling the
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   130
  // Simulator::Destroy().
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   131
  //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   132
  while (m_destroyEvents.empty () == false) 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   133
    {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   134
      Ptr<EventImpl> ev = m_destroyEvents.front ().PeekEventImpl ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   135
      m_destroyEvents.pop_front ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   136
      NS_LOG_LOGIC ("handle destroy " << ev);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   137
      if (ev->IsCancelled () == false)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   138
        {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   139
          ev->Invoke ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   140
        }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   141
    }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   142
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   143
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   144
void
5507
915abd2b907b Simulator::SetScheduler now takes an ObjectFactory
Guillaume Seguin <guillaume@segu.in>
parents: 5302
diff changeset
   145
RealtimeSimulatorImpl::SetScheduler (ObjectFactory schedulerFactory)
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   146
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   147
  NS_LOG_FUNCTION (this << schedulerFactory);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   148
5507
915abd2b907b Simulator::SetScheduler now takes an ObjectFactory
Guillaume Seguin <guillaume@segu.in>
parents: 5302
diff changeset
   149
  Ptr<Scheduler> scheduler = schedulerFactory.Create<Scheduler> ();
915abd2b907b Simulator::SetScheduler now takes an ObjectFactory
Guillaume Seguin <guillaume@segu.in>
parents: 5302
diff changeset
   150
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   151
  { 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   152
    CriticalSection cs (m_mutex);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   153
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   154
    if (m_events != 0)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   155
      {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   156
        while (m_events->IsEmpty () == false)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   157
          {
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   158
            Scheduler::Event next = m_events->RemoveNext ();
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   159
            scheduler->Insert (next);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   160
          }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   161
      }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   162
    m_events = scheduler;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   163
  }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   164
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   165
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   166
void
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   167
RealtimeSimulatorImpl::ProcessOneEvent (void)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   168
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   169
  //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   170
  // The idea here is to wait until the next event comes due.  In the case of
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   171
  // a realtime simulation, we want real time to be consumed between events.
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   172
  // It is the realtime synchronizer that causes real time to be consumed by
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   173
  // doing some kind of a wait.
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   174
  //
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   175
  // We need to be able to have external events (such as a packet reception event)
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   176
  // cause us to re-evaluate our state.  The way this works is that the synchronizer
6273
8d70de29d514 spell check, mostly in comments.
Andrey Mazo <mazo@iitp.ru>
parents: 6113
diff changeset
   177
  // gets interrupted and returns.  So, there is a possibility that things may change
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   178
  // out from under us dynamically.  In this case, we need to re-evaluate how long to 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   179
  // wait in a for-loop until we have waited sucessfully (until a timeout) for the 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   180
  // event at the head of the event list.
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   181
  //
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   182
  // m_synchronizer->Synchronize will return true if the wait was completed without 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   183
  // interruption, otherwise it will return false indicating that something has changed
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   184
  // out from under us.  If we sit in the for-loop trying to synchronize until 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   185
  // Synchronize() returns true, we will have successfully synchronized the execution 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   186
  // time of the next event with the wall clock time of the synchronizer.
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   187
  //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   188
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   189
  for (;;) 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   190
    {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   191
      uint64_t tsDelay = 0;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   192
      uint64_t tsNext = 0;
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   193
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   194
      //
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   195
      // It is important to understand that m_currentTs is interpreted only as the 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   196
      // timestamp  of the last event we executed.  Current time can a bit of a 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   197
      // slippery concept in realtime mode.  What we have here is a discrete event 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   198
      // simulator, so the last event is, by defintion, executed entirely at a single
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   199
      //  discrete time.  This is the definition of m_currentTs.  It really has 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   200
      // nothing to do with the current real time, except that we are trying to arrange
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   201
      // that at the instant of the beginning of event execution, the current real time
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   202
      // and m_currentTs coincide.
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   203
      //
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   204
      // We use tsNow as the indication of the current real time.
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   205
      //
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   206
      uint64_t tsNow;
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   207
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   208
      { 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   209
        CriticalSection cs (m_mutex);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   210
        //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   211
        // Since we are in realtime mode, the time to delay has got to be the 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   212
        // difference between the current realtime and the timestamp of the next 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   213
        // event.  Since m_currentTs is actually the timestamp of the last event we 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   214
        // executed, it's not particularly meaningful for us here since real time has
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   215
        // certainly elapsed since it was last updated.
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   216
        //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   217
        // It is possible that the current realtime has drifted past the next event
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   218
        // time so we need to be careful about that and not delay in that case.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   219
        //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   220
        NS_ASSERT_MSG (m_synchronizer->Realtime (), 
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   221
                       "RealtimeSimulatorImpl::ProcessOneEvent (): Synchronizer reports not Realtime ()");
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   222
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   223
        //
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   224
        // tsNow is set to the normalized current real time.  When the simulation was
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   225
        // started, the current real time was effectively set to zero; so tsNow is
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   226
        // the current "real" simulation time.
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   227
        //
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   228
        // tsNext is the simulation time of the next event we want to execute.
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   229
        //
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   230
        tsNow = m_synchronizer->GetCurrentRealtime ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   231
        tsNext = NextTs ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   232
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   233
        //
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   234
        // tsDelay is therefore the real time we need to delay in order to bring the
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   235
        // real time in sync with the simulation time.  If we wait for this amount of
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   236
        // real time, we will accomplish moving the simulation time at the same rate
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   237
        // as the real time.  This is typically called "pacing" the simulation time.
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   238
        //
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   239
        // We do have to be careful if we are falling behind.  If so, tsDelay must be
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   240
        // zero.  If we're late, don't dawdle.
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   241
        //
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   242
        if (tsNext <= tsNow)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   243
          {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   244
            tsDelay = 0;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   245
          }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   246
        else
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   247
          {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   248
            tsDelay = tsNext - tsNow;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   249
          }
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   250
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   251
        //
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   252
        // We've figured out how long we need to delay in order to pace the 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   253
        // simulation time with the real time.  We're going to sleep, but need
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   254
        // to work with the synchronizer to make sure we're awakened if something 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   255
        // external happens (like a packet is received).  This next line resets
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   256
        // the synchronizer so that any future event will cause it to interrupt.
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   257
        //
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   258
        m_synchronizer->SetCondition (false);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   259
      }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   260
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   261
      //
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   262
      // We have a time to delay.  This time may actually not be valid anymore
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   263
      // since we released the critical section immediately above, and a real-time
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   264
      // ScheduleReal or ScheduleRealNow may have snuck in, well, between the 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   265
      // closing brace above and this comment so to speak.  If this is the case, 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   266
      // that schedule operation will have done a synchronizer Signal() that 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   267
      // will set the condition variable to true and cause the Synchronize call 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   268
      // below to return immediately.
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   269
      //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   270
      // It's easiest to understand if you just consider a short tsDelay that only
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   271
      // requires a SpinWait down in the synchronizer.  What will happen is that 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   272
      // whan Synchronize calls SpinWait, SpinWait will look directly at its 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   273
      // condition variable.  Note that we set this condition variable to false 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   274
      // inside the critical section above. 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   275
      //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   276
      // SpinWait will go into a forever loop until either the time has expired or
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   277
      // until the condition variable becomes true.  A true condition indicates that
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   278
      // the wait should stop.  The condition is set to true by one of the Schedule
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   279
      // methods of the simulator; so if we are in a wait down in Synchronize, and
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   280
      // a Simulator::ScheduleReal is done, the wait down in Synchronize will exit and
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   281
      // Synchronize will return false.  This means we have not actually synchronized
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   282
      // to the event expiration time.  If no real-time schedule operation is done
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   283
      // while down in Synchronize, the wait will time out and Synchronize will return 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   284
      // true.  This indicates that we have synchronized to the event time.
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   285
      //
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   286
      // So we need to stay in this for loop, looking for the next event timestamp and 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   287
      // attempting to sleep until its due.  If we've slept until the timestamp is due, 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   288
      // Synchronize returns true and we break out of the sync loop.  If an external
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   289
      // event happens that requires a re-schedule, Synchronize returns false and
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   290
      // we re-evaluate our timing by continuing in the loop.
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   291
      //
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   292
      // It is expected that tsDelay become shorter as external events interrupt our
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   293
      // waits.
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   294
      //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   295
      if (m_synchronizer->Synchronize (tsNow, tsDelay))
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   296
        {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   297
          NS_LOG_LOGIC ("Interrupted ...");
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   298
          break;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   299
        }
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   300
 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   301
      //
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   302
      // If we get to this point, we have been interrupted during a wait by a real-time
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   303
      // schedule operation.  This means all bets are off regarding tsDelay and we need
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   304
      // to re-evaluate what it is we want to do.  We'll loop back around in the 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   305
      // for-loop and start again from scratch.
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   306
      //
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   307
    }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   308
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   309
  //
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   310
  // If we break out of the for-loop above, we have waited until the time specified
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   311
  // by the event that was at the head of the event list when we started the process.
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   312
  // Since there is a bunch of code that was executed outside a critical section (the
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   313
  // Synchronize call) we cannot be sure that the event at the head of the event list
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   314
  // is the one we think it is.  What we can be sure of is that it is time to execute
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   315
  // whatever event is at the head of this list if the list is in time order.
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   316
  //
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   317
  Scheduler::Event next;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   318
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   319
  { 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   320
    CriticalSection cs (m_mutex);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   321
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   322
    // 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   323
    // We do know we're waiting for an event, so there had better be an event on the 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   324
    // event queue.  Let's pull it off.  When we release the critical section, the
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   325
    // event we're working on won't be on the list and so subsequent operations won't
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   326
    // mess with us.
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   327
    //
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   328
    NS_ASSERT_MSG (m_events->IsEmpty () == false, 
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   329
                   "RealtimeSimulatorImpl::ProcessOneEvent(): event queue is empty");
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   330
    next = m_events->RemoveNext ();
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   331
    m_unscheduledEvents--;
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   332
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   333
    //
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   334
    // We cannot make any assumption that "next" is the same event we originally waited 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   335
    // for.  We can only assume that only that it must be due and cannot cause time 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   336
    // to move backward.
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   337
    //
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   338
    NS_ASSERT_MSG (next.key.m_ts >= m_currentTs,
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   339
                   "RealtimeSimulatorImpl::ProcessOneEvent(): "
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   340
                   "next.GetTs() earlier than m_currentTs (list order error)");
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   341
    NS_LOG_LOGIC ("handle " << next.key.m_ts);
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   342
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   343
    // 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   344
    // Update the current simulation time to be the timestamp of the event we're 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   345
    // executing.  From the rest of the simulation's point of view, simulation time
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   346
    // is frozen until the next event is executed.
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   347
    //
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   348
    m_currentTs = next.key.m_ts;
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   349
    m_currentContext = next.key.m_context;
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   350
    m_currentUid = next.key.m_uid;
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   351
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   352
    // 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   353
    // We're about to run the event and we've done our best to synchronize this
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   354
    // event execution time to real time.  Now, if we're in SYNC_HARD_LIMIT mode
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   355
    // we have to decide if we've done a good enough job and if we haven't, we've
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   356
    // been asked to commit ritual suicide.
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   357
    //
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   358
    // We check the simulation time against the current real time to make this
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   359
    // judgement.
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   360
    //
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   361
    if (m_synchronizationMode == SYNC_HARD_LIMIT)
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   362
      {
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   363
        uint64_t tsFinal = m_synchronizer->GetCurrentRealtime ();
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   364
        uint64_t tsJitter;
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   365
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   366
        if (tsFinal >= m_currentTs)
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   367
          {
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   368
            tsJitter = tsFinal - m_currentTs;
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   369
          }
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   370
        else
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   371
          {
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   372
            tsJitter = m_currentTs - tsFinal;
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   373
          }
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   374
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   375
        if (tsJitter > static_cast<uint64_t>(m_hardLimit.GetTimeStep ()))
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   376
          {
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   377
            NS_FATAL_ERROR ("RealtimeSimulatorImpl::ProcessOneEvent (): "
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   378
                            "Hard real-time limit exceeded (jitter = " << tsJitter << ")");
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   379
          }
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   380
      }
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   381
  }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   382
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   383
  //
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   384
  // We have got the event we're about to execute completely disentangled from the 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   385
  // event list so we can execute it outside a critical section without fear of someone
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   386
  // changing things out from under us.
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   387
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   388
  EventImpl *event = next.impl;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   389
  m_synchronizer->EventStart ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   390
  event->Invoke ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   391
  m_synchronizer->EventEnd ();
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   392
  event->Unref ();
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   393
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   394
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   395
bool 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   396
RealtimeSimulatorImpl::IsFinished (void) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   397
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   398
  bool rc;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   399
  {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   400
    CriticalSection cs (m_mutex);
4085
31945ecb17e2 Make Simulator::IsFinished () take into account the stop time. Needed to run custom simulation loops (see bug 375)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4057
diff changeset
   401
    rc = m_events->IsEmpty () || m_stop;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   402
  }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   403
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   404
  return rc;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   405
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   406
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   407
//
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   408
// Peeks into event list.  Should be called with critical section locked.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   409
//
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   410
uint64_t
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   411
RealtimeSimulatorImpl::NextTs (void) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   412
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   413
  NS_ASSERT_MSG (m_events->IsEmpty () == false, 
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   414
                 "RealtimeSimulatorImpl::NextTs(): event queue is empty");
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   415
  Scheduler::Event ev = m_events->PeekNext ();
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   416
  return ev.key.m_ts;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   417
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   418
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   419
void
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   420
RealtimeSimulatorImpl::Run (void)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   421
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   422
  NS_LOG_FUNCTION (this);
3802
b796f3c38e3e implement a non-realtime version of RunOneEvent for realtime simulator and allow Run, Stop, Run
Craig Dowell <craigdo@ee.washington.edu>
parents: 3801
diff changeset
   423
b796f3c38e3e implement a non-realtime version of RunOneEvent for realtime simulator and allow Run, Stop, Run
Craig Dowell <craigdo@ee.washington.edu>
parents: 3801
diff changeset
   424
  NS_ASSERT_MSG (m_running == false, 
b796f3c38e3e implement a non-realtime version of RunOneEvent for realtime simulator and allow Run, Stop, Run
Craig Dowell <craigdo@ee.washington.edu>
parents: 3801
diff changeset
   425
                 "RealtimeSimulatorImpl::Run(): Simulator already running");
b796f3c38e3e implement a non-realtime version of RunOneEvent for realtime simulator and allow Run, Stop, Run
Craig Dowell <craigdo@ee.washington.edu>
parents: 3801
diff changeset
   426
7780
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
   427
  // Set the current threadId as the main threadId
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
   428
  m_main = SystemThread::Self();
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
   429
4057
a68a42e667f6 bug 459: Simulator::Run does nothing after Simulator::Stop is called
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4009
diff changeset
   430
  m_stop = false;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   431
  m_running = true;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   432
  m_synchronizer->SetOrigin (m_currentTs);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   433
7794
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   434
  // Sleep until signalled
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   435
  uint64_t tsNow;
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   436
  uint64_t tsDelay = 1000000000; // wait time of 1 second (in nanoseconds)
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   437
 
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   438
  while (!m_stop) 
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   439
    {
7794
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   440
      bool process = false;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   441
      {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   442
        CriticalSection cs (m_mutex);
7794
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   443
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   444
        if (!m_events->IsEmpty ())
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   445
          {
7794
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   446
            process = true;
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   447
          }
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   448
        else
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   449
          {
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   450
            // Get current timestamp while holding the critical section
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   451
            tsNow = m_synchronizer->GetCurrentRealtime ();
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   452
          }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   453
      }
7794
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   454
 
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   455
      if (!process)
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   456
        {
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   457
          // Sleep until signalled
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   458
          tsNow = m_synchronizer->Synchronize (tsNow, tsDelay);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   459
7794
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   460
          // Re-check event queue
3b9223ce47df bug 1000: Make RealtimeSimulatorImpl last until stop
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
   461
          continue;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   462
        }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   463
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   464
      ProcessOneEvent ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   465
    }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   466
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   467
  //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   468
  // If the simulator stopped naturally by lack of events, make a
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   469
  // consistency test to check that we didn't lose any events along the way.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   470
  //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   471
  {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   472
    CriticalSection cs (m_mutex);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   473
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   474
    NS_ASSERT_MSG (m_events->IsEmpty () == false || m_unscheduledEvents == 0,
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   475
                   "RealtimeSimulatorImpl::Run(): Empty queue and unprocessed events");
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   476
  }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   477
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   478
  m_running = false;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   479
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   480
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   481
bool
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   482
RealtimeSimulatorImpl::Running (void) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   483
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   484
  return m_running;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   485
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   486
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   487
bool
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   488
RealtimeSimulatorImpl::Realtime (void) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   489
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   490
  return m_synchronizer->Realtime ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   491
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   492
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   493
void 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   494
RealtimeSimulatorImpl::Stop (void)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   495
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   496
  NS_LOG_FUNCTION (this);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   497
  m_stop = true;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   498
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   499
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   500
void 
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   501
RealtimeSimulatorImpl::Stop (Time const &time)
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   502
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   503
  NS_LOG_FUNCTION (this << time);
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   504
  Simulator::Schedule (time, &Simulator::Stop);
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   505
}
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   506
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   507
//
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   508
// Schedule an event for a _relative_ time in the future.
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   509
//
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   510
EventId
3808
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3807
diff changeset
   511
RealtimeSimulatorImpl::Schedule (Time const &time, EventImpl *impl)
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   512
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   513
  NS_LOG_FUNCTION (this << time << impl);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   514
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   515
  Scheduler::Event ev;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   516
  {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   517
    CriticalSection cs (m_mutex);
3796
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   518
    //
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   519
    // This is the reason we had to bring the absolute time calcualtion in from the
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   520
    // simulator.h into the implementation.  Since the implementations may be 
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   521
    // multi-threaded, we need this calculation to be atomic.  You can see it is
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   522
    // here since we are running in a CriticalSection.
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   523
    //
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   524
    Time tAbsolute = Simulator::Now () + time;
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   525
    NS_ASSERT_MSG (tAbsolute.IsPositive (), "RealtimeSimulatorImpl::Schedule(): Negative time");
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   526
    NS_ASSERT_MSG (tAbsolute >= TimeStep (m_currentTs), "RealtimeSimulatorImpl::Schedule(): time < m_currentTs");
3808
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3807
diff changeset
   527
    ev.impl = impl;
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   528
    ev.key.m_ts = (uint64_t) tAbsolute.GetTimeStep ();
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   529
    ev.key.m_context = GetContext ();
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   530
    ev.key.m_uid = m_uid;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   531
    m_uid++;
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   532
    m_unscheduledEvents++;
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   533
    m_events->Insert (ev);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   534
    m_synchronizer->Signal ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   535
  }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   536
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   537
  return EventId (impl, ev.key.m_ts, ev.key.m_context, ev.key.m_uid);
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   538
}
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   539
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   540
void
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   541
RealtimeSimulatorImpl::ScheduleWithContext (uint32_t context, Time const &time, EventImpl *impl)
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   542
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   543
  NS_LOG_FUNCTION (this << context << time << impl);
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   544
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   545
  {
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   546
    CriticalSection cs (m_mutex);
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   547
    uint64_t ts;
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   548
7780
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
   549
    if (SystemThread::Equals (m_main))
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
   550
      {
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
   551
        ts = m_currentTs + time.GetTimeStep ();
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
   552
      }
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
   553
    else
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
   554
      {
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
   555
        //
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
   556
        // If the simulator is running, we're pacing and have a meaningful 
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
   557
        // realtime clock.  If we're not, then m_currentTs is where we stopped.
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
   558
        // 
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
   559
        ts = m_running ? m_synchronizer->GetCurrentRealtime () : m_currentTs;
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
   560
        ts += time.GetTimeStep ();
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
   561
      }
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7386
diff changeset
   562
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   563
    NS_ASSERT_MSG (ts >= m_currentTs, "RealtimeSimulatorImpl::ScheduleRealtime(): schedule for time < m_currentTs");
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   564
    Scheduler::Event ev;
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   565
    ev.impl = impl;
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   566
    ev.key.m_ts = ts;
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   567
    ev.key.m_context = context;
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   568
    ev.key.m_uid = m_uid;
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   569
    m_uid++;
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   570
    m_unscheduledEvents++;
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   571
    m_events->Insert (ev);
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   572
    m_synchronizer->Signal ();
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   573
  }
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   574
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   575
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   576
EventId
3808
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3807
diff changeset
   577
RealtimeSimulatorImpl::ScheduleNow (EventImpl *impl)
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   578
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   579
  NS_LOG_FUNCTION (this << impl);
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   580
  Scheduler::Event ev;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   581
  {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   582
    CriticalSection cs (m_mutex);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   583
3808
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3807
diff changeset
   584
    ev.impl = impl;
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   585
    ev.key.m_ts = m_currentTs;
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   586
    ev.key.m_context = GetContext ();
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   587
    ev.key.m_uid = m_uid;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   588
    m_uid++;
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   589
    m_unscheduledEvents++;
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   590
    m_events->Insert (ev);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   591
    m_synchronizer->Signal ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   592
  }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   593
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   594
  return EventId (impl, ev.key.m_ts, ev.key.m_context, ev.key.m_uid);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   595
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   596
3798
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   597
Time
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   598
RealtimeSimulatorImpl::Now (void) const
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   599
{
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   600
  return TimeStep (m_currentTs);
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   601
}
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   602
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   603
//
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   604
// Schedule an event for a _relative_ time in the future.
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   605
//
3809
5e007004402e move MakeEvent out of Simulator
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3808
diff changeset
   606
void
5822
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
   607
RealtimeSimulatorImpl::ScheduleRealtimeWithContext (uint32_t context, Time const &time, EventImpl *impl)
3798
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   608
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   609
  NS_LOG_FUNCTION (this << context << time << impl);
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   610
3799
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   611
  {
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   612
    CriticalSection cs (m_mutex);
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   613
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   614
    uint64_t ts = m_synchronizer->GetCurrentRealtime () + time.GetTimeStep ();
3816
edd4928b2046 remove realtime public API.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3812
diff changeset
   615
    NS_ASSERT_MSG (ts >= m_currentTs, "RealtimeSimulatorImpl::ScheduleRealtime(): schedule for time < m_currentTs");
3809
5e007004402e move MakeEvent out of Simulator
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3808
diff changeset
   616
    Scheduler::Event ev;
5e007004402e move MakeEvent out of Simulator
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3808
diff changeset
   617
    ev.impl = impl;
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   618
    ev.key.m_ts = ts;
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   619
    ev.key.m_uid = m_uid;
3799
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   620
    m_uid++;
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   621
    m_unscheduledEvents++;
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   622
    m_events->Insert (ev);
3799
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   623
    m_synchronizer->Signal ();
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   624
  }
3798
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   625
}
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   626
3809
5e007004402e move MakeEvent out of Simulator
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3808
diff changeset
   627
void
5822
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
   628
RealtimeSimulatorImpl::ScheduleRealtime (Time const &time, EventImpl *impl)
3798
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   629
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   630
  NS_LOG_FUNCTION (this << time << impl);
5822
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
   631
  ScheduleRealtimeWithContext (GetContext (), time, impl);
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
   632
}
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
   633
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
   634
void
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
   635
RealtimeSimulatorImpl::ScheduleRealtimeNowWithContext (uint32_t context, EventImpl *impl)
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
   636
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   637
  NS_LOG_FUNCTION (this << context << impl);
3799
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   638
  {
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   639
    CriticalSection cs (m_mutex);
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   640
3800
804beebf1ba5 added test for m_running in ScheduleRealNow
Craig Dowell <craigdo@ee.washington.edu>
parents: 3799
diff changeset
   641
    //
804beebf1ba5 added test for m_running in ScheduleRealNow
Craig Dowell <craigdo@ee.washington.edu>
parents: 3799
diff changeset
   642
    // If the simulator is running, we're pacing and have a meaningful 
804beebf1ba5 added test for m_running in ScheduleRealNow
Craig Dowell <craigdo@ee.washington.edu>
parents: 3799
diff changeset
   643
    // realtime clock.  If we're not, then m_currentTs is were we stopped.
804beebf1ba5 added test for m_running in ScheduleRealNow
Craig Dowell <craigdo@ee.washington.edu>
parents: 3799
diff changeset
   644
    // 
804beebf1ba5 added test for m_running in ScheduleRealNow
Craig Dowell <craigdo@ee.washington.edu>
parents: 3799
diff changeset
   645
    uint64_t ts = m_running ? m_synchronizer->GetCurrentRealtime () : m_currentTs;
5822
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
   646
    NS_ASSERT_MSG (ts >= m_currentTs, 
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
   647
                   "RealtimeSimulatorImpl::ScheduleRealtimeNowWithContext(): schedule for time < m_currentTs");
3809
5e007004402e move MakeEvent out of Simulator
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3808
diff changeset
   648
    Scheduler::Event ev;
5e007004402e move MakeEvent out of Simulator
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3808
diff changeset
   649
    ev.impl = impl;
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   650
    ev.key.m_ts = ts;
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   651
    ev.key.m_uid = m_uid;
5822
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
   652
    ev.key.m_context = context;
3799
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   653
    m_uid++;
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   654
    m_unscheduledEvents++;
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   655
    m_events->Insert (ev);
3799
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   656
    m_synchronizer->Signal ();
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   657
  }
3798
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   658
}
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   659
5822
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
   660
void
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
   661
RealtimeSimulatorImpl::ScheduleRealtimeNow (EventImpl *impl)
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
   662
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   663
  NS_LOG_FUNCTION (this << impl);
5822
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
   664
  ScheduleRealtimeNowWithContext (GetContext (), impl);
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
   665
}
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
   666
3798
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   667
Time
3801
dc1f5e534e56 slight reorganization and naming change for added realtime methods
Craig Dowell <craigdo@ee.washington.edu>
parents: 3800
diff changeset
   668
RealtimeSimulatorImpl::RealtimeNow (void) const
3798
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   669
{
3799
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   670
  return TimeStep (m_synchronizer->GetCurrentRealtime ());
3798
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   671
}
898d8a14b88f add dangerous realtime scheduling methods in an intentionally harder to find and use way
Craig Dowell <craigdo@ee.washington.edu>
parents: 3796
diff changeset
   672
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   673
EventId
3808
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3807
diff changeset
   674
RealtimeSimulatorImpl::ScheduleDestroy (EventImpl *impl)
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   675
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   676
  NS_LOG_FUNCTION (this << impl);
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   677
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   678
  EventId id;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   679
  {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   680
    CriticalSection cs (m_mutex);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   681
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   682
    //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   683
    // Time doesn't really matter here (especially in realtime mode).  It is 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   684
    // overridden by the uid of 2 which identifies this as an event to be 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   685
    // executed at Simulator::Destroy time.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   686
    //
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   687
    id = EventId (Ptr<EventImpl> (impl, false), m_currentTs, 0xffffffff, 2);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   688
    m_destroyEvents.push_back (id);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   689
    m_uid++;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   690
  }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   691
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   692
  return id;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   693
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   694
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   695
Time 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   696
RealtimeSimulatorImpl::GetDelayLeft (const EventId &id) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   697
{
3561
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   698
  //
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   699
  // If the event has expired, there is no delay until it runs.  It is not the
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   700
  // case that there is a negative time until it runs.
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   701
  //
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   702
  if (IsExpired (id))
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   703
    {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   704
      return TimeStep (0);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   705
    }
3561
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   706
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   707
  return TimeStep (id.GetTs () - m_currentTs);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   708
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   709
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   710
void
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   711
RealtimeSimulatorImpl::Remove (const EventId &id)
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   712
{
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   713
  if (id.GetUid () == 2)
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   714
    {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   715
      // destroy events.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   716
      for (DestroyEvents::iterator i = m_destroyEvents.begin (); 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   717
           i != m_destroyEvents.end (); 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   718
           i++)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   719
        {
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   720
          if (*i == id)
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   721
            {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   722
              m_destroyEvents.erase (i);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   723
              break;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   724
            }
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   725
        }
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   726
      return;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   727
    }
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   728
  if (IsExpired (id))
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   729
    {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   730
      return;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   731
    }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   732
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   733
  {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   734
    CriticalSection cs (m_mutex);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   735
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   736
    Scheduler::Event event;
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   737
    event.impl = id.PeekEventImpl ();
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   738
    event.key.m_ts = id.GetTs ();
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   739
    event.key.m_context = id.GetContext ();
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   740
    event.key.m_uid = id.GetUid ();
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   741
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   742
    m_events->Remove (event);
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   743
    m_unscheduledEvents--;
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   744
    event.impl->Cancel ();
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   745
    event.impl->Unref ();
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   746
  }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   747
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   748
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   749
void
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   750
RealtimeSimulatorImpl::Cancel (const EventId &id)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   751
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   752
  if (IsExpired (id) == false)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   753
    {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   754
      id.PeekEventImpl ()->Cancel ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   755
    }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   756
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   757
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   758
bool
11091
4bbd7b9a2889 Standardize EventId arguments as id.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
   759
RealtimeSimulatorImpl::IsExpired (const EventId &id) const
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   760
{
11091
4bbd7b9a2889 Standardize EventId arguments as id.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
   761
  if (id.GetUid () == 2)
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   762
    {
11091
4bbd7b9a2889 Standardize EventId arguments as id.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
   763
      if (id.PeekEventImpl () == 0 ||
4bbd7b9a2889 Standardize EventId arguments as id.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
   764
          id.PeekEventImpl ()->IsCancelled ())
5302
5ed72d440db4 fix old bug: canceled destroy events are expired
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4552
diff changeset
   765
        {
5ed72d440db4 fix old bug: canceled destroy events are expired
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4552
diff changeset
   766
          return true;
5ed72d440db4 fix old bug: canceled destroy events are expired
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 4552
diff changeset
   767
        }
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   768
      // destroy events.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   769
      for (DestroyEvents::const_iterator i = m_destroyEvents.begin (); 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   770
           i != m_destroyEvents.end (); i++)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   771
        {
11091
4bbd7b9a2889 Standardize EventId arguments as id.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
   772
          if (*i == id)
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   773
            {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   774
              return false;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   775
            }
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   776
        }
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   777
      return true;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   778
    }
3561
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   779
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   780
  //
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   781
  // If the time of the event is less than the current timestamp of the 
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   782
  // simulator, the simulator has gone past the invocation time of the 
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   783
  // event, so the statement ev.GetTs () < m_currentTs does mean that 
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   784
  // the event has been fired even in realtime mode.
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   785
  //
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   786
  // The same is true for the next line involving the m_currentUid.
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   787
  //
11091
4bbd7b9a2889 Standardize EventId arguments as id.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
   788
  if (id.PeekEventImpl () == 0 ||
4bbd7b9a2889 Standardize EventId arguments as id.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
   789
      id.GetTs () < m_currentTs ||
4bbd7b9a2889 Standardize EventId arguments as id.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
   790
      (id.GetTs () == m_currentTs && id.GetUid () <= m_currentUid) ||
4bbd7b9a2889 Standardize EventId arguments as id.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10968
diff changeset
   791
      id.PeekEventImpl ()->IsCancelled ()) 
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   792
    {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   793
      return true;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   794
    }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   795
  else
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   796
    {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   797
      return false;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   798
    }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   799
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   800
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   801
Time 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   802
RealtimeSimulatorImpl::GetMaximumSimulationTime (void) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   803
{
9894
ac4e52a91d5d Doxygenate todo's
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9191
diff changeset
   804
  /// \todo I am fairly certain other compilers use other non-standard
ac4e52a91d5d Doxygenate todo's
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9191
diff changeset
   805
  /// post-fixes to indicate 64 bit constants.
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   806
  return TimeStep (0x7fffffffffffffffLL);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   807
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   808
6113
0ce37bf4f1c1 Merge distributed simulation code
Josh Pelkey <jpelkey@gatech.edu>
parents: 5872
diff changeset
   809
// System ID for non-distributed simulation is always zero
0ce37bf4f1c1 Merge distributed simulation code
Josh Pelkey <jpelkey@gatech.edu>
parents: 5872
diff changeset
   810
uint32_t 
0ce37bf4f1c1 Merge distributed simulation code
Josh Pelkey <jpelkey@gatech.edu>
parents: 5872
diff changeset
   811
RealtimeSimulatorImpl::GetSystemId (void) const
0ce37bf4f1c1 Merge distributed simulation code
Josh Pelkey <jpelkey@gatech.edu>
parents: 5872
diff changeset
   812
{
0ce37bf4f1c1 Merge distributed simulation code
Josh Pelkey <jpelkey@gatech.edu>
parents: 5872
diff changeset
   813
  return 0;
0ce37bf4f1c1 Merge distributed simulation code
Josh Pelkey <jpelkey@gatech.edu>
parents: 5872
diff changeset
   814
}
0ce37bf4f1c1 Merge distributed simulation code
Josh Pelkey <jpelkey@gatech.edu>
parents: 5872
diff changeset
   815
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   816
uint32_t
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   817
RealtimeSimulatorImpl::GetContext (void) const
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   818
{
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   819
  return m_currentContext;
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   820
}
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   821
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   822
void 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   823
RealtimeSimulatorImpl::SetSynchronizationMode (enum SynchronizationMode mode)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   824
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   825
  NS_LOG_FUNCTION (this << mode);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   826
  m_synchronizationMode = mode;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   827
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   828
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   829
RealtimeSimulatorImpl::SynchronizationMode
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   830
RealtimeSimulatorImpl::GetSynchronizationMode (void) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   831
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   832
  NS_LOG_FUNCTION (this);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   833
  return m_synchronizationMode;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   834
}
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   835
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   836
void 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   837
RealtimeSimulatorImpl::SetHardLimit (Time limit)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   838
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   839
  NS_LOG_FUNCTION (this << limit);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   840
  m_hardLimit = limit;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   841
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   842
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   843
Time
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   844
RealtimeSimulatorImpl::GetHardLimit (void) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   845
{
9134
7a750f032acd Clean up function logging of core module.
Maja Grubišić <maja.grubisic@live.com>
parents: 9063
diff changeset
   846
  NS_LOG_FUNCTION (this);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   847
  return m_hardLimit;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   848
}
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 6821
diff changeset
   849
7386
2310ed220a61 standardize ns-3 namespace declaration format
Vedran Miletić <rivanvx@gmail.com>
parents: 7383
diff changeset
   850
} // namespace ns3