Permalink
Browse files

The mutex locking was not happening early enough, therefore QCachingL…

…ocale was not 100% thread-safe yet. Now it is. Mutex locking has also been made more elegant, thanks to QMutexLocker.
  • Loading branch information...
1 parent 01ea44b commit e6f51533bc15b12a2270a3ba0be87fce79644fb4 @wimleers committed Dec 19, 2010
Showing with 8 additions and 13 deletions.
  1. +8 −13 QCachingLocale.h
View
@@ -4,11 +4,12 @@
#include <QSystemLocale>
#include <QVariant>
#include <QMutex>
+#include <QMutexLocker>
class QCachingLocale : public QSystemLocale {
public:
virtual QVariant query(QueryType type, QVariant in) const {
- int inAsInt;
+ static int inAsInt;
switch(type) {
case QSystemLocale::LanguageId:
case QSystemLocale::CountryId:
@@ -26,24 +27,20 @@ class QCachingLocale : public QSystemLocale {
case QSystemLocale::AMText:
case QSystemLocale::PMText:
// in = empty
- if (!this->cacheInNone.contains(type)) {
- QCachingLocale::inNoneMutex.lock();
+ QMutexLocker(&this->inNoneMutex);
+ if (!this->cacheInNone.contains(type))
this->cacheInNone[type] = QSystemLocale::query(type, in);
- QCachingLocale::inNoneMutex.unlock();
- }
return this->cacheInNone[type];
case QSystemLocale::TimeFormatShort:
case QSystemLocale::DayNameLong:
case QSystemLocale::DayNameShort:
case QSystemLocale::MonthNameLong:
case QSystemLocale::MonthNameShort:
// in = int
+ QMutexLocker(&this->inIntMutex);
inAsInt = in.toInt();
- if (!this->cacheInInt.contains(type) || !this->cacheInInt[type].contains(inAsInt)) {
- QCachingLocale::inIntMutex.lock();
+ if (!this->cacheInInt.contains(type) || !this->cacheInInt[type].contains(inAsInt))
this->cacheInInt[type][inAsInt] = QSystemLocale::query(type, in);
- QCachingLocale::inIntMutex.unlock();
- }
return this->cacheInInt[type][inAsInt];
case QSystemLocale::DateToStringLong:
case QSystemLocale::DateToStringShort:
@@ -55,12 +52,10 @@ class QCachingLocale : public QSystemLocale {
case QSystemLocale::DateTimeToStringShort:
// in = QDateTime
// QDate, QTime, QDateTime can be converted to unix timestamps.
+ QMutexLocker(&this->inIntMutex);
inAsInt = in.toDateTime().toTime_t();
- if (!this->cacheInInt.contains(type) || !this->cacheInInt[type].contains(inAsInt)) {
- QCachingLocale::inIntMutex.lock();
+ if (!this->cacheInInt.contains(type) || !this->cacheInInt[type].contains(inAsInt))
this->cacheInInt[type][inAsInt] = QSystemLocale::query(type, in);
- QCachingLocale::inIntMutex.unlock();
- }
return this->cacheInInt[type][inAsInt];
default:
// In Qt 4.7, the above cases should cover *all* cases. This

0 comments on commit e6f5153

Please sign in to comment.