From 9175daae0d1cacbc18c96c0cdfbe27f08a51f700 Mon Sep 17 00:00:00 2001 From: lordmulder Date: Wed, 9 May 2012 02:54:41 +0200 Subject: [PATCH] Some code refactoring regarding the QWaitCondition/QMutex in FileAnalyzer_Task. --- src/Config.h | 2 +- src/Thread_FileAnalyzer.cpp | 8 ++++-- src/Thread_FileAnalyzer_Task.cpp | 47 ++++++++++++++++++++++++-------- src/Thread_FileAnalyzer_Task.h | 17 +++--------- 4 files changed, 46 insertions(+), 28 deletions(-) diff --git a/src/Config.h b/src/Config.h index 0a4174a9..a819a502 100644 --- a/src/Config.h +++ b/src/Config.h @@ -30,7 +30,7 @@ #define VER_LAMEXP_MINOR_LO 5 #define VER_LAMEXP_TYPE Alpha #define VER_LAMEXP_PATCH 1 -#define VER_LAMEXP_BUILD 1018 +#define VER_LAMEXP_BUILD 1019 /////////////////////////////////////////////////////////////////////////////// // Tool versions (minimum expected versions!) diff --git a/src/Thread_FileAnalyzer.cpp b/src/Thread_FileAnalyzer.cpp index d407cc98..8ce0d5ac 100644 --- a/src/Thread_FileAnalyzer.cpp +++ b/src/Thread_FileAnalyzer.cpp @@ -133,7 +133,11 @@ void FileAnalyzer::run() QThreadPool *pool = new QThreadPool(); QThread::msleep(333); - if(pool->maxThreadCount() < 2) + if(QThread::idealThreadCount() > 1) + { + pool->setMaxThreadCount((QThread::idealThreadCount() * 3) / 2); + } + else { pool->setMaxThreadCount(2); } @@ -177,7 +181,7 @@ void FileAnalyzer::run() { emit progressMaxChanged(nFiles += count); } - QThread::msleep(5); + QThread::msleep(8); } } diff --git a/src/Thread_FileAnalyzer_Task.cpp b/src/Thread_FileAnalyzer_Task.cpp index 39e14236..4ba84e88 100644 --- a/src/Thread_FileAnalyzer_Task.cpp +++ b/src/Thread_FileAnalyzer_Task.cpp @@ -79,8 +79,10 @@ AnalyzeTask::AnalyzeTask(const QString &inputFile, const QString &templateFile, AnalyzeTask::~AnalyzeTask(void) { - QWriteLocker lock(&s_lock); + s_waitMutex.lock(); s_threadIdx_finished = qMax(s_threadIdx_finished, m_threadIdx + 1ui64); + s_waitMutex.unlock(); + s_waitCond.wakeAll(); } @@ -99,8 +101,10 @@ void AnalyzeTask::run() qWarning("WARNING: Caught an in exception AnalyzeTask thread!"); } - QWriteLocker lock(&s_lock); + s_waitMutex.lock(); s_threadIdx_finished = qMax(s_threadIdx_finished, m_threadIdx + 1ui64); + s_waitMutex.unlock(); + s_waitCond.wakeAll(); } @@ -710,28 +714,34 @@ unsigned int AnalyzeTask::parseDuration(const QString &str) unsigned __int64 AnalyzeTask::makeThreadIdx(void) { - QWriteLocker lock(&s_lock); - return s_threadIdx_created++; + s_waitMutex.lock(); + unsigned __int64 idx = s_threadIdx_created++; + s_waitMutex.unlock(); + + return idx; } void AnalyzeTask::waitForPreviousThreads(void) { //This function will block until all threads with a *lower* index have terminated. //Required to make sure that the files will be added in the "correct" order! - + for(int i = 0; i < 64; i++) { - QReadLocker lock(&s_lock); + s_waitMutex.lock(); + if((s_threadIdx_finished >= m_threadIdx) || *m_abortFlag) { + s_waitMutex.unlock(); break; } - lock.unlock(); - if(!AnalyzeTask::waitForOneThread(1250)) + if(!s_waitCond.wait(&s_waitMutex, 1250)) { qWarning("FileAnalyzerTask: Timeout, retrying!"); } + + s_waitMutex.unlock(); } } @@ -787,12 +797,20 @@ int AnalyzeTask::getAdditionalFiles(QStringList &fileList) return 0; } +bool AnalyzeTask::waitForOneThread(unsigned long timeout) +{ + bool ret = false; + + s_waitMutex.lock(); + ret = s_waitCond.wait(&s_waitMutex, timeout); + s_waitMutex.unlock(); + + return ret; +} + void AnalyzeTask::reset(void) { QWriteLocker lock(&s_lock); - - s_threadIdx_created = 0; - s_threadIdx_finished = 0; s_filesAccepted = 0; s_filesRejected = 0; s_filesDenied = 0; @@ -800,8 +818,13 @@ void AnalyzeTask::reset(void) s_filesCueSheet = 0; s_additionalFiles.clear(); s_recentlyAdded.clear(); -} + lock.unlock(); + s_waitMutex.lock(); + s_threadIdx_created = 0; + s_threadIdx_finished = 0; + s_waitMutex.unlock(); +} //////////////////////////////////////////////////////////// // EVENTS //////////////////////////////////////////////////////////// diff --git a/src/Thread_FileAnalyzer_Task.h b/src/Thread_FileAnalyzer_Task.h index 008e38b4..7e791612 100644 --- a/src/Thread_FileAnalyzer_Task.h +++ b/src/Thread_FileAnalyzer_Task.h @@ -54,14 +54,7 @@ public: static unsigned int filesCueSheet(void); //Wait till the next running thread terminates - static __forceinline bool waitForOneThread(unsigned long timeout) - { - bool ret = false; - s_waitMutex.lock(); - ret = s_waitCond.wait(&s_waitMutex, timeout); - s_waitMutex.unlock(); - return ret; - } + static bool waitForOneThread(unsigned long timeout); signals: void fileSelected(const QString &fileName); @@ -107,21 +100,19 @@ private: volatile bool *m_abortFlag; - static QReadWriteLock s_lock; static QMutex s_waitMutex; static QWaitCondition s_waitCond; - static unsigned __int64 s_threadIdx_created; static unsigned __int64 s_threadIdx_finished; - - static QStringList s_recentlyAdded; - static QStringList s_additionalFiles; + static QReadWriteLock s_lock; static unsigned int s_filesAccepted; static unsigned int s_filesRejected; static unsigned int s_filesDenied; static unsigned int s_filesDummyCDDA; static unsigned int s_filesCueSheet; + static QStringList s_recentlyAdded; + static QStringList s_additionalFiles; static unsigned __int64 makeThreadIdx(void); };