1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-13 17:43:59 +02:00

More work

This commit is contained in:
Jeff Mitchell
2012-07-10 07:02:17 -04:00
parent c7002a1364
commit f7ffead6c2
4 changed files with 26 additions and 18 deletions

View File

@@ -66,7 +66,7 @@ Database::~Database()
qDebug() << Q_FUNC_INFO;
m_workerRW->quit();
foreach ( DatabaseWorkerThread *thread, m_workers )
foreach ( DatabaseWorkerThread *thread, m_workerThreads )
{
if ( thread->worker() )
thread->quit();
@@ -75,12 +75,12 @@ Database::~Database()
m_workerRW->wait( 60000 );
delete m_workerRW;
m_workerRW = 0;
foreach ( DatabaseWorkerThread *thread, m_workers )
foreach ( DatabaseWorkerThread *thread, m_workerThreads )
{
thread->wait( 60000 );
delete thread;
}
m_workers.clear();
m_workerThreads.clear();
qDeleteAll( m_implHash.values() );
delete m_impl;
@@ -118,21 +118,24 @@ Database::enqueue( const QSharedPointer<DatabaseCommand>& lc )
{
// find existing amount of worker threads for commandname
// create new thread if < WORKER_THREADS
if ( m_workers.count() < m_maxConcurrentThreads )
if ( m_workerThreads.count() < m_maxConcurrentThreads )
{
DatabaseWorkerThread* worker = new DatabaseWorkerThread( this, false );
worker->start();
DatabaseWorkerThread* workerThread = new DatabaseWorkerThread( this, false );
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "P1" << ( workerThread->worker() ? "true" : "false" );
workerThread->start();
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "P2" << ( workerThread->worker() ? "true" : "false" );
m_workers << worker;
m_workerThreads << workerThread;
}
// find thread for commandname with lowest amount of outstanding jobs and enqueue job
int busyThreads = 0;
QWeakPointer< DatabaseWorker > happyWorker;
for ( int i = 0; i < m_workers.count(); i++ )
for ( int i = 0; i < m_workerThreads.count(); i++ )
{
DatabaseWorkerThread* workerThread = m_workers.at( i );
DatabaseWorkerThread* workerThread = m_workerThreads.at( i );
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << i << ( workerThread->worker() ? "true" : "false" );
if ( workerThread->worker() && !workerThread->worker().data()->busy() )
{
happyWorker = workerThread->worker();
@@ -145,6 +148,8 @@ Database::enqueue( const QSharedPointer<DatabaseCommand>& lc )
}
// qDebug() << "Enqueueing command to thread:" << happyThread << busyThreads << lc->commandname();
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << m_workerThreads.count() << m_maxConcurrentThreads;
Q_ASSERT( happyWorker );
happyWorker.data()->enqueue( lc );
}
}

View File

@@ -77,7 +77,7 @@ private:
DatabaseImpl* m_impl;
DatabaseWorkerThread* m_workerRW;
QList< DatabaseWorkerThread* > m_workers;
QList< DatabaseWorkerThread* > m_workerThreads;
int m_maxConcurrentThreads;
QHash< QThread*, DatabaseImpl* > m_implHash;

View File

@@ -40,17 +40,17 @@ DatabaseWorkerThread::DatabaseWorkerThread( Database* db, bool mutates )
, m_db( db )
, m_mutates( mutates )
{
Q_UNUSED( db );
Q_UNUSED( mutates );
}
void
DatabaseWorkerThread::run()
{
tDebug() << Q_FUNC_INFO << "DatabaseWorkerThread starting...";
m_worker = QWeakPointer< DatabaseWorker >( new DatabaseWorker( m_db, m_mutates ) );
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "P5" << ( m_worker ? "true" : "false" );
exec();
tDebug() << Q_FUNC_INFO << "DatabaseWorker finishing...";
tDebug() << Q_FUNC_INFO << "DatabaseWorkerThread finishing...";
if ( m_worker )
delete m_worker.data();
}
@@ -61,13 +61,19 @@ DatabaseWorkerThread::~DatabaseWorkerThread()
}
QWeakPointer< DatabaseWorker >
DatabaseWorkerThread::worker() const
{
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "P4" << ( m_worker ? "true" : "false" );
return m_worker;
}
DatabaseWorker::DatabaseWorker( Database* db, bool mutates )
: QObject()
, m_db( db )
, m_outstanding( 0 )
{
Q_UNUSED( db );
Q_UNUSED( mutates );
tDebug() << Q_FUNC_INFO << "New db connection with name:" << Database::instance()->impl()->database().connectionName() << "on thread" << this->thread();
}
@@ -84,9 +90,6 @@ DatabaseWorker::~DatabaseWorker()
tDebug() << "Outstanding db command to finish:" << cmd->guid() << cmd->commandname();
}
}
thread()->quit();
wait();
}

View File

@@ -71,7 +71,7 @@ public:
DatabaseWorkerThread( Database* db, bool mutates );
~DatabaseWorkerThread();
QWeakPointer< DatabaseWorker > worker() { return m_worker; }
QWeakPointer< DatabaseWorker > worker() const;
protected:
void run();