From b69bb01f11f5104d8e807c7a2bdc92d3ffa394b4 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Sun, 29 Jan 2012 20:32:22 +0100 Subject: Use QBasicMutex instead of Q_GLOBAL_STATIC QMutex QBasicMutex is a POD and can be used as a static global object. in qpicture.cpp factoryLoader is used only once, and under the mutex, so there is no need for Q_GLOBAL_STATIC for it, it can be a function static in qhostinfo_unix.cpp the code seemed wrong while compiled with namespace and QT_NO_GETADDRINFO. I also could get rid of one include because it was included earlier. Change-Id: I3c700203c3e067266c20733f4bda8031446dbb86 Reviewed-by: Bradley T. Hughes --- src/corelib/io/qprocess_unix.cpp | 13 ++++--------- src/corelib/io/qsettings.cpp | 18 ++++++++++-------- src/corelib/plugin/qlibrary.cpp | 6 +++--- src/corelib/thread/qthreadstorage.cpp | 12 ++++++------ src/corelib/tools/qregexp.cpp | 6 +++--- src/gui/image/qpicture.cpp | 16 ++++++---------- src/gui/kernel/qguiapplication.cpp | 6 +++--- src/gui/kernel/qtouchdevice.cpp | 10 +++++----- src/network/bearer/qnetworkconfigmanager.cpp | 4 ++-- src/network/kernel/qhostinfo_unix.cpp | 9 +++------ src/network/kernel/qhostinfo_win.cpp | 8 ++------ 11 files changed, 47 insertions(+), 61 deletions(-) diff --git a/src/corelib/io/qprocess_unix.cpp b/src/corelib/io/qprocess_unix.cpp index d3e45b19d7..9e35978c77 100644 --- a/src/corelib/io/qprocess_unix.cpp +++ b/src/corelib/io/qprocess_unix.cpp @@ -169,16 +169,14 @@ private: }; -Q_GLOBAL_STATIC(QMutex, processManagerGlobalMutex) - static QProcessManager *processManagerInstance = 0; static QProcessManager *processManager() { // The constructor of QProcessManager should be called only once // so we cannot use Q_GLOBAL_STATIC directly for QProcessManager - QMutex *mutex = processManagerGlobalMutex(); - QMutexLocker locker(mutex); + static QBasicMutex processManagerGlobalMutex; + QMutexLocker locker(&processManagerGlobalMutex); if (!processManagerInstance) QProcessPrivate::initializeProcessManager(); @@ -550,10 +548,6 @@ inline pid_t qt_fork() #endif } -#ifdef Q_OS_MAC -Q_GLOBAL_STATIC(QMutex, cfbundleMutex); -#endif - void QProcessPrivate::startProcess() { Q_Q(QProcess); @@ -604,7 +598,8 @@ void QProcessPrivate::startProcess() { // CFBundle is not reentrant, since CFBundleCreate might return a reference // to a cached bundle object. Protect the bundle calls with a mutex lock. - QMutexLocker lock(cfbundleMutex()); + static QBasicMutex cfbundleMutex; + QMutexLocker lock(&cfbundleMutex); QCFType bundle = CFBundleCreate(0, url); url = CFBundleCopyExecutableURL(bundle); } diff --git a/src/corelib/io/qsettings.cpp b/src/corelib/io/qsettings.cpp index 3d2ac329ca..2021c42c4d 100644 --- a/src/corelib/io/qsettings.cpp +++ b/src/corelib/io/qsettings.cpp @@ -122,7 +122,9 @@ Q_GLOBAL_STATIC(ConfFileHash, usedHashFunc) Q_GLOBAL_STATIC(ConfFileCache, unusedCacheFunc) Q_GLOBAL_STATIC(PathHash, pathHashFunc) Q_GLOBAL_STATIC(CustomFormatVector, customFormatVectorFunc) -Q_GLOBAL_STATIC(QMutex, globalMutex) + +static QBasicMutex settingsGlobalMutex; + static QSettings::Format globalDefaultFormat = QSettings::NativeFormat; #ifndef Q_OS_WIN @@ -277,7 +279,7 @@ QConfFile *QConfFile::fromName(const QString &fileName, bool _userPerms) ConfFileCache *unusedCache = unusedCacheFunc(); QConfFile *confFile = 0; - QMutexLocker locker(globalMutex()); + QMutexLocker locker(&settingsGlobalMutex); if (!(confFile = usedHash->value(absPath))) { if ((confFile = unusedCache->take(absPath))) @@ -292,7 +294,7 @@ QConfFile *QConfFile::fromName(const QString &fileName, bool _userPerms) void QConfFile::clearCache() { - QMutexLocker locker(globalMutex()); + QMutexLocker locker(&settingsGlobalMutex); unusedCacheFunc()->clear(); } @@ -992,7 +994,7 @@ void QConfFileSettingsPrivate::initFormat() #endif if (format > QSettings::IniFormat) { - QMutexLocker locker(globalMutex()); + QMutexLocker locker(&settingsGlobalMutex); const CustomFormatVector *customFormatVector = customFormatVectorFunc(); int i = (int)format - (int)QSettings::CustomFormat1; @@ -1127,7 +1129,7 @@ static QString getPath(QSettings::Format format, QSettings::Scope scope) Q_ASSERT((int)QSettings::NativeFormat == 0); Q_ASSERT((int)QSettings::IniFormat == 1); - QMutexLocker locker(globalMutex()); + QMutexLocker locker(&settingsGlobalMutex); PathHash *pathHash = pathHashFunc(); if (pathHash->isEmpty()) initDefaultPaths(&locker); @@ -1195,7 +1197,7 @@ QConfFileSettingsPrivate::QConfFileSettingsPrivate(const QString &fileName, QConfFileSettingsPrivate::~QConfFileSettingsPrivate() { - QMutexLocker locker(globalMutex()); + QMutexLocker locker(&settingsGlobalMutex); ConfFileHash *usedHash = usedHashFunc(); ConfFileCache *unusedCache = unusedCacheFunc(); @@ -3437,7 +3439,7 @@ void QSettings::setUserIniPath(const QString &dir) */ void QSettings::setPath(Format format, Scope scope, const QString &path) { - QMutexLocker locker(globalMutex()); + QMutexLocker locker(&settingsGlobalMutex); PathHash *pathHash = pathHashFunc(); if (pathHash->isEmpty()) initDefaultPaths(&locker); @@ -3520,7 +3522,7 @@ QSettings::Format QSettings::registerFormat(const QString &extension, ReadFunc r Q_ASSERT(caseSensitivity == Qt::CaseSensitive); #endif - QMutexLocker locker(globalMutex()); + QMutexLocker locker(&settingsGlobalMutex); CustomFormatVector *customFormatVector = customFormatVectorFunc(); int index = customFormatVector->size(); if (index == 16) // the QSettings::Format enum has room for 16 custom formats diff --git a/src/corelib/plugin/qlibrary.cpp b/src/corelib/plugin/qlibrary.cpp index a754f1b56a..dc08b32db1 100644 --- a/src/corelib/plugin/qlibrary.cpp +++ b/src/corelib/plugin/qlibrary.cpp @@ -76,7 +76,7 @@ QT_BEGIN_NAMESPACE # define QT_NO_DEBUG_PLUGIN_CHECK #endif -Q_GLOBAL_STATIC(QMutex, qt_library_mutex) +static QBasicMutex qt_library_mutex; /*! \class QLibrary @@ -452,7 +452,7 @@ QLibraryPrivate::QLibraryPrivate(const QString &canonicalFileName, const QString QLibraryPrivate *QLibraryPrivate::findOrCreate(const QString &fileName, const QString &version) { - QMutexLocker locker(qt_library_mutex()); + QMutexLocker locker(&qt_library_mutex); if (QLibraryPrivate *lib = libraryMap()->value(fileName)) { lib->libraryRefCount.ref(); return lib; @@ -526,7 +526,7 @@ bool QLibraryPrivate::unload() void QLibraryPrivate::release() { - QMutexLocker locker(qt_library_mutex()); + QMutexLocker locker(&qt_library_mutex); if (!libraryRefCount.deref()) delete this; } diff --git a/src/corelib/thread/qthreadstorage.cpp b/src/corelib/thread/qthreadstorage.cpp index 43a8056c33..1dfa3305bc 100644 --- a/src/corelib/thread/qthreadstorage.cpp +++ b/src/corelib/thread/qthreadstorage.cpp @@ -71,13 +71,13 @@ void qtsDebug(const char *fmt, ...) # define DEBUG_MSG if(false)qDebug #endif -Q_GLOBAL_STATIC(QMutex, mutex) +static QBasicMutex destructorsMutex; typedef QVector DestructorMap; Q_GLOBAL_STATIC(DestructorMap, destructors) QThreadStorageData::QThreadStorageData(void (*func)(void *)) { - QMutexLocker locker(mutex()); + QMutexLocker locker(&destructorsMutex); DestructorMap *destr = destructors(); if (!destr) { /* @@ -109,7 +109,7 @@ QThreadStorageData::QThreadStorageData(void (*func)(void *)) QThreadStorageData::~QThreadStorageData() { DEBUG_MSG("QThreadStorageData: Released id %d", id); - QMutexLocker locker(mutex()); + QMutexLocker locker(&destructorsMutex); if (destructors()) (*destructors())[id] = 0; } @@ -153,7 +153,7 @@ void **QThreadStorageData::set(void *p) value, data->thread); - QMutexLocker locker(mutex()); + QMutexLocker locker(&destructorsMutex); DestructorMap *destr = destructors(); void (*destructor)(void *) = destr ? destr->value(id) : 0; locker.unlock(); @@ -174,7 +174,7 @@ void **QThreadStorageData::set(void *p) void QThreadStorageData::finish(void **p) { QVector *tls = reinterpret_cast *>(p); - if (!tls || tls->isEmpty() || !mutex()) + if (!tls || tls->isEmpty() || !destructors()) return; // nothing to do DEBUG_MSG("QThreadStorageData: Destroying storage for thread %p", QThread::currentThread()); @@ -190,7 +190,7 @@ void QThreadStorageData::finish(void **p) continue; } - QMutexLocker locker(mutex()); + QMutexLocker locker(&destructorsMutex); void (*destructor)(void *) = destructors()->value(i); locker.unlock(); diff --git a/src/corelib/tools/qregexp.cpp b/src/corelib/tools/qregexp.cpp index 70cae5ceee..421d15c436 100644 --- a/src/corelib/tools/qregexp.cpp +++ b/src/corelib/tools/qregexp.cpp @@ -3809,7 +3809,7 @@ uint qHash(const QRegExpEngineKey &key) typedef QCache EngineCache; Q_GLOBAL_STATIC(EngineCache, globalEngineCache) -Q_GLOBAL_STATIC(QMutex, mutex) +static QBasicMutex globalEngineCacheMutex; #endif // QT_NO_REGEXP_OPTIM static void derefEngine(QRegExpEngine *eng, const QRegExpEngineKey &key) @@ -3817,7 +3817,7 @@ static void derefEngine(QRegExpEngine *eng, const QRegExpEngineKey &key) if (!eng->ref.deref()) { #if !defined(QT_NO_REGEXP_OPTIM) if (globalEngineCache()) { - QMutexLocker locker(mutex()); + QMutexLocker locker(&globalEngineCacheMutex); QT_TRY { globalEngineCache()->insert(key, eng, 4 + key.pattern.length() / 4); } QT_CATCH(const std::bad_alloc &) { @@ -3839,7 +3839,7 @@ static void prepareEngine_helper(QRegExpPrivate *priv) bool initMatchState = !priv->eng; #if !defined(QT_NO_REGEXP_OPTIM) if (!priv->eng && globalEngineCache()) { - QMutexLocker locker(mutex()); + QMutexLocker locker(&globalEngineCacheMutex); priv->eng = globalEngineCache()->take(priv->engineKey); if (priv->eng != 0) priv->eng->ref.ref(); diff --git a/src/gui/image/qpicture.cpp b/src/gui/image/qpicture.cpp index f9f96f4f13..089cc5011c 100644 --- a/src/gui/image/qpicture.cpp +++ b/src/gui/image/qpicture.cpp @@ -1425,20 +1425,16 @@ QPictureHandler::QPictureHandler(const char *f, const char *h, const QByteArray& typedef QList QPHList; Q_GLOBAL_STATIC(QPHList, pictureHandlers) -#ifndef QT_NO_LIBRARY -Q_GLOBAL_STATIC(QMutex, mutex) -Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, factoryLoader, - (QPictureFormatInterface_iid, - QLatin1String("/pictureformats"))) -#endif void qt_init_picture_plugins() { #ifndef QT_NO_LIBRARY - QMutexLocker locker(mutex()); - QFactoryLoader *loader = factoryLoader(); - QStringList keys = loader->keys(); + static QBasicMutex mutex; + QMutexLocker locker(&mutex); + static QFactoryLoader loader(QPictureFormatInterface_iid, + QStringLiteral("/pictureformats")); + QStringList keys = loader.keys(); for (int i = 0; i < keys.count(); ++i) - if (QPictureFormatInterface *format = qobject_cast(loader->instance(keys.at(i)))) + if (QPictureFormatInterface *format = qobject_cast(loader.instance(keys.at(i)))) format->installIOHandler(keys.at(i)); #endif } diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index 9f7bc24119..a97647f504 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -123,7 +123,7 @@ QList QGuiApplicationPrivate::screen_list; QWindowList QGuiApplicationPrivate::window_list; QWindow *QGuiApplicationPrivate::focus_window = 0; -Q_GLOBAL_STATIC(QMutex, applicationFontMutex) +static QBasicMutex applicationFontMutex; QFont *QGuiApplicationPrivate::app_font = 0; extern void qRegisterGuiVariant(); @@ -1327,7 +1327,7 @@ void QGuiApplication::setPalette(const QPalette &pal) QFont QGuiApplication::font() { - QMutexLocker locker(applicationFontMutex()); + QMutexLocker locker(&applicationFontMutex); if (!QGuiApplicationPrivate::app_font) QGuiApplicationPrivate::app_font = new QFont(QGuiApplicationPrivate::platformIntegration()->fontDatabase()->defaultFont()); @@ -1336,7 +1336,7 @@ QFont QGuiApplication::font() void QGuiApplication::setFont(const QFont &font) { - QMutexLocker locker(applicationFontMutex()); + QMutexLocker locker(&applicationFontMutex); if (!QGuiApplicationPrivate::app_font) QGuiApplicationPrivate::app_font = new QFont(font); else diff --git a/src/gui/kernel/qtouchdevice.cpp b/src/gui/kernel/qtouchdevice.cpp index bb83fea977..b0543819df 100644 --- a/src/gui/kernel/qtouchdevice.cpp +++ b/src/gui/kernel/qtouchdevice.cpp @@ -177,11 +177,11 @@ void QTouchDevice::setName(const QString &name) typedef QList TouchDevices; Q_GLOBAL_STATIC(TouchDevices, deviceList) -Q_GLOBAL_STATIC(QMutex, devicesMutex) +static QBasicMutex devicesMutex; static void cleanupDevicesList() { - QMutexLocker lock(devicesMutex()); + QMutexLocker lock(&devicesMutex); qDeleteAll(*deviceList()); deviceList()->clear(); } @@ -193,7 +193,7 @@ static void cleanupDevicesList() */ QList QTouchDevice::devices() { - QMutexLocker lock(devicesMutex()); + QMutexLocker lock(&devicesMutex); QList *devList = deviceList(); QList constDevList; for (int i = 0, count = devList->count(); i != count; ++i) @@ -206,7 +206,7 @@ QList QTouchDevice::devices() */ bool QTouchDevicePrivate::isRegistered(QTouchDevice *dev) { - QMutexLocker lock(devicesMutex()); + QMutexLocker lock(&devicesMutex); return deviceList()->contains(dev); } @@ -215,7 +215,7 @@ bool QTouchDevicePrivate::isRegistered(QTouchDevice *dev) */ void QTouchDevicePrivate::registerDevice(QTouchDevice *dev) { - QMutexLocker lock(devicesMutex()); + QMutexLocker lock(&devicesMutex); if (deviceList()->isEmpty()) qAddPostRoutine(cleanupDevicesList); deviceList()->append(dev); diff --git a/src/network/bearer/qnetworkconfigmanager.cpp b/src/network/bearer/qnetworkconfigmanager.cpp index 484cad9395..511bec8f23 100644 --- a/src/network/bearer/qnetworkconfigmanager.cpp +++ b/src/network/bearer/qnetworkconfigmanager.cpp @@ -55,7 +55,6 @@ QT_BEGIN_NAMESPACE static QBasicAtomicPointer connManager_ptr; -Q_GLOBAL_STATIC(QMutex, connManager_mutex) static void connManager_cleanup() { @@ -74,7 +73,8 @@ QNetworkConfigurationManagerPrivate *qNetworkConfigurationManagerPrivate() { QNetworkConfigurationManagerPrivate *ptr = connManager_ptr.loadAcquire(); if (!ptr) { - QMutexLocker locker(connManager_mutex()); + static QBasicMutex connManager_mutex; + QMutexLocker locker(&connManager_mutex); if (!(ptr = connManager_ptr.loadAcquire())) { ptr = new QNetworkConfigurationManagerPrivate; diff --git a/src/network/kernel/qhostinfo_unix.cpp b/src/network/kernel/qhostinfo_unix.cpp index 934d5c4c0e..71eb240812 100644 --- a/src/network/kernel/qhostinfo_unix.cpp +++ b/src/network/kernel/qhostinfo_unix.cpp @@ -63,10 +63,7 @@ #endif #if defined (QT_NO_GETADDRINFO) -#include -QT_BEGIN_NAMESPACE -Q_GLOBAL_STATIC(QMutex, getHostByNameMutex) -QT_END_NAMESPACE +static QBasicMutex getHostByNameMutex; #endif QT_BEGIN_NAMESPACE @@ -267,7 +264,7 @@ QHostInfo QHostInfoAgent::fromName(const QString &hostName) // reentrant on all platforms. For now this is okay since we only // use one QHostInfoAgent, but if more agents are introduced, locking // must be provided. - QMutexLocker locker(::getHostByNameMutex()); + QMutexLocker locker(&getHostByNameMutex); hostent *result = gethostbyname(aceHostname.constData()); if (result) { if (result->h_addrtype == AF_INET) { @@ -348,7 +345,7 @@ QString QHostInfo::localDomainName() #if defined(QT_NO_GETADDRINFO) // We have to call res_init to be sure that _res was initialized // So, for systems without getaddrinfo (which is thread-safe), we lock the mutex too - QMutexLocker locker(::getHostByNameMutex()); + QMutexLocker locker(&getHostByNameMutex); #endif local_res_init(); QString domainName = QUrl::fromAce(local_res->defdname); diff --git a/src/network/kernel/qhostinfo_win.cpp b/src/network/kernel/qhostinfo_win.cpp index be1d067cef..a00389e3ed 100644 --- a/src/network/kernel/qhostinfo_win.cpp +++ b/src/network/kernel/qhostinfo_win.cpp @@ -95,15 +95,11 @@ static void resolveLibrary() #endif } -#if defined(Q_OS_WINCE) -#include -Q_GLOBAL_STATIC(QMutex, qPrivCEMutex) -#endif - QHostInfo QHostInfoAgent::fromName(const QString &hostName) { #if defined(Q_OS_WINCE) - QMutexLocker locker(qPrivCEMutex()); + static QBasicMutex qPrivCEMutex; + QMutexLocker locker(&qPrivCEMutex); #endif QWindowsSockInit winSock; -- cgit v1.2.3