KiCad PCB EDA Suite
symbol_async_loader.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2021 Jon Evans <jon@craftyjon.com>
5  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <thread>
22 
23 #include <core/wx_stl_compat.h>
24 #include <symbol_async_loader.h>
25 #include <symbol_lib_table.h>
26 #include <progress_reporter.h>
27 
28 
29 SYMBOL_ASYNC_LOADER::SYMBOL_ASYNC_LOADER( const std::vector<wxString>& aNicknames,
30  SYMBOL_LIB_TABLE* aTable, bool aOnlyPowerSymbols,
31  std::unordered_map<wxString, std::vector<LIB_SYMBOL*>>* aOutput,
32  PROGRESS_REPORTER* aReporter ) :
33  m_nicknames( aNicknames ),
34  m_table( aTable ),
35  m_onlyPowerSymbols( aOnlyPowerSymbols ),
36  m_output( aOutput ),
37  m_reporter( aReporter ),
38  m_nextLibrary( 0 ),
39  m_canceled( false )
40 {
41  wxASSERT( m_table );
42  m_threadCount = std::max<size_t>( 1, std::thread::hardware_concurrency() - 1 );
43 
44  m_returns.resize( m_threadCount );
45 }
46 
47 
48 
50 {
51  Abort();
52 }
53 
54 
56 {
57  for( size_t ii = 0; ii < m_threadCount; ++ii )
58  m_returns[ii] = std::async( std::launch::async, &SYMBOL_ASYNC_LOADER::worker, this );
59 }
60 
61 
63 {
64  for( size_t ii = 0; ii < m_threadCount; ++ii )
65  {
66  if( !m_returns[ii].valid() )
67  continue;
68 
69  m_returns[ii].wait();
70 
71  const std::vector<LOADED_PAIR>& ret = m_returns[ii].get();
72 
73  if( m_output && !ret.empty() )
74  {
75  for( const LOADED_PAIR& pair : ret )
76  {
77  // Don't show libraries that had no power symbols
78  if( m_onlyPowerSymbols && pair.second.empty() )
79  continue;
80 
81  // *Do* show empty libraries in the normal case
82  m_output->insert( pair );
83  }
84  }
85  }
86 
87  return true;
88 }
89 
90 
92 {
93  m_canceled.store( true );
94  Join();
95 }
96 
97 
99 {
100  return m_nextLibrary.load() >= m_nicknames.size();
101 }
102 
103 
104 std::vector<SYMBOL_ASYNC_LOADER::LOADED_PAIR> SYMBOL_ASYNC_LOADER::worker()
105 {
106  std::vector<LOADED_PAIR> ret;
107 
108  bool onlyPower = m_onlyPowerSymbols;
109 
110  for( size_t libraryIndex = m_nextLibrary++; libraryIndex < m_nicknames.size();
111  libraryIndex = m_nextLibrary++ )
112  {
113  if( m_canceled.load() )
114  break;
115 
116  const wxString& nickname = m_nicknames[libraryIndex];
117  LOADED_PAIR pair( nickname, {} );
118 
119  try
120  {
121  m_table->LoadSymbolLib( pair.second, nickname, onlyPower );
122  ret.emplace_back( std::move( pair ) );
123  }
124  catch( const IO_ERROR& ioe )
125  {
126  wxString msg = wxString::Format( _( "Error loading symbol library %s.\n\n%s\n" ),
127  nickname, ioe.What() );
128 
129  std::lock_guard<std::mutex> lock( m_errorMutex );
130  m_errors += msg;
131  }
132 
133  if( m_reporter )
134  m_reporter->AdvancePhase( wxString::Format( _( "Loading library %s..." ), nickname ) );
135  }
136 
137  return ret;
138 }
bool m_onlyPowerSymbols
Handle to map that will be filled with the loaded parts per library.
void Start()
Spins up threads to load all the libraries in m_nicknames.
std::atomic< size_t > m_nextLibrary
bool Join()
Finalizes the threads and combines the output into the target output map.
A progress reporter interface for use in multi-threaded environments.
std::pair< wxString, std::vector< LIB_SYMBOL * > > LOADED_PAIR
void Abort()
Cancels a load in-progress.
bool Done()
Returns a string containing any errors generated during the load.
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
virtual void AdvancePhase()=0
Use the next available virtual zone of the dialog progress bar.
std::atomic_bool m_canceled
std::unordered_map< wxString, std::vector< LIB_SYMBOL * > > * m_output
Progress reporter (may be null)
#define _(s)
SYMBOL_LIB_TABLE * m_table
True if we are loading only power symbols.
std::vector< wxString > m_nicknames
Handle to the symbol library table being loaded into.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
std::vector< LOADED_PAIR > worker()
< Worker job that loads libraries and returns a list of pairs of <nickname, loaded parts>
void LoadSymbolLib(std::vector< LIB_SYMBOL * > &aAliasList, const wxString &aNickname, bool aPowerSymbolsOnly=false)
std::vector< std::future< std::vector< LOADED_PAIR > > > m_returns
PROGRESS_REPORTER * m_reporter
SYMBOL_ASYNC_LOADER(const std::vector< wxString > &aNicknames, SYMBOL_LIB_TABLE *aTable, bool aOnlyPowerSymbols=false, std::unordered_map< wxString, std::vector< LIB_SYMBOL * >> *aOutput=nullptr, PROGRESS_REPORTER *aReporter=nullptr)
Constructs a loader for symbol libraries.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75