Use QBasicMutex instead of Q_GLOBAL_STATIC QMutex
authorOlivier Goffart <ogoffart@woboq.com>
Sun, 29 Jan 2012 19:32:22 +0000 (20:32 +0100)
committerQt by Nokia <qt-info@nokia.com>
Wed, 1 Feb 2012 21:23:55 +0000 (22:23 +0100)
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 <bradley.hughes@nokia.com>
src/corelib/io/qprocess_unix.cpp
src/corelib/io/qsettings.cpp
src/corelib/plugin/qlibrary.cpp
src/corelib/thread/qthreadstorage.cpp
src/corelib/tools/qregexp.cpp
src/gui/image/qpicture.cpp
src/gui/kernel/qguiapplication.cpp
src/gui/kernel/qtouchdevice.cpp
src/network/bearer/qnetworkconfigmanager.cpp
src/network/kernel/qhostinfo_unix.cpp
src/network/kernel/qhostinfo_win.cpp

index d3e45b1..9e35978 100644 (file)
@@ -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<CFBundleRef> bundle = CFBundleCreate(0, url);
             url = CFBundleCopyExecutableURL(bundle);
         }
index 3d2ac32..2021c42 100644 (file)
@@ -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
index a754f1b..dc08b32 100644 (file)
@@ -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;
 }
index 43a8056..1dfa330 100644 (file)
@@ -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<void (*)(void *)> 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<void *> *tls = reinterpret_cast<QVector<void *> *>(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();
 
index 70cae5c..421d15c 100644 (file)
@@ -3809,7 +3809,7 @@ uint qHash(const QRegExpEngineKey &key)
 
 typedef QCache<QRegExpEngineKey, QRegExpEngine> 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();
index f9f96f4..089cc50 100644 (file)
@@ -1425,20 +1425,16 @@ QPictureHandler::QPictureHandler(const char *f, const char *h, const QByteArray&
 typedef QList<QPictureHandler *> 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<QPictureFormatInterface*>(loader->instance(keys.at(i))))
+        if (QPictureFormatInterface *format = qobject_cast<QPictureFormatInterface*>(loader.instance(keys.at(i))))
             format->installIOHandler(keys.at(i));
 #endif
 }
index 9f7bc24..a97647f 100644 (file)
@@ -123,7 +123,7 @@ QList<QScreen *> 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
index bb83fea..b054381 100644 (file)
@@ -177,11 +177,11 @@ void QTouchDevice::setName(const QString &name)
 
 typedef QList<QTouchDevice *> 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<const QTouchDevice *> QTouchDevice::devices()
 {
-    QMutexLocker lock(devicesMutex());
+    QMutexLocker lock(&devicesMutex);
     QList<QTouchDevice *> *devList = deviceList();
     QList<const QTouchDevice *> constDevList;
     for (int i = 0, count = devList->count(); i != count; ++i)
@@ -206,7 +206,7 @@ QList<const QTouchDevice *> 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);
index 484cad9..511bec8 100644 (file)
@@ -55,7 +55,6 @@
 QT_BEGIN_NAMESPACE
 
 static QBasicAtomicPointer<QNetworkConfigurationManagerPrivate> 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;
 
index 934d5c4..71eb240 100644 (file)
 #endif
 
 #if defined (QT_NO_GETADDRINFO)
-#include <qmutex.h>
-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);
index be1d067..a00389e 100644 (file)
@@ -95,15 +95,11 @@ static void resolveLibrary()
 #endif
 }
 
-#if defined(Q_OS_WINCE)
-#include <qmutex.h>
-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;