Now using a QWaitCondition to synchronize the FileAnalyzer threads.

This commit is contained in:
LoRd_MuldeR 2012-05-05 03:55:27 +02:00
parent 2b514558f9
commit 160b997c76
5 changed files with 38 additions and 16 deletions

View File

@ -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!)

View File

@ -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();

View File

@ -34,6 +34,7 @@
#include <QDebug>
#include <QImage>
#include <QThreadPool>
#include <QTime>
////////////////////////////////////////////////////////////
// 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!");

View File

@ -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<DWORD>(qBound(1ui64, m_threadIdx - s_threadIdx_finished, 8ui64)) * 25);
lock.unlock();
Sleep(sleepInterval);
waitForOneThread(1250);
}
}

View File

@ -23,7 +23,9 @@
#include <QRunnable>
#include <QReadWriteLock>
#include <QWaitCondition>
#include <QStringList>
#include <QMutex>
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);
};