From 0c68f55937c78909ac2e9d6f900e4198930402af Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Wed, 16 Jan 2013 14:50:03 -0500 Subject: [PATCH] Safer thread stopping, should fix an assert --- src/libtomahawk/filemetadata/MusicScanner.cpp | 63 ++++++++++++------- src/libtomahawk/filemetadata/MusicScanner.h | 18 +++++- src/libtomahawk/infosystem/InfoSystem.cpp | 4 +- 3 files changed, 58 insertions(+), 27 deletions(-) diff --git a/src/libtomahawk/filemetadata/MusicScanner.cpp b/src/libtomahawk/filemetadata/MusicScanner.cpp index 5f90dfcbb..a083291d5 100644 --- a/src/libtomahawk/filemetadata/MusicScanner.cpp +++ b/src/libtomahawk/filemetadata/MusicScanner.cpp @@ -34,9 +34,6 @@ #include "utils/Logger.h" -using namespace Tomahawk; - - void DirLister::go() { @@ -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 ) @@ -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; } @@ -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" ); } @@ -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; } @@ -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(); @@ -414,3 +430,4 @@ MusicScanner::readFile( const QFileInfo& fi ) m_scanned++; return m; } + diff --git a/src/libtomahawk/filemetadata/MusicScanner.h b/src/libtomahawk/filemetadata/MusicScanner.h index 06601e5bc..1190cd1c4 100644 --- a/src/libtomahawk/filemetadata/MusicScanner.h +++ b/src/libtomahawk/filemetadata/MusicScanner.h @@ -79,6 +79,21 @@ private: 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 { @@ -125,8 +140,7 @@ private: QVariantList m_filesToDelete; quint32 m_batchsize; - QPointer< DirLister > m_dirLister; - QThread* m_dirListerThreadController; + DirListerThreadController* m_dirListerThreadController; }; #endif diff --git a/src/libtomahawk/infosystem/InfoSystem.cpp b/src/libtomahawk/infosystem/InfoSystem.cpp index f49857b2d..0ba727535 100644 --- a/src/libtomahawk/infosystem/InfoSystem.cpp +++ b/src/libtomahawk/infosystem/InfoSystem.cpp @@ -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 ); @@ -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 );