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>
|
|
|
|
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* 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 ),
|
|
|
|
m_nextLibrary( 0 ),
|
|
|
|
m_canceled( false )
|
2021-03-17 00:48:10 +00:00
|
|
|
{
|
2021-03-20 00:23:54 +00:00
|
|
|
wxASSERT( m_table );
|
|
|
|
m_threadCount = std::max<size_t>( 1, std::thread::hardware_concurrency() - 1 );
|
2021-03-17 00:48:10 +00:00
|
|
|
|
|
|
|
m_returns.resize( m_threadCount );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SYMBOL_ASYNC_LOADER::~SYMBOL_ASYNC_LOADER()
|
|
|
|
{
|
|
|
|
Abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SYMBOL_ASYNC_LOADER::Abort()
|
|
|
|
{
|
|
|
|
m_canceled.store( true );
|
|
|
|
Join();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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++ )
|
|
|
|
{
|
|
|
|
if( m_canceled.load() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
const wxString& nickname = m_nicknames[libraryIndex];
|
|
|
|
LOADED_PAIR pair( nickname, {} );
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_reporter )
|
2021-06-23 22:53:08 +00:00
|
|
|
m_reporter->AdvancePhase( wxString::Format( _( "Loading library %s..." ), nickname ) );
|
2021-03-17 00:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|