/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file GUIRunThread.h14/// @author Daniel Krajzewicz15/// @author Jakob Erdmann16/// @author Michael Behrisch17/// @date Sept 200218///19// The thread that runs the simulation20/****************************************************************************/21#pragma once22#include <config.h>2324#include <string>25#include <vector>26#include <set>27#include <iostream>28#include <utils/foxtools/fxheader.h>29#include <utils/foxtools/MFXSingleEventThread.h>30#include <utils/foxtools/MFXThreadEvent.h>31#include <utils/foxtools/MFXSynchQue.h>32#include <utils/common/SUMOTime.h>333435// ===========================================================================36// class declarations37// ===========================================================================38class GUINet;39class GUIEvent;40class OutputDevice;4142// ===========================================================================43// class definition44// ===========================================================================45/**46* @class GUIRunThread47* This thread executes the given simulation stepwise to allow parallel48* visualisation.49* The avoidance of collisions between the simulation execution and its50* visualisation is done individually for every lane using mutexes51*/52class GUIRunThread : public MFXSingleEventThread {5354public:55/// @brief constructor56GUIRunThread(FXApp* app, MFXInterThreadEventClient* mw,57double& simDelay, MFXSynchQue<GUIEvent*>& eq,58FXEX::MFXThreadEvent& ev);5960/// @brief destructor61virtual ~GUIRunThread();6263/// @brief initialises the thread with the new simulation64virtual bool init(GUINet* net, SUMOTime start, SUMOTime end);6566/// @brief starts the execution67virtual FXint run();6869/// @brief called when the user presses the "resume"-button70/// @note this method resumes the execution after a break71void resume();7273/// @brief called when the user presses the "single step"-button74/// @note this method allows the thread to perform a single simulation step75void singleStep();7677/// @brief starts the simulation (execution of one step after another)78virtual void begin();7980/// @brief halts the simulation execution81void stop();8283/// @brief returns the information whether a network has been loaded84bool networkAvailable() const;8586/// @brief check if simulation is startable87virtual bool simulationIsStartable() const;8889/// @brief check if simulation is stopableo90virtual bool simulationIsStopable() const;9192/// @brief check if simulation is stepable93virtual bool simulationIsStepable() const;9495/// @brief deletes the existing simulation96virtual void deleteSim();9798/// @brief returns the loaded network99GUINet& getNet() const;100101/// @brief halts the thread before it shall be deleted102void prepareDestruction();103104/// @brief Retrieves messages from the loading module105void retrieveMessage(const MsgHandler::MsgType type, const std::string& msg);106107/// @brief get simulation begin time108SUMOTime getSimBegin() {109return mySimStartTime;110}111112/// @brief get simulation end time113SUMOTime getSimEndTime() const {114return mySimEndTime;115}116117/// @brief get list of breakpoints118std::vector<SUMOTime>& getBreakpoints() {119return myBreakpoints;120}121122/// @brief get breakpoint lock123FXMutex& getBreakpointLock() {124return myBreakpointLock;125}126127/// @brief enable lib SUMO128void enableLibsumo() {129myAmLibsumo = true;130}131132/// @brief try simulation step133void tryStep();134135protected:136/// @brief make simulation step137void makeStep();138139/// @brief wait for snapshots140void waitForSnapshots(const SUMOTime snapshotTime);141142protected:143/// @brief the loaded simulation network144GUINet* myNet;145146/// @brief the times the simulation starts and ends with147SUMOTime mySimStartTime, mySimEndTime;148149/// @brief information whether the simulation is halting (is not being executed)150bool myHalting;151152/// @brief information whether the thread shall be stopped153/// @note if not, the thread stays in an endless loop)154bool myQuit;155156/// @brief information whether a simulation step is being performed157/// @note otherwise the thread may be waiting or the simulation is maybe not performed at all158bool mySimulationInProgress;159160/// @brief flag to check if all is ok161bool myOk;162163/// @brief information whether the thread is running in single step mode164bool mySingle;165166/// @brief whether the simulation already ended167bool myHaveSignaledEnd;168169/// @brief @brief The instances of message retriever encapsulations170/// @note Needed to be deleted from the handler later on171OutputDevice* myErrorRetriever, *myMessageRetriever, *myWarningRetriever;172173/// @brief simulation delay174double& mySimDelay;175176/// @brief event queue177MFXSynchQue<GUIEvent*>& myEventQue;178179/// @brief thrower events180FXEX::MFXThreadEvent& myEventThrow;181182/// @brief mutex for lock simulation183FXMutex mySimulationLock;184185/// @brief @brief List of breakpoints186std::vector<SUMOTime> myBreakpoints;187188/// @brief @brief Lock for modifying the list of breakpoints189FXMutex myBreakpointLock;190191/// @brief end of the last simulation step192long myLastEndMillis;193194/// @brief last time the simulation took a microsecond break for the fox event loop to catch up (#9028)195long myLastBreakMillis;196197/// @brief whether we are running in libsumo198bool myAmLibsumo;199};200201202