2008-10-10 15:24:56 -07:00
|
|
|
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
|
|
|
|
|
|
|
|
|
#include "ns3/simulator.h"
|
2008-10-27 12:47:33 +01:00
|
|
|
#include "ns3/realtime-simulator-impl.h"
|
2008-10-10 15:24:56 -07:00
|
|
|
#include "ns3/nstime.h"
|
|
|
|
|
#include "ns3/log.h"
|
|
|
|
|
#include "ns3/system-thread.h"
|
|
|
|
|
#include "ns3/string.h"
|
|
|
|
|
#include "ns3/config.h"
|
|
|
|
|
#include "ns3/global-value.h"
|
2008-10-27 12:47:33 +01:00
|
|
|
#include "ns3/ptr.h"
|
2008-10-10 15:24:56 -07:00
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
|
|
using namespace ns3;
|
|
|
|
|
|
|
|
|
|
NS_LOG_COMPONENT_DEFINE ("TestSync");
|
|
|
|
|
|
|
|
|
|
bool gFirstRun = false;
|
|
|
|
|
|
2008-10-27 12:47:33 +01:00
|
|
|
void
|
2008-10-10 15:24:56 -07:00
|
|
|
inserted_function (void)
|
|
|
|
|
{
|
|
|
|
|
NS_ASSERT (gFirstRun);
|
|
|
|
|
NS_LOG_UNCOND ("inserted_function() called at " <<
|
|
|
|
|
Simulator::Now ().GetSeconds () << " s");
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-27 12:47:33 +01:00
|
|
|
void
|
2008-10-10 15:24:56 -07:00
|
|
|
background_function (void)
|
|
|
|
|
{
|
|
|
|
|
NS_ASSERT (gFirstRun);
|
|
|
|
|
NS_LOG_UNCOND ("background_function() called at " <<
|
|
|
|
|
Simulator::Now ().GetSeconds () << " s");
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-27 12:47:33 +01:00
|
|
|
void
|
2008-10-10 15:24:56 -07:00
|
|
|
first_function (void)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_UNCOND ("first_function() called at " <<
|
|
|
|
|
Simulator::Now ().GetSeconds () << " s");
|
|
|
|
|
gFirstRun = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class FakeNetDevice
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
FakeNetDevice ();
|
2008-10-10 16:43:43 -07:00
|
|
|
void Doit3 (void);
|
|
|
|
|
void Doit4 (void);
|
2008-10-10 15:24:56 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FakeNetDevice::FakeNetDevice ()
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-27 12:47:33 +01:00
|
|
|
void
|
2008-10-10 16:43:43 -07:00
|
|
|
FakeNetDevice::Doit3 (void)
|
2008-10-10 15:24:56 -07:00
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
|
|
|
|
sleep (1);
|
2008-10-29 22:52:22 -07:00
|
|
|
for (uint32_t i = 0; i < 10000; ++i)
|
2008-10-10 15:24:56 -07:00
|
|
|
{
|
|
|
|
|
//
|
2008-10-10 16:43:43 -07:00
|
|
|
// Exercise the realtime relative now path
|
|
|
|
|
//
|
2008-10-27 12:47:33 +01:00
|
|
|
DynamicCast<RealtimeSimulatorImpl> (Simulator::GetImplementation ())->ScheduleRealtimeNow (MakeEvent (&inserted_function));
|
2008-10-10 16:43:43 -07:00
|
|
|
usleep (1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-27 12:47:33 +01:00
|
|
|
void
|
2008-10-10 16:43:43 -07:00
|
|
|
FakeNetDevice::Doit4 (void)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION_NOARGS ();
|
|
|
|
|
sleep (1);
|
2008-10-29 22:52:22 -07:00
|
|
|
for (uint32_t i = 0; i < 10000; ++i)
|
2008-10-10 16:43:43 -07:00
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
// Exercise the realtime relative schedule path
|
|
|
|
|
//
|
2008-10-27 12:47:33 +01:00
|
|
|
DynamicCast<RealtimeSimulatorImpl> (Simulator::GetImplementation ())->ScheduleRealtime (Seconds (0), MakeEvent (&inserted_function));
|
2008-10-10 15:24:56 -07:00
|
|
|
usleep (1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-27 12:47:33 +01:00
|
|
|
void
|
2008-10-10 15:24:56 -07:00
|
|
|
test (void)
|
|
|
|
|
{
|
|
|
|
|
GlobalValue::Bind ("SimulatorImplementationType",
|
|
|
|
|
StringValue ("ns3::RealtimeSimulatorImpl"));
|
|
|
|
|
|
|
|
|
|
FakeNetDevice fnd;
|
|
|
|
|
|
|
|
|
|
//
|
2008-10-15 14:51:16 +02:00
|
|
|
// Make sure ScheduleNow works when the system isn't running
|
2008-10-10 15:24:56 -07:00
|
|
|
//
|
2008-10-27 12:47:33 +01:00
|
|
|
DynamicCast<RealtimeSimulatorImpl> (Simulator::GetImplementation ())->ScheduleRealtimeNow(MakeEvent (&first_function));
|
2008-10-10 15:24:56 -07:00
|
|
|
|
|
|
|
|
//
|
2008-10-29 22:52:22 -07:00
|
|
|
// drive the progression of m_currentTs at a ten millisecond rate from the main thread
|
2008-10-10 15:24:56 -07:00
|
|
|
//
|
|
|
|
|
for (double d = 0.; d < 14.999; d += 0.01)
|
|
|
|
|
{
|
|
|
|
|
Simulator::Schedule (Seconds (d), &background_function);
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-10 16:43:43 -07:00
|
|
|
Ptr<SystemThread> st3 = Create<SystemThread> (
|
|
|
|
|
MakeCallback (&FakeNetDevice::Doit3, &fnd));
|
|
|
|
|
st3->Start ();
|
|
|
|
|
|
|
|
|
|
Ptr<SystemThread> st4 = Create<SystemThread> (
|
|
|
|
|
MakeCallback (&FakeNetDevice::Doit4, &fnd));
|
|
|
|
|
st4->Start ();
|
|
|
|
|
|
2008-10-10 15:24:56 -07:00
|
|
|
Simulator::Stop (Seconds (15.0));
|
|
|
|
|
Simulator::Run ();
|
2008-10-10 16:43:43 -07:00
|
|
|
st3->Join ();
|
|
|
|
|
st4->Join ();
|
2008-10-10 15:24:56 -07:00
|
|
|
Simulator::Destroy ();
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-27 12:47:33 +01:00
|
|
|
int
|
2008-10-10 15:24:56 -07:00
|
|
|
main (int argc, char *argv[])
|
|
|
|
|
{
|
2008-10-27 12:47:33 +01:00
|
|
|
while (true)
|
2008-10-10 15:24:56 -07:00
|
|
|
{
|
|
|
|
|
test ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|