7
mirror of https://gitlab.com/kicad/code/kicad.git synced 2025-03-30 05:56:55 +00:00

Move thread pool into true singleton

Thread pool needs to be stored in a single location for all of KiCad
otherwise each kiface will spin up its own pool of persistent threads
This commit is contained in:
Seth Hillbrand 2025-01-05 10:15:28 -08:00
parent 6c0e5508a9
commit 452e69de85
37 changed files with 147 additions and 67 deletions

View File

@ -32,7 +32,7 @@
#include "../color_rgba.h"
#include "3d_fastmath.h"
#include "3d_math.h"
#include <core/thread_pool.h>
#include <thread_pool.h>
#include <core/profile.h> // To use GetRunningMicroSecs or another profiling utility
#include <wx/log.h>

View File

@ -187,6 +187,7 @@ set( KICOMMON_SRCS
searchhelpfilefullpath.cpp
string_utils.cpp
systemdirsappend.cpp
thread_pool.cpp
ui_events.cpp
trace_helpers.cpp
wildcards_and_files_ext.cpp
@ -308,6 +309,7 @@ target_include_directories( kicommon
$<TARGET_PROPERTY:kiapi,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:picosha2,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:nlohmann_json_schema_validator,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:thread-pool,INTERFACE_INCLUDE_DIRECTORIES>
)
add_dependencies( kicommon pegtl version_header )

View File

@ -124,6 +124,7 @@ static const wxChar EnableSnapAnchorsDebug[] = wxT( "EnableSnapAnchorsDebug" );
static const wxChar MinParallelAngle[] = wxT( "MinParallelAngle" );
static const wxChar HoleWallPaintingMultiplier[] = wxT( "HoleWallPaintingMultiplier" );
static const wxChar MsgPanelShowUuids[] = wxT( "MsgPanelShowUuids" );
static const wxChar MaximumThreads[] = wxT( "MaximumThreads" );
} // namespace KEYS
@ -297,6 +298,8 @@ ADVANCED_CFG::ADVANCED_CFG()
m_MinParallelAngle = 0.001;
m_HoleWallPaintingMultiplier = 1.5;
m_MaximumThreads = 0;
loadFromConfigFile();
}
@ -570,6 +573,10 @@ void ADVANCED_CFG::loadSettings( wxConfigBase& aCfg )
&m_MsgPanelShowUuids,
m_MsgPanelShowUuids ) );
configParams.push_back( new PARAM_CFG_INT( true, AC_KEYS::MaximumThreads,
&m_MaximumThreads, m_MaximumThreads,
0, 500 ) );
// Special case for trace mask setting...we just grab them and set them immediately
// Because we even use wxLogTrace inside of advanced config
wxString traceMasks;

View File

@ -28,7 +28,7 @@
#include <lib_id.h>
#include <progress_reporter.h>
#include <string_utils.h>
#include <core/thread_pool.h>
#include <thread_pool.h>
#include <wildcards_and_files_ext.h>
#include <kiplatform/io.h>

View File

@ -49,7 +49,7 @@
#include <macros.h>
#include <geometry/geometry_utils.h>
#include <core/thread_pool.h>
#include <thread_pool.h>
#include <core/profile.h>
#include <trace_helpers.h>

View File

@ -63,7 +63,7 @@
#include <settings/settings_manager.h>
#include <string_utils.h>
#include <systemdirsappend.h>
#include <core/thread_pool.h>
#include <thread_pool.h>
#include <trace_helpers.h>
#include <widgets/wx_splash.h>
@ -472,6 +472,10 @@ bool PGM_BASE::InitPgm( bool aHeadless, bool aSkipPyInit, bool aIsUnitTest )
#ifdef KICAD_USE_SENTRY
sentryInit();
#endif
// Initialize the singleton instance
m_singleton.Init();
wxString pgm_name;
/// Should never happen but boost unit_test isn't playing nicely in some cases

View File

@ -21,21 +21,28 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <advanced_config.h>
#include <pgm_base.h>
#include <thread_pool.h>
#include <core/thread_pool.h>
// Under mingw, there is a problem with the destructor when creating a static instance
// of a thread_pool: probably the DTOR is called too late, and the application hangs.
// so we create it on the heap.
static thread_pool* tp = nullptr;
thread_pool& GetKiCadThreadPool()
{
#if 0 // Turn this on to disable multi-threading for debugging
if( !tp ) tp = new thread_pool( 1 );
#else
if( !tp ) tp = new thread_pool;
#endif
if( tp )
return *tp;
// If we have a PGM_BASE, use its thread pool
if( PGM_BASE* pgm = PgmOrNull() )
{
tp = &pgm->GetThreadPool();
return *tp;
}
// Otherwise, we are running in scripting or some other context where we don't have a PGM_BASE
// so we need to create our own thread pool
int num_threads = std::max( 0, ADVANCED_CFG::GetCfg().m_MaximumThreads );
tp = new thread_pool( num_threads );
return *tp;
}

View File

@ -47,7 +47,7 @@
#include <project/net_settings.h>
#include <widgets/ui_common.h>
#include <string_utils.h>
#include <core/thread_pool.h>
#include <thread_pool.h>
#include <wx/log.h>
#include <advanced_config.h> // for realtime connectivity switch in release builds

View File

@ -26,7 +26,7 @@
#include <utility>
#include <core/thread_pool.h>
#include <thread_pool.h>
#include <ki_exception.h>
#include <sim/sim_library_spice.h>
#include <sim/spice_grammar.h>

View File

@ -722,6 +722,15 @@ public:
*/
double m_HoleWallPaintingMultiplier;
/**
* Default value for the maximum number of threads to use for parallel processing.
* Setting this value to 0 or less will mean that we use the number of cores available
*
* Setting name: "MaximumThreads"
* Default value: 0
*/
int m_MaximumThreads;
///@}
private:

View File

@ -32,6 +32,7 @@
#define PGM_BASE_H_
#include <kicommon.h>
#include <singleton.h>
#include <exception>
#include <map>
#include <vector>
@ -104,33 +105,13 @@ public:
PGM_BASE();
virtual ~PGM_BASE();
#if 0
/*
Derived classes must implement these two functions: OnPgmInit() and
OnPgmExit(), and since they are only called from same source file as their
implementation, these need not be virtual here. In fact, in the case of
python project manager's class PGM_PYTHON, these functions are actually
written in python. In total there are three implementations, corresponding
to the three defines given by kiface.h's KFCTL_* #defines.
*/
/**
* This is the first executed function (like main() ).
*
* @return true if the application can be started.
*/
virtual bool OnPgmInit() = 0; // call this from wxApp::OnInit()
virtual void OnPgmExit() = 0; // call this from wxApp::OnExit()
#endif
/**
* Builds the UTF8 based argv variable
*/
void BuildArgvUtf8();
BS::thread_pool& GetThreadPool() { return *m_singleton.m_ThreadPool; }
/**
* Specific to MacOSX (not used under Linux or Windows).
*
@ -429,6 +410,8 @@ protected:
wxString m_text_editor;
KICAD_SINGLETON m_singleton;
#ifdef KICAD_USE_SENTRY
wxFileName m_sentry_optin_fn;
wxFileName m_sentry_uid_fn;

51
include/singleton.h Normal file
View File

@ -0,0 +1,51 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright The 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/>.
*/
#ifndef KICAD_SINGLETON_H
#define KICAD_SINGLETON_H
#include <advanced_config.h>
#include <bs_thread_pool.hpp>
#include <gal/opengl/gl_context_mgr.h>
class KICAD_SINGLETON
{
public:
KICAD_SINGLETON(){};
~KICAD_SINGLETON()
{
// This will wait for all threads to finish and then join them to the main thread
delete m_ThreadPool;
m_ThreadPool = nullptr;
};
void Init()
{
int num_threads = std::max( 0, ADVANCED_CFG::GetCfg().m_MaximumThreads );
m_ThreadPool = new BS::thread_pool( num_threads );
}
BS::thread_pool* m_ThreadPool;
};
#endif // KICAD_SINGLETON_H

View File

@ -26,6 +26,7 @@
#define INCLUDE_THREAD_POOL_H_
#include <bs_thread_pool.hpp>
#include <import_export.h>
using thread_pool = BS::thread_pool;
@ -36,7 +37,7 @@ using thread_pool = BS::thread_pool;
*
* @return Reference to the current (potentially newly constructed) thread pool
*/
thread_pool& GetKiCadThreadPool();
APIEXPORT thread_pool& GetKiCadThreadPool();
#endif /* INCLUDE_THREAD_POOL_H_ */

View File

@ -167,6 +167,7 @@ else()
common
gal
common #repeated due to a circular dependency between gal and common
kicommon
core
${wxWidgets_LIBRARIES}
)
@ -176,6 +177,7 @@ else()
common
gal
common #repeated due to a circular dependency between gal and common
kicommon
core
${wxWidgets_LIBRARIES}
)

View File

@ -49,7 +49,7 @@
#include <background_jobs_monitor.h>
#include <core/thread_pool.h>
#include <thread_pool.h>
#include <build_version.h>

View File

@ -10,7 +10,6 @@ add_library( core STATIC
observable.cpp
profile.cpp
utf8.cpp
thread_pool.cpp
version_compare.cpp
wx_stl_compat.cpp
)
@ -21,7 +20,6 @@ target_link_libraries( core PUBLIC
target_include_directories( core PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
$<TARGET_PROPERTY:thread-pool,INTERFACE_INCLUDE_DIRECTORIES>
PRIVATE
${CMAKE_BINARY_DIR} # to get config.h
)

View File

@ -63,7 +63,7 @@
#include <tool/tool_manager.h>
#include <tool/selection_conditions.h>
#include <string_utils.h>
#include <core/thread_pool.h>
#include <thread_pool.h>
#include <zone.h>
#include <mutex>

View File

@ -33,7 +33,7 @@
#include <progress_reporter.h>
#include <geometry/geometry_utils.h>
#include <board_commit.h>
#include <core/thread_pool.h>
#include <thread_pool.h>
#include <pcb_shape.h>
#include <wx/log.h>

View File

@ -40,7 +40,7 @@
#include <geometry/shape_circle.h>
#include <ratsnest/ratsnest_data.h>
#include <progress_reporter.h>
#include <core/thread_pool.h>
#include <thread_pool.h>
#include <trigo.h>
#include <drc/drc_rtree.h>

View File

@ -37,7 +37,7 @@
#include <set>
#include <vector>
#include <core/thread_pool.h>
#include <thread_pool.h>
#include <io/io_mgr.h>
#include <jobs/job_export_pcb_odb.h>
#include <pcb_io/pcb_io_mgr.h>

View File

@ -24,7 +24,7 @@
#include <common.h>
#include <board_design_settings.h>
#include <footprint.h>
#include <core/thread_pool.h>
#include <thread_pool.h>
#include <zone.h>
#include <connectivity/connectivity_data.h>
#include <drc/drc_engine.h>

View File

@ -41,7 +41,7 @@
#include <pad.h>
#include <pcb_track.h>
#include <core/profile.h>
#include <core/thread_pool.h>
#include <thread_pool.h>
#include <zone.h>

View File

@ -45,7 +45,7 @@
#include <math/vector2d.h>
#include <pcb_shape.h>
#include <progress_reporter.h>
#include <core/thread_pool.h>
#include <thread_pool.h>
#include <pcb_track.h>
#include <pad.h>
#include <zone.h>

View File

@ -29,7 +29,7 @@
#include <pcb_shape.h>
#include <pad.h>
#include <pcb_track.h>
#include <core/thread_pool.h>
#include <thread_pool.h>
#include <zone.h>
#include <geometry/seg.h>

View File

@ -31,7 +31,7 @@
#include <drc/drc_test_provider.h>
#include <pad.h>
#include <progress_reporter.h>
#include <core/thread_pool.h>
#include <thread_pool.h>
#include <zone.h>
#include <mutex>

Some files were not shown because too many files have changed in this diff Show More