Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make the query method thread-safe.

  • Loading branch information...
commit 01ea44b03a0dd6aa8cf5dcae48f7c25d377ff54f 1 parent 21db99d
Wim Leers authored
Showing with 15 additions and 0 deletions.
  1. +15 −0 QCachingLocale.h
15 QCachingLocale.h
View
@@ -3,6 +3,7 @@
#include <QSystemLocale>
#include <QVariant>
+#include <QMutex>
class QCachingLocale : public QSystemLocale {
public:
@@ -26,7 +27,9 @@ class QCachingLocale : public QSystemLocale {
case QSystemLocale::PMText:
// in = empty
if (!this->cacheInNone.contains(type)) {
+ QCachingLocale::inNoneMutex.lock();
this->cacheInNone[type] = QSystemLocale::query(type, in);
+ QCachingLocale::inNoneMutex.unlock();
}
return this->cacheInNone[type];
case QSystemLocale::TimeFormatShort:
@@ -37,7 +40,9 @@ class QCachingLocale : public QSystemLocale {
// in = int
inAsInt = in.toInt();
if (!this->cacheInInt.contains(type) || !this->cacheInInt[type].contains(inAsInt)) {
+ QCachingLocale::inIntMutex.lock();
this->cacheInInt[type][inAsInt] = QSystemLocale::query(type, in);
+ QCachingLocale::inIntMutex.unlock();
}
return this->cacheInInt[type][inAsInt];
case QSystemLocale::DateToStringLong:
@@ -52,7 +57,9 @@ class QCachingLocale : public QSystemLocale {
// QDate, QTime, QDateTime can be converted to unix timestamps.
inAsInt = in.toDateTime().toTime_t();
if (!this->cacheInInt.contains(type) || !this->cacheInInt[type].contains(inAsInt)) {
+ QCachingLocale::inIntMutex.lock();
this->cacheInInt[type][inAsInt] = QSystemLocale::query(type, in);
+ QCachingLocale::inIntMutex.unlock();
}
return this->cacheInInt[type][inAsInt];
default:
@@ -63,6 +70,11 @@ class QCachingLocale : public QSystemLocale {
}
}
private:
+ // Mutexes are necessary to make the query method thread-safe (they are
+ // used to serialize access for insertions to the two QHashes).
+ static QMutex inNoneMutex;
+ static QMutex inIntMutex;
+
// Cache for QSystemLocale queries that don't use the 'in' parameter.
mutable QHash<QueryType, QVariant> cacheInNone;
// Cache for QSystemLocale queries that use an 'in' parameter that is either
@@ -70,4 +82,7 @@ class QCachingLocale : public QSystemLocale {
mutable QHash<QueryType, QHash<int, QVariant> > cacheInInt;
};
+QMutex QCachingLocale::inNoneMutex;
+QMutex QCachingLocale::inIntMutex;
+
#endif // QCACHINGLOCALE_H
Please sign in to comment.
Something went wrong with that request. Please try again.