Some code refactoring regarding the QWaitCondition/QMutex in FileAnalyzer_Task.

This commit is contained in:
LoRd_MuldeR 2012-05-09 02:54:41 +02:00
parent 814b725fe3
commit 9175daae0d
4 changed files with 46 additions and 28 deletions

View File

@ -30,7 +30,7 @@
#define VER_LAMEXP_MINOR_LO 5 #define VER_LAMEXP_MINOR_LO 5
#define VER_LAMEXP_TYPE Alpha #define VER_LAMEXP_TYPE Alpha
#define VER_LAMEXP_PATCH 1 #define VER_LAMEXP_PATCH 1
#define VER_LAMEXP_BUILD 1018 #define VER_LAMEXP_BUILD 1019
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Tool versions (minimum expected versions!) // Tool versions (minimum expected versions!)

View File

@ -133,7 +133,11 @@ void FileAnalyzer::run()
QThreadPool *pool = new QThreadPool(); QThreadPool *pool = new QThreadPool();
QThread::msleep(333); QThread::msleep(333);
if(pool->maxThreadCount() < 2) if(QThread::idealThreadCount() > 1)
{
pool->setMaxThreadCount((QThread::idealThreadCount() * 3) / 2);
}
else
{ {
pool->setMaxThreadCount(2); pool->setMaxThreadCount(2);
} }
@ -177,7 +181,7 @@ void FileAnalyzer::run()
{ {
emit progressMaxChanged(nFiles += count); emit progressMaxChanged(nFiles += count);
} }
QThread::msleep(5); QThread::msleep(8);
} }
} }

View File

@ -79,8 +79,10 @@ AnalyzeTask::AnalyzeTask(const QString &inputFile, const QString &templateFile,
AnalyzeTask::~AnalyzeTask(void) AnalyzeTask::~AnalyzeTask(void)
{ {
QWriteLocker lock(&s_lock); s_waitMutex.lock();
s_threadIdx_finished = qMax(s_threadIdx_finished, m_threadIdx + 1ui64); s_threadIdx_finished = qMax(s_threadIdx_finished, m_threadIdx + 1ui64);
s_waitMutex.unlock();
s_waitCond.wakeAll(); s_waitCond.wakeAll();
} }
@ -99,8 +101,10 @@ void AnalyzeTask::run()
qWarning("WARNING: Caught an in exception AnalyzeTask thread!"); 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_threadIdx_finished = qMax(s_threadIdx_finished, m_threadIdx + 1ui64);
s_waitMutex.unlock();
s_waitCond.wakeAll(); s_waitCond.wakeAll();
} }
@ -710,8 +714,11 @@ unsigned int AnalyzeTask::parseDuration(const QString &str)
unsigned __int64 AnalyzeTask::makeThreadIdx(void) unsigned __int64 AnalyzeTask::makeThreadIdx(void)
{ {
QWriteLocker lock(&s_lock); s_waitMutex.lock();
return s_threadIdx_created++; unsigned __int64 idx = s_threadIdx_created++;
s_waitMutex.unlock();
return idx;
} }
void AnalyzeTask::waitForPreviousThreads(void) void AnalyzeTask::waitForPreviousThreads(void)
@ -721,17 +728,20 @@ void AnalyzeTask::waitForPreviousThreads(void)
for(int i = 0; i < 64; i++) for(int i = 0; i < 64; i++)
{ {
QReadLocker lock(&s_lock); s_waitMutex.lock();
if((s_threadIdx_finished >= m_threadIdx) || *m_abortFlag) if((s_threadIdx_finished >= m_threadIdx) || *m_abortFlag)
{ {
s_waitMutex.unlock();
break; break;
} }
lock.unlock();
if(!AnalyzeTask::waitForOneThread(1250)) if(!s_waitCond.wait(&s_waitMutex, 1250))
{ {
qWarning("FileAnalyzerTask: Timeout, retrying!"); qWarning("FileAnalyzerTask: Timeout, retrying!");
} }
s_waitMutex.unlock();
} }
} }
@ -787,12 +797,20 @@ int AnalyzeTask::getAdditionalFiles(QStringList &fileList)
return 0; 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) void AnalyzeTask::reset(void)
{ {
QWriteLocker lock(&s_lock); QWriteLocker lock(&s_lock);
s_threadIdx_created = 0;
s_threadIdx_finished = 0;
s_filesAccepted = 0; s_filesAccepted = 0;
s_filesRejected = 0; s_filesRejected = 0;
s_filesDenied = 0; s_filesDenied = 0;
@ -800,8 +818,13 @@ void AnalyzeTask::reset(void)
s_filesCueSheet = 0; s_filesCueSheet = 0;
s_additionalFiles.clear(); s_additionalFiles.clear();
s_recentlyAdded.clear(); s_recentlyAdded.clear();
} lock.unlock();
s_waitMutex.lock();
s_threadIdx_created = 0;
s_threadIdx_finished = 0;
s_waitMutex.unlock();
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// EVENTS // EVENTS
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -54,14 +54,7 @@ public:
static unsigned int filesCueSheet(void); static unsigned int filesCueSheet(void);
//Wait till the next running thread terminates //Wait till the next running thread terminates
static __forceinline bool waitForOneThread(unsigned long timeout) static bool waitForOneThread(unsigned long timeout);
{
bool ret = false;
s_waitMutex.lock();
ret = s_waitCond.wait(&s_waitMutex, timeout);
s_waitMutex.unlock();
return ret;
}
signals: signals:
void fileSelected(const QString &fileName); void fileSelected(const QString &fileName);
@ -107,21 +100,19 @@ private:
volatile bool *m_abortFlag; volatile bool *m_abortFlag;
static QReadWriteLock s_lock;
static QMutex s_waitMutex; static QMutex s_waitMutex;
static QWaitCondition s_waitCond; static QWaitCondition s_waitCond;
static unsigned __int64 s_threadIdx_created; static unsigned __int64 s_threadIdx_created;
static unsigned __int64 s_threadIdx_finished; static unsigned __int64 s_threadIdx_finished;
static QStringList s_recentlyAdded; static QReadWriteLock s_lock;
static QStringList s_additionalFiles;
static unsigned int s_filesAccepted; static unsigned int s_filesAccepted;
static unsigned int s_filesRejected; static unsigned int s_filesRejected;
static unsigned int s_filesDenied; static unsigned int s_filesDenied;
static unsigned int s_filesDummyCDDA; static unsigned int s_filesDummyCDDA;
static unsigned int s_filesCueSheet; static unsigned int s_filesCueSheet;
static QStringList s_recentlyAdded;
static QStringList s_additionalFiles;
static unsigned __int64 makeThreadIdx(void); static unsigned __int64 makeThreadIdx(void);
}; };