From 160b997c76584ea54d36ec55c290cd37f59b3138 Mon Sep 17 00:00:00 2001 From: lordmulder Date: Sat, 5 May 2012 03:55:27 +0200 Subject: [PATCH] Now using a QWaitCondition to synchronize the FileAnalyzer threads. --- src/Config.h | 2 +- src/Dialog_WorkingBanner.cpp | 1 + src/Thread_FileAnalyzer.cpp | 18 ++++++++++-------- src/Thread_FileAnalyzer_Task.cpp | 9 ++++++--- src/Thread_FileAnalyzer_Task.h | 24 ++++++++++++++++++++---- 5 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/Config.h b/src/Config.h index c6e89a32..93d089aa 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 1002 +#define VER_LAMEXP_BUILD 1005 /////////////////////////////////////////////////////////////////////////////// // Tool versions (minimum expected versions!) diff --git a/src/Dialog_WorkingBanner.cpp b/src/Dialog_WorkingBanner.cpp index 42fb0930..cd5d7e89 100644 --- a/src/Dialog_WorkingBanner.cpp +++ b/src/Dialog_WorkingBanner.cpp @@ -50,6 +50,7 @@ WorkingBanner::WorkingBanner(QWidget *parent) //Start animation m_working = new QMovie(":/images/Busy.gif"); + m_working->setSpeed(25); labelWorking->setMovie(m_working); m_working->start(); diff --git a/src/Thread_FileAnalyzer.cpp b/src/Thread_FileAnalyzer.cpp index 4abfdf44..545c0d51 100644 --- a/src/Thread_FileAnalyzer.cpp +++ b/src/Thread_FileAnalyzer.cpp @@ -34,6 +34,7 @@ #include #include #include +#include //////////////////////////////////////////////////////////// // Constructor @@ -136,19 +137,19 @@ void FileAnalyzer::run() { const QString currentFile = QDir::fromNativeSeparators(m_inputFiles.takeFirst()); - if(m_inputFiles.isEmpty()) - { - pool->waitForDone(); - } - AnalyzeTask *task = new AnalyzeTask(currentFile, m_templateFile->filePath(), &m_abortFlag); connect(task, SIGNAL(fileSelected(QString)), this, SIGNAL(fileSelected(QString)), Qt::DirectConnection); connect(task, SIGNAL(fileAnalyzed(AudioFileModel)), this, SIGNAL(fileAnalyzed(AudioFileModel)), Qt::DirectConnection); while(!pool->tryStart(task)) { - pool->waitForDone(250); //No more free threads, wait for active threads! - if(m_abortFlag) { LAMEXP_DELETE(task); break; } + AnalyzeTask::waitForOneThread(1250); + + if(m_abortFlag) + { + LAMEXP_DELETE(task); + break; + } } if(m_abortFlag) @@ -160,6 +161,7 @@ void FileAnalyzer::run() QThread::yieldCurrentThread(); } + //One of the Analyze tasks may have gathered additional files from a playlist! if(!m_bAborted) { pool->waitForDone(); @@ -169,7 +171,7 @@ void FileAnalyzer::run() pool->waitForDone(); LAMEXP_DELETE(pool); - + if(m_bAborted) { qWarning("Operation cancelled by user!"); diff --git a/src/Thread_FileAnalyzer_Task.cpp b/src/Thread_FileAnalyzer_Task.cpp index ca841f0b..0b70971b 100644 --- a/src/Thread_FileAnalyzer_Task.cpp +++ b/src/Thread_FileAnalyzer_Task.cpp @@ -46,6 +46,8 @@ /* static vars */ QReadWriteLock AnalyzeTask::s_lock; +QMutex AnalyzeTask::s_waitMutex; +QWaitCondition AnalyzeTask::s_waitCond; unsigned __int64 AnalyzeTask::s_threadIdx_created; unsigned __int64 AnalyzeTask::s_threadIdx_finished; unsigned int AnalyzeTask::s_filesAccepted; @@ -79,6 +81,7 @@ AnalyzeTask::~AnalyzeTask(void) { QWriteLocker lock(&s_lock); s_threadIdx_finished = qMax(s_threadIdx_finished, m_threadIdx + 1); + s_waitCond.wakeAll(); } //////////////////////////////////////////////////////////// @@ -695,16 +698,16 @@ 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 < 240; i++) + for(int i = 0; i < 64; i++) { QReadLocker lock(&s_lock); if((s_threadIdx_finished >= m_threadIdx) || *m_abortFlag) { break; } - DWORD sleepInterval = 100 + (static_cast(qBound(1ui64, m_threadIdx - s_threadIdx_finished, 8ui64)) * 25); lock.unlock(); - Sleep(sleepInterval); + + waitForOneThread(1250); } } diff --git a/src/Thread_FileAnalyzer_Task.h b/src/Thread_FileAnalyzer_Task.h index d328180a..6f541009 100644 --- a/src/Thread_FileAnalyzer_Task.h +++ b/src/Thread_FileAnalyzer_Task.h @@ -23,7 +23,9 @@ #include #include +#include #include +#include class AudioFileModel; class QFile; @@ -51,12 +53,21 @@ public: static unsigned int filesDummyCDDA(void); static unsigned int filesCueSheet(void); + //Wait till the next running thread terminates + static void waitForOneThread(unsigned long timeout) + { + s_waitMutex.lock(); + s_waitCond.wait(&s_waitMutex, timeout); + s_waitMutex.unlock(); + } + + void run(void); + signals: void fileSelected(const QString &fileName); void fileAnalyzed(const AudioFileModel &file); protected: - void run(void); private: enum cover_t @@ -93,15 +104,20 @@ 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 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); }; -- 2.11.0