Protect s_ModelInfoMap against multithreading init

Encountered in QA tests on windows. Probably also a issue during kicad use if you trigger a spice netlist export without previously doing any spice model editing


(cherry picked from commit e14dfdf4b08bd920bcb443b695ce775addc5444a)

Co-authored-by: Marek Roszko <mark.roszko@gmail.com>
This commit is contained in:
Mark Roszko 2025-05-01 19:58:16 +00:00
parent c55d2c092e
commit 7498f1b1a6

View File

@ -320,12 +320,25 @@ SIM_MODEL_NGSPICE::MODEL_TYPE SIM_MODEL_NGSPICE::getModelType() const
}
static std::mutex s_ModelInfoMapMutex;
static std::unique_ptr<NGSPICE_MODEL_INFO_MAP> s_ModelInfoMap;
const SIM_MODEL_NGSPICE::MODEL_INFO& SIM_MODEL_NGSPICE::ModelInfo( MODEL_TYPE aType )
{
// Because spice netlisting has paralleizing, and we can encounter a first init of
// s_ModelInfoMap as a result
// Note, we could just init this at the static variable declaration above
// or do away with making it a unique_ptr altogether and static decl the map directly
// but its a real big boi if you look at what it will contain
// so lets avoid pulling it to memory if a user isn't simming
if( !s_ModelInfoMap )
s_ModelInfoMap = std::make_unique<NGSPICE_MODEL_INFO_MAP>();
{
std::lock_guard<std::mutex> lock( s_ModelInfoMapMutex );
if( !s_ModelInfoMap )
{
s_ModelInfoMap = std::make_unique<NGSPICE_MODEL_INFO_MAP>();
}
}
return s_ModelInfoMap->modelInfos.at( aType );
}