Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1394)

Unified Diff: src/simulator/default-simulator-impl.cc

Issue 2334041: thread-safe implementation of ScheduleWithContext
Patch Set: Created 13 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/simulator/default-simulator-impl.h ('k') | src/simulator/realtime-simulator-impl.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/simulator/default-simulator-impl.cc
===================================================================
--- a/src/simulator/default-simulator-impl.cc
+++ b/src/simulator/default-simulator-impl.cc
@@ -27,6 +27,7 @@
#include "ns3/pointer.h"
#include "ns3/assert.h"
#include "ns3/log.h"
+#include "ns3/tls-data.h"
#include <math.h>
@@ -127,6 +128,8 @@
m_currentUid = next.key.m_uid;
next.impl->Invoke ();
next.impl->Unref ();
+
+ ProcessEventsWithContext ();
}
bool
@@ -152,6 +155,9 @@
void
DefaultSimulatorImpl::Run (void)
{
+ TlsSetInRun (true);
+ ProcessEventsWithContext ();
+
m_stop = false;
while (!m_events->IsEmpty () && !m_stop)
{
@@ -161,12 +167,38 @@
// If the simulator stopped naturally by lack of events, make a
// consistency test to check that we didn't lose any events along the way.
NS_ASSERT(!m_events->IsEmpty () || m_unscheduledEvents == 0);
+
+ TlsSetInRun (false);
}
void
+DefaultSimulatorImpl::ProcessEventsWithContext (void)
+{
+ m_eventsWithContextMutex.Lock ();
+ while (!m_eventsWithContext.empty ())
+ {
+ EventWithContext event = m_eventsWithContext.front ();
+ m_eventsWithContext.pop_front ();
+ Scheduler::Event ev;
+ ev.impl = event.event;
+ ev.key.m_ts = m_currentTs + event.timestamp;
+ ev.key.m_context = event.context;
+ ev.key.m_uid = m_uid;
+ m_uid++;
+ m_unscheduledEvents++;
+ m_events->Insert (ev);
+ }
+ m_eventsWithContextMutex.Unlock ();
+}
+
+
+void
DefaultSimulatorImpl::RunOneEvent (void)
{
+ TlsSetInRun (true);
+ ProcessEventsWithContext ();
ProcessOneEvent ();
+ TlsSetInRun (false);
}
void
@@ -207,14 +239,27 @@
{
NS_LOG_FUNCTION (this << context << time.GetTimeStep () << m_currentTs << event);
- Scheduler::Event ev;
- ev.impl = event;
- ev.key.m_ts = m_currentTs + time.GetTimeStep ();
- ev.key.m_context = context;
- ev.key.m_uid = m_uid;
- m_uid++;
- m_unscheduledEvents++;
- m_events->Insert (ev);
+ if (TlsIsInRun ())
+ {
+ Scheduler::Event ev;
+ ev.impl = event;
+ ev.key.m_ts = m_currentTs + time.GetTimeStep ();
+ ev.key.m_context = context;
+ ev.key.m_uid = m_uid;
+ m_uid++;
+ m_unscheduledEvents++;
+ m_events->Insert (ev);
+ }
+ else
+ {
+ struct EventWithContext ev;
+ ev.context = context;
+ ev.timestamp = time.GetTimeStep ();
+ ev.event = event;
+ m_eventsWithContextMutex.Lock ();
+ m_eventsWithContext.push_back (ev);
+ m_eventsWithContextMutex.Unlock ();
+ }
}
EventId
« no previous file with comments | « src/simulator/default-simulator-impl.h ('k') | src/simulator/realtime-simulator-impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b