diff --git a/common/fp_lib_table.cpp b/common/fp_lib_table.cpp index 7048cf9b7e..4491b8548b 100644 --- a/common/fp_lib_table.cpp +++ b/common/fp_lib_table.cpp @@ -157,6 +157,7 @@ void FP_LIB_TABLE::Parse( LIB_TABLE_LEXER* in ) case T_uri: if( sawUri ) in->Duplicate( tok ); + sawUri = true; in->NeedSYMBOLorNUMBER(); row->SetFullURI( in->FromUTF8() ); @@ -165,6 +166,7 @@ void FP_LIB_TABLE::Parse( LIB_TABLE_LEXER* in ) case T_type: if( sawType ) in->Duplicate( tok ); + sawType = true; in->NeedSYMBOLorNUMBER(); row->SetType( in->FromUTF8() ); @@ -173,6 +175,7 @@ void FP_LIB_TABLE::Parse( LIB_TABLE_LEXER* in ) case T_options: if( sawOpts ) in->Duplicate( tok ); + sawOpts = true; in->NeedSYMBOLorNUMBER(); row->SetOptions( in->FromUTF8() ); @@ -181,6 +184,7 @@ void FP_LIB_TABLE::Parse( LIB_TABLE_LEXER* in ) case T_descr: if( sawDesc ) in->Duplicate( tok ); + sawDesc = true; in->NeedSYMBOLorNUMBER(); row->SetDescr( in->FromUTF8() ); @@ -189,6 +193,7 @@ void FP_LIB_TABLE::Parse( LIB_TABLE_LEXER* in ) case T_disabled: if( sawDisabled ) in->Duplicate( tok ); + sawDisabled = true; row->SetEnabled( false ); break; diff --git a/common/lib_table_base.cpp b/common/lib_table_base.cpp index 3402308d0a..0aa89502ee 100644 --- a/common/lib_table_base.cpp +++ b/common/lib_table_base.cpp @@ -217,17 +217,14 @@ const wxString LIB_TABLE::GetDescription( const wxString& aNickname ) bool LIB_TABLE::HasLibrary( const wxString& aNickname, bool aCheckEnabled ) const { - const LIB_TABLE_ROW* row = findRow( aNickname, aCheckEnabled ); - - if( row == nullptr ) - return false; - - return true; + return findRow( aNickname, aCheckEnabled ) != nullptr; } bool LIB_TABLE::HasLibraryWithPath( const wxString& aPath ) const { + std::shared_lock lock( m_mutex ); + for( const LIB_TABLE_ROW& row : m_rows ) { if( row.GetFullURI() == aPath ) @@ -253,7 +250,7 @@ wxString LIB_TABLE::GetFullURI( const wxString& aNickname, bool aExpandEnvVars ) LIB_TABLE_ROW* LIB_TABLE::findRow( const wxString& aNickName, bool aCheckIfEnabled ) const { - LIB_TABLE* cur = (LIB_TABLE*) this; + const LIB_TABLE* cur = this; do { @@ -296,16 +293,18 @@ LIB_TABLE_ROW* LIB_TABLE::findRow( const wxString& aNickName, bool aCheckIfEnabl const LIB_TABLE_ROW* LIB_TABLE::FindRowByURI( const wxString& aURI ) { - LIB_TABLE* cur = this; + const LIB_TABLE* cur = this; do { - for( unsigned i = 0; i < cur->m_rows.size(); i++ ) + std::shared_lock lock( cur->m_mutex ); + + for( const LIB_TABLE_ROW& row : cur->m_rows) { - const wxString tmp = cur->m_rows[i].GetFullURI( true ); + const wxString tmp = row.GetFullURI( true ); if( m_io->UrisAreEquivalent( tmp, aURI ) ) - return &cur->m_rows[i]; + return &row; } // not found, search fall back table(s), if any @@ -326,6 +325,8 @@ std::vector LIB_TABLE::GetLogicalLibs() do { + std::shared_lock lock( cur->m_mutex ); + for( const LIB_TABLE_ROW& row : cur->m_rows ) { if( row.GetIsEnabled() ) @@ -337,8 +338,8 @@ std::vector LIB_TABLE::GetLogicalLibs() ret.reserve( unique.size() ); // return a sorted, unique set of nicknames in a std::vector to caller - for( std::set< wxString >::const_iterator it = unique.begin(); it!=unique.end(); ++it ) - ret.push_back( *it ); + for( const wxString& nickname : unique ) + ret.push_back( nickname ); // We want to allow case-sensitive duplicates but sort by case-insensitive ordering std::sort( ret.begin(), ret.end(), @@ -356,7 +357,6 @@ bool LIB_TABLE::InsertRow( LIB_TABLE_ROW* aRow, bool doReplace ) std::lock_guard lock( m_mutex ); doInsertRow( aRow, doReplace ); - reindex(); return true; } @@ -482,6 +482,8 @@ void LIB_TABLE::reindex() bool LIB_TABLE::migrate() { + std::lock_guard lock( m_mutex ); + bool table_updated = false; for( LIB_TABLE_ROW& row : m_rows ) @@ -540,9 +542,7 @@ void LIB_TABLE::Save( const wxString& aFileName ) const std::unique_ptr sf = m_io->GetWriter( aFileName ); if( !sf ) - { THROW_IO_ERROR( wxString::Format( _( "Failed to get writer for %s" ), aFileName ) ); - } // Force the lib table version to 7 before saving m_version = 7;