Some more improvements of ExtractorTask class.

This commit is contained in:
LoRd_MuldeR 2013-10-16 18:24:16 +02:00
parent 33e04007fb
commit 2f543e11ab

@ -31,7 +31,6 @@
#include <QDir> #include <QDir>
#include <QLibrary> #include <QLibrary>
#include <QResource> #include <QResource>
#include <QTime>
#include <QTextStream> #include <QTextStream>
#include <QRunnable> #include <QRunnable>
#include <QThreadPool> #include <QThreadPool>
@ -39,7 +38,10 @@
/* helper macros */ /* helper macros */
#define PRINT_CPU_TYPE(X) case X: qDebug("Selected CPU is: " #X) #define PRINT_CPU_TYPE(X) case X: qDebug("Selected CPU is: " #X)
/* constants */
static const double g_allowedExtractDelay = 12.0; static const double g_allowedExtractDelay = 12.0;
static const size_t BUFF_SIZE = 512;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// ExtractorTask class // ExtractorTask class
@ -50,27 +52,36 @@ class ExtractorTask : public QRunnable
public: public:
ExtractorTask(const QDir &appDir, const QString &toolName, const QString &toolShortName, const QByteArray &toolHash, const unsigned int toolVersion, const QString &toolTag) ExtractorTask(const QDir &appDir, const QString &toolName, const QString &toolShortName, const QByteArray &toolHash, const unsigned int toolVersion, const QString &toolTag)
: :
QRunnable(), m_appDir(appDir), m_toolName(toolName), m_toolShortName(toolShortName), m_toolHash(toolHash), m_toolVersion(toolVersion), m_toolTag(toolTag) m_appDir(appDir),
m_toolName(toolName),
m_toolShortName(toolShortName),
m_toolHash(toolHash),
m_toolVersion(toolVersion),
m_toolTag(toolTag)
{ {
/* Nothing to do */ /* Nothing to do */
} }
static void clearFlags(void) static void clearFlags(void)
{ {
s_mutex.lock(); QMutexLocker lock(&s_mutex);
s_bAbort = s_bCustom = false; s_bExcept = false;
s_errMsg[0] = '\0'; s_bCustom = false;
s_mutex.unlock(); s_errMsg[0] = char(0);
} }
static bool getAbort(void) { bool ret; s_mutex.lock(); ret = s_bAbort; s_mutex.unlock(); return ret; } static bool getExcept(void) { bool ret; QMutexLocker lock(&s_mutex); ret = s_bExcept; return ret; }
static bool getCustom(void) { bool ret; s_mutex.lock(); ret = s_bCustom; s_mutex.unlock(); return ret; } static bool getCustom(void) { bool ret; QMutexLocker lock(&s_mutex); ret = s_bCustom; return ret; }
static void getError(char *buffer, const size_t buffSize) static bool getErrMsg(char *buffer, const size_t buffSize)
{ {
s_mutex.lock(); QMutexLocker lock(&s_mutex);
strncpy_s(buffer, 1024, s_errMsg, _TRUNCATE); if(s_errMsg[0])
s_mutex.unlock(); {
strncpy_s(buffer, BUFF_SIZE, s_errMsg, _TRUNCATE);
return true;
}
return false;
} }
protected: protected:
@ -78,63 +89,62 @@ protected:
{ {
try try
{ {
LockedFile *lockedFile = NULL; if(!getExcept()) doExtract();
unsigned int version = m_toolVersion;
if(!s_bAbort)
{
QFileInfo customTool(QString("%1/tools/%2/%3").arg(m_appDir.canonicalPath(), QString::number(lamexp_version_build()), m_toolShortName));
if(customTool.exists() && customTool.isFile())
{
qDebug("Setting up file: %s <- %s", m_toolShortName.toLatin1().constData(), m_appDir.relativeFilePath(customTool.canonicalFilePath()).toLatin1().constData());
lockedFile = new LockedFile(customTool.canonicalFilePath()); version = UINT_MAX; s_bCustom = true;
}
else
{
qDebug("Extracting file: %s -> %s", m_toolName.toLatin1().constData(), m_toolShortName.toLatin1().constData());
lockedFile = new LockedFile(QString(":/tools/%1").arg(m_toolName), QString("%1/lxp_%2").arg(lamexp_temp_folder2(), m_toolShortName), m_toolHash);
}
if(lockedFile)
{
//QMutexLocker lock(&s_mutex);
lamexp_register_tool(m_toolShortName, lockedFile, version, &m_toolTag);
}
}
} }
catch(char *errorMsg) catch(char *errorMsg)
{ {
qWarning("At least one of the required tools could not be initialized:\n%s", errorMsg); QMutexLocker lock(&s_mutex);
if(s_mutex.tryLock()) if(!s_bExcept)
{ {
if(!s_bAbort) s_bExcept = true;
{ strncpy_s(s_errMsg, BUFF_SIZE, errorMsg, _TRUNCATE);
strncpy_s(s_errMsg, 1024, errorMsg, _TRUNCATE); lock.unlock();
s_bAbort = true; qWarning("ExtractorTask error:\n%s", errorMsg);
}
s_mutex.unlock();
} }
} }
} }
void doExtract(void)
{
LockedFile *lockedFile = NULL;
unsigned int version = m_toolVersion;
QFileInfo customTool(QString("%1/tools/%2/%3").arg(m_appDir.canonicalPath(), QString::number(lamexp_version_build()), m_toolShortName));
if(customTool.exists() && customTool.isFile())
{
qDebug("Setting up file: %s <- %s", m_toolShortName.toLatin1().constData(), m_appDir.relativeFilePath(customTool.canonicalFilePath()).toLatin1().constData());
lockedFile = new LockedFile(customTool.canonicalFilePath()); version = UINT_MAX; s_bCustom = true;
}
else
{
qDebug("Extracting file: %s -> %s", m_toolName.toLatin1().constData(), m_toolShortName.toLatin1().constData());
lockedFile = new LockedFile(QString(":/tools/%1").arg(m_toolName), QString("%1/lxp_%2").arg(lamexp_temp_folder2(), m_toolShortName), m_toolHash);
}
if(lockedFile)
{
lamexp_register_tool(m_toolShortName, lockedFile, version, &m_toolTag);
}
}
private: private:
const QDir m_appDir; const QDir m_appDir;
const QString m_toolName; const QString m_toolName;
const QString m_toolShortName; const QString m_toolShortName;
const QString m_toolTag;
const unsigned int m_toolVersion;
const QByteArray m_toolHash; const QByteArray m_toolHash;
const unsigned int m_toolVersion;
const QString m_toolTag;
static volatile bool s_bAbort; static volatile bool s_bExcept;
static volatile bool s_bCustom; static volatile bool s_bCustom;
static QMutex s_mutex; static QMutex s_mutex;
static char s_errMsg[1024]; static char s_errMsg[BUFF_SIZE];
}; };
volatile bool ExtractorTask::s_bAbort = false;
volatile bool ExtractorTask::s_bCustom = false;
char ExtractorTask::s_errMsg[1024] = {'\0'};
QMutex ExtractorTask::s_mutex; QMutex ExtractorTask::s_mutex;
volatile bool ExtractorTask::s_bExcept = false;
volatile bool ExtractorTask::s_bCustom = false;
char ExtractorTask::s_errMsg[BUFF_SIZE] = {'\0'};
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Constructor // Constructor
@ -199,9 +209,9 @@ void InitializationThread::run()
QMap<QString, QString> mapVersTag; QMap<QString, QString> mapVersTag;
//Init properties //Init properties
for(int i = 0; i < INT_MAX; i++) for(int i = 0; true; i++)
{ {
if(!g_lamexp_tools[i].pcName && !g_lamexp_tools[i].pcHash && !g_lamexp_tools[i].uiVersion) if(!(g_lamexp_tools[i].pcName || g_lamexp_tools[i].pcHash || g_lamexp_tools[i].uiVersion))
{ {
break; break;
} }
@ -233,8 +243,7 @@ void InitializationThread::run()
LockedFile::selfTest(); LockedFile::selfTest();
ExtractorTask::clearFlags(); ExtractorTask::clearFlags();
QTime timer; const long long timeExtractStart = lamexp_perfcounter_value();
timer.start();
//Extract all files //Extract all files
while(!toolsList.isEmpty()) while(!toolsList.isEmpty())
@ -258,7 +267,6 @@ void InitializationThread::run()
if(toolCpuType & cpuSupport) if(toolCpuType & cpuSupport)
{ {
pool->start(new ExtractorTask(appDir, toolName, toolShortName, toolHash, toolVersion, toolVersTag)); pool->start(new ExtractorTask(appDir, toolName, toolShortName, toolHash, toolVersion, toolVersTag));
QThread::yieldCurrentThread();
} }
} }
catch(char *errorMsg) catch(char *errorMsg)
@ -272,12 +280,18 @@ void InitializationThread::run()
pool->waitForDone(); pool->waitForDone();
LAMEXP_DELETE(pool); LAMEXP_DELETE(pool);
const long long timeExtractEnd = lamexp_perfcounter_value();
//Make sure all files were extracted correctly //Make sure all files were extracted correctly
if(ExtractorTask::getAbort()) if(ExtractorTask::getExcept())
{ {
char errorMsg[1024]; char errorMsg[BUFF_SIZE];
ExtractorTask::getError(errorMsg, 1024); if(ExtractorTask::getErrMsg(errorMsg, BUFF_SIZE))
qFatal("At least one of the required tools could not be initialized:\n%s", errorMsg); {
qFatal("At least one of the required tools could not be initialized:\n%s", errorMsg);
return;
}
qFatal("At least one of the required tools could not be initialized!");
return; return;
} }
@ -302,7 +316,7 @@ void InitializationThread::run()
} }
//Check delay //Check delay
double delayExtract = static_cast<double>(timer.elapsed()) / 1000.0; double delayExtract = static_cast<double>(timeExtractEnd - timeExtractStart) / static_cast<double>(lamexp_perfcounter_frequ());
if(delayExtract > g_allowedExtractDelay) if(delayExtract > g_allowedExtractDelay)
{ {
m_slowIndicator = true; m_slowIndicator = true;
@ -311,7 +325,7 @@ void InitializationThread::run()
} }
else else
{ {
qDebug("Extracting the tools took %.3f seconds (OK).\n", delayExtract); qDebug("Extracting the tools took %.5f seconds (OK).\n", delayExtract);
} }
//Register all translations //Register all translations