From bad462a228ca898ed7e42b548f1e8fe3defc06a0 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Wed, 21 Mar 2018 17:22:56 +0100 Subject: [PATCH] Spice simulator: reload ngSpice dll on error ngSpice frequently ends up a simulation with an error: "Error: ngspice.dll cannot recover and awaits to be detached" The only way forward is to reload the shared library and reinitialize ngSpice. Fixes: lp:1753101 * https://bugs.launchpad.net/kicad/+bug/1753101 --- eeschema/sim/ngspice.cpp | 24 +++++++++++++++--------- eeschema/sim/ngspice.h | 5 +++-- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/eeschema/sim/ngspice.cpp b/eeschema/sim/ngspice.cpp index 8916b7c63d..3ba4b97662 100644 --- a/eeschema/sim/ngspice.cpp +++ b/eeschema/sim/ngspice.cpp @@ -50,12 +50,6 @@ NGSPICE::~NGSPICE() void NGSPICE::Init() { - if( m_error ) - { - delete m_dll; - init_dll(); - } - Command( "reset" ); } @@ -247,6 +241,7 @@ bool NGSPICE::IsRunning() bool NGSPICE::Command( const string& aCmd ) { LOCALE_IO c_locale; // ngspice works correctly only with C locale + validate(); m_ngSpice_Command( (char*) aCmd.c_str() ); return true; } @@ -454,13 +449,24 @@ int NGSPICE::cbBGThreadRunning( bool is_running, int id, void* user ) int NGSPICE::cbControlledExit( int status, bool immediate, bool exit_upon_quit, int id, void* user ) { // Something went wrong, reload the dll - //NGSPICE* sim = reinterpret_cast( user ); - //sim->m_initialized = false; - //printf("stat %d immed %d quit %d\n", status, !!immediate, !!exit_upon_quit); + NGSPICE* sim = reinterpret_cast( user ); + sim->m_error = true; return 0; } + +void NGSPICE::validate() +{ + if( m_error ) + { + delete m_dll; + m_initialized = false; + init_dll(); + } +} + + const std::string NGSPICE::GetNetlist() const { return m_netlist; diff --git a/eeschema/sim/ngspice.h b/eeschema/sim/ngspice.h index 33d28ca3c2..c3d80f4fff 100644 --- a/eeschema/sim/ngspice.h +++ b/eeschema/sim/ngspice.h @@ -119,9 +119,10 @@ private: static int cbBGThreadRunning( bool is_running, int id, void* user ); static int cbControlledExit( int status, bool immediate, bool exit_upon_quit, int id, void* user ); - void dump(); + // Assures ngspice is in a valid state and reinitializes it if need be + void validate(); - ///> Error flag, meaning ngspice needs to be reloaded + ///> Error flag indicating that ngspice needs to be reloaded bool m_error; ///> NGspice should be initialized only once