GUIRunThread Class Reference

#include <GUIRunThread.h>

Inheritance diagram for GUIRunThread:

FXSingleEventThread FXObject FXThread

Detailed Description

This thread executes the given simulation stepwise to allow parallel visualisation. The avoidance of collisions between the simulation execution and her visualisation is done individually for every lane using mutexes

Definition at line 62 of file GUIRunThread.h.


Public Types

enum  { ID_THREAD_EVENT }

Public Member Functions

virtual void begin ()
virtual void deleteSim ()
GUINetgetNet () const
 GUIRunThread (MFXInterThreadEventClient *mw, FXRealSpinDial &simDelay, MFXEventQue &eq, FXEX::FXThreadEvent &ev)
 constructor
virtual void init (GUINet *net, SUMOTime start, SUMOTime end)
 initialises the thread with the new simulation
long onThreadEvent (FXObject *, FXSelector, void *)
long onThreadSignal (FXObject *, FXSelector, void *)
void prepareDestruction ()
void resume ()
void retrieveMessage (const MsgHandler::MsgType type, const std::string &msg)
 Retrieves messages from the loading module.
virtual FXint run ()
 starts the execution
void signal (FXuint seltype)
void signal ()
bool simulationAvailable () const
virtual bool simulationIsStartable () const
virtual bool simulationIsStepable () const
virtual bool simulationIsStopable () const
void singleStep ()
void stop ()
virtual ~GUIRunThread ()
 destructor

Static Public Member Functions

static void sleep (long ms)

Protected Member Functions

void makeStep () throw ()

Protected Attributes

OutputDevicemyErrorRetriever
 The instances of message retriever encapsulations Needed to be deleted from the handler later on.
MFXEventQuemyEventQue
FXEX::FXThreadEventmyEventThrow
bool myHalting
 information whether the simulation is halting (is not being executed)
OutputDevicemyMessageRetriever
GUINetmyNet
 the loaded simulation network
bool myOk
bool myQuit
FXRealSpinDialmySimDelay
SUMOTime mySimEndTime
SUMOTime mySimStartTime
 the times the simulation starts and ends with
bool mySimulationInProgress
MFXMutex mySimulationLock
bool mySingle
OutputDevicemyWarningRetriever

Member Enumeration Documentation

anonymous enum [inherited]

Enumerator:
ID_THREAD_EVENT 

Definition at line 31 of file FXSingleEventThread.h.

00031          {
00032         ID_THREAD_EVENT
00033     };


Constructor & Destructor Documentation

GUIRunThread::GUIRunThread ( MFXInterThreadEventClient mw,
FXRealSpinDial simDelay,
MFXEventQue eq,
FXEX::FXThreadEvent ev 
)

GUIRunThread::~GUIRunThread (  )  [virtual]

destructor

Definition at line 83 of file GUIRunThread.cpp.

References deleteSim(), myErrorRetriever, myMessageRetriever, myNet, myQuit, mySimulationInProgress, and myWarningRetriever.

00083                             {
00084     // the thread shall stop
00085     myQuit = true;
00086     deleteSim();
00087     delete myErrorRetriever;
00088     delete myMessageRetriever;
00089     delete myWarningRetriever;
00090     // wait for the thread
00091     while (mySimulationInProgress||myNet!=0);
00092 }


Member Function Documentation

void GUIRunThread::begin (  )  [virtual]

starts the simulation (execution of one step after another)

Definition at line 242 of file GUIRunThread.cpp.

References myOk.

Referenced by GUIApplicationWindow::onCmdStart(), and GUIApplicationWindow::onCmdStep().

00242                     {
00243     myOk = true;
00244 }

void GUIRunThread::deleteSim (  )  [virtual]

GUINet & GUIRunThread::getNet (  )  const

returns the loaded network

Definition at line 282 of file GUIRunThread.cpp.

References myNet.

Referenced by GUIApplicationWindow::getCurrentSimTime(), GUIApplicationWindow::handleEvent_SimulationStep(), GUIApplicationWindow::openNewView(), and run().

00282                            {
00283     return *myNet;
00284 }

void GUIRunThread::init ( GUINet net,
SUMOTime  start,
SUMOTime  end 
) [virtual]

void GUIRunThread::makeStep (  )  throw () [protected]

Definition at line 154 of file GUIRunThread.cpp.

References MFXEventQue::add(), DELTA_T, MSNet::getCurrentTimeStep(), MsgHandler::getErrorInstance(), OptionsCont::getOptions(), GUINet::guiSimulationStep(), MsgHandler::inform(), MFXMutex::lock(), myEventQue, myEventThrow, myHalting, myNet, myOk, mySimEndTime, mySimulationInProgress, mySimulationLock, mySingle, FXEX::FXThreadEvent::signal(), MSNet::SIMSTATE_CONNECTION_CLOSED, MSNet::SIMSTATE_END_STEP_REACHED, MSNet::SIMSTATE_ERROR_IN_SIM, MSNet::SIMSTATE_NO_FURTHER_VEHICLES, MSNet::SIMSTATE_RUNNING, MSNet::SIMSTATE_TOO_MANY_VEHICLES, MSNet::simulationState(), MSNet::simulationStep(), MFXMutex::unlock(), and traci::TraCIServer::wasClosed().

Referenced by run().

00154                                {
00155     GUIEvent *e = 0;
00156     // simulation is being perfomed
00157     mySimulationInProgress = true;
00158     // execute a single step
00159     try {
00160         mySimulationLock.lock();
00161         myNet->simulationStep();
00162         myNet->guiSimulationStep();
00163         mySimulationLock.unlock();
00164 
00165         // inform parent that a step has been performed
00166         e = new GUIEvent_SimulationStep();
00167         myEventQue.add(e);
00168         myEventThrow.signal();
00169 
00170         e = 0;
00171         MSNet::SimulationState state = myNet->simulationState(mySimEndTime);
00172 #ifndef NO_TRACI
00173         if (state!=MSNet::SIMSTATE_RUNNING) {
00174             if (OptionsCont::getOptions().getInt("remote-port")!=0&&!traci::TraCIServer::wasClosed()) {
00175                 state = MSNet::SIMSTATE_RUNNING;
00176             }
00177         }
00178 #endif
00179         switch (state) {
00180         case MSNet::SIMSTATE_END_STEP_REACHED:
00181         case MSNet::SIMSTATE_NO_FURTHER_VEHICLES:
00182         case MSNet::SIMSTATE_CONNECTION_CLOSED:
00183         case MSNet::SIMSTATE_TOO_MANY_VEHICLES:
00184             e = new GUIEvent_SimulationEnded(state, myNet->getCurrentTimeStep()-DELTA_T);
00185             break;
00186         default:
00187             break;
00188         }
00189         if (e!=0) {
00190             myEventQue.add(e);
00191             myEventThrow.signal();
00192             myHalting = true;
00193         }
00194         // stop the execution when only a single step should have
00195         //  been performed
00196         if (mySingle) {
00197             myHalting = true;
00198         }
00199         // simulation step is over
00200         mySimulationInProgress = false;
00201     } catch (ProcessError &e2) {
00202         if (string(e2.what())!=string("Process Error") && std::string(e2.what())!=string("")) {
00203             MsgHandler::getErrorInstance()->inform(e2.what());
00204         }
00205         MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
00206         mySimulationLock.unlock();
00207         mySimulationInProgress = false;
00208         e = new GUIEvent_SimulationEnded(MSNet::SIMSTATE_ERROR_IN_SIM, myNet->getCurrentTimeStep());
00209         myEventQue.add(e);
00210         myEventThrow.signal();
00211         myHalting = true;
00212         myOk = false;
00213 #ifndef _DEBUG
00214     } catch (...) {
00215         mySimulationLock.unlock();
00216         mySimulationInProgress = false;
00217         e = new GUIEvent_SimulationEnded(MSNet::SIMSTATE_ERROR_IN_SIM, myNet->getCurrentTimeStep());
00218         myEventQue.add(e);
00219         myEventThrow.signal();
00220         myHalting = true;
00221         myOk = false;
00222 #endif
00223     }
00224 }

long FXSingleEventThread::onThreadEvent ( FXObject ,
FXSelector  ,
void *   
) [inherited]

Definition at line 106 of file FXSingleEventThread.cpp.

References MFXInterThreadEventClient::eventOccured(), and FXSingleEventThread::myClient.

00106                                                               {
00107     myClient->eventOccured();
00108     /*
00109     FXuint seltype1 = FXSELTYPE(SEL_THREAD);
00110     if(myTarget && myTarget->handle(this,FXSEL(seltype1,mySelector),NULL)) {
00111     }
00112     FXuint seltype = FXSELTYPE(sel);
00113     return myTarget && myTarget->handle(this,FXSEL(seltype,mySelector),NULL);
00114     */
00115     return 1;
00116 }

long FXSingleEventThread::onThreadSignal ( FXObject ,
FXSelector  ,
void *   
) [inherited]

Definition at line 92 of file FXSingleEventThread.cpp.

References PIPE_READ, and FXEX::SEL_THREAD.

00092                                                                 {
00093     FXuint seltype=SEL_THREAD;
00094 #ifndef WIN32
00095     ::read(event[PIPE_READ],&seltype,sizeof(seltype));
00096 #else
00097     //FIXME need win32 support
00098 #endif
00099     FXSelector sel = FXSEL(SEL_THREAD,0);
00100     handle(this,sel,0);
00101     return 0;
00102 }

void GUIRunThread::prepareDestruction (  ) 

halts the thread before it shall be deleted

Definition at line 288 of file GUIRunThread.cpp.

References myHalting, and myQuit.

Referenced by GUIApplicationWindow::~GUIApplicationWindow().

00288                                  {
00289     myHalting = true;
00290     myQuit = true;
00291 }

void GUIRunThread::resume (  ) 

called when the user presses the "resume"-button, this method resumes the execution after a break

Definition at line 228 of file GUIRunThread.cpp.

References myHalting, and mySingle.

Referenced by GUIApplicationWindow::onCmdStart().

00228                      {
00229     mySingle = false;
00230     myHalting = false;
00231 }

void GUIRunThread::retrieveMessage ( const MsgHandler::MsgType  type,
const std::string &  msg 
)

Retrieves messages from the loading module.

Definition at line 295 of file GUIRunThread.cpp.

References MFXEventQue::add(), myEventQue, myEventThrow, and FXEX::FXThreadEvent::signal().

Referenced by GUIRunThread().

00295                                                                                 {
00296     GUIEvent *e = new GUIEvent_Message(type, msg);
00297     myEventQue.add(e);
00298     myEventThrow.signal();
00299 }

FXint GUIRunThread::run (  )  [virtual]

starts the execution

Reimplemented from FXSingleEventThread.

Definition at line 109 of file GUIRunThread.cpp.

References deleteSim(), gBreakpoints, SysUtils::getCurrentMillis(), MSNet::getCurrentTimeStep(), getNet(), FX::FXRealSpinDial::getValue(), makeStep(), myHalting, myNet, myOk, myQuit, mySimDelay, GUINet::setIdleDuration(), GUINet::setSimDuration(), FXSingleEventThread::sleep(), stop(), and SUMOReal.

00109                   {
00110     long beg = 0;
00111     long end = 0;
00112     long end2 = -1;
00113     // perform an endless loop
00114     while (!myQuit) {
00115         // if the simulation shall be perfomed, do it
00116         if (!myHalting&&myNet!=0&&myOk) {
00117             if (getNet().logSimulationDuration()) {
00118                 beg = SysUtils::getCurrentMillis();
00119                 if (end2!=-1) {
00120                     getNet().setIdleDuration((int)(beg-end2));
00121                 }
00122             }
00123             // check whether we shall stop at this step
00124             bool haltAfter =
00125                 find(gBreakpoints.begin(), gBreakpoints.end(), myNet->getCurrentTimeStep())!=gBreakpoints.end();
00126             // do the step
00127             makeStep();
00128             // stop if wished
00129             if (haltAfter) {
00130                 stop();
00131             }
00132             // wait if wanted
00133             SUMOReal val = (SUMOReal) mySimDelay.getValue();
00134             if (getNet().logSimulationDuration()) {
00135                 end = SysUtils::getCurrentMillis();
00136                 getNet().setSimDuration((int)(end-beg));
00137                 end2 = SysUtils::getCurrentMillis();
00138             }
00139             if ((int) val!=0) {
00140                 sleep((int) val);
00141             }
00142         } else {
00143             // sleep if the siulation is not running
00144             sleep(500);
00145         }
00146     }
00147     // delete a maybe existing simulation at the end
00148     deleteSim();
00149     return 0;
00150 }

void FXSingleEventThread::signal ( FXuint  seltype  )  [inherited]

Definition at line 82 of file FXSingleEventThread.cpp.

References FXSingleEventThread::event, and PIPE_WRITE.

00082                                           {
00083 #ifndef WIN32
00084     ::write(event[PIPE_WRITE],&seltype,sizeof(seltype));
00085 #else
00086     ::SetEvent(event);
00087 #endif
00088 }

void FXSingleEventThread::signal (  )  [inherited]

Definition at line 71 of file FXSingleEventThread.cpp.

References FXSingleEventThread::event, PIPE_WRITE, and FXEX::SEL_THREAD.

00071                             {
00072     FXuint seltype=SEL_THREAD;
00073 #ifndef WIN32
00074     ::write(event[PIPE_WRITE],&seltype,sizeof(seltype));
00075 #else
00076     ::SetEvent(event);
00077 #endif
00078 }

bool GUIRunThread::simulationAvailable (  )  const

returns the information whether a simulation has been loaded

Definition at line 255 of file GUIRunThread.cpp.

References myNet.

Referenced by GUIApplicationWindow::onCmdStart(), GUIApplicationWindow::onCmdStep(), GUIApplicationWindow::onUpdAddMicro(), GUIApplicationWindow::onUpdEditBreakpoints(), GUIApplicationWindow::onUpdEditChosen(), and GUIApplicationWindow::openNewView().

00255                                         {
00256     return myNet!=0;
00257 }

bool GUIRunThread::simulationIsStartable (  )  const [virtual]

Definition at line 303 of file GUIRunThread.cpp.

References myHalting, and myNet.

Referenced by GUIApplicationWindow::handleEvent_SimulationLoaded(), and GUIApplicationWindow::onUpdStart().

00303                                           {
00304     return myNet!=0&&myHalting;
00305 }

bool GUIRunThread::simulationIsStepable (  )  const [virtual]

Definition at line 315 of file GUIRunThread.cpp.

References myHalting, and myNet.

Referenced by GUIApplicationWindow::onUpdStep().

00315                                          {
00316     return myNet!=0&&myHalting;
00317 }

bool GUIRunThread::simulationIsStopable (  )  const [virtual]

Definition at line 309 of file GUIRunThread.cpp.

References myHalting, and myNet.

Referenced by GUIApplicationWindow::onUpdStop().

00309                                          {
00310     return myNet!=0&&(!myHalting);
00311 }

void GUIRunThread::singleStep (  ) 

called when the user presses the "single step"-button, this method allows the thread to perform a single simulation step

Definition at line 235 of file GUIRunThread.cpp.

References myHalting, and mySingle.

Referenced by GUIApplicationWindow::onCmdStep().

00235                          {
00236     mySingle = true;
00237     myHalting = false;
00238 }

void FXSingleEventThread::sleep ( long  ms  )  [static, inherited]

Definition at line 120 of file FXSingleEventThread.cpp.

Referenced by run().

00120                                   {
00121 #ifdef WIN32
00122     Sleep(ms);
00123 #else
00124     long long us = ms * 1000;
00125     usleep(us);
00126 #endif
00127 }

void GUIRunThread::stop (  ) 

halts the simulation execution

Definition at line 248 of file GUIRunThread.cpp.

References myHalting, and mySingle.

Referenced by GUIApplicationWindow::onCmdStop(), and run().

00248                    {
00249     mySingle = false;
00250     myHalting = true;
00251 }


Field Documentation

The instances of message retriever encapsulations Needed to be deleted from the handler later on.

Definition at line 139 of file GUIRunThread.h.

Referenced by deleteSim(), GUIRunThread(), init(), and ~GUIRunThread().

Definition at line 143 of file GUIRunThread.h.

Referenced by makeStep(), and retrieveMessage().

Definition at line 145 of file GUIRunThread.h.

Referenced by makeStep(), and retrieveMessage().

information whether the simulation is halting (is not being executed)

Definition at line 121 of file GUIRunThread.h.

Referenced by deleteSim(), makeStep(), prepareDestruction(), resume(), run(), simulationIsStartable(), simulationIsStepable(), simulationIsStopable(), singleStep(), and stop().

Definition at line 139 of file GUIRunThread.h.

Referenced by deleteSim(), GUIRunThread(), init(), and ~GUIRunThread().

bool GUIRunThread::myOk [protected]

Definition at line 132 of file GUIRunThread.h.

Referenced by begin(), makeStep(), and run().

information whether the thread shall be stopped (if not, the thread stays in an endless loop)

Definition at line 125 of file GUIRunThread.h.

Referenced by prepareDestruction(), run(), and ~GUIRunThread().

Definition at line 141 of file GUIRunThread.h.

Referenced by run().

Definition at line 118 of file GUIRunThread.h.

Referenced by init(), and makeStep().

the times the simulation starts and ends with

Definition at line 118 of file GUIRunThread.h.

Referenced by deleteSim(), and init().

information whether a simulation step is being performed (otherwise the thread may be waiting or the simulation is maybe not performed at all)

Definition at line 130 of file GUIRunThread.h.

Referenced by deleteSim(), makeStep(), and ~GUIRunThread().

Definition at line 147 of file GUIRunThread.h.

Referenced by deleteSim(), and makeStep().

information whether the thread is running in single step mode

Definition at line 135 of file GUIRunThread.h.

Referenced by makeStep(), resume(), singleStep(), and stop().

Definition at line 139 of file GUIRunThread.h.

Referenced by deleteSim(), GUIRunThread(), init(), and ~GUIRunThread().


The documentation for this class was generated from the following files:

Generated on Wed May 5 00:06:45 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6