2021-03-17 00:48:10 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2021 Jon Evans <jon@craftyjon.com>
|
2022-02-27 11:54:12 +00:00
|
|
|
* Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2021-03-17 00:48:10 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
|
2021-03-20 15:35:37 +00:00
|
|
|
#include <core/wx_stl_compat.h>
|
2021-03-17 00:48:10 +00:00
|
|
|
#include <symbol_async_loader.h>
|
|
|
|
#include <symbol_lib_table.h>
|
2021-08-14 20:05:21 +00:00
|
|
|
#include <progress_reporter.h>
|
2021-03-17 00:48:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
SYMBOL_ASYNC_LOADER::SYMBOL_ASYNC_LOADER( const std::vector<wxString>& aNicknames,
|
2021-03-20 00:23:54 +00:00
|
|
|
SYMBOL_LIB_TABLE* aTable, bool aOnlyPowerSymbols,
|
2021-06-10 18:51:46 +00:00
|
|
|
std::unordered_map<wxString, std::vector<LIB_SYMBOL*>>* aOutput,
|
2021-03-17 00:48:10 +00:00
|
|
|
PROGRESS_REPORTER* aReporter ) :
|
|
|
|
m_nicknames( aNicknames ),
|
2021-03-20 00:23:54 +00:00
|
|
|
m_table( aTable ),
|
|
|
|
m_onlyPowerSymbols( aOnlyPowerSymbols ),
|
2021-03-17 00:48:10 +00:00
|
|
|
m_output( aOutput ),
|
2021-03-21 13:19:18 +00:00
|
|
|
m_reporter( aReporter ),
|
2022-02-27 11:54:12 +00:00
|
|
|
m_nextLibrary( 0 )
|
2021-03-17 00:48:10 +00:00
|
|
|
{
|
2021-03-20 00:23:54 +00:00
|
|
|
wxASSERT( m_table );
|
2021-12-15 01:17:43 +00:00
|
|
|
m_threadCount = std::max<size_t>( 1, std::thread::hardware_concurrency() );
|
2021-03-17 00:48:10 +00:00
|
|
|
|
|
|
|
m_returns.resize( m_threadCount );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SYMBOL_ASYNC_LOADER::~SYMBOL_ASYNC_LOADER()
|
|
|
|
{
|
2022-02-27 11:54:12 +00:00
|
|
|
Join();
|
2021-03-17 00:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SYMBOL_ASYNC_LOADER::Start()
|
|
|
|
{
|
|
|
|
for( size_t ii = 0; ii < m_threadCount; ++ii )
|
|
|
|
m_returns[ii] = std::async( std::launch::async, &SYMBOL_ASYNC_LOADER::worker, this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SYMBOL_ASYNC_LOADER::Join()
|
|
|
|
{
|
|
|
|
for( size_t ii = 0; ii < m_threadCount; ++ii )
|
|
|
|
{
|
|
|
|
if( !m_returns[ii].valid() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
m_returns[ii].wait();
|
|
|
|
|
|
|
|
const std::vector<LOADED_PAIR>& ret = m_returns[ii].get();
|
|
|
|
|
2021-03-20 00:23:54 +00:00
|
|
|
if( m_output && !ret.empty() )
|
2021-03-17 00:48:10 +00:00
|
|
|
{
|
|
|
|
for( const LOADED_PAIR& pair : ret )
|
2021-03-26 17:30:47 +00:00
|
|
|
{
|
|
|
|
// Don't show libraries that had no power symbols
|
|
|
|
if( m_onlyPowerSymbols && pair.second.empty() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// *Do* show empty libraries in the normal case
|
2021-03-20 00:23:54 +00:00
|
|
|
m_output->insert( pair );
|
2021-03-26 17:30:47 +00:00
|
|
|
}
|
2021-03-17 00:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SYMBOL_ASYNC_LOADER::Done()
|
|
|
|
{
|
|
|
|
return m_nextLibrary.load() >= m_nicknames.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<SYMBOL_ASYNC_LOADER::LOADED_PAIR> SYMBOL_ASYNC_LOADER::worker()
|
|
|
|
{
|
|
|
|
std::vector<LOADED_PAIR> ret;
|
|
|
|
|
|
|
|
bool onlyPower = m_onlyPowerSymbols;
|
|
|
|
|
|
|
|
for( size_t libraryIndex = m_nextLibrary++; libraryIndex < m_nicknames.size();
|
|
|
|
libraryIndex = m_nextLibrary++ )
|
|
|
|
{
|
2022-02-27 11:54:12 +00:00
|
|
|
const wxString& nickname = m_nicknames[libraryIndex];
|
|
|
|
|
|
|
|
if( m_reporter )
|
|
|
|
m_reporter->AdvancePhase( wxString::Format( _( "Loading library %s..." ), nickname ) );
|
|
|
|
|
|
|
|
if( m_reporter && m_reporter->IsCancelled() )
|
2021-03-17 00:48:10 +00:00
|
|
|
break;
|
|
|
|
|
2022-02-27 11:54:12 +00:00
|
|
|
LOADED_PAIR pair( nickname, {} );
|
2021-03-17 00:48:10 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-03-20 00:23:54 +00:00
|
|
|
m_table->LoadSymbolLib( pair.second, nickname, onlyPower );
|
2021-03-17 00:48:10 +00:00
|
|
|
ret.emplace_back( std::move( pair ) );
|
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
|
|
|
wxString msg = wxString::Format( _( "Error loading symbol library %s.\n\n%s\n" ),
|
|
|
|
nickname, ioe.What() );
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock( m_errorMutex );
|
|
|
|
m_errors += msg;
|
|
|
|
}
|
2023-11-15 17:26:53 +00:00
|
|
|
catch( std::exception& e )
|
|
|
|
{
|
|
|
|
wxString msg = wxString::Format( _( "Error loading symbol library %s.\n\n%s\n" ),
|
|
|
|
nickname, e.what() );
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock( m_errorMutex );
|
|
|
|
m_errors += msg;
|
|
|
|
}
|
2021-03-17 00:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|