src/core/model/realtime-simulator-impl.h
author Peter D. Barnes, Jr. <barnes26@llnl.gov>
Mon, 15 Jul 2013 14:19:57 -0700
changeset 9949 254133001f4f
parent 9868 ce6f889c7c1c
child 11091 4bbd7b9a2889
permissions -rw-r--r--
Fix strict aliasing warning.
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
#ifndef REALTIME_SIMULATOR_IMPL_H
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    20
#define REALTIME_SIMULATOR_IMPL_H
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    21
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    22
#include "simulator-impl.h"
7780
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7383
diff changeset
    23
#include "system-thread.h"
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    24
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    25
#include "scheduler.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    26
#include "synchronizer.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    27
#include "event-impl.h"
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    28
7383
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 6821
diff changeset
    29
#include "ptr.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 6821
diff changeset
    30
#include "assert.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 6821
diff changeset
    31
#include "log.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 6821
diff changeset
    32
#include "system-mutex.h"
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    33
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    34
#include <list>
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
namespace ns3 {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
9868
ce6f889c7c1c doxygenate simulator group
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 7796
diff changeset
    38
/**
ce6f889c7c1c doxygenate simulator group
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 7796
diff changeset
    39
 * \ingroup simulator
ce6f889c7c1c doxygenate simulator group
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 7796
diff changeset
    40
 */
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
class RealtimeSimulatorImpl : public SimulatorImpl
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    42
{
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
public:
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
  static TypeId GetTypeId (void);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
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
   * Enumeration of the types of packets supported in the class.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
   *
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    49
   */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
  enum SynchronizationMode {
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    51
    SYNC_BEST_EFFORT, /** Make a best effort to keep synced to real-time */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    52
    SYNC_HARD_LIMIT, /** Keep to real-time within a tolerance or die trying */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
  };
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    54
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
  RealtimeSimulatorImpl ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    56
  ~RealtimeSimulatorImpl ();
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
3808
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    58
  virtual void Destroy ();
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    59
  virtual bool IsFinished (void) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    60
  virtual void Stop (void);
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
    61
  virtual void Stop (Time const &time);
3808
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    62
  virtual EventId Schedule (Time const &time, EventImpl *event);
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
    63
  virtual void ScheduleWithContext (uint32_t context, Time const &time, EventImpl *event);
3808
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    64
  virtual EventId ScheduleNow (EventImpl *event);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    65
  virtual EventId ScheduleDestroy (EventImpl *event);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    66
  virtual void Remove (const EventId &ev);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    67
  virtual void Cancel (const EventId &ev);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    68
  virtual bool IsExpired (const EventId &ev) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    69
  virtual void Run (void);
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    70
  virtual Time Now (void) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    71
  virtual Time GetDelayLeft (const EventId &id) const;
ace932ee6eef avoid using Ptr<EventImpl>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3801
diff changeset
    72
  virtual Time GetMaximumSimulationTime (void) const;
5507
915abd2b907b Simulator::SetScheduler now takes an ObjectFactory
Guillaume Seguin <guillaume@segu.in>
parents: 4552
diff changeset
    73
  virtual void SetScheduler (ObjectFactory schedulerFactory);
6113
0ce37bf4f1c1 Merge distributed simulation code
Josh Pelkey <jpelkey@gatech.edu>
parents: 5872
diff changeset
    74
  virtual uint32_t GetSystemId (void) const; 
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
    75
  virtual uint32_t GetContext (void) const;
3809
5e007004402e move MakeEvent out of Simulator
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3808
diff changeset
    76
5822
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
    77
  void ScheduleRealtimeWithContext (uint32_t context, Time const &time, EventImpl *event);
3816
edd4928b2046 remove realtime public API.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3812
diff changeset
    78
  void ScheduleRealtime (Time const &time, EventImpl *event);
5822
c16bcd1c6647 Get emu working again: Add Dix/Llc option, add and use contextual realtime schedule ops, don't refcount realtime simulator impl
Craig Dowell <craigdo@ee.washington.edu>
parents: 5521
diff changeset
    79
  void ScheduleRealtimeNowWithContext (uint32_t context, EventImpl *event);
3816
edd4928b2046 remove realtime public API.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 3812
diff changeset
    80
  void ScheduleRealtimeNow (EventImpl *event);
3801
dc1f5e534e56 slight reorganization and naming change for added realtime methods
Craig Dowell <craigdo@ee.washington.edu>
parents: 3798
diff changeset
    81
  Time RealtimeNow (void) const;
3560
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
  void SetSynchronizationMode (RealtimeSimulatorImpl::SynchronizationMode mode);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    84
  RealtimeSimulatorImpl::SynchronizationMode GetSynchronizationMode (void) const;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    85
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
  void SetHardLimit (Time limit);
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
  Time GetHardLimit (void) const;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
private:
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    90
  bool Running (void) const;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    91
  bool Realtime (void) const;
7796
369bf20998e8 bug 603: Simulator::Next is useless
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 7780
diff changeset
    92
  uint64_t NextTs (void) const;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
  void ProcessOneEvent (void);
5872
188ecd74fcb4 It is not legal to call Dispose from within a destructor
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 5822
diff changeset
    94
  virtual void DoDispose (void);
3560
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
  typedef std::list<EventId> DestroyEvents;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    97
  DestroyEvents m_destroyEvents;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    98
  bool m_stop;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    99
  bool m_running;
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
  // The following variables are protected using the m_mutex
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   102
  Ptr<Scheduler> m_events;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   103
  int m_unscheduledEvents;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
  uint32_t m_uid;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   105
  uint32_t m_currentUid;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   106
  uint64_t m_currentTs;
5521
37c6c83d4252 Introduce Simulator::ScheduleWithContext* and Simulator::GetContext
Guillaume Seguin <guillaume@segu.in>
parents: 5507
diff changeset
   107
  uint32_t m_currentContext;
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
  mutable SystemMutex m_mutex;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   110
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   111
  Ptr<Synchronizer> m_synchronizer;
3561
e388935fa948 ifdef out ability to feed real-time through simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 3560
diff changeset
   112
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   113
  /**
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   114
   * The policy to use if the simulation cannot keep synchronized to real-time.
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   115
   */
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   116
  SynchronizationMode m_synchronizationMode;
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   117
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   118
  /**
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   119
   * The maximum allowable drift from real-time in SYNC_HARD_LIMIT mode.
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
  Time m_hardLimit;
7780
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7383
diff changeset
   122
69c3c67b1b97 Finally enable a thread-safe ScheduleWithContext
Alina Quereilhac <alina.quereilhac@inria.fr>
parents: 7383
diff changeset
   123
  SystemThread::ThreadId m_main;
3560
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   124
};
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
5aa65b1ea001 add realtime simulator and associated tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   126
} // namespace ns3
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
#endif /* REALTIME_SIMULATOR_IMPL_H */