kicad/pcbnew/tools/zone_filler_tool.cpp

268 lines
7.0 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2014-2017 CERN
* Copyright (C) 2014-2018 KiCad Developers, see AUTHORS.txt for contributors.
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* 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 2
* 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, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <cstdint>
#include <thread>
#include <zone.h>
#include <connectivity/connectivity_data.h>
#include <board_commit.h>
#include <board_design_settings.h>
#include <widgets/progress_reporter.h>
#include <widgets/infobar.h>
#include <wx/event.h>
#include <wx/hyperlink.h>
#include <tool/tool_manager.h>
#include "pcb_actions.h"
#include "zone_filler_tool.h"
#include "zone_filler.h"
2019-06-09 21:57:23 +00:00
ZONE_FILLER_TOOL::ZONE_FILLER_TOOL() :
PCB_TOOL_BASE( "pcbnew.ZoneFiller" ),
m_fillInProgress( false )
{
}
ZONE_FILLER_TOOL::~ZONE_FILLER_TOOL()
{
}
void ZONE_FILLER_TOOL::Reset( RESET_REASON aReason )
{
}
void ZONE_FILLER_TOOL::CheckAllZones( wxWindow* aCaller, PROGRESS_REPORTER* aReporter )
{
if( !getEditFrame<PCB_EDIT_FRAME>()->m_ZoneFillsDirty || m_fillInProgress )
return;
m_fillInProgress = true;
std::vector<ZONE*> toFill;
for( ZONE* zone : board()->Zones() )
toFill.push_back(zone);
BOARD_COMMIT commit( this );
ZONE_FILLER filler( frame()->GetBoard(), &commit );
2020-09-14 17:54:14 +00:00
if( aReporter )
filler.SetProgressReporter( aReporter );
else
filler.InstallNewProgressReporter( aCaller, _( "Checking Zones" ), 4 );
std::lock_guard<KISPINLOCK> lock( board()->GetConnectivity()->GetLock() );
if( filler.Fill( toFill, true, aCaller ) )
{
commit.Push( _( "Fill Zone(s)" ), false );
getEditFrame<PCB_EDIT_FRAME>()->m_ZoneFillsDirty = false;
}
else
{
commit.Revert();
}
canvas()->Refresh();
m_fillInProgress = false;
}
void ZONE_FILLER_TOOL::singleShotRefocus( wxIdleEvent& )
{
canvas()->SetFocus();
canvas()->Unbind( wxEVT_IDLE, &ZONE_FILLER_TOOL::singleShotRefocus, this );
}
void ZONE_FILLER_TOOL::FillAllZones( wxWindow* aCaller, PROGRESS_REPORTER* aReporter )
{
PCB_EDIT_FRAME* frame = getEditFrame<PCB_EDIT_FRAME>();
BOARD_COMMIT commit( this );
std::vector<ZONE*> toFill;
if( m_fillInProgress )
return;
m_fillInProgress = true;
for( ZONE* zone : board()->Zones() )
toFill.push_back( zone );
board()->IncrementTimeStamp(); // Clear caches
ZONE_FILLER filler( board(), &commit );
2020-09-14 17:54:14 +00:00
if( !board()->GetDesignSettings().m_DRCEngine->RulesValid() )
{
WX_INFOBAR* infobar = frame->GetInfoBar();
wxHyperlinkCtrl* button = new wxHyperlinkCtrl( infobar, wxID_ANY, _("Show DRC rules"),
wxEmptyString );
button->Bind( wxEVT_COMMAND_HYPERLINK,
std::function<void( wxHyperlinkEvent& aEvent )>(
[frame]( wxHyperlinkEvent& aEvent )
{
frame->ShowBoardSetupDialog( _( "Rules" ) );
} ) );
infobar->RemoveAllButtons();
infobar->AddButton( button );
infobar->ShowMessageFor( _( "Zone fills may be inaccurate. DRC rules contain errors." ),
10000, wxICON_WARNING );
}
2020-09-14 17:54:14 +00:00
if( aReporter )
filler.SetProgressReporter( aReporter );
else
filler.InstallNewProgressReporter( aCaller, _( "Fill All Zones" ), 3 );
Fix issues with zone filling connectivity locking Two issues found with the locking system used to prevent access to stale connectivity data during the zone fill process: 1) a std::mutex has undefined behavior if you try to use it to guard against access from the same thread. Because of the use of wx event loops (and coroutines) it is entirely possible, and in some situations inevitable, that the same thread will try to redraw the ratsnest in the middle of zone refilling. 2) The mutex was only guarding the ZONE_FILLER::Fill method, but the callers of that method also do connectivity updates as part of the COMMIT::Push. Redrawing the ratsnest after the Fill but before the Push will result in stale connectivity pointers to zone filled areas. Fixed (1) by switching to a trivial spinlock implementation. Spinlocks would generally not be desirable if the contention for the connectivity data crossed thread boundaries, but at the moment I believe it's guaranteed that the reads and writes to connectivity that are guarded by this lock happen from the main UI thread. The writes are also quite rare compared to reads, and reads are generally fast, so I'm not really worried about the UI thread spinning for any real amount of time. Fixed (2) by moving the locking location up to the call sites of ZONE_FILLER::Fill. This issue was quite difficult to reproduce, but I found a fairly reliable way: It only happens (for me) on Windows, MSYS2 build, with wxWidgets 3.0 It also only happens if I restrict PcbNew to use 2 CPU cores. With those conditions, I can reproduce the issue described in #6471 by repeatedly editing a zone properties and changing its net. The crash is especially easy to trigger if you press some keys (such as 'e' for edit) while the progress dialog is displayed. It's easiest to do this in a debug build as the slower KiCad is running, the bigger the window is to trigger this bug. Fixes https://gitlab.com/kicad/code/kicad/-/issues/6471 Fixes https://gitlab.com/kicad/code/kicad/-/issues/7048
2021-01-18 17:24:07 +00:00
std::lock_guard<KISPINLOCK> lock( board()->GetConnectivity()->GetLock() );
if( filler.Fill( toFill ) )
{
commit.Push( _( "Fill Zone(s)" ), true ); // Allow undoing zone fill
frame->m_ZoneFillsDirty = false;
}
else
{
commit.Revert();
}
if( filler.IsDebug() )
frame->UpdateUserInterface();
canvas()->Refresh();
m_fillInProgress = false;
// wxWidgets has keyboard focus issues after the progress reporter. Re-setting the focus
// here doesn't work, so we delay it to an idle event.
canvas()->Bind( wxEVT_IDLE, &ZONE_FILLER_TOOL::singleShotRefocus, this );
}
int ZONE_FILLER_TOOL::ZoneFill( const TOOL_EVENT& aEvent )
{
if( m_fillInProgress )
{
wxBell();
return -1;
}
m_fillInProgress = true;
std::vector<ZONE*> toFill;
BOARD_COMMIT commit( this );
if( ZONE* passedZone = aEvent.Parameter<ZONE*>() )
{
toFill.push_back( passedZone );
}
else
{
for( EDA_ITEM* item : selection() )
{
if( ZONE* zone = dynamic_cast<ZONE*>( item ) )
toFill.push_back( zone );
}
}
ZONE_FILLER filler( board(), &commit );
filler.InstallNewProgressReporter( frame(), _( "Fill Zone" ), 4 );
Fix issues with zone filling connectivity locking Two issues found with the locking system used to prevent access to stale connectivity data during the zone fill process: 1) a std::mutex has undefined behavior if you try to use it to guard against access from the same thread. Because of the use of wx event loops (and coroutines) it is entirely possible, and in some situations inevitable, that the same thread will try to redraw the ratsnest in the middle of zone refilling. 2) The mutex was only guarding the ZONE_FILLER::Fill method, but the callers of that method also do connectivity updates as part of the COMMIT::Push. Redrawing the ratsnest after the Fill but before the Push will result in stale connectivity pointers to zone filled areas. Fixed (1) by switching to a trivial spinlock implementation. Spinlocks would generally not be desirable if the contention for the connectivity data crossed thread boundaries, but at the moment I believe it's guaranteed that the reads and writes to connectivity that are guarded by this lock happen from the main UI thread. The writes are also quite rare compared to reads, and reads are generally fast, so I'm not really worried about the UI thread spinning for any real amount of time. Fixed (2) by moving the locking location up to the call sites of ZONE_FILLER::Fill. This issue was quite difficult to reproduce, but I found a fairly reliable way: It only happens (for me) on Windows, MSYS2 build, with wxWidgets 3.0 It also only happens if I restrict PcbNew to use 2 CPU cores. With those conditions, I can reproduce the issue described in #6471 by repeatedly editing a zone properties and changing its net. The crash is especially easy to trigger if you press some keys (such as 'e' for edit) while the progress dialog is displayed. It's easiest to do this in a debug build as the slower KiCad is running, the bigger the window is to trigger this bug. Fixes https://gitlab.com/kicad/code/kicad/-/issues/6471 Fixes https://gitlab.com/kicad/code/kicad/-/issues/7048
2021-01-18 17:24:07 +00:00
std::lock_guard<KISPINLOCK> lock( board()->GetConnectivity()->GetLock() );
if( filler.Fill( toFill ) )
commit.Push( _( "Fill Zone(s)" ), true ); // Allow undoing zone fill
else
commit.Revert();
canvas()->Refresh();
m_fillInProgress = false;
return 0;
}
int ZONE_FILLER_TOOL::ZoneFillAll( const TOOL_EVENT& aEvent )
{
FillAllZones( frame() );
return 0;
}
int ZONE_FILLER_TOOL::ZoneUnfill( const TOOL_EVENT& aEvent )
{
BOARD_COMMIT commit( this );
for( EDA_ITEM* item : selection() )
{
assert( item->Type() == PCB_ZONE_T || item->Type() == PCB_FP_ZONE_T );
ZONE* zone = static_cast<ZONE*>( item );
commit.Modify( zone );
zone->UnFill();
}
commit.Push( _( "Unfill Zone" ) );
canvas()->Refresh();
return 0;
}
int ZONE_FILLER_TOOL::ZoneUnfillAll( const TOOL_EVENT& aEvent )
{
BOARD_COMMIT commit( this );
for( ZONE* zone : board()->Zones() )
{
commit.Modify( zone );
zone->UnFill();
}
commit.Push( _( "Unfill All Zones" ) );
canvas()->Refresh();
return 0;
}
void ZONE_FILLER_TOOL::setTransitions()
{
// Zone actions
Go( &ZONE_FILLER_TOOL::ZoneFill, PCB_ACTIONS::zoneFill.MakeEvent() );
Go( &ZONE_FILLER_TOOL::ZoneFillAll, PCB_ACTIONS::zoneFillAll.MakeEvent() );
Go( &ZONE_FILLER_TOOL::ZoneUnfill, PCB_ACTIONS::zoneUnfill.MakeEvent() );
Go( &ZONE_FILLER_TOOL::ZoneUnfillAll, PCB_ACTIONS::zoneUnfillAll.MakeEvent() );
}