From 97e12f9b95d3c88be3ed757f801763de2b80232f Mon Sep 17 00:00:00 2001 From: Alejandro Wainzinger Date: Sun, 14 Aug 2011 02:32:47 -0700 Subject: [PATCH] Create QThread subclasses for InfoSystemWorker and InfoSystemCache, so we don't try to delete them from a different thread (illegal in Qt), instead delete from QThread subclass' destructor. --- src/libtomahawk/infosystem/infosystem.cpp | 64 ++++++++++++++++++++--- src/libtomahawk/infosystem/infosystem.h | 36 ++++++++++++- 2 files changed, 90 insertions(+), 10 deletions(-) diff --git a/src/libtomahawk/infosystem/infosystem.cpp b/src/libtomahawk/infosystem/infosystem.cpp index 9bc46a1f6..1618aec80 100644 --- a/src/libtomahawk/infosystem/infosystem.cpp +++ b/src/libtomahawk/infosystem/infosystem.cpp @@ -58,14 +58,14 @@ InfoSystem::InfoSystem( QObject *parent ) qDebug() << Q_FUNC_INFO; - m_infoSystemCacheThreadController = new QThread( this ); - m_cache = QWeakPointer< InfoSystemCache >( new InfoSystemCache() ); - m_cache.data()->moveToThread( m_infoSystemCacheThreadController ); + m_infoSystemCacheThreadController = new InfoSystemCacheThread( this ); + m_cache = m_infoSystemCacheThreadController->cache(); + //m_cache.data()->moveToThread( m_infoSystemCacheThreadController ); m_infoSystemCacheThreadController->start( QThread::IdlePriority ); - m_infoSystemWorkerThreadController = new QThread( this ); - m_worker = QWeakPointer< InfoSystemWorker>( new InfoSystemWorker() ); - m_worker.data()->moveToThread( m_infoSystemWorkerThreadController ); + m_infoSystemWorkerThreadController = new InfoSystemWorkerThread( this ); + m_worker = m_infoSystemWorkerThreadController->worker(); + //m_worker.data()->moveToThread( m_infoSystemWorkerThreadController ); m_infoSystemWorkerThreadController->start(); QMetaObject::invokeMethod( m_worker.data(), "init", Qt::QueuedConnection, Q_ARG( QWeakPointer< Tomahawk::InfoSystem::InfoSystemCache >, m_cache ) ); @@ -89,7 +89,7 @@ InfoSystem::~InfoSystem() m_infoSystemWorkerThreadController->quit(); m_infoSystemWorkerThreadController->wait( 60000 ); - delete m_worker.data(); + //delete m_worker.data(); delete m_infoSystemWorkerThreadController; m_infoSystemWorkerThreadController = 0; } @@ -100,7 +100,7 @@ InfoSystem::~InfoSystem() m_infoSystemCacheThreadController->quit(); m_infoSystemCacheThreadController->wait( 60000 ); - delete m_cache.data(); + //delete m_cache.data(); delete m_infoSystemCacheThreadController; m_infoSystemCacheThreadController = 0; } @@ -155,6 +155,54 @@ InfoSystem::pushInfo( const QString &caller, const InfoTypeMap &input ) QMetaObject::invokeMethod( m_worker.data(), "pushInfo", Qt::QueuedConnection, Q_ARG( QString, caller ), Q_ARG( Tomahawk::InfoSystem::InfoType, type ), Q_ARG( QVariant, input[ type ] ) ); } + +InfoSystemCacheThread::InfoSystemCacheThread( QObject *parent ) + : QThread( parent ) +{ + m_cache = QWeakPointer< InfoSystemCache >( new InfoSystemCache() ); +} + +InfoSystemCacheThread::~InfoSystemCacheThread() +{ + delete m_cache.data(); +} + +void +InfoSystemCacheThread::InfoSystemCacheThread::run() +{ + exec(); +} + +QWeakPointer< InfoSystemCache > +InfoSystemCacheThread::cache() const +{ + return m_cache; +} + +InfoSystemWorkerThread::InfoSystemWorkerThread( QObject *parent ) + : QThread( parent ) +{ + m_worker = QWeakPointer< InfoSystemWorker >( new InfoSystemWorker() ); +} + +InfoSystemWorkerThread::~InfoSystemWorkerThread() +{ + delete m_worker.data(); +} + +void +InfoSystemWorkerThread::InfoSystemWorkerThread::run() +{ + exec(); +} + +QWeakPointer< InfoSystemWorker > +InfoSystemWorkerThread::worker() const +{ + return m_worker; +} + + } //namespace InfoSystem } //namespace Tomahawk diff --git a/src/libtomahawk/infosystem/infosystem.h b/src/libtomahawk/infosystem/infosystem.h index d20bbd8c3..bc000bae8 100644 --- a/src/libtomahawk/infosystem/infosystem.h +++ b/src/libtomahawk/infosystem/infosystem.h @@ -159,6 +159,36 @@ private: typedef QWeakPointer< InfoPlugin > InfoPluginPtr; +class InfoSystemCacheThread : public QThread +{ + Q_OBJECT + +public: + InfoSystemCacheThread( QObject *parent ); + virtual ~InfoSystemCacheThread(); + + void run(); + QWeakPointer< InfoSystemCache > cache() const; + +private: + QWeakPointer< InfoSystemCache > m_cache; +}; + +class InfoSystemWorkerThread : public QThread +{ + Q_OBJECT + +public: + InfoSystemWorkerThread( QObject *parent ); + virtual ~InfoSystemWorkerThread(); + + void run(); + QWeakPointer< InfoSystemWorker > worker() const; + +private: + QWeakPointer< InfoSystemWorker > m_worker; +}; + class DLLEXPORT InfoSystem : public QObject { Q_OBJECT @@ -185,8 +215,8 @@ public slots: private: QWeakPointer< InfoSystemCache > m_cache; QWeakPointer< InfoSystemWorker > m_worker; - QThread* m_infoSystemCacheThreadController; - QThread* m_infoSystemWorkerThreadController; + InfoSystemCacheThread* m_infoSystemCacheThreadController; + InfoSystemWorkerThread* m_infoSystemWorkerThreadController; static InfoSystem* s_instance; }; @@ -195,6 +225,8 @@ private: } + + inline uint qHash( Tomahawk::InfoSystem::InfoCriteriaHash hash ) { QCryptographicHash md5( QCryptographicHash::Md5 );