A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
main-test-sync.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 University of Washington
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18#include "ns3/command-line.h"
19#include "ns3/config.h"
20#include "ns3/global-value.h"
21#include "ns3/log.h"
22#include "ns3/nstime.h"
23#include "ns3/ptr.h"
24#include "ns3/realtime-simulator-impl.h"
25#include "ns3/simulator.h"
26#include "ns3/string.h"
27
28#include <chrono> // seconds, milliseconds
29#include <thread> // sleep_for
30
31/**
32 * \file
33 * \ingroup core-examples
34 * \ingroup scheduler
35 * An example of scheduling events in a background thread.
36 *
37 * See \ref ns3::SimulatorImpl::ScheduleWithContext
38 */
39
40using namespace ns3;
41
42NS_LOG_COMPONENT_DEFINE("TestSync");
43
44namespace
45{
46
47/** Check that the event functions run in the intended order. */
48bool gFirstRun = false;
49
50/** An event method called many times from the background thread. */
51void
52inserted_function()
53{
54 NS_ASSERT(gFirstRun);
55 NS_LOG_UNCOND("inserted_function() called at " << Simulator::Now().GetSeconds() << " s");
56}
57
58/** An event method called many times from the main thread. */
59void
60background_function()
61{
62 NS_ASSERT(gFirstRun);
63 NS_LOG_UNCOND("background_function() called at " << Simulator::Now().GetSeconds() << " s");
64}
65
66/** An event method called once from the main thread. */
67void
68first_function()
69{
70 NS_LOG_UNCOND("first_function() called at " << Simulator::Now().GetSeconds() << " s");
71 gFirstRun = true;
72}
73
74/** Example class with a method for the background task. */
75class FakeNetDevice
76{
77 public:
78 /** Constructor. */
79 FakeNetDevice();
80 /** The thread entry point. */
81 void Doit3();
82};
83
84FakeNetDevice::FakeNetDevice()
85{
87}
88
89void
90FakeNetDevice::Doit3()
91{
93 std::this_thread::sleep_for(std::chrono::seconds(1));
94
95 for (uint32_t i = 0; i < 10000; ++i)
96 {
97 //
98 // Exercise the realtime relative now path
99 //
101 Seconds(0.0),
102 MakeEvent(&inserted_function));
103 std::this_thread::sleep_for(std::chrono::milliseconds(1));
104 }
105}
106
107/**
108 * Example use of std::thread.
109 *
110 * This example is a complete simulation.
111 * It schedules \c first_function and many executions of \c background_function
112 * to execute in the main (foreground) thread. It also launches a background
113 * thread with an instance of FakeNetDevice, which schedules many instances of
114 * \c inserted_function.
115 */
116void
117test()
118{
119 GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
120
121 FakeNetDevice fnd;
122
123 //
124 // Make sure ScheduleNow works when the system isn't running
125 //
126 Simulator::ScheduleWithContext(0xffffffff, Seconds(0.0), MakeEvent(&first_function));
127
128 //
129 // drive the progression of m_currentTs at a ten millisecond rate from the main thread
130 //
131 for (double d = 0.; d < 14.999; d += 0.01)
132 {
133 Simulator::Schedule(Seconds(d), &background_function);
134 }
135
136 std::thread st3 = std::thread(&FakeNetDevice::Doit3, &fnd);
137
140
141 if (st3.joinable())
142 {
143 st3.join();
144 }
145
147}
148
149} // unnamed namespace
150
151int
152main(int argc, char* argv[])
153{
154 CommandLine cmd(__FILE__);
155 cmd.Parse(argc, argv);
156
157 while (true)
158 {
159 test();
160 }
161
162 return 0;
163}
Parse command-line arguments.
Definition: command-line.h:232
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
@ NO_CONTEXT
Flag for events not associated with any particular context.
Definition: simulator.h:210
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Hold variables of type string.
Definition: string.h:56
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
std::enable_if_t< std::is_member_pointer_v< MEM >, EventImpl * > MakeEvent(MEM mem_ptr, OBJ obj, Ts... args)
Make an EventImpl from class method members which take varying numbers of arguments.
Definition: make-event.h:144
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Every class exported by the ns3 library is enclosed in the ns3 namespace.
-ns3 Test suite for the ns3 wrapper script