OLD | NEW |
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 /* | 2 /* |
3 * Copyright (c) 2005,2006 INRIA | 3 * Copyright (c) 2005,2006 INRIA |
4 * | 4 * |
5 * This program is free software; you can redistribute it and/or modify | 5 * This program is free software; you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 2 as | 6 * it under the terms of the GNU General Public License version 2 as |
7 * published by the Free Software Foundation; | 7 * published by the Free Software Foundation; |
8 * | 8 * |
9 * This program is distributed in the hope that it will be useful, | 9 * This program is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with this program; if not, write to the Free Software | 15 * along with this program; if not, write to the Free Software |
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 * | 17 * |
18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> | 18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
19 */ | 19 */ |
20 | 20 |
21 #include "simulator.h" | 21 #include "simulator.h" |
22 #include "default-simulator-impl.h" | 22 #include "default-simulator-impl.h" |
23 #include "scheduler.h" | 23 #include "scheduler.h" |
24 #include "event-impl.h" | 24 #include "event-impl.h" |
25 | 25 |
26 #include "ns3/ptr.h" | 26 #include "ns3/ptr.h" |
27 #include "ns3/pointer.h" | 27 #include "ns3/pointer.h" |
28 #include "ns3/assert.h" | 28 #include "ns3/assert.h" |
29 #include "ns3/log.h" | 29 #include "ns3/log.h" |
| 30 #include "ns3/tls-data.h" |
30 | 31 |
31 #include <math.h> | 32 #include <math.h> |
32 | 33 |
33 NS_LOG_COMPONENT_DEFINE ("DefaultSimulatorImpl"); | 34 NS_LOG_COMPONENT_DEFINE ("DefaultSimulatorImpl"); |
34 | 35 |
35 namespace ns3 { | 36 namespace ns3 { |
36 | 37 |
37 NS_OBJECT_ENSURE_REGISTERED (DefaultSimulatorImpl); | 38 NS_OBJECT_ENSURE_REGISTERED (DefaultSimulatorImpl); |
38 | 39 |
39 TypeId | 40 TypeId |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 121 |
121 NS_ASSERT (next.key.m_ts >= m_currentTs); | 122 NS_ASSERT (next.key.m_ts >= m_currentTs); |
122 m_unscheduledEvents--; | 123 m_unscheduledEvents--; |
123 | 124 |
124 NS_LOG_LOGIC ("handle " << next.key.m_ts); | 125 NS_LOG_LOGIC ("handle " << next.key.m_ts); |
125 m_currentTs = next.key.m_ts; | 126 m_currentTs = next.key.m_ts; |
126 m_currentContext = next.key.m_context; | 127 m_currentContext = next.key.m_context; |
127 m_currentUid = next.key.m_uid; | 128 m_currentUid = next.key.m_uid; |
128 next.impl->Invoke (); | 129 next.impl->Invoke (); |
129 next.impl->Unref (); | 130 next.impl->Unref (); |
| 131 |
| 132 ProcessEventsWithContext (); |
130 } | 133 } |
131 | 134 |
132 bool· | 135 bool· |
133 DefaultSimulatorImpl::IsFinished (void) const | 136 DefaultSimulatorImpl::IsFinished (void) const |
134 { | 137 { |
135 return m_events->IsEmpty () || m_stop; | 138 return m_events->IsEmpty () || m_stop; |
136 } | 139 } |
137 | 140 |
138 uint64_t | 141 uint64_t |
139 DefaultSimulatorImpl::NextTs (void) const | 142 DefaultSimulatorImpl::NextTs (void) const |
140 { | 143 { |
141 NS_ASSERT (!m_events->IsEmpty ()); | 144 NS_ASSERT (!m_events->IsEmpty ()); |
142 Scheduler::Event ev = m_events->PeekNext (); | 145 Scheduler::Event ev = m_events->PeekNext (); |
143 return ev.key.m_ts; | 146 return ev.key.m_ts; |
144 } | 147 } |
145 | 148 |
146 Time | 149 Time |
147 DefaultSimulatorImpl::Next (void) const | 150 DefaultSimulatorImpl::Next (void) const |
148 { | 151 { |
149 return TimeStep (NextTs ()); | 152 return TimeStep (NextTs ()); |
150 } | 153 } |
151 | 154 |
152 void | 155 void |
153 DefaultSimulatorImpl::Run (void) | 156 DefaultSimulatorImpl::Run (void) |
154 { | 157 { |
| 158 TlsSetInRun (true); |
| 159 ProcessEventsWithContext (); |
| 160 |
155 m_stop = false; | 161 m_stop = false; |
156 while (!m_events->IsEmpty () && !m_stop)· | 162 while (!m_events->IsEmpty () && !m_stop)· |
157 { | 163 { |
158 ProcessOneEvent (); | 164 ProcessOneEvent (); |
159 } | 165 } |
160 | 166 |
161 // If the simulator stopped naturally by lack of events, make a | 167 // If the simulator stopped naturally by lack of events, make a |
162 // consistency test to check that we didn't lose any events along the way. | 168 // consistency test to check that we didn't lose any events along the way. |
163 NS_ASSERT(!m_events->IsEmpty () || m_unscheduledEvents == 0); | 169 NS_ASSERT(!m_events->IsEmpty () || m_unscheduledEvents == 0); |
| 170 |
| 171 TlsSetInRun (false); |
164 } | 172 } |
165 | 173 |
166 void | 174 void |
| 175 DefaultSimulatorImpl::ProcessEventsWithContext (void) |
| 176 { |
| 177 m_eventsWithContextMutex.Lock (); |
| 178 while (!m_eventsWithContext.empty ()) |
| 179 { |
| 180 EventWithContext event = m_eventsWithContext.front (); |
| 181 m_eventsWithContext.pop_front (); |
| 182 Scheduler::Event ev; |
| 183 ev.impl = event.event; |
| 184 ev.key.m_ts = m_currentTs + event.timestamp; |
| 185 ev.key.m_context = event.context; |
| 186 ev.key.m_uid = m_uid; |
| 187 m_uid++; |
| 188 m_unscheduledEvents++; |
| 189 m_events->Insert (ev); |
| 190 } |
| 191 m_eventsWithContextMutex.Unlock (); |
| 192 } |
| 193 |
| 194 |
| 195 void |
167 DefaultSimulatorImpl::RunOneEvent (void) | 196 DefaultSimulatorImpl::RunOneEvent (void) |
168 { | 197 { |
| 198 TlsSetInRun (true); |
| 199 ProcessEventsWithContext (); |
169 ProcessOneEvent (); | 200 ProcessOneEvent (); |
| 201 TlsSetInRun (false); |
170 } | 202 } |
171 | 203 |
172 void· | 204 void· |
173 DefaultSimulatorImpl::Stop (void) | 205 DefaultSimulatorImpl::Stop (void) |
174 { | 206 { |
175 m_stop = true; | 207 m_stop = true; |
176 } | 208 } |
177 | 209 |
178 void· | 210 void· |
179 DefaultSimulatorImpl::Stop (Time const &time) | 211 DefaultSimulatorImpl::Stop (Time const &time) |
(...skipping 20 matching lines...) Expand all Loading... |
200 m_unscheduledEvents++; | 232 m_unscheduledEvents++; |
201 m_events->Insert (ev); | 233 m_events->Insert (ev); |
202 return EventId (event, ev.key.m_ts, ev.key.m_context, ev.key.m_uid); | 234 return EventId (event, ev.key.m_ts, ev.key.m_context, ev.key.m_uid); |
203 } | 235 } |
204 | 236 |
205 void | 237 void |
206 DefaultSimulatorImpl::ScheduleWithContext (uint32_t context, Time const &time, E
ventImpl *event) | 238 DefaultSimulatorImpl::ScheduleWithContext (uint32_t context, Time const &time, E
ventImpl *event) |
207 { | 239 { |
208 NS_LOG_FUNCTION (this << context << time.GetTimeStep () << m_currentTs << even
t); | 240 NS_LOG_FUNCTION (this << context << time.GetTimeStep () << m_currentTs << even
t); |
209 | 241 |
210 Scheduler::Event ev; | 242 if (TlsIsInRun ()) |
211 ev.impl = event; | 243 { |
212 ev.key.m_ts = m_currentTs + time.GetTimeStep (); | 244 Scheduler::Event ev; |
213 ev.key.m_context = context; | 245 ev.impl = event; |
214 ev.key.m_uid = m_uid; | 246 ev.key.m_ts = m_currentTs + time.GetTimeStep (); |
215 m_uid++; | 247 ev.key.m_context = context; |
216 m_unscheduledEvents++; | 248 ev.key.m_uid = m_uid; |
217 m_events->Insert (ev); | 249 m_uid++; |
| 250 m_unscheduledEvents++; |
| 251 m_events->Insert (ev); |
| 252 } |
| 253 else |
| 254 { |
| 255 struct EventWithContext ev; |
| 256 ev.context = context; |
| 257 ev.timestamp = time.GetTimeStep (); |
| 258 ev.event = event; |
| 259 m_eventsWithContextMutex.Lock (); |
| 260 m_eventsWithContext.push_back (ev); |
| 261 m_eventsWithContextMutex.Unlock (); |
| 262 } |
218 } | 263 } |
219 | 264 |
220 EventId | 265 EventId |
221 DefaultSimulatorImpl::ScheduleNow (EventImpl *event) | 266 DefaultSimulatorImpl::ScheduleNow (EventImpl *event) |
222 { | 267 { |
223 Scheduler::Event ev; | 268 Scheduler::Event ev; |
224 ev.impl = event; | 269 ev.impl = event; |
225 ev.key.m_ts = m_currentTs; | 270 ev.key.m_ts = m_currentTs; |
226 ev.key.m_context = GetContext (); | 271 ev.key.m_context = GetContext (); |
227 ev.key.m_uid = m_uid; | 272 ev.key.m_uid = m_uid; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 | 390 |
346 uint32_t | 391 uint32_t |
347 DefaultSimulatorImpl::GetContext (void) const | 392 DefaultSimulatorImpl::GetContext (void) const |
348 { | 393 { |
349 return m_currentContext; | 394 return m_currentContext; |
350 } | 395 } |
351 | 396 |
352 } // namespace ns3 | 397 } // namespace ns3 |
353 | 398 |
354 | 399 |
OLD | NEW |