bug 1000: Make RealtimeSimulatorImpl last until stop
authorMathieu Lacage <mathieu.lacage@cutebugs.net>
Mon, 26 Mar 2012 06:13:16 -0400
changeset 7794 3b9223ce47df
parent 7793 1c81a8a91b1e
child 7795 c1740487ced6
bug 1000: Make RealtimeSimulatorImpl last until stop
examples/realtime/realtime-udp-echo.cc
src/core/model/realtime-simulator-impl.cc
--- a/examples/realtime/realtime-udp-echo.cc	Sun Mar 25 14:26:45 2012 +0200
+++ b/examples/realtime/realtime-udp-echo.cc	Mon Mar 26 06:13:16 2012 -0400
@@ -113,6 +113,7 @@
   //
   // Now, do the actual simulation.
   //
+  Simulator::Stop (Seconds (11.0));
   NS_LOG_INFO ("Run Simulation.");
   Simulator::Run ();
   Simulator::Destroy ();
--- a/src/core/model/realtime-simulator-impl.cc	Sun Mar 25 14:26:45 2012 +0200
+++ b/src/core/model/realtime-simulator-impl.cc	Mon Mar 26 06:13:16 2012 -0400
@@ -433,27 +433,34 @@
   m_running = true;
   m_synchronizer->SetOrigin (m_currentTs);
 
-  for (;;) 
+  // Sleep until signalled
+  uint64_t tsNow;
+  uint64_t tsDelay = 1000000000; // wait time of 1 second (in nanoseconds)
+ 
+  while (!m_stop) 
     {
-      bool done = false;
-
+      bool process = false;
       {
         CriticalSection cs (m_mutex);
-        //
-        // In all cases we stop when the event list is empty.  If you are doing a 
-        // realtime simulation and you want it to extend out for some time, you must
-        // call StopAt.  In the realtime case, this will stick a placeholder event out
-        // at the end of time.
-        //
-        if (m_stop || m_events->IsEmpty ())
+
+        if (!m_events->IsEmpty ())
           {
-            done = true;
+            process = true;
+          }
+        else
+          {
+            // Get current timestamp while holding the critical section
+            tsNow = m_synchronizer->GetCurrentRealtime ();
           }
       }
+ 
+      if (!process)
+        {
+          // Sleep until signalled
+          tsNow = m_synchronizer->Synchronize (tsNow, tsDelay);
 
-      if (done)
-        {
-          break;
+          // Re-check event queue
+          continue;
         }
 
       ProcessOneEvent ();