Files
unison/src/core/examples/main-test-sync.cc

162 lines
3.9 KiB
C++
Raw Normal View History

2014-10-06 10:24:56 -07:00
/*
* Copyright (c) 2008 University of Washington
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
2016-09-03 01:06:28 -04:00
#include "ns3/command-line.h"
#include "ns3/config.h"
#include "ns3/global-value.h"
2022-10-07 20:08:35 +00:00
#include "ns3/log.h"
#include "ns3/nstime.h"
2008-10-27 12:47:33 +01:00
#include "ns3/ptr.h"
2022-10-07 20:08:35 +00:00
#include "ns3/realtime-simulator-impl.h"
#include "ns3/simulator.h"
#include "ns3/string.h"
2022-10-07 20:08:35 +00:00
#include <chrono> // seconds, milliseconds
#include <thread> // sleep_for
2015-01-16 11:55:23 -08:00
/**
* \file
2017-09-17 21:18:44 -07:00
* \ingroup core-examples
2015-01-16 11:55:23 -08:00
* \ingroup scheduler
* An example of scheduling events in a background thread.
*
* See \ref ns3::SimulatorImpl::ScheduleWithContext
2015-01-16 11:55:23 -08:00
*/
using namespace ns3;
2022-10-07 20:08:35 +00:00
NS_LOG_COMPONENT_DEFINE("TestSync");
2017-09-17 21:18:44 -07:00
2022-10-07 20:08:35 +00:00
namespace
{
2015-01-16 11:55:23 -08:00
/** Check that the event functions run in the intended order. */
bool gFirstRun = false;
2015-01-16 11:55:23 -08:00
/** An event method called many times from the background thread. */
void
2022-10-07 20:08:35 +00:00
inserted_function()
{
2022-10-07 20:08:35 +00:00
NS_ASSERT(gFirstRun);
NS_LOG_UNCOND("inserted_function() called at " << Simulator::Now().GetSeconds() << " s");
}
2015-01-16 11:55:23 -08:00
/** An event method called many times from the main thread. */
void
2022-10-07 20:08:35 +00:00
background_function()
{
2022-10-07 20:08:35 +00:00
NS_ASSERT(gFirstRun);
NS_LOG_UNCOND("background_function() called at " << Simulator::Now().GetSeconds() << " s");
}
2015-01-16 11:55:23 -08:00
/** An event method called once from the main thread. */
void
2022-10-07 20:08:35 +00:00
first_function()
{
2022-10-07 20:08:35 +00:00
NS_LOG_UNCOND("first_function() called at " << Simulator::Now().GetSeconds() << " s");
gFirstRun = true;
}
2015-01-16 11:55:23 -08:00
/** Example class with a method for the background task. */
class FakeNetDevice
{
2022-10-07 20:08:35 +00:00
public:
/** Constructor. */
FakeNetDevice();
/** The thread entry point. */
void Doit3();
};
2022-10-07 20:08:35 +00:00
FakeNetDevice::FakeNetDevice()
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION_NOARGS();
}
2008-10-27 12:47:33 +01:00
void
2022-10-07 20:08:35 +00:00
FakeNetDevice::Doit3()
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION_NOARGS();
std::this_thread::sleep_for(std::chrono::seconds(1));
2022-10-07 20:08:35 +00:00
for (uint32_t i = 0; i < 10000; ++i)
{
2022-10-07 20:08:35 +00:00
//
// Exercise the realtime relative now path
//
Simulator::ScheduleWithContext(Simulator::NO_CONTEXT,
Seconds(0.0),
MakeEvent(&inserted_function));
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
2015-01-16 11:55:23 -08:00
/**
* Example use of std::thread.
2015-01-16 11:55:23 -08:00
*
* This example is a complete simulation.
* It schedules \c first_function and many executions of \c background_function
* to execute in the main (foreground) thread. It also launches a background
* thread with an instance of FakeNetDevice, which schedules many instances of
* \c inserted_function.
*/
void
2022-10-07 20:08:35 +00:00
test()
{
2022-10-07 20:08:35 +00:00
GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
2022-10-07 20:08:35 +00:00
FakeNetDevice fnd;
2022-10-07 20:08:35 +00:00
//
// Make sure ScheduleNow works when the system isn't running
//
Simulator::ScheduleWithContext(0xffffffff, Seconds(0.0), MakeEvent(&first_function));
2022-10-07 20:08:35 +00:00
//
// drive the progression of m_currentTs at a ten millisecond rate from the main thread
//
for (double d = 0.; d < 14.999; d += 0.01)
{
2022-10-07 20:08:35 +00:00
Simulator::Schedule(Seconds(d), &background_function);
}
2022-10-07 20:08:35 +00:00
std::thread st3 = std::thread(&FakeNetDevice::Doit3, &fnd);
2022-10-07 20:08:35 +00:00
Simulator::Stop(Seconds(15.0));
Simulator::Run();
2022-10-07 20:08:35 +00:00
if (st3.joinable())
{
2022-10-07 20:08:35 +00:00
st3.join();
}
2022-10-07 20:08:35 +00:00
Simulator::Destroy();
}
2022-10-07 20:08:35 +00:00
} // unnamed namespace
2017-09-17 21:18:44 -07:00
2008-10-27 12:47:33 +01:00
int
2022-10-07 20:08:35 +00:00
main(int argc, char* argv[])
{
2022-10-07 20:08:35 +00:00
CommandLine cmd(__FILE__);
cmd.Parse(argc, argv);
2022-10-07 20:08:35 +00:00
while (true)
{
2022-10-07 20:08:35 +00:00
test();
}
}