Eclipse SUMO - Simulation of Urban MObility
GUIRunThread.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // The thread that runs the simulation
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <cassert>
25 #include <string>
26 #include <iostream>
27 #include <algorithm>
28 
29 #include <guisim/GUINet.h>
33 #include "GUIApplicationWindow.h"
34 #include "GUIRunThread.h"
35 #include "GUIGlobals.h"
39 #include <utils/common/SysUtils.h>
45 #include <libsumo/Simulation.h>
46 
47 
48 // ===========================================================================
49 // member method definitions
50 // ===========================================================================
52  double& simDelay, FXSynchQue<GUIEvent*>& eq,
54  : FXSingleEventThread(app, parent),
55  myNet(nullptr), myHalting(true), myQuit(false), mySimulationInProgress(false), myOk(true), myHaveSignaledEnd(false),
56  mySimDelay(simDelay), myEventQue(eq), myEventThrow(ev) {
60 }
61 
62 
64  // the thread shall stop
65  myQuit = true;
66  deleteSim();
67  delete myErrorRetriever;
68  delete myMessageRetriever;
69  delete myWarningRetriever;
70  // wait for the thread
71  while (mySimulationInProgress || myNet != nullptr);
72 }
73 
74 
75 bool
77  assert(net != 0);
78  // assign new values
79  myOk = true;
80  myNet = net;
81  mySimStartTime = start;
82  mySimEndTime = end;
83  myHaveSignaledEnd = false;
84  // register message callbacks
87  if (!OptionsCont::getOptions().getBool("no-warnings")) {
89  }
90  // preload the routes especially for TraCI
91  mySimulationLock.lock();
92  try {
93  net->setCurrentTimeStep(start);
94  net->loadRoutes();
95  } catch (ProcessError& e2) {
96  if (std::string(e2.what()) != std::string("Process Error") && std::string(e2.what()) != std::string("")) {
97  WRITE_ERROR(e2.what());
98  }
99  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
100  myHalting = true;
101  myOk = false;
102  mySimulationInProgress = false;
103 #ifndef _DEBUG
104  } catch (...) {
105  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
106  myHalting = true;
107  myOk = false;
108  mySimulationInProgress = false;
109 #endif
110  }
111  mySimulationLock.unlock();
112  return myOk;
113 }
114 
115 
116 FXint
118  long beg = 0;
119  long end = -1;
120  // perform an endless loop
121  while (!myQuit) {
122  // if the simulation shall be perfomed, do it
123  if (!myHalting && myNet != nullptr && myOk) {
125  if (end != -1) {
126  getNet().setIdleDuration((int)(beg - end));
127  }
128  // check whether we shall stop at this step
129  myBreakpointLock.lock();
130  const bool haltAfter = std::find(myBreakpoints.begin(), myBreakpoints.end(), myNet->getCurrentTimeStep()) != myBreakpoints.end();
131  myBreakpointLock.unlock();
132  // do the step
133  makeStep();
135  // stop if wished
136  if (haltAfter) {
137  stop();
138  }
139  // wait if wanted (delay is per simulated second)
140  long wait = (long)(mySimDelay * TS);
142  getNet().setSimDuration((int)(end - beg));
143  wait -= (end - beg);
144  if (wait > 0) {
145  sleep(wait);
146  }
147  } else {
148  // sleep if the simulation is not running
149  sleep(50);
150  }
151  }
152  // delete a maybe existing simulation at the end
153  deleteSim();
154  return 0;
155 }
156 
157 
158 void
160  GUIEvent* e = nullptr;
161  // simulation is being perfomed
162  mySimulationInProgress = true;
163  // execute a single step
164  try {
165  mySimulationLock.lock();
168  mySimulationLock.unlock();
169 
170  // inform parent that a step has been performed
171  e = new GUIEvent_SimulationStep();
174 
175  e = nullptr;
177  switch (state) {
185  // ensure that files are closed (deleteSim is called a bit later by the gui thread)
186  // MSNet destructor may trigger MsgHandler (via routing device cleanup). Closing output devices here is not safe
187  // OutputDevice::closeAll();
188  myHaveSignaledEnd = true;
189  }
190  break;
191  default:
192  break;
193  }
194  if (e != nullptr) {
197  myHalting = true;
198  }
199  // stop the execution when only a single step should have
200  // been performed
201  if (mySingle) {
202  myHalting = true;
203  }
204  // simulation step is over
205  mySimulationInProgress = false;
206  } catch (ProcessError& e2) {
207  if (std::string(e2.what()) != std::string("Process Error") && std::string(e2.what()) != std::string("")) {
208  WRITE_ERROR(e2.what());
209  }
210  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
211  mySimulationLock.unlock();
212  mySimulationInProgress = false;
216  myHalting = true;
217  myOk = false;
218 #ifndef _DEBUG
219  } catch (...) {
220  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
221  mySimulationLock.unlock();
222  mySimulationInProgress = false;
226  myHalting = true;
227  myOk = false;
228 #endif
229  }
230 }
231 
232 
233 void
235  mySingle = false;
236  myHalting = false;
237 }
238 
239 
240 void
242  mySingle = true;
243  myHalting = false;
244 }
245 
246 
247 void
249  // report the begin when wished
250  WRITE_MESSAGE("Simulation started with time: " + time2string(mySimStartTime));
251  myOk = true;
252 }
253 
254 
255 void
257  mySingle = false;
258  myHalting = true;
259 }
260 
261 
262 bool
264  return myNet != nullptr;
265 }
266 
267 
268 void
270  myHalting = true;
271  // remove message callbacks
275  //
276  mySimulationLock.lock();
277  if (myNet != nullptr) {
279  }
280  while (mySimulationInProgress);
281  delete myNet;
283  myNet = nullptr;
285  mySimulationLock.unlock();
287 }
288 
289 
290 GUINet&
292  return *myNet;
293 }
294 
295 
296 void
298  myHalting = true;
299  myQuit = true;
300 }
301 
302 
303 void
304 GUIRunThread::retrieveMessage(const MsgHandler::MsgType type, const std::string& msg) {
305  GUIEvent* e = new GUIEvent_Message(type, msg);
308 }
309 
310 
311 bool
313  return myNet != nullptr && myHalting;
314 }
315 
316 
317 bool
319  return myNet != nullptr && (!myHalting);
320 }
321 
322 
323 bool
325  return myNet != nullptr && myHalting;
326 }
327 
328 
329 void
332  if (mw != nullptr) {
333  for (GUIGlChildWindow* const window : mw->getViews()) {
334  window->getView()->waitForSnapshots(snapshotTime);
335  }
336  }
337 }
338 
339 
340 /****************************************************************************/
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:278
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:284
SUMOTime DELTA_T
Definition: SUMOTime.cpp:37
std::string time2string(SUMOTime t)
convert SUMOTime to string
Definition: SUMOTime.cpp:68
#define TS
Definition: SUMOTime.h:40
long long int SUMOTime
Definition: SUMOTime.h:31
static void sleep(long ms)
void push_back(T what)
Definition: FXSynchQue.h:114
Event sent when the the simulation is over.
void clear()
Clears this container.
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
const std::vector< GUIGlChildWindow * > & getViews() const
get views
static GUIMainWindow * getInstance()
get instance
A MSNet extended by some values for usage within the gui.
Definition: GUINet.h:81
void setIdleDuration(int val)
Sets the duration of the last step's idle part.
Definition: GUINet.cpp:416
void simulationStep()
Performs a single simulation step (locking the simulation)
Definition: GUINet.cpp:229
void setSimDuration(int val)
Sets the duration of the last step's simulation part.
Definition: GUINet.cpp:400
void guiSimulationStep()
Some further steps needed for gui processing.
Definition: GUINet.cpp:222
double & mySimDelay
Definition: GUIRunThread.h:150
OutputDevice * myErrorRetriever
The instances of message retriever encapsulations Needed to be deleted from the handler later on.
Definition: GUIRunThread.h:148
GUINet * myNet
the loaded simulation network
Definition: GUIRunThread.h:121
SUMOTime mySimEndTime
Definition: GUIRunThread.h:124
FXMutex myBreakpointLock
Lock for modifying the list of breakpoints.
Definition: GUIRunThread.h:162
FXEX::FXThreadEvent & myEventThrow
Definition: GUIRunThread.h:154
GUINet & getNet() const
std::vector< SUMOTime > myBreakpoints
List of breakpoints.
Definition: GUIRunThread.h:159
virtual FXint run()
starts the execution
virtual void begin()
virtual bool simulationIsStartable() const
GUIRunThread(FXApp *app, MFXInterThreadEventClient *mw, double &simDelay, FXSynchQue< GUIEvent * > &eq, FXEX::FXThreadEvent &ev)
constructor
FXSynchQue< GUIEvent * > & myEventQue
Definition: GUIRunThread.h:152
virtual bool simulationIsStepable() const
bool mySimulationInProgress
Definition: GUIRunThread.h:136
virtual bool init(GUINet *net, SUMOTime start, SUMOTime end)
initialises the thread with the new simulation
bool myHalting
information whether the simulation is halting (is not being executed)
Definition: GUIRunThread.h:127
virtual ~GUIRunThread()
destructor
virtual bool simulationIsStopable() const
OutputDevice * myMessageRetriever
Definition: GUIRunThread.h:148
OutputDevice * myWarningRetriever
Definition: GUIRunThread.h:148
bool simulationAvailable() const
virtual void deleteSim()
void retrieveMessage(const MsgHandler::MsgType type, const std::string &msg)
Retrieves messages from the loading module.
void waitForSnapshots(const SUMOTime snapshotTime)
SUMOTime mySimStartTime
the times the simulation starts and ends with
Definition: GUIRunThread.h:124
void prepareDestruction()
FXMutex mySimulationLock
Definition: GUIRunThread.h:156
bool myHaveSignaledEnd
whether the simulation already ended
Definition: GUIRunThread.h:144
SimulationState
Possible states of a simulation - running or stopped with different reasons.
Definition: MSNet.h:94
@ SIMSTATE_TOO_MANY_TELEPORTS
The simulation had too many teleports.
Definition: MSNet.h:110
@ SIMSTATE_NO_FURTHER_VEHICLES
The simulation does not contain further vehicles.
Definition: MSNet.h:102
@ SIMSTATE_LOADING
The simulation is loading.
Definition: MSNet.h:96
@ SIMSTATE_ERROR_IN_SIM
An error occurred during the simulation step.
Definition: MSNet.h:106
@ SIMSTATE_CONNECTION_CLOSED
The connection to a client was closed by the client.
Definition: MSNet.h:104
@ SIMSTATE_END_STEP_REACHED
The final simulation step has been performed.
Definition: MSNet.h:100
static std::string getStateMessage(SimulationState state)
Returns the message to show if a certain state occurs.
Definition: MSNet.cpp:718
SimulationState adaptToState(const SimulationState state) const
Called after a simulation step, this method adapts the current simulation state if necessary.
Definition: MSNet.cpp:697
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:313
void closeSimulation(SUMOTime start, const std::string &reason="")
Closes the simulation (all files, connections, etc.)
Definition: MSNet.cpp:505
void setCurrentTimeStep(const SUMOTime step)
Sets the current simulation step (used by state loading)
Definition: MSNet.h:321
SimulationState simulationState(SUMOTime stopTime) const
This method returns the current simulation state. It should not modify status.
Definition: MSNet.cpp:667
void loadRoutes()
loads routes for the next few steps
Definition: MSNet.cpp:410
virtual void addRetriever(OutputDevice *retriever)
Adds a further retriever to the instance responsible for a certain msg type.
Definition: MsgHandler.cpp:175
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:117
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:67
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:252
virtual void removeRetriever(OutputDevice *retriever)
Removes the retriever from the handler.
Definition: MsgHandler.cpp:183
@ MT_MESSAGE
The message is only something to show.
Definition: MsgHandler.h:45
@ MT_WARNING
The message is a warning.
Definition: MsgHandler.h:47
@ MT_ERROR
The message is an error.
Definition: MsgHandler.h:49
static MsgHandler * getMessageInstance()
Returns the instance to add normal messages to.
Definition: MsgHandler.cpp:54
Encapsulates an object's method for using it as a message retriever.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
static void closeAll(bool keepErrorRetrievers=false)
static long getCurrentMillis()
Returns the current time in milliseconds.
Definition: SysUtils.cpp:39