1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-21 08:19:42 +01:00

Properly delete infosystem objects in the correct thread

This commit is contained in:
Jeff Mitchell 2011-05-03 18:44:09 -04:00
parent eb31702711
commit 33530084b0
5 changed files with 60 additions and 53 deletions

View File

@ -60,80 +60,87 @@ InfoSystem::InfoSystem(QObject *parent)
qDebug() << Q_FUNC_INFO;
m_infoSystemCacheThreadController = new QThread( this );
m_cache = new InfoSystemCache();
m_cache->moveToThread( m_infoSystemCacheThreadController );
m_cache = QWeakPointer< InfoSystemCache >( new InfoSystemCache() );
m_cache.data()->moveToThread( m_infoSystemCacheThreadController );
m_infoSystemCacheThreadController->start( QThread::IdlePriority );
m_infoSystemWorkerThreadController = new QThread( this );
m_worker = new InfoSystemWorker();
m_worker->moveToThread( m_infoSystemWorkerThreadController );
m_worker = QWeakPointer< InfoSystemWorker>( new InfoSystemWorker() );
m_worker.data()->moveToThread( m_infoSystemWorkerThreadController );
m_infoSystemWorkerThreadController->start();
QMetaObject::invokeMethod( m_worker, "init", Qt::QueuedConnection );
QMetaObject::invokeMethod( m_worker.data(), "init", Qt::QueuedConnection, Q_ARG( QWeakPointer< Tomahawk::InfoSystem::InfoSystemCache >, m_cache ) );
connect( TomahawkSettings::instance(), SIGNAL( changed() ), SLOT( newNam() ) );
connect( m_cache, SIGNAL( info( QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, Tomahawk::InfoSystem::InfoCustomData ) ),
connect( m_cache.data(), SIGNAL( info( QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, Tomahawk::InfoSystem::InfoCustomData ) ),
this, SLOT( infoSlot( QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, Tomahawk::InfoSystem::InfoCustomData ) ), Qt::UniqueConnection );
connect( m_worker, SIGNAL( info( QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, Tomahawk::InfoSystem::InfoCustomData ) ),
connect( m_worker.data(), SIGNAL( info( QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, Tomahawk::InfoSystem::InfoCustomData ) ),
this, SLOT( infoSlot( QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, Tomahawk::InfoSystem::InfoCustomData ) ), Qt::UniqueConnection );
}
InfoSystem::~InfoSystem()
{
qDebug() << Q_FUNC_INFO << " beginning";
if ( m_infoSystemWorkerThreadController )
m_infoSystemWorkerThreadController->quit();
qDebug() << Q_FUNC_INFO << " sent quit signals";
if( m_infoSystemWorkerThreadController )
qDebug() << "THREAD I'M RUNNING IN: " << QThread::currentThread();
if ( !m_worker.isNull() )
{
while( !m_infoSystemWorkerThreadController->isFinished() )
QMetaObject::invokeMethod( m_worker.data(), "deleteLater", Qt::QueuedConnection );
while( !m_worker.isNull() )
{
qDebug() << Q_FUNC_INFO << " worker thread controller not finished, processing events";
qDebug() << Q_FUNC_INFO << " worker not deleted, processing events";
QCoreApplication::processEvents( QEventLoop::AllEvents, 200 );
TomahawkUtils::Sleep::msleep( 100 );
}
qDebug() << Q_FUNC_INFO << " worker is finished, deleting worker";
if( m_worker )
{
qDebug() << "THREAD I'M RUNNING IN: " << QThread::currentThread();
delete m_worker;
m_worker = 0;
}
if ( m_infoSystemWorkerThreadController )
m_infoSystemWorkerThreadController->quit();
qDebug() << Q_FUNC_INFO << " worker finished being deleted";
delete m_infoSystemWorkerThreadController;
m_infoSystemWorkerThreadController = 0;
if( m_infoSystemWorkerThreadController )
{
while( !m_infoSystemWorkerThreadController->isFinished() )
{
qDebug() << Q_FUNC_INFO << " worker thread controller not finished, processing events";
QCoreApplication::processEvents( QEventLoop::AllEvents, 200 );
TomahawkUtils::Sleep::msleep( 100 );
}
delete m_infoSystemWorkerThreadController;
m_infoSystemWorkerThreadController = 0;
}
}
qDebug() << Q_FUNC_INFO << " done deleting worker";
if ( m_infoSystemCacheThreadController )
m_infoSystemCacheThreadController->quit();
if( m_infoSystemCacheThreadController )
if ( !m_cache.isNull() )
{
while( !m_infoSystemCacheThreadController->isFinished() )
QMetaObject::invokeMethod( m_cache.data(), "deleteLater", Qt::QueuedConnection );
while( !m_cache.isNull() )
{
qDebug() << Q_FUNC_INFO << " cache thread controller not finished, processing events";
qDebug() << Q_FUNC_INFO << " worker not deleted, processing events";
QCoreApplication::processEvents( QEventLoop::AllEvents, 200 );
TomahawkUtils::Sleep::msleep( 100 );
}
if( m_cache )
if ( m_infoSystemCacheThreadController )
m_infoSystemCacheThreadController->quit();
if( m_infoSystemCacheThreadController )
{
delete m_cache;
m_cache = 0;
while( !m_infoSystemCacheThreadController->isFinished() )
{
qDebug() << Q_FUNC_INFO << " worker thread controller not finished, processing events";
QCoreApplication::processEvents( QEventLoop::AllEvents, 200 );
TomahawkUtils::Sleep::msleep( 100 );
}
delete m_infoSystemCacheThreadController;
m_infoSystemCacheThreadController = 0;
}
delete m_infoSystemCacheThreadController;
m_infoSystemCacheThreadController = 0;
}
qDebug() << Q_FUNC_INFO << " done deleting cache";
}
@ -142,7 +149,7 @@ void
InfoSystem::newNam() const
{
qDebug() << Q_FUNC_INFO;
QMetaObject::invokeMethod( m_worker, "newNam", Qt::QueuedConnection );
QMetaObject::invokeMethod( m_worker.data(), "newNam", Qt::QueuedConnection );
}
@ -153,7 +160,7 @@ InfoSystem::getInfo( const QString &caller, const InfoType type, const QVariant&
m_dataTracker[caller][type] = m_dataTracker[caller][type] + 1;
qDebug() << "current count in dataTracker for type" << type << "is" << m_dataTracker[caller][type];
QMetaObject::invokeMethod( m_worker, "getInfo", Qt::QueuedConnection, Q_ARG( QString, caller ), Q_ARG( Tomahawk::InfoSystem::InfoType, type ), Q_ARG( QVariant, input ), Q_ARG( Tomahawk::InfoSystem::InfoCustomData, customData ) );
QMetaObject::invokeMethod( m_worker.data(), "getInfo", Qt::QueuedConnection, Q_ARG( QString, caller ), Q_ARG( Tomahawk::InfoSystem::InfoType, type ), Q_ARG( QVariant, input ), Q_ARG( Tomahawk::InfoSystem::InfoCustomData, customData ) );
}
@ -170,7 +177,7 @@ InfoSystem::pushInfo( const QString &caller, const InfoType type, const QVariant
{
qDebug() << Q_FUNC_INFO;
QMetaObject::invokeMethod( m_worker, "pushInfo", Qt::QueuedConnection, Q_ARG( QString, caller ), Q_ARG( Tomahawk::InfoSystem::InfoType, type ), Q_ARG( QVariant, input ) );
QMetaObject::invokeMethod( m_worker.data(), "pushInfo", Qt::QueuedConnection, Q_ARG( QString, caller ), Q_ARG( Tomahawk::InfoSystem::InfoType, type ), Q_ARG( QVariant, input ) );
}

View File

@ -153,8 +153,6 @@ public:
void pushInfo( const QString &caller, const InfoType type, const QVariant &input );
void pushInfo( const QString &caller, const InfoMap &input );
InfoSystemCache* getCache() const { return m_cache; }
signals:
void info( QString caller, Tomahawk::InfoSystem::InfoType, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomData customData );
void finished( QString target );
@ -167,8 +165,8 @@ public slots:
private:
QHash< QString, QHash< InfoType, int > > m_dataTracker;
InfoSystemCache* m_cache;
InfoSystemWorker* m_worker;
QWeakPointer< InfoSystemCache > m_cache;
QWeakPointer< InfoSystemWorker > m_worker;
QThread* m_infoSystemCacheThreadController;
QThread* m_infoSystemWorkerThreadController;
@ -200,5 +198,6 @@ inline uint qHash( Tomahawk::InfoSystem::InfoCriteriaHash hash )
Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoGenericMap );
Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoCustomData );
Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoCriteriaHash );
Q_DECLARE_METATYPE( QWeakPointer< Tomahawk::InfoSystem::InfoSystemCache > );
#endif // TOMAHAWK_INFOSYSTEM_H

View File

@ -54,8 +54,10 @@ InfoSystemWorker::~InfoSystemWorker()
}
void InfoSystemWorker::init()
void
InfoSystemWorker::init( QWeakPointer< Tomahawk::InfoSystem::InfoSystemCache> cache )
{
qDebug() << Q_FUNC_INFO << " and cache is " << cache.data();
InfoPluginPtr enptr( new EchoNestPlugin() );
m_plugins.append( enptr );
registerInfoTypes( enptr, enptr.data()->supportedGetTypes(), enptr.data()->supportedPushTypes() );
@ -66,8 +68,6 @@ void InfoSystemWorker::init()
m_plugins.append( lfmptr );
registerInfoTypes( lfmptr, lfmptr.data()->supportedGetTypes(), lfmptr.data()->supportedPushTypes() );
InfoSystemCache *cache = InfoSystem::instance()->getCache();
Q_FOREACH( InfoPluginPtr plugin, m_plugins )
{
connect(
@ -81,11 +81,11 @@ void InfoSystemWorker::init()
connect(
plugin.data(),
SIGNAL( getCachedInfo( Tomahawk::InfoSystem::InfoCriteriaHash, qint64, QString, Tomahawk::InfoSystem::InfoType, QVariant, Tomahawk::InfoSystem::InfoCustomData ) ),
cache,
cache.data(),
SLOT( getCachedInfoSlot( Tomahawk::InfoSystem::InfoCriteriaHash, qint64, QString, Tomahawk::InfoSystem::InfoType, QVariant, Tomahawk::InfoSystem::InfoCustomData ) )
);
connect(
cache,
cache.data(),
SIGNAL( notInCache( Tomahawk::InfoSystem::InfoCriteriaHash, QString, Tomahawk::InfoSystem::InfoType, QVariant, Tomahawk::InfoSystem::InfoCustomData ) ),
plugin.data(),
SLOT( notInCacheSlot( Tomahawk::InfoSystem::InfoCriteriaHash, QString, Tomahawk::InfoSystem::InfoType, QVariant, Tomahawk::InfoSystem::InfoCustomData ) )
@ -93,7 +93,7 @@ void InfoSystemWorker::init()
connect(
plugin.data(),
SIGNAL( updateCache( Tomahawk::InfoSystem::InfoCriteriaHash, qint64, Tomahawk::InfoSystem::InfoType, QVariant ) ),
cache,
cache.data(),
SLOT( updateCacheSlot( Tomahawk::InfoSystem::InfoCriteriaHash, qint64, Tomahawk::InfoSystem::InfoType, QVariant ) )
);
connect(

View File

@ -52,7 +52,7 @@ signals:
void namChanged( QNetworkAccessManager* );
public slots:
void init();
void init( QWeakPointer< Tomahawk::InfoSystem::InfoSystemCache > cache );
void getInfo( const QString caller, const Tomahawk::InfoSystem::InfoType type, const QVariant input, const Tomahawk::InfoSystem::InfoCustomData customData );
void pushInfo( const QString caller, const Tomahawk::InfoSystem::InfoType type, const QVariant input );
void newNam();

View File

@ -375,6 +375,7 @@ TomahawkApp::registerMetaTypes()
qRegisterMetaType< QHash< QString, QVariant > >( "Tomahawk::InfoSystem::InfoCustomData" );
qRegisterMetaType< QHash< QString, QString > >( "Tomahawk::InfoSystem::InfoCriteriaHash" );
qRegisterMetaType< Tomahawk::InfoSystem::InfoType >( "Tomahawk::InfoSystem::InfoType" );
qRegisterMetaType< QWeakPointer< Tomahawk::InfoSystem::InfoSystemCache > >( "QWeakPointer< Tomahawk::InfoSystem::InfoSystemCache >" );
}