Line data Source code
1 : /****************************************************************************/ 2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 3 : // Copyright (C) 2001-2024 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 : /****************************************************************************/ 14 : /// @file GUIRunThread.h 15 : /// @author Daniel Krajzewicz 16 : /// @author Jakob Erdmann 17 : /// @author Michael Behrisch 18 : /// @date Sept 2002 19 : /// 20 : // The thread that runs the simulation 21 : /****************************************************************************/ 22 : #pragma once 23 : #include <config.h> 24 : 25 : #include <string> 26 : #include <vector> 27 : #include <set> 28 : #include <iostream> 29 : #include <utils/foxtools/fxheader.h> 30 : #include <utils/foxtools/MFXSingleEventThread.h> 31 : #include <utils/foxtools/MFXThreadEvent.h> 32 : #include <utils/foxtools/MFXSynchQue.h> 33 : #include <utils/common/SUMOTime.h> 34 : 35 : 36 : // =========================================================================== 37 : // class declarations 38 : // =========================================================================== 39 : class GUINet; 40 : class GUIEvent; 41 : class OutputDevice; 42 : 43 : // =========================================================================== 44 : // class definition 45 : // =========================================================================== 46 : /** 47 : * @class GUIRunThread 48 : * This thread executes the given simulation stepwise to allow parallel 49 : * visualisation. 50 : * The avoidance of collisions between the simulation execution and its 51 : * visualisation is done individually for every lane using mutexes 52 : */ 53 : class GUIRunThread : public MFXSingleEventThread { 54 : 55 : public: 56 : /// @brief constructor 57 : GUIRunThread(FXApp* app, MFXInterThreadEventClient* mw, 58 : double& simDelay, MFXSynchQue<GUIEvent*>& eq, 59 : FXEX::MFXThreadEvent& ev); 60 : 61 : /// @brief destructor 62 : virtual ~GUIRunThread(); 63 : 64 : /// @brief initialises the thread with the new simulation 65 : virtual bool init(GUINet* net, SUMOTime start, SUMOTime end); 66 : 67 : /// @brief starts the execution 68 : virtual FXint run(); 69 : 70 : /// @brief called when the user presses the "resume"-button 71 : /// @note this method resumes the execution after a break 72 : void resume(); 73 : 74 : /// @brief called when the user presses the "single step"-button 75 : /// @note this method allows the thread to perform a single simulation step 76 : void singleStep(); 77 : 78 : /// @brief starts the simulation (execution of one step after another) 79 : virtual void begin(); 80 : 81 : /// @brief halts the simulation execution 82 : void stop(); 83 : 84 : /// @brief returns the information whether a network has been loaded 85 : bool networkAvailable() const; 86 : 87 : /// @brief check if simulation is startable 88 : virtual bool simulationIsStartable() const; 89 : 90 : /// @brief check if simulation is stopableo 91 : virtual bool simulationIsStopable() const; 92 : 93 : /// @brief check if simulation is stepable 94 : virtual bool simulationIsStepable() const; 95 : 96 : /// @brief deletes the existing simulation 97 : virtual void deleteSim(); 98 : 99 : /// @brief returns the loaded network 100 : GUINet& getNet() const; 101 : 102 : /// @brief halts the thread before it shall be deleted 103 : void prepareDestruction(); 104 : 105 : /// @brief Retrieves messages from the loading module 106 : void retrieveMessage(const MsgHandler::MsgType type, const std::string& msg); 107 : 108 : /// @brief get simulation begin time 109 : SUMOTime getSimBegin() { 110 0 : return mySimStartTime; 111 : } 112 : 113 : /// @brief get simulation end time 114 : SUMOTime getSimEndTime() const { 115 0 : return mySimEndTime; 116 : } 117 : 118 : /// @brief get list of breakpoints 119 : std::vector<SUMOTime>& getBreakpoints() { 120 0 : return myBreakpoints; 121 : } 122 : 123 : /// @brief get breakpoint lock 124 : FXMutex& getBreakpointLock() { 125 0 : return myBreakpointLock; 126 : } 127 : 128 : /// @brief enable lib SUMO 129 : void enableLibsumo() { 130 479 : myAmLibsumo = true; 131 : } 132 : 133 : /// @brief try simulation step 134 : void tryStep(); 135 : 136 : protected: 137 : /// @brief make simulation step 138 : void makeStep(); 139 : 140 : /// @brief wait for snapshots 141 : void waitForSnapshots(const SUMOTime snapshotTime); 142 : 143 : protected: 144 : /// @brief the loaded simulation network 145 : GUINet* myNet; 146 : 147 : /// @brief the times the simulation starts and ends with 148 : SUMOTime mySimStartTime, mySimEndTime; 149 : 150 : /// @brief information whether the simulation is halting (is not being executed) 151 : bool myHalting; 152 : 153 : /// @brief information whether the thread shall be stopped 154 : /// @note if not, the thread stays in an endless loop) 155 : bool myQuit; 156 : 157 : /// @brief information whether a simulation step is being performed 158 : /// @note otherwise the thread may be waiting or the simulation is maybe not performed at all 159 : bool mySimulationInProgress; 160 : 161 : /// @brief flag to check if all is ok 162 : bool myOk; 163 : 164 : /// @brief information whether the thread is running in single step mode 165 : bool mySingle; 166 : 167 : /// @brief whether the simulation already ended 168 : bool myHaveSignaledEnd; 169 : 170 : /// @brief @brief The instances of message retriever encapsulations 171 : /// @note Needed to be deleted from the handler later on 172 : OutputDevice* myErrorRetriever, *myMessageRetriever, *myWarningRetriever; 173 : 174 : /// @brief simulation delay 175 : double& mySimDelay; 176 : 177 : /// @brief event queue 178 : MFXSynchQue<GUIEvent*>& myEventQue; 179 : 180 : /// @brief thrower events 181 : FXEX::MFXThreadEvent& myEventThrow; 182 : 183 : /// @brief mutex for lock simulation 184 : FXMutex mySimulationLock; 185 : 186 : /// @brief @brief List of breakpoints 187 : std::vector<SUMOTime> myBreakpoints; 188 : 189 : /// @brief @brief Lock for modifying the list of breakpoints 190 : FXMutex myBreakpointLock; 191 : 192 : /// @brief end of the last simulation step 193 : long myLastEndMillis; 194 : 195 : /// @brief last time the simulation took a microsecond break for the fox event loop to catch up (#9028) 196 : long myLastBreakMillis; 197 : 198 : /// @brief whether we are running in libsumo 199 : bool myAmLibsumo; 200 : };