Skip to content

Commit

Permalink
Safer thread stopping, should fix an assert
Browse files Browse the repository at this point in the history
  • Loading branch information
jefferai committed Jan 16, 2013
1 parent c1ea6d7 commit 0c68f55
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 27 deletions.
63 changes: 40 additions & 23 deletions src/libtomahawk/filemetadata/MusicScanner.cpp
Expand Up @@ -34,9 +34,6 @@

#include "utils/Logger.h"

using namespace Tomahawk;


void
DirLister::go()
{
Expand Down Expand Up @@ -100,6 +97,38 @@ DirLister::scanDir( QDir dir, int depth )
}


DirListerThreadController::DirListerThreadController( QObject *parent )
: QThread( parent )
{
tDebug() << Q_FUNC_INFO;
}


DirListerThreadController::~DirListerThreadController()
{
tDebug() << Q_FUNC_INFO;
}


void
DirListerThreadController::run()
{
m_dirLister = QPointer< DirLister >( new DirLister( m_paths ) );
connect( m_dirLister.data(), SIGNAL( fileToScan( QFileInfo ) ),
parent(), SLOT( scanFile( QFileInfo ) ), Qt::QueuedConnection );

// queued, so will only fire after all dirs have been scanned:
connect( m_dirLister.data(), SIGNAL( finished() ),
parent(), SLOT( postOps() ), Qt::QueuedConnection );

QMetaObject::invokeMethod( m_dirLister.data(), "go", Qt::QueuedConnection );

exec();
if( !m_dirLister.isNull() )
delete m_dirLister.data();
}


MusicScanner::MusicScanner( ScanManager::ScanMode scanMode, const QStringList& paths, quint32 bs )
: QObject()
, m_scanMode( scanMode )
Expand All @@ -125,12 +154,11 @@ MusicScanner::~MusicScanner()
{
tDebug() << Q_FUNC_INFO;

if ( !m_dirLister.isNull() )
if ( m_dirListerThreadController )
{
m_dirListerThreadController->quit();;
m_dirListerThreadController->quit();
m_dirListerThreadController->wait( 60000 );

delete m_dirLister.data();
delete m_dirListerThreadController;
m_dirListerThreadController = 0;
}
Expand Down Expand Up @@ -182,20 +210,9 @@ MusicScanner::scan()
return;
}

m_dirListerThreadController = new QThread( this );

m_dirLister = QPointer< DirLister >( new DirLister( m_paths ) );
m_dirLister.data()->moveToThread( m_dirListerThreadController );

connect( m_dirLister.data(), SIGNAL( fileToScan( QFileInfo ) ),
SLOT( scanFile( QFileInfo ) ), Qt::QueuedConnection );

// queued, so will only fire after all dirs have been scanned:
connect( m_dirLister.data(), SIGNAL( finished() ),
SLOT( postOps() ), Qt::QueuedConnection );

m_dirListerThreadController = new DirListerThreadController( this );
m_dirListerThreadController->setPaths( m_paths );
m_dirListerThreadController->start( QThread::IdlePriority );
QMetaObject::invokeMethod( m_dirLister.data(), "go" );
}


Expand Down Expand Up @@ -250,12 +267,11 @@ MusicScanner::postOps()
void
MusicScanner::cleanup()
{
if ( !m_dirLister.isNull() )
if ( m_dirListerThreadController )
{
m_dirListerThreadController->quit();;
m_dirListerThreadController->quit();
m_dirListerThreadController->wait( 60000 );

delete m_dirLister.data();
delete m_dirListerThreadController;
m_dirListerThreadController = 0;
}
Expand Down Expand Up @@ -368,7 +384,7 @@ MusicScanner::readFile( const QFileInfo& fi )
int bitrate = 0;
int duration = 0;

Tag *tag = Tag::fromFile( f );
Tomahawk::Tag *tag = Tomahawk::Tag::fromFile( f );
if ( f.audioProperties() )
{
TagLib::AudioProperties *properties = f.audioProperties();
Expand Down Expand Up @@ -414,3 +430,4 @@ MusicScanner::readFile( const QFileInfo& fi )
m_scanned++;
return m;
}

18 changes: 16 additions & 2 deletions src/libtomahawk/filemetadata/MusicScanner.h
Expand Up @@ -79,6 +79,21 @@ private slots:
bool m_deleting;
};

class DirListerThreadController : public QThread
{
Q_OBJECT

public:
DirListerThreadController( QObject* parent );
virtual ~DirListerThreadController();

void setPaths( const QStringList& paths ) { m_paths = paths; }
void run();

private:
QPointer< DirLister > m_dirLister;
QStringList m_paths;
};

class MusicScanner : public QObject
{
Expand Down Expand Up @@ -125,8 +140,7 @@ private slots:
QVariantList m_filesToDelete;
quint32 m_batchsize;

QPointer< DirLister > m_dirLister;
QThread* m_dirListerThreadController;
DirListerThreadController* m_dirListerThreadController;
};

#endif
4 changes: 2 additions & 2 deletions src/libtomahawk/infosystem/InfoSystem.cpp
Expand Up @@ -123,7 +123,7 @@ InfoSystem::~InfoSystem()
{
tDebug() << Q_FUNC_INFO << " beginning";

if ( m_infoSystemWorkerThreadController->worker() )
if ( m_infoSystemWorkerThreadController )
{
m_infoSystemWorkerThreadController->quit();
m_infoSystemWorkerThreadController->wait( 60000 );
Expand All @@ -133,7 +133,7 @@ InfoSystem::~InfoSystem()
}
tDebug() << Q_FUNC_INFO << " done deleting worker";

if( m_infoSystemCacheThreadController->cache() )
if( m_infoSystemCacheThreadController )
{
m_infoSystemCacheThreadController->quit();
m_infoSystemCacheThreadController->wait( 60000 );
Expand Down

0 comments on commit 0c68f55

Please sign in to comment.