pcbnew: locking connectivity using RAII

Rather than depend on proper unlocking for each exit, we move the
connectivity lock mutex into an RAII-type configuration that
automatically unlocks on exit.
This commit is contained in:
Seth Hillbrand 2018-12-19 22:15:53 -07:00
parent 24f9bfa13b
commit 55f2a79957
6 changed files with 18 additions and 67 deletions

View File

@ -1,56 +0,0 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 KiCad Developers, see change_log.txt for contributors.
* @author Tomasz Wlostowski <tomasz.wlostowski@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
*/
#ifndef __LOCKABLE_H
#define __LOCKABLE_H
#include <thread>
#include <mutex>
class LOCKABLE
{
public:
LOCKABLE() {};
~LOCKABLE() {};
void Lock()
{
m_lock.lock();
}
void Unlock()
{
m_lock.unlock();
}
bool TryLock()
{
return m_lock.try_lock();
}
private:
std::mutex m_lock;
};
#endif

View File

@ -27,7 +27,6 @@
#define __CONNECTIVITY_DATA_H #define __CONNECTIVITY_DATA_H
#include <core/typeinfo.h> #include <core/typeinfo.h>
#include <core/lockable.h>
#include <wx/string.h> #include <wx/string.h>
#include <vector> #include <vector>
@ -77,7 +76,7 @@ struct RN_DYNAMIC_LINE
}; };
// a wrapper class encompassing the connectivity computation algorithm and the // a wrapper class encompassing the connectivity computation algorithm and the
class CONNECTIVITY_DATA : public LOCKABLE class CONNECTIVITY_DATA
{ {
public: public:
CONNECTIVITY_DATA(); CONNECTIVITY_DATA();
@ -235,6 +234,11 @@ public:
return m_connAlgo; return m_connAlgo;
} }
std::mutex& GetLock()
{
return m_lock;
}
void MarkItemNetAsDirty( BOARD_ITEM* aItem ); void MarkItemNetAsDirty( BOARD_ITEM* aItem );
void SetProgressReporter( PROGRESS_REPORTER* aReporter ); void SetProgressReporter( PROGRESS_REPORTER* aReporter );
@ -253,6 +257,8 @@ private:
std::vector<RN_NET*> m_nets; std::vector<RN_NET*> m_nets;
PROGRESS_REPORTER* m_progressReporter; PROGRESS_REPORTER* m_progressReporter;
std::mutex m_lock;
}; };
#endif #endif

View File

@ -42,6 +42,7 @@
#include <gal/graphics_abstraction_layer.h> #include <gal/graphics_abstraction_layer.h>
#include <functional> #include <functional>
#include <thread>
using namespace std::placeholders; using namespace std::placeholders;
const LAYER_NUM GAL_LAYER_ORDER[] = const LAYER_NUM GAL_LAYER_ORDER[] =

View File

@ -97,7 +97,9 @@ void PCB_BASE_FRAME::DrawGeneralRatsnest( wxDC* aDC, int aNetcode )
auto connectivity = m_Pcb->GetConnectivity(); auto connectivity = m_Pcb->GetConnectivity();
if( !connectivity->TryLock() ) std::unique_lock<std::mutex> lock( connectivity->GetLock(), std::try_to_lock );
if( !lock )
return; return;
COLOR4D color = Settings().Colors().GetItemColor( LAYER_RATSNEST ); COLOR4D color = Settings().Colors().GetItemColor( LAYER_RATSNEST );
@ -131,8 +133,6 @@ void PCB_BASE_FRAME::DrawGeneralRatsnest( wxDC* aDC, int aNetcode )
} }
} }
} }
connectivity->Unlock();
} }

View File

@ -60,7 +60,9 @@ const BOX2I RATSNEST_VIEWITEM::ViewBBox() const
void RATSNEST_VIEWITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const void RATSNEST_VIEWITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
{ {
if( !m_data->TryLock() ) std::unique_lock<std::mutex> lock( m_data->GetLock(), std::try_to_lock );
if( !lock )
return; return;
constexpr int CROSS_SIZE = 200000; constexpr int CROSS_SIZE = 200000;
@ -148,8 +150,6 @@ void RATSNEST_VIEWITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
} }
} }
} }
m_data->Unlock();
} }

View File

@ -85,7 +85,9 @@ bool ZONE_FILLER::Fill( std::vector<ZONE_CONTAINER*> aZones, bool aCheck )
std::vector<CN_ZONE_ISOLATED_ISLAND_LIST> toFill; std::vector<CN_ZONE_ISOLATED_ISLAND_LIST> toFill;
auto connectivity = m_board->GetConnectivity(); auto connectivity = m_board->GetConnectivity();
if( !connectivity->TryLock() ) std::unique_lock<std::mutex> lock( connectivity->GetLock(), std::try_to_lock );
if( !lock )
return false; return false;
for( auto zone : aZones ) for( auto zone : aZones )
@ -229,7 +231,6 @@ bool ZONE_FILLER::Fill( std::vector<ZONE_CONTAINER*> aZones, bool aCheck )
m_commit->Revert(); m_commit->Revert();
connectivity->SetProgressReporter( nullptr ); connectivity->SetProgressReporter( nullptr );
connectivity->Unlock();
return false; return false;
} }
} }
@ -304,7 +305,6 @@ bool ZONE_FILLER::Fill( std::vector<ZONE_CONTAINER*> aZones, bool aCheck )
connectivity->RecalculateRatsnest(); connectivity->RecalculateRatsnest();
} }
connectivity->Unlock();
return true; return true;
} }