diff --git a/eeschema/sim/sim_plot_frame.cpp b/eeschema/sim/sim_plot_frame.cpp index bccf39d6f1..3c5b244364 100644 --- a/eeschema/sim/sim_plot_frame.cpp +++ b/eeschema/sim/sim_plot_frame.cpp @@ -476,10 +476,17 @@ void SIM_PLOT_FRAME::StartSimulation( const wxString& aSimCommand ) return; } - m_simulator->LoadNetlist( formatter.GetString() ); - updateTuners(); - applyTuners(); - m_simulator->Run(); + std::unique_lock simulatorLock( m_simulator->GetMutex(), std::try_to_lock ); + + if( simulatorLock.owns_lock() ) + { + m_simulator->LoadNetlist( formatter.GetString() ); + updateTuners(); + applyTuners(); + m_simulator->Run(); + } + else + DisplayErrorMessage( this, _( "Another simulation is already running." ) ); } @@ -1808,12 +1815,20 @@ void SIM_PLOT_FRAME::onSimUpdate( wxCommandEvent& aEvent ) } else { - // Incremental update - m_simConsole->Clear(); + std::unique_lock simulatorLock( m_simulator->GetMutex(), std::try_to_lock ); - // Do not export netlist, it is already stored in the simulator - applyTuners(); - m_simulator->Run(); + if( simulatorLock.owns_lock() ) + { + // Incremental update + m_simConsole->Clear(); + + // Do not export netlist, it is already stored in the simulator + applyTuners(); + + m_simulator->Run(); + } + else + DisplayErrorMessage( this, _( "Another simulation is already running." ) ); } } diff --git a/eeschema/sim/spice_simulator.h b/eeschema/sim/spice_simulator.h index e005da9b1d..d1eb52ffd7 100644 --- a/eeschema/sim/spice_simulator.h +++ b/eeschema/sim/spice_simulator.h @@ -30,6 +30,7 @@ #include "sim_types.h" #include "spice_settings.h" +#include #include #include #include @@ -63,6 +64,14 @@ public: */ virtual void Init( const SPICE_SIMULATOR_SETTINGS* aSettings = nullptr ) = 0; + /* + * @return mutex for exclusive access to the simulator. + */ + std::mutex& GetMutex() + { + return m_mutex; + } + /** * Load a netlist for the simulation. * @@ -204,6 +213,10 @@ protected: ///< We don't own this. We are just borrowing it from the #SCHEMATIC_SETTINGS. std::shared_ptr m_settings; + +private: + ///< For interprocess synchronisation. + std::mutex m_mutex; }; #endif /* SPICE_SIMULATOR_H */