forked from qt-creator/qt-creator
Utils::Id: Make access to internal cache thread safe
Secure all accesses to internal cache with QReadWriteLock. Move firstUnusedId into secured scope, too. Fixes: QTCREATORBUG-28415 Change-Id: I99d23213ec169b2b74748f54c98b834f88ab6a3d Reviewed-by: Eike Ziller <eike.ziller@qt.io> Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
#include <QDataStream>
|
#include <QDataStream>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
|
#include <QReadWriteLock>
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
|
|
||||||
namespace Utils {
|
namespace Utils {
|
||||||
@@ -76,22 +77,32 @@ struct IdCache : public QHash<StringHolder, quintptr>
|
|||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static QHash<quintptr, StringHolder> stringFromId;
|
static QHash<quintptr, StringHolder> stringFromId;
|
||||||
static IdCache idFromString;
|
static IdCache idFromString;
|
||||||
|
static QReadWriteLock s_cacheMutex;
|
||||||
|
|
||||||
static quintptr theId(const char *str, int n = 0)
|
static quintptr theId(const char *str, int n = 0)
|
||||||
{
|
{
|
||||||
static quintptr firstUnusedId = 10 * 1000 * 1000;
|
|
||||||
QTC_ASSERT(str && *str, return 0);
|
QTC_ASSERT(str && *str, return 0);
|
||||||
StringHolder sh(str, n);
|
StringHolder sh(str, n);
|
||||||
int res = idFromString.value(sh, 0);
|
int res = 0;
|
||||||
|
{
|
||||||
|
QReadLocker lock(&s_cacheMutex); // Try quick read locker first
|
||||||
|
res = idFromString.value(sh, 0);
|
||||||
|
}
|
||||||
if (res == 0) {
|
if (res == 0) {
|
||||||
|
QWriteLocker lock(&s_cacheMutex);
|
||||||
|
res = idFromString.value(sh, 0); // Some other thread could have added it to the cache
|
||||||
|
// in meantime, after read lock was released and before
|
||||||
|
// write lock was acquired. Re-read it again.
|
||||||
|
if (res == 0) {
|
||||||
|
static quintptr firstUnusedId = 10 * 1000 * 1000;
|
||||||
res = firstUnusedId++;
|
res = firstUnusedId++;
|
||||||
sh.str = qstrdup(sh.str);
|
sh.str = qstrdup(sh.str);
|
||||||
idFromString[sh] = res;
|
idFromString[sh] = res;
|
||||||
stringFromId[res] = sh;
|
stringFromId[res] = sh;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,6 +138,7 @@ Id::Id(const char *name)
|
|||||||
|
|
||||||
QByteArray Id::name() const
|
QByteArray Id::name() const
|
||||||
{
|
{
|
||||||
|
QReadLocker lock(&s_cacheMutex);
|
||||||
return stringFromId.value(m_id).str;
|
return stringFromId.value(m_id).str;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,6 +154,7 @@ QByteArray Id::name() const
|
|||||||
|
|
||||||
QString Id::toString() const
|
QString Id::toString() const
|
||||||
{
|
{
|
||||||
|
QReadLocker lock(&s_cacheMutex);
|
||||||
return QString::fromUtf8(stringFromId.value(m_id).str);
|
return QString::fromUtf8(stringFromId.value(m_id).str);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,6 +201,7 @@ Id Id::fromName(const QByteArray &name)
|
|||||||
|
|
||||||
QVariant Id::toSetting() const
|
QVariant Id::toSetting() const
|
||||||
{
|
{
|
||||||
|
QReadLocker lock(&s_cacheMutex);
|
||||||
return QVariant(QString::fromUtf8(stringFromId.value(m_id).str));
|
return QVariant(QString::fromUtf8(stringFromId.value(m_id).str));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -280,6 +294,7 @@ Id Id::withPrefix(const char *prefix) const
|
|||||||
|
|
||||||
bool Id::operator==(const char *name) const
|
bool Id::operator==(const char *name) const
|
||||||
{
|
{
|
||||||
|
QReadLocker lock(&s_cacheMutex);
|
||||||
const char *string = stringFromId.value(m_id).str;
|
const char *string = stringFromId.value(m_id).str;
|
||||||
if (string && name)
|
if (string && name)
|
||||||
return strcmp(string, name) == 0;
|
return strcmp(string, name) == 0;
|
||||||
@@ -290,6 +305,7 @@ bool Id::operator==(const char *name) const
|
|||||||
// For debugging purposes
|
// For debugging purposes
|
||||||
QTCREATOR_UTILS_EXPORT const char *nameForId(quintptr id)
|
QTCREATOR_UTILS_EXPORT const char *nameForId(quintptr id)
|
||||||
{
|
{
|
||||||
|
QReadLocker lock(&s_cacheMutex);
|
||||||
return stringFromId.value(id).str;
|
return stringFromId.value(id).str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user