Made tool registry thread-safe, using a QReadWriteLock.

This commit is contained in:
LoRd_MuldeR 2012-05-03 23:11:16 +02:00
parent 6cfcf067cc
commit 1da4db9594
3 changed files with 18 additions and 5 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 993 #define VER_LAMEXP_BUILD 994
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Tool versions (minimum expected versions!) // Tool versions (minimum expected versions!)

View File

@ -45,6 +45,9 @@
#include <QTimer> #include <QTimer>
#include <QLibraryInfo> #include <QLibraryInfo>
#include <QEvent> #include <QEvent>
#include <QReadWriteLock>
#include <QReadLocker>
#include <QWriteLocker>
//LameXP includes //LameXP includes
#include "Resource.h" #include "Resource.h"
@ -198,6 +201,7 @@ static QString g_lamexp_temp_folder;
//Tools //Tools
static QMap<QString, LockedFile*> g_lamexp_tool_registry; static QMap<QString, LockedFile*> g_lamexp_tool_registry;
static QMap<QString, unsigned int> g_lamexp_tool_versions; static QMap<QString, unsigned int> g_lamexp_tool_versions;
static QReadWriteLock g_lamexp_tool_lock;
//Languages //Languages
static struct static struct
@ -1485,6 +1489,8 @@ bool lamexp_clean_folder(const QString &folderPath)
*/ */
void lamexp_register_tool(const QString &toolName, LockedFile *file, unsigned int version) void lamexp_register_tool(const QString &toolName, LockedFile *file, unsigned int version)
{ {
QWriteLocker writeLock(&g_lamexp_tool_lock);
if(g_lamexp_tool_registry.contains(toolName.toLower())) if(g_lamexp_tool_registry.contains(toolName.toLower()))
{ {
throw "lamexp_register_tool: Tool is already registered!"; throw "lamexp_register_tool: Tool is already registered!";
@ -1499,6 +1505,7 @@ void lamexp_register_tool(const QString &toolName, LockedFile *file, unsigned in
*/ */
bool lamexp_check_tool(const QString &toolName) bool lamexp_check_tool(const QString &toolName)
{ {
QReadLocker readLock(&g_lamexp_tool_lock);
return g_lamexp_tool_registry.contains(toolName.toLower()); return g_lamexp_tool_registry.contains(toolName.toLower());
} }
@ -1507,6 +1514,8 @@ bool lamexp_check_tool(const QString &toolName)
*/ */
const QString lamexp_lookup_tool(const QString &toolName) const QString lamexp_lookup_tool(const QString &toolName)
{ {
QReadLocker readLock(&g_lamexp_tool_lock);
if(g_lamexp_tool_registry.contains(toolName.toLower())) if(g_lamexp_tool_registry.contains(toolName.toLower()))
{ {
return g_lamexp_tool_registry.value(toolName.toLower())->filePath(); return g_lamexp_tool_registry.value(toolName.toLower())->filePath();
@ -1522,6 +1531,8 @@ const QString lamexp_lookup_tool(const QString &toolName)
*/ */
unsigned int lamexp_tool_version(const QString &toolName) unsigned int lamexp_tool_version(const QString &toolName)
{ {
QReadLocker readLock(&g_lamexp_tool_lock);
if(g_lamexp_tool_versions.contains(toolName.toLower())) if(g_lamexp_tool_versions.contains(toolName.toLower()))
{ {
return g_lamexp_tool_versions.value(toolName.toLower()); return g_lamexp_tool_versions.value(toolName.toLower());

View File

@ -36,7 +36,6 @@
#include <QRunnable> #include <QRunnable>
#include <QThreadPool> #include <QThreadPool>
#include <QMutex> #include <QMutex>
#include <QMutexLocker>
/* 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)
@ -98,8 +97,11 @@ protected:
catch(char *errorMsg) catch(char *errorMsg)
{ {
qWarning("At least one of the required tools could not be initialized:\n%s", 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_bAbort) { s_bAbort = true; strncpy_s(s_errMsg, 1024, errorMsg, _TRUNCATE); } {
if(!s_bAbort) { s_bAbort = true; strncpy_s(s_errMsg, 1024, errorMsg, _TRUNCATE); }
s_mutex.unlock();
}
} }
} }