src/simulator/realtime-simulator-impl.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed, 15 Oct 2008 13:05:33 +0200
changeset 3807 268b86ce5435
parent 3802 b796f3c38e3e
child 3808 ace932ee6eef
permissions -rw-r--r--
don't use EventId in Schedulers: use Scheduler::Event instead.
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
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    26
#include "ns3/ptr.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    27
#include "ns3/pointer.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    28
#include "ns3/assert.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    29
#include "ns3/fatal-error.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    30
#include "ns3/log.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    31
#include "ns3/system-mutex.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    32
#include "ns3/boolean.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    33
#include "ns3/enum.h"
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
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    36
#include <math.h>
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
NS_LOG_COMPONENT_DEFINE ("RealtimeSimulatorImpl");
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
namespace ns3 {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    42
NS_OBJECT_ENSURE_REGISTERED (RealtimeSimulatorImpl);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
TypeId
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
RealtimeSimulatorImpl::GetTypeId (void)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
  static TypeId tid = TypeId ("ns3::RealtimeSimulatorImpl")
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
    .SetParent<Object> ()
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    49
    .AddConstructor<RealtimeSimulatorImpl> ()
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
    .AddAttribute ("SynchronizationMode", 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    51
                   "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
    52
                   EnumValue (SYNC_BEST_EFFORT),
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
                   MakeEnumAccessor (&RealtimeSimulatorImpl::SetSynchronizationMode),
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    54
                   MakeEnumChecker (SYNC_BEST_EFFORT, "BestEffort",
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
                                    SYNC_HARD_LIMIT, "HardLimit"))
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    56
    .AddAttribute ("HardLimit", 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
                   "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
    58
                   TimeValue (Seconds (0.1)),
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    59
                   MakeTimeAccessor (&RealtimeSimulatorImpl::m_hardLimit),
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    60
                   MakeTimeChecker ())
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    61
    ;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    62
  return tid;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    63
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    64
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    65
void
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    66
RealtimeEventLock::Lock (void)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    67
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    68
  m_eventMutex.Lock ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    69
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    70
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    71
void
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    72
RealtimeEventLock::Unlock (void)
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
  m_eventMutex.Unlock ();
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
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    77
RealtimeSimulatorImpl::RealtimeSimulatorImpl ()
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    78
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    79
  NS_LOG_FUNCTION_NOARGS ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    80
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    81
  EventImpl::SetEventLock (&m_eventLock);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    82
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    83
  m_stop = false;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    84
  m_stopAt = 0;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    85
  m_running = false;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
  // uids are allocated from 4.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
  // uid 0 is "invalid" events
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
  // uid 1 is "now" events
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
  // uid 2 is "destroy" events
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    90
  m_uid = 4; 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    91
  // 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
    92
  m_currentUid = 0;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
  m_currentTs = 0;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    94
  m_unscheduledEvents = 0;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
  // 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
    97
  // 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
    98
  m_synchronizer = CreateObject<WallClockSynchronizer> ();
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
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   101
RealtimeSimulatorImpl::~RealtimeSimulatorImpl ()
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   102
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   103
  NS_LOG_FUNCTION_NOARGS ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
  while (m_events->IsEmpty () == false)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   105
    {
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   106
      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
   107
      next.impl->Unref ();
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   108
    }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   109
  m_events = 0;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   110
  m_synchronizer = 0;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   111
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   112
  EventImpl::SetNoEventLock ();
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
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   115
void
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   116
RealtimeSimulatorImpl::Destroy ()
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
  NS_LOG_FUNCTION_NOARGS ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   119
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
  //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   121
  // 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
   122
  // 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
   123
  // 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
   124
  // 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
   125
  // 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
   126
  // Simulator::Destroy().
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   127
  //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   128
  while (m_destroyEvents.empty () == false) 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   129
    {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   130
      Ptr<EventImpl> ev = m_destroyEvents.front ().PeekEventImpl ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   131
      m_destroyEvents.pop_front ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   132
      NS_LOG_LOGIC ("handle destroy " << ev);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   133
      if (ev->IsCancelled () == false)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   134
        {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   135
          ev->Invoke ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   136
        }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   137
    }
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
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   140
void
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   141
RealtimeSimulatorImpl::SetScheduler (Ptr<Scheduler> scheduler)
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
  NS_LOG_FUNCTION_NOARGS ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   144
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   145
  { 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   146
    CriticalSection cs (m_mutex);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   147
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   148
    if (m_events != 0)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   149
      {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   150
        while (m_events->IsEmpty () == false)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   151
          {
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   152
            Scheduler::Event next = m_events->RemoveNext ();
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   153
            scheduler->Insert (next);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   154
          }
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
    m_events = scheduler;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   157
  }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   158
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   159
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   160
Ptr<Scheduler>
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   161
RealtimeSimulatorImpl::GetScheduler (void) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   162
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   163
  NS_LOG_FUNCTION_NOARGS ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   164
  return m_events;
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
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   167
void
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   168
RealtimeSimulatorImpl::ProcessOneEvent (void)
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
  NS_LOG_FUNCTION_NOARGS ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   171
  //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   172
  // The idea here is to wait until the next event comes due.  In the case of
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
   173
  // a realtime simulation, we want real time to be consumed between events.  
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   174
  // 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
   175
  // doing some kind of a wait.
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   176
  //
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
   177
  // 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
   178
  // cause us to re-evaluate our state.  The way this works is that the synchronizer
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
  // gets interrupted and returs.  So, there is a possibility that things may change
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
  // 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
   181
  // 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
   182
  // 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
   183
  //
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
   184
  // 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
   185
  // 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
   186
  // 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
   187
  // 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
   188
  // 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
   189
  //
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
  for (;;) 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   192
    {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   193
      uint64_t tsDelay = 0;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   194
      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
   195
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   196
      //
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
   197
      // 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
   198
      // 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
   199
      // 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
   200
      // 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
   201
      //  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
   202
      // 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
   203
      // 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
   204
      // 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
   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
      // 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
   207
      //
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   208
      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
   209
3560
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
        CriticalSection cs (m_mutex);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   212
        //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   213
        // 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
   214
        // 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
   215
        // 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
   216
        // 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
   217
        // certainly elapsed since it was last updated.
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   218
        //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   219
        // 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
   220
        // 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
   221
        //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   222
        NS_ASSERT_MSG (m_synchronizer->Realtime (), 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   223
          "RealtimeSimulatorImpl::ProcessOneEvent (): Synchronizer reports not Realtime ()");
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   224
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
   225
        //
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
        // 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
   227
        // 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
   228
        // 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
   229
        //
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   230
        // 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
   231
        //
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   232
        tsNow = m_synchronizer->GetCurrentRealtime ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   233
        tsNext = NextTs ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   234
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
   235
        //
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
        // 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
   237
        // 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
   238
        // 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
   239
        // 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
   240
        //
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
        // 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
   242
        // 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
   243
        //
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   244
        if (tsNext <= tsNow)
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
            tsDelay = 0;
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
        else
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   249
          {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   250
            tsDelay = tsNext - tsNow;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   251
          }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   252
      
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
   253
        //
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
        // 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
   255
        // 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
   256
        // 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
   257
        // 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
   258
        // 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
   259
        //
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   260
        m_synchronizer->SetCondition (false);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   261
      }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   262
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   263
      //
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
   264
      // 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
   265
      // 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
   266
      // 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
   267
      // 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
   268
      // 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
   269
      // 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
   270
      // below to return immediately.
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   271
      //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   272
      // 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
   273
      // 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
   274
      // 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
   275
      // 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
   276
      // inside the critical section above. 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   277
      //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   278
      // 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
   279
      // 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
   280
      // 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
   281
      // 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
   282
      // 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
   283
      // 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
   284
      // 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
   285
      // 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
   286
      // 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
   287
      //
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
   288
      // 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
   289
      // 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
   290
      // 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
   291
      // event happens that requires a re-schedule, Synchronize returns false 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
   292
      // 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
   293
      //
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
   294
      // 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
   295
      // waits.
3560
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
      if (m_synchronizer->Synchronize (tsNow, tsDelay))
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   298
        {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   299
          NS_LOG_LOGIC ("Interrupted ...");
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   300
          break;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   301
        }
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
   302
 
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
      //
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
      // 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
   305
      // 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
   306
      // 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
   307
      // 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
   308
      //
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   309
    }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   310
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   311
  //
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
   312
  // 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
   313
  // 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
   314
  // 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
   315
  // 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
   316
  // 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
   317
  // 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
   318
  //
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   319
  Scheduler::Event next;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   320
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   321
  { 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   322
    CriticalSection cs (m_mutex);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   323
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
   324
    // 
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
    // 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
   326
    // 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
   327
    // 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
   328
    // 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
   329
    //
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   330
    NS_ASSERT_MSG (m_events->IsEmpty () == false, 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   331
      "RealtimeSimulatorImpl::ProcessOneEvent(): event queue is empty");
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   332
    next = m_events->RemoveNext ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   333
    --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
   334
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
    //
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
    // 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
   337
    // 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
   338
    // 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
   339
    //
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   340
    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
   341
                   "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
   342
                   "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
   343
    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
   344
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
    // 
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
    // 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
   347
    // 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
   348
    // 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
   349
    //
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_currentTs = next.key.m_ts;
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   351
    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
   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
    // 
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
    // 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
   355
    // 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
   356
    // 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
   357
    // 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
   358
    //
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
    // 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
   360
    // 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
   361
    //
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
    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
   363
      {
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 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
   365
        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
   366
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
        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
   368
          {
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
            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
   370
          }
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
        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
   372
          {
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
            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
   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
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
        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
   377
          {
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
            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
   379
                            "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
   380
          }
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   381
      }
3560
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
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   384
  //
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
   385
  // 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
   386
  // 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
   387
  // changing things out from under us.
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   388
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   389
  EventImpl *event = next.impl;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   390
  m_synchronizer->EventStart ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   391
  event->Invoke ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   392
  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
   393
  event->Unref ();
3560
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
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   396
bool 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   397
RealtimeSimulatorImpl::IsFinished (void) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   398
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   399
  NS_LOG_FUNCTION_NOARGS ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   400
  bool rc;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   401
  {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   402
    CriticalSection cs (m_mutex);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   403
    rc = m_events->IsEmpty ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   404
  }
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
  return rc;
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
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
// 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
   411
//
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   412
uint64_t
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   413
RealtimeSimulatorImpl::NextTs (void) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   414
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   415
  NS_LOG_FUNCTION_NOARGS ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   416
  NS_ASSERT_MSG (m_events->IsEmpty () == false, 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   417
    "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
   418
  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
   419
  return ev.key.m_ts;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   420
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   421
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   422
//
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   423
// Calls NextTs().  Should be called with critical section locked.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   424
//
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   425
Time
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   426
RealtimeSimulatorImpl::Next (void) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   427
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   428
  NS_LOG_FUNCTION_NOARGS ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   429
  return TimeStep (NextTs ());
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   430
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   431
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   432
void
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   433
RealtimeSimulatorImpl::Run (void)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   434
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   435
  NS_LOG_FUNCTION_NOARGS ();
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
   436
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
   437
  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
   438
                 "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
   439
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   440
  m_running = true;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   441
  m_synchronizer->SetOrigin (m_currentTs);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   442
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   443
  for (;;) 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   444
    {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   445
      bool done = false;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   446
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   447
      {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   448
        CriticalSection cs (m_mutex);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   449
        //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   450
        // In all cases we stop when the event list is empty.  If you are doing a 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   451
        // realtime simulation and you want it to extend out for some time, you must
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   452
        // call StopAt.  In the realtime case, this will stick a placeholder event out
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   453
        // at the end of time.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   454
        //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   455
        if (m_stop || m_events->IsEmpty ())
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   456
          {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   457
            done = true;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   458
          }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   459
        //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   460
        // We also want to stop the simulator at some time even if there are events 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   461
        // that have been scheduled out in the future.  If we're in realtime mode, we 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   462
        // actually have time passing, so we must look at the realtime clock to see if 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   463
        // we're past the end time.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   464
        //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   465
        if (m_stopAt && m_stopAt <= m_synchronizer->GetCurrentRealtime ())
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
            done = true;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   468
          }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   469
      }
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
      if (done)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   472
        {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   473
          break;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   474
        }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   475
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   476
      ProcessOneEvent ();
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
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
  // 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
   481
  // 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
   482
  //
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
    CriticalSection cs (m_mutex);
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
    NS_ASSERT_MSG (m_events->IsEmpty () == false || m_unscheduledEvents == 0,
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   487
      "RealtimeSimulatorImpl::Run(): Empty queue and unprocessed events");
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   488
  }
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
  m_running = false;
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
bool
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   494
RealtimeSimulatorImpl::Running (void) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   495
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   496
  NS_LOG_FUNCTION_NOARGS ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   497
  return m_running;
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
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   500
bool
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   501
RealtimeSimulatorImpl::Realtime (void) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   502
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   503
  NS_LOG_FUNCTION_NOARGS ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   504
  return m_synchronizer->Realtime ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   505
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   506
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
   507
//
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
   508
// This will run the first event on the queue without considering any realtime
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
   509
// synchronization.  It's mainly implemented to allow simulations requiring
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
   510
// the multithreaded ScheduleRealtimeNow() functions the possibility of driving
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
   511
// the simulation from their own event loop.
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
   512
//
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
   513
// It is expected that if there are any realtime requirements, the responsibility
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
   514
// for synchronizing with real time in an external event loop will be picked up
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
   515
// by that loop.  For example, they may call Simulator::Next() to find the 
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
   516
// execution time of the next event and wait for that time somehow -- then call
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
   517
// RunOneEvent to fire the event.
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
   518
// 
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   519
void
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   520
RealtimeSimulatorImpl::RunOneEvent (void)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   521
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   522
  NS_LOG_FUNCTION_NOARGS ();
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
   523
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
   524
  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
   525
                 "RealtimeSimulatorImpl::RunOneEvent(): An internal simulator event loop is 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
   526
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
   527
  EventImpl *event = 0;
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
   528
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
   529
  //
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
   530
  // Run this in a critical section in case there's another thread around that
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
   531
  // may be inserting things onto the event list.
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
   532
  //
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
   533
  {
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
   534
    CriticalSection cs (m_mutex);
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
   535
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   536
    Scheduler::Event next = m_events->RemoveNext ();
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
   537
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   538
    NS_ASSERT (next.key.m_ts >= m_currentTs);
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
   539
    --m_unscheduledEvents;
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
   540
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   541
    NS_LOG_LOGIC ("handle " << next.key.m_ts);
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   542
    m_currentTs = next.key.m_ts;
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   543
    m_currentUid = next.key.m_ts;
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   544
    event = next.impl;
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
   545
  }
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
   546
  event->Invoke ();
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   547
  event->Unref ();
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   548
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   549
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   550
void 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   551
RealtimeSimulatorImpl::Stop (void)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   552
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   553
  NS_LOG_FUNCTION_NOARGS ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   554
  m_stop = true;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   555
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   556
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   557
static void Placeholder (void) {}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   558
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
   559
//
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   560
// Schedule a stop for a _relative_ time in the future.  If the simulation
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   561
// hasn't started yet, this will effectively be an absolute 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
   562
//
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   563
void 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   564
RealtimeSimulatorImpl::Stop (Time const &time)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   565
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   566
  NS_LOG_FUNCTION (time);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   567
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
   568
  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
   569
  NS_ASSERT (tAbsolute.IsPositive ());
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   570
  NS_ASSERT (tAbsolute >= TimeStep (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
   571
  m_stopAt = tAbsolute.GetTimeStep ();
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
   572
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   573
  //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   574
  // For the realtime case, we need a real event sitting out at the end of time
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   575
  // to keep the simulator running (sleeping) while there are no other events 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   576
  // present.  If an "external" device in another thread decides to schedule an
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   577
  // event, the sleeping synchronizer will be awakened and the new event will
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   578
  // be run.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   579
  //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   580
  // The easiest thing to do is to call back up into the simulator to take 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   581
  // advantage of all of the nice event wrappers.  This will call back down into
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
   582
  // RealtimeSimulatorImpl::Schedule to do the work.  This path interprets 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
   583
  // time as relative, so pass the relative time.
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   584
  //
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
   585
  Simulator::Schedule (time, &Placeholder);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   586
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   587
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
   588
//
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   589
// 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
   590
//
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   591
EventId
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   592
RealtimeSimulatorImpl::Schedule (Time const &time, const Ptr<EventImpl> &impl)
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   593
{
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   594
  NS_LOG_FUNCTION (time << impl);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   595
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   596
  Scheduler::Event ev;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   597
  {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   598
    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
   599
    //
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   600
    // 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
   601
    // 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
   602
    // 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
   603
    // 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
   604
    //
75c6a3d424d9 make simulator schedule calls explicitly relative throughout. fixes multithread problem in realtime simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3565
diff changeset
   605
    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
   606
    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
   607
    NS_ASSERT_MSG (tAbsolute >= TimeStep (m_currentTs), "RealtimeSimulatorImpl::Schedule(): time < m_currentTs");
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   608
    ev.impl = GetPointer (impl);
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   609
    ev.key.m_ts = (uint64_t) tAbsolute.GetTimeStep ();
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   610
    ev.key.m_uid = m_uid;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   611
    m_uid++;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   612
    ++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
   613
    m_events->Insert (ev);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   614
    m_synchronizer->Signal ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   615
  }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   616
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   617
  return EventId (impl, ev.key.m_ts, ev.key.m_uid);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   618
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   619
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   620
EventId
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   621
RealtimeSimulatorImpl::ScheduleNow (const Ptr<EventImpl> &impl)
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   622
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   623
  NS_LOG_FUNCTION_NOARGS ();
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   624
  Scheduler::Event ev;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   625
  {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   626
    CriticalSection cs (m_mutex);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   627
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   628
    ev.impl = GetPointer (impl);
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   629
    ev.key.m_ts = m_currentTs;
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   630
    ev.key.m_uid = m_uid;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   631
    m_uid++;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   632
    ++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
   633
    m_events->Insert (ev);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   634
    m_synchronizer->Signal ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   635
  }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   636
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   637
  return EventId (impl, ev.key.m_ts, ev.key.m_uid);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   638
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   639
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
   640
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
   641
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
   642
{
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
   643
  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
   644
}
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
   645
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
   646
//
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
   647
// 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
   648
//
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
   649
EventId
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   650
RealtimeSimulatorImpl::ScheduleRealtime (Time const &time, const Ptr<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
   651
{
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   652
  NS_LOG_FUNCTION (time << impl);
3799
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   653
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   654
  Scheduler::Event ev;
3799
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   655
  {
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   656
    CriticalSection cs (m_mutex);
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   657
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   658
    uint64_t ts = m_synchronizer->GetCurrentRealtime () + time.GetTimeStep ();
3801
dc1f5e534e56 slight reorganization and naming change for added realtime methods
Craig Dowell <craigdo@ee.washington.edu>
parents: 3800
diff changeset
   659
    NS_ASSERT_MSG (ts >= m_currentTs, "RealtimeSimulatorImpl::ScheduleRealtime(): schedule for time < m_currentTs");
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   660
    ev.impl = GetPointer (impl);
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   661
    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
   662
    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
   663
    m_uid++;
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   664
    ++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
   665
    m_events->Insert (ev);
3799
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   666
    m_synchronizer->Signal ();
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   667
  }
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   668
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   669
  return EventId (impl, ev.key.m_ts, ev.key.m_uid);
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
   670
}
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
EventId
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   673
RealtimeSimulatorImpl::ScheduleRealtimeNow (const Ptr<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
   674
{
3799
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   675
  NS_LOG_FUNCTION_NOARGS ();
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   676
  Scheduler::Event ev;
3799
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   677
  {
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   678
    CriticalSection cs (m_mutex);
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   679
3800
804beebf1ba5 added test for m_running in ScheduleRealNow
Craig Dowell <craigdo@ee.washington.edu>
parents: 3799
diff changeset
   680
    //
804beebf1ba5 added test for m_running in ScheduleRealNow
Craig Dowell <craigdo@ee.washington.edu>
parents: 3799
diff changeset
   681
    // 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
   682
    // 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
   683
    // 
804beebf1ba5 added test for m_running in ScheduleRealNow
Craig Dowell <craigdo@ee.washington.edu>
parents: 3799
diff changeset
   684
    uint64_t ts = m_running ? m_synchronizer->GetCurrentRealtime () : m_currentTs;
3801
dc1f5e534e56 slight reorganization and naming change for added realtime methods
Craig Dowell <craigdo@ee.washington.edu>
parents: 3800
diff changeset
   685
    NS_ASSERT_MSG (ts >= m_currentTs, "RealtimeSimulatorImpl::ScheduleRealrimeNow(): schedule for time < m_currentTs");
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   686
    ev.impl = GetPointer (impl);
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   687
    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
   688
    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
   689
    m_uid++;
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   690
    ++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
   691
    m_events->Insert (ev);
3799
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   692
    m_synchronizer->Signal ();
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   693
  }
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   694
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   695
  return EventId (impl, ev.key.m_ts, ev.key.m_uid);
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
   696
}
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
   697
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
   698
Time
3801
dc1f5e534e56 slight reorganization and naming change for added realtime methods
Craig Dowell <craigdo@ee.washington.edu>
parents: 3800
diff changeset
   699
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
   700
{
3799
ceea04d07e60 it helps to remember to add function implementations
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
   701
  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
   702
}
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
   703
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   704
EventId
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   705
RealtimeSimulatorImpl::ScheduleDestroy (const Ptr<EventImpl> &impl)
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   706
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   707
  NS_LOG_FUNCTION_NOARGS ();
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   708
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   709
  EventId id;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   710
  {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   711
    CriticalSection cs (m_mutex);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   712
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   713
    //
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   714
    // 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
   715
    // 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
   716
    // executed at Simulator::Destroy time.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   717
    //
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   718
    id = EventId (impl, m_currentTs, 2);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   719
    m_destroyEvents.push_back (id);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   720
    m_uid++;
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
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   723
  return id;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   724
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   725
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   726
Time 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   727
RealtimeSimulatorImpl::GetDelayLeft (const EventId &id) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   728
{
3561
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   729
  //
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   730
  // 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
   731
  // 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
   732
  //
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   733
  if (IsExpired (id))
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   734
    {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   735
      return TimeStep (0);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   736
    }
3561
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   737
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   738
  return TimeStep (id.GetTs () - m_currentTs);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   739
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   740
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   741
void
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   742
RealtimeSimulatorImpl::Remove (const EventId &id)
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   743
{
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   744
  if (id.GetUid () == 2)
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   745
    {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   746
      // destroy events.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   747
      for (DestroyEvents::iterator i = m_destroyEvents.begin (); 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   748
           i != m_destroyEvents.end (); 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   749
           i++)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   750
        {
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   751
          if (*i == id)
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   752
            {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   753
              m_destroyEvents.erase (i);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   754
              break;
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
      return;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   758
    }
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   759
  if (IsExpired (id))
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   760
    {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   761
      return;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   762
    }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   763
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   764
  {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   765
    CriticalSection cs (m_mutex);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   766
3807
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   767
    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
   768
    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
   769
    event.key.m_ts = id.GetTs ();
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   770
    event.key.m_uid = id.GetUid ();
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   771
    
268b86ce5435 don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3802
diff changeset
   772
    m_events->Remove (event);
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   773
    --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
   774
    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
   775
    event.impl->Unref ();
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   776
  }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   777
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   778
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   779
void
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   780
RealtimeSimulatorImpl::Cancel (const EventId &id)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   781
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   782
  if (IsExpired (id) == false)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   783
    {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   784
      id.PeekEventImpl ()->Cancel ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   785
    }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   786
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   787
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   788
bool
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   789
RealtimeSimulatorImpl::IsExpired (const EventId &ev) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   790
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   791
  if (ev.GetUid () == 2)
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
      // destroy events.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   794
      for (DestroyEvents::const_iterator i = m_destroyEvents.begin (); 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   795
           i != m_destroyEvents.end (); i++)
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
          if (*i == ev)
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
              return false;
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
         }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   802
      return true;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   803
    }
3561
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   804
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   805
  //
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   806
  // 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
   807
  // 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
   808
  // 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
   809
  // 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
   810
  //
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   811
  // 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
   812
  //
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   813
  if (ev.PeekEventImpl () == 0 ||
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   814
      ev.GetTs () < m_currentTs ||
3561
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   815
      (ev.GetTs () == m_currentTs && ev.GetUid () <= m_currentUid) ||
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   816
      ev.PeekEventImpl ()->IsCancelled ()) 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   817
    {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   818
      return true;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   819
    }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   820
  else
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   821
    {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   822
      return false;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   823
    }
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   824
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   825
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   826
Time 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   827
RealtimeSimulatorImpl::GetMaximumSimulationTime (void) const
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
  // XXX: I am fairly certain other compilers use other non-standard
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   830
  // post-fixes to indicate 64 bit constants.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   831
  return TimeStep (0x7fffffffffffffffLL);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   832
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   833
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   834
void 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   835
RealtimeSimulatorImpl::SetSynchronizationMode (enum SynchronizationMode mode)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   836
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   837
  NS_LOG_FUNCTION (mode);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   838
  m_synchronizationMode = mode;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   839
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   840
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   841
RealtimeSimulatorImpl::SynchronizationMode
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   842
RealtimeSimulatorImpl::GetSynchronizationMode (void) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   843
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   844
  NS_LOG_FUNCTION_NOARGS ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   845
  return m_synchronizationMode;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   846
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   847
  
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   848
void 
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   849
RealtimeSimulatorImpl::SetHardLimit (Time limit)
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   850
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   851
  NS_LOG_FUNCTION (limit);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   852
  m_hardLimit = limit;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   853
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   854
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   855
Time
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   856
RealtimeSimulatorImpl::GetHardLimit (void) const
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   857
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   858
  NS_LOG_FUNCTION_NOARGS ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   859
  return m_hardLimit;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   860
}
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   861
  
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   862
}; // namespace ns3