Fix FOOTPRINT_PREVIEW_PANEL loader thread synchronization

This commit is contained in:
Chris Pavlina 2017-03-05 22:22:25 -05:00
parent 6f3ec438cb
commit 541b91fd15
2 changed files with 72 additions and 71 deletions

View File

@ -38,6 +38,7 @@ class KIWAY;
class IO_MGR;
class BOARD;
class wxStaticText;
class FP_LOADER_THREAD;
enum FOOTPRINT_STATUS {
FPS_NOT_FOUND = 0,
@ -47,6 +48,8 @@ enum FOOTPRINT_STATUS {
class FOOTPRINT_PREVIEW_PANEL : public PCB_DRAW_PANEL_GAL, public KIWAY_HOLDER
{
friend class FP_LOADER_THREAD;
public:
@ -104,12 +107,9 @@ public:
protected:
class LOADER_THREAD : public wxThread
class IFACE
{
public:
LOADER_THREAD ( FOOTPRINT_PREVIEW_PANEL *aParent );
~LOADER_THREAD ();
/**
* Threadsafe accessor to retrieve an entry from the cache.
*/
@ -121,10 +121,6 @@ protected:
*/
CACHE_ENTRY AddToQueue( LIB_ID const & aEntry );
protected:
void* Entry() override;
FOOTPRINT_PREVIEW_PANEL *m_parent;
/**
* Threadsafe accessor to pop from the loader queue. Returns a
* cache entry or an empty option if there is none.
@ -144,9 +140,9 @@ protected:
std::deque<CACHE_ENTRY> m_loaderQueue;
std::map<LIB_ID, CACHE_ENTRY> m_cachedFootprints;
wxMutex m_loaderLock;
};
FOOTPRINT_PREVIEW_PANEL(
KIWAY* aKiway, wxWindow* aParent,
KIGFX::GAL_DISPLAY_OPTIONS& aOpts, GAL_TYPE aGalType );
@ -157,7 +153,8 @@ private:
void renderFootprint( MODULE *module );
std::unique_ptr<LOADER_THREAD> m_loader;
FP_LOADER_THREAD* m_loader;
std::shared_ptr<IFACE> m_iface;
std::unique_ptr<BOARD> m_dummyBoard;

View File

@ -36,78 +36,27 @@
#include <wx/stattext.h>
FOOTPRINT_PREVIEW_PANEL::LOADER_THREAD::LOADER_THREAD ( FOOTPRINT_PREVIEW_PANEL* aParent ) :
wxThread ( wxTHREAD_JOINABLE ),
m_parent( aParent )
class FP_LOADER_THREAD: public wxThread
{
FOOTPRINT_PREVIEW_PANEL* m_parent;
std::shared_ptr<FOOTPRINT_PREVIEW_PANEL::IFACE> m_iface;
}
public:
FP_LOADER_THREAD( FOOTPRINT_PREVIEW_PANEL* aParent,
std::shared_ptr<FOOTPRINT_PREVIEW_PANEL::IFACE> const& aIface ):
wxThread( wxTHREAD_DETACHED ),
m_parent( aParent ),
m_iface( aIface )
{}
~FP_LOADER_THREAD()
{}
FOOTPRINT_PREVIEW_PANEL::LOADER_THREAD::~LOADER_THREAD ()
{
}
boost::optional<FOOTPRINT_PREVIEW_PANEL::CACHE_ENTRY>
FOOTPRINT_PREVIEW_PANEL::LOADER_THREAD::PopFromQueue()
{
wxMutexLocker lock( m_loaderLock );
if( m_loaderQueue.empty() )
virtual void* Entry() override
{
return boost::none;
}
else
{
auto ent = m_loaderQueue.front();
m_loaderQueue.pop_front();
return ent;
}
}
FOOTPRINT_PREVIEW_PANEL::CACHE_ENTRY
FOOTPRINT_PREVIEW_PANEL::LOADER_THREAD::AddToQueue( LIB_ID const & aEntry )
{
wxMutexLocker lock( m_loaderLock );
CACHE_ENTRY ent = { aEntry, NULL, FPS_LOADING };
m_cachedFootprints[aEntry] = ent;
m_loaderQueue.push_back( ent );
return ent;
}
void FOOTPRINT_PREVIEW_PANEL::LOADER_THREAD::AddToCache(
FOOTPRINT_PREVIEW_PANEL::CACHE_ENTRY const & aEntry )
{
wxMutexLocker lock( m_loaderLock );
m_cachedFootprints[aEntry.fpid] = aEntry;
}
boost::optional<FOOTPRINT_PREVIEW_PANEL::CACHE_ENTRY>
FOOTPRINT_PREVIEW_PANEL::LOADER_THREAD::GetFromCache( LIB_ID const & aFPID )
{
wxMutexLocker lock( m_loaderLock );
auto it = m_cachedFootprints.find( aFPID );
if( it != m_cachedFootprints.end() )
return it->second;
else
return boost::none;
}
void* FOOTPRINT_PREVIEW_PANEL::LOADER_THREAD::Entry()
{
while(!TestDestroy())
{
auto ent = PopFromQueue();
auto ent = m_iface->PopFromQueue();
if( ent )
{
@ -133,7 +82,7 @@ void* FOOTPRINT_PREVIEW_PANEL::LOADER_THREAD::Entry()
if(ent->status != FPS_NOT_FOUND )
ent->status = FPS_READY;
AddToCache( *ent );
m_iface->AddToCache( *ent );
if( ent->fpid == m_parent->m_currentFPID )
{
@ -147,6 +96,60 @@ void* FOOTPRINT_PREVIEW_PANEL::LOADER_THREAD::Entry()
}
return NULL;
}
};
boost::optional<FOOTPRINT_PREVIEW_PANEL::CACHE_ENTRY>
FOOTPRINT_PREVIEW_PANEL::IFACE::PopFromQueue()
{
wxMutexLocker lock( m_loaderLock );
if( m_loaderQueue.empty() )
{
return boost::none;
}
else
{
auto ent = m_loaderQueue.front();
m_loaderQueue.pop_front();
return ent;
}
}
FOOTPRINT_PREVIEW_PANEL::CACHE_ENTRY
FOOTPRINT_PREVIEW_PANEL::IFACE::AddToQueue( LIB_ID const & aEntry )
{
wxMutexLocker lock( m_loaderLock );
CACHE_ENTRY ent = { aEntry, NULL, FPS_LOADING };
m_cachedFootprints[aEntry] = ent;
m_loaderQueue.push_back( ent );
return ent;
}
void FOOTPRINT_PREVIEW_PANEL::IFACE::AddToCache(
FOOTPRINT_PREVIEW_PANEL::CACHE_ENTRY const & aEntry )
{
wxMutexLocker lock( m_loaderLock );
m_cachedFootprints[aEntry.fpid] = aEntry;
}
boost::optional<FOOTPRINT_PREVIEW_PANEL::CACHE_ENTRY>
FOOTPRINT_PREVIEW_PANEL::IFACE::GetFromCache( LIB_ID const & aFPID )
{
wxMutexLocker lock( m_loaderLock );
auto it = m_cachedFootprints.find( aFPID );
if( it != m_cachedFootprints.end() )
return it->second;
else
return boost::none;
}
@ -159,7 +162,8 @@ FOOTPRINT_PREVIEW_PANEL::FOOTPRINT_PREVIEW_PANEL(
m_hidesizer( NULL )
{
m_loader = std::make_unique<LOADER_THREAD>( this );
m_iface = std::make_shared<IFACE>();
m_loader = new FP_LOADER_THREAD( this, m_iface );
m_loader->Run();
SetStealsFocus( false );
@ -189,12 +193,12 @@ FOOTPRINT_PREVIEW_PANEL::~FOOTPRINT_PREVIEW_PANEL( )
FOOTPRINT_PREVIEW_PANEL::CACHE_ENTRY
FOOTPRINT_PREVIEW_PANEL::CacheFootprint ( const LIB_ID& aFPID )
{
auto opt_ent = m_loader->GetFromCache( aFPID );
auto opt_ent = m_iface->GetFromCache( aFPID );
if( opt_ent )
return *opt_ent;
else
return m_loader->AddToQueue( aFPID );
return m_iface->AddToQueue( aFPID );
}