From f3db64af0579628bc406e7a8c2fe3453b96360f6 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Sun, 16 Apr 2023 15:35:58 +0100 Subject: [PATCH] Multi-threading safety. (Possible fix for KICAD-74.) (cherry picked from commit b76ef8c7e2bed4b6e11f0f38c28d42f340fcf8f5) --- pcbnew/connectivity/connectivity_data.cpp | 10 +++++++++- pcbnew/ratsnest/ratsnest_data.cpp | 10 +--------- pcbnew/ratsnest/ratsnest_data.h | 13 +++++++++---- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/pcbnew/connectivity/connectivity_data.cpp b/pcbnew/connectivity/connectivity_data.cpp index 5c623ad7e9..fa5d4fb8b7 100644 --- a/pcbnew/connectivity/connectivity_data.cpp +++ b/pcbnew/connectivity/connectivity_data.cpp @@ -182,13 +182,21 @@ void CONNECTIVITY_DATA::updateRatsnest() thread_pool& tp = GetKiCadThreadPool(); tp.push_loop( dirty_nets.size(), - [&]( const int a, const int b) + [&]( const int a, const int b ) { for( int ii = a; ii < b; ++ii ) dirty_nets[ii]->UpdateNet(); } ); tp.wait_for_tasks(); + tp.push_loop( dirty_nets.size(), + [&]( const int a, const int b ) + { + for( int ii = a; ii < b; ++ii ) + dirty_nets[ii]->OptimizeRNEdges(); + } ); + tp.wait_for_tasks(); + #ifdef PROFILE rnUpdate.Show(); #endif diff --git a/pcbnew/ratsnest/ratsnest_data.cpp b/pcbnew/ratsnest/ratsnest_data.cpp index f7035fa452..bad7b70272 100644 --- a/pcbnew/ratsnest/ratsnest_data.cpp +++ b/pcbnew/ratsnest/ratsnest_data.cpp @@ -328,7 +328,7 @@ void RN_NET::compute() } -void RN_NET::optimizeRNEdges() +void RN_NET::OptimizeRNEdges() { auto optimizeZoneAnchor = [&]( const VECTOR2I& aPos, const LSET& aLayerSet, @@ -448,14 +448,6 @@ void RN_NET::UpdateNet() { compute(); -#ifdef PROFILE - PROF_TIMER cnt( "optimize" ); -#endif - optimizeRNEdges(); -#ifdef PROFILE - cnt.Show(); -#endif - m_dirty = false; } diff --git a/pcbnew/ratsnest/ratsnest_data.h b/pcbnew/ratsnest/ratsnest_data.h index f1d339bdf0..3553e3909f 100644 --- a/pcbnew/ratsnest/ratsnest_data.h +++ b/pcbnew/ratsnest/ratsnest_data.h @@ -76,6 +76,15 @@ public: * Recompute ratsnest for a net. */ void UpdateNet(); + + /** + * Find optimal ends of RNEdges. The MST will have found the closest anchors, but when + * zones are involved we might have points closer than the anchors. + * + * Normally called after UpdateNet(), but from a separate multi-threaded loop for safety. + */ + void OptimizeRNEdges(); + void Clear(); void AddCluster( std::shared_ptr aCluster ); @@ -94,10 +103,6 @@ protected: ///< Compute the minimum spanning tree using Kruskal's algorithm void kruskalMST( const std::vector &aEdges ); - ///< Find optimal ends of RNEdges. The MST will have found the closest anchors, but when - ///< zones are involved we might have points closer than the anchors. - void optimizeRNEdges(); - protected: ///< Vector of nodes std::multiset, CN_PTR_CMP> m_nodes;