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 <bradley.hughes@nokia.com>
This commit is contained in:
Olivier Goffart 2012-01-29 20:32:22 +01:00 committed by Qt by Nokia
parent c094891db3
commit b69bb01f11
11 changed files with 47 additions and 61 deletions

View File

@ -169,16 +169,14 @@ private:
}; };
Q_GLOBAL_STATIC(QMutex, processManagerGlobalMutex)
static QProcessManager *processManagerInstance = 0; static QProcessManager *processManagerInstance = 0;
static QProcessManager *processManager() static QProcessManager *processManager()
{ {
// The constructor of QProcessManager should be called only once // The constructor of QProcessManager should be called only once
// so we cannot use Q_GLOBAL_STATIC directly for QProcessManager // so we cannot use Q_GLOBAL_STATIC directly for QProcessManager
QMutex *mutex = processManagerGlobalMutex(); static QBasicMutex processManagerGlobalMutex;
QMutexLocker locker(mutex); QMutexLocker locker(&processManagerGlobalMutex);
if (!processManagerInstance) if (!processManagerInstance)
QProcessPrivate::initializeProcessManager(); QProcessPrivate::initializeProcessManager();
@ -550,10 +548,6 @@ inline pid_t qt_fork()
#endif #endif
} }
#ifdef Q_OS_MAC
Q_GLOBAL_STATIC(QMutex, cfbundleMutex);
#endif
void QProcessPrivate::startProcess() void QProcessPrivate::startProcess()
{ {
Q_Q(QProcess); Q_Q(QProcess);
@ -604,7 +598,8 @@ void QProcessPrivate::startProcess()
{ {
// CFBundle is not reentrant, since CFBundleCreate might return a reference // CFBundle is not reentrant, since CFBundleCreate might return a reference
// to a cached bundle object. Protect the bundle calls with a mutex lock. // 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); QCFType<CFBundleRef> bundle = CFBundleCreate(0, url);
url = CFBundleCopyExecutableURL(bundle); url = CFBundleCopyExecutableURL(bundle);
} }

View File

@ -122,7 +122,9 @@ Q_GLOBAL_STATIC(ConfFileHash, usedHashFunc)
Q_GLOBAL_STATIC(ConfFileCache, unusedCacheFunc) Q_GLOBAL_STATIC(ConfFileCache, unusedCacheFunc)
Q_GLOBAL_STATIC(PathHash, pathHashFunc) Q_GLOBAL_STATIC(PathHash, pathHashFunc)
Q_GLOBAL_STATIC(CustomFormatVector, customFormatVectorFunc) Q_GLOBAL_STATIC(CustomFormatVector, customFormatVectorFunc)
Q_GLOBAL_STATIC(QMutex, globalMutex)
static QBasicMutex settingsGlobalMutex;
static QSettings::Format globalDefaultFormat = QSettings::NativeFormat; static QSettings::Format globalDefaultFormat = QSettings::NativeFormat;
#ifndef Q_OS_WIN #ifndef Q_OS_WIN
@ -277,7 +279,7 @@ QConfFile *QConfFile::fromName(const QString &fileName, bool _userPerms)
ConfFileCache *unusedCache = unusedCacheFunc(); ConfFileCache *unusedCache = unusedCacheFunc();
QConfFile *confFile = 0; QConfFile *confFile = 0;
QMutexLocker locker(globalMutex()); QMutexLocker locker(&settingsGlobalMutex);
if (!(confFile = usedHash->value(absPath))) { if (!(confFile = usedHash->value(absPath))) {
if ((confFile = unusedCache->take(absPath))) if ((confFile = unusedCache->take(absPath)))
@ -292,7 +294,7 @@ QConfFile *QConfFile::fromName(const QString &fileName, bool _userPerms)
void QConfFile::clearCache() void QConfFile::clearCache()
{ {
QMutexLocker locker(globalMutex()); QMutexLocker locker(&settingsGlobalMutex);
unusedCacheFunc()->clear(); unusedCacheFunc()->clear();
} }
@ -992,7 +994,7 @@ void QConfFileSettingsPrivate::initFormat()
#endif #endif
if (format > QSettings::IniFormat) { if (format > QSettings::IniFormat) {
QMutexLocker locker(globalMutex()); QMutexLocker locker(&settingsGlobalMutex);
const CustomFormatVector *customFormatVector = customFormatVectorFunc(); const CustomFormatVector *customFormatVector = customFormatVectorFunc();
int i = (int)format - (int)QSettings::CustomFormat1; 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::NativeFormat == 0);
Q_ASSERT((int)QSettings::IniFormat == 1); Q_ASSERT((int)QSettings::IniFormat == 1);
QMutexLocker locker(globalMutex()); QMutexLocker locker(&settingsGlobalMutex);
PathHash *pathHash = pathHashFunc(); PathHash *pathHash = pathHashFunc();
if (pathHash->isEmpty()) if (pathHash->isEmpty())
initDefaultPaths(&locker); initDefaultPaths(&locker);
@ -1195,7 +1197,7 @@ QConfFileSettingsPrivate::QConfFileSettingsPrivate(const QString &fileName,
QConfFileSettingsPrivate::~QConfFileSettingsPrivate() QConfFileSettingsPrivate::~QConfFileSettingsPrivate()
{ {
QMutexLocker locker(globalMutex()); QMutexLocker locker(&settingsGlobalMutex);
ConfFileHash *usedHash = usedHashFunc(); ConfFileHash *usedHash = usedHashFunc();
ConfFileCache *unusedCache = unusedCacheFunc(); ConfFileCache *unusedCache = unusedCacheFunc();
@ -3437,7 +3439,7 @@ void QSettings::setUserIniPath(const QString &dir)
*/ */
void QSettings::setPath(Format format, Scope scope, const QString &path) void QSettings::setPath(Format format, Scope scope, const QString &path)
{ {
QMutexLocker locker(globalMutex()); QMutexLocker locker(&settingsGlobalMutex);
PathHash *pathHash = pathHashFunc(); PathHash *pathHash = pathHashFunc();
if (pathHash->isEmpty()) if (pathHash->isEmpty())
initDefaultPaths(&locker); initDefaultPaths(&locker);
@ -3520,7 +3522,7 @@ QSettings::Format QSettings::registerFormat(const QString &extension, ReadFunc r
Q_ASSERT(caseSensitivity == Qt::CaseSensitive); Q_ASSERT(caseSensitivity == Qt::CaseSensitive);
#endif #endif
QMutexLocker locker(globalMutex()); QMutexLocker locker(&settingsGlobalMutex);
CustomFormatVector *customFormatVector = customFormatVectorFunc(); CustomFormatVector *customFormatVector = customFormatVectorFunc();
int index = customFormatVector->size(); int index = customFormatVector->size();
if (index == 16) // the QSettings::Format enum has room for 16 custom formats if (index == 16) // the QSettings::Format enum has room for 16 custom formats

View File

@ -76,7 +76,7 @@ QT_BEGIN_NAMESPACE
# define QT_NO_DEBUG_PLUGIN_CHECK # define QT_NO_DEBUG_PLUGIN_CHECK
#endif #endif
Q_GLOBAL_STATIC(QMutex, qt_library_mutex) static QBasicMutex qt_library_mutex;
/*! /*!
\class QLibrary \class QLibrary
@ -452,7 +452,7 @@ QLibraryPrivate::QLibraryPrivate(const QString &canonicalFileName, const QString
QLibraryPrivate *QLibraryPrivate::findOrCreate(const QString &fileName, const QString &version) 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)) { if (QLibraryPrivate *lib = libraryMap()->value(fileName)) {
lib->libraryRefCount.ref(); lib->libraryRefCount.ref();
return lib; return lib;
@ -526,7 +526,7 @@ bool QLibraryPrivate::unload()
void QLibraryPrivate::release() void QLibraryPrivate::release()
{ {
QMutexLocker locker(qt_library_mutex()); QMutexLocker locker(&qt_library_mutex);
if (!libraryRefCount.deref()) if (!libraryRefCount.deref())
delete this; delete this;
} }

View File

@ -71,13 +71,13 @@ void qtsDebug(const char *fmt, ...)
# define DEBUG_MSG if(false)qDebug # define DEBUG_MSG if(false)qDebug
#endif #endif
Q_GLOBAL_STATIC(QMutex, mutex) static QBasicMutex destructorsMutex;
typedef QVector<void (*)(void *)> DestructorMap; typedef QVector<void (*)(void *)> DestructorMap;
Q_GLOBAL_STATIC(DestructorMap, destructors) Q_GLOBAL_STATIC(DestructorMap, destructors)
QThreadStorageData::QThreadStorageData(void (*func)(void *)) QThreadStorageData::QThreadStorageData(void (*func)(void *))
{ {
QMutexLocker locker(mutex()); QMutexLocker locker(&destructorsMutex);
DestructorMap *destr = destructors(); DestructorMap *destr = destructors();
if (!destr) { if (!destr) {
/* /*
@ -109,7 +109,7 @@ QThreadStorageData::QThreadStorageData(void (*func)(void *))
QThreadStorageData::~QThreadStorageData() QThreadStorageData::~QThreadStorageData()
{ {
DEBUG_MSG("QThreadStorageData: Released id %d", id); DEBUG_MSG("QThreadStorageData: Released id %d", id);
QMutexLocker locker(mutex()); QMutexLocker locker(&destructorsMutex);
if (destructors()) if (destructors())
(*destructors())[id] = 0; (*destructors())[id] = 0;
} }
@ -153,7 +153,7 @@ void **QThreadStorageData::set(void *p)
value, value,
data->thread); data->thread);
QMutexLocker locker(mutex()); QMutexLocker locker(&destructorsMutex);
DestructorMap *destr = destructors(); DestructorMap *destr = destructors();
void (*destructor)(void *) = destr ? destr->value(id) : 0; void (*destructor)(void *) = destr ? destr->value(id) : 0;
locker.unlock(); locker.unlock();
@ -174,7 +174,7 @@ void **QThreadStorageData::set(void *p)
void QThreadStorageData::finish(void **p) void QThreadStorageData::finish(void **p)
{ {
QVector<void *> *tls = reinterpret_cast<QVector<void *> *>(p); QVector<void *> *tls = reinterpret_cast<QVector<void *> *>(p);
if (!tls || tls->isEmpty() || !mutex()) if (!tls || tls->isEmpty() || !destructors())
return; // nothing to do return; // nothing to do
DEBUG_MSG("QThreadStorageData: Destroying storage for thread %p", QThread::currentThread()); DEBUG_MSG("QThreadStorageData: Destroying storage for thread %p", QThread::currentThread());
@ -190,7 +190,7 @@ void QThreadStorageData::finish(void **p)
continue; continue;
} }
QMutexLocker locker(mutex()); QMutexLocker locker(&destructorsMutex);
void (*destructor)(void *) = destructors()->value(i); void (*destructor)(void *) = destructors()->value(i);
locker.unlock(); locker.unlock();

View File

@ -3809,7 +3809,7 @@ uint qHash(const QRegExpEngineKey &key)
typedef QCache<QRegExpEngineKey, QRegExpEngine> EngineCache; typedef QCache<QRegExpEngineKey, QRegExpEngine> EngineCache;
Q_GLOBAL_STATIC(EngineCache, globalEngineCache) Q_GLOBAL_STATIC(EngineCache, globalEngineCache)
Q_GLOBAL_STATIC(QMutex, mutex) static QBasicMutex globalEngineCacheMutex;
#endif // QT_NO_REGEXP_OPTIM #endif // QT_NO_REGEXP_OPTIM
static void derefEngine(QRegExpEngine *eng, const QRegExpEngineKey &key) 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 (!eng->ref.deref()) {
#if !defined(QT_NO_REGEXP_OPTIM) #if !defined(QT_NO_REGEXP_OPTIM)
if (globalEngineCache()) { if (globalEngineCache()) {
QMutexLocker locker(mutex()); QMutexLocker locker(&globalEngineCacheMutex);
QT_TRY { QT_TRY {
globalEngineCache()->insert(key, eng, 4 + key.pattern.length() / 4); globalEngineCache()->insert(key, eng, 4 + key.pattern.length() / 4);
} QT_CATCH(const std::bad_alloc &) { } QT_CATCH(const std::bad_alloc &) {
@ -3839,7 +3839,7 @@ static void prepareEngine_helper(QRegExpPrivate *priv)
bool initMatchState = !priv->eng; bool initMatchState = !priv->eng;
#if !defined(QT_NO_REGEXP_OPTIM) #if !defined(QT_NO_REGEXP_OPTIM)
if (!priv->eng && globalEngineCache()) { if (!priv->eng && globalEngineCache()) {
QMutexLocker locker(mutex()); QMutexLocker locker(&globalEngineCacheMutex);
priv->eng = globalEngineCache()->take(priv->engineKey); priv->eng = globalEngineCache()->take(priv->engineKey);
if (priv->eng != 0) if (priv->eng != 0)
priv->eng->ref.ref(); priv->eng->ref.ref();

View File

@ -1425,20 +1425,16 @@ QPictureHandler::QPictureHandler(const char *f, const char *h, const QByteArray&
typedef QList<QPictureHandler *> QPHList; typedef QList<QPictureHandler *> QPHList;
Q_GLOBAL_STATIC(QPHList, pictureHandlers) 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() void qt_init_picture_plugins()
{ {
#ifndef QT_NO_LIBRARY #ifndef QT_NO_LIBRARY
QMutexLocker locker(mutex()); static QBasicMutex mutex;
QFactoryLoader *loader = factoryLoader(); QMutexLocker locker(&mutex);
QStringList keys = loader->keys(); static QFactoryLoader loader(QPictureFormatInterface_iid,
QStringLiteral("/pictureformats"));
QStringList keys = loader.keys();
for (int i = 0; i < keys.count(); ++i) 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)); format->installIOHandler(keys.at(i));
#endif #endif
} }

View File

@ -123,7 +123,7 @@ QList<QScreen *> QGuiApplicationPrivate::screen_list;
QWindowList QGuiApplicationPrivate::window_list; QWindowList QGuiApplicationPrivate::window_list;
QWindow *QGuiApplicationPrivate::focus_window = 0; QWindow *QGuiApplicationPrivate::focus_window = 0;
Q_GLOBAL_STATIC(QMutex, applicationFontMutex) static QBasicMutex applicationFontMutex;
QFont *QGuiApplicationPrivate::app_font = 0; QFont *QGuiApplicationPrivate::app_font = 0;
extern void qRegisterGuiVariant(); extern void qRegisterGuiVariant();
@ -1327,7 +1327,7 @@ void QGuiApplication::setPalette(const QPalette &pal)
QFont QGuiApplication::font() QFont QGuiApplication::font()
{ {
QMutexLocker locker(applicationFontMutex()); QMutexLocker locker(&applicationFontMutex);
if (!QGuiApplicationPrivate::app_font) if (!QGuiApplicationPrivate::app_font)
QGuiApplicationPrivate::app_font = QGuiApplicationPrivate::app_font =
new QFont(QGuiApplicationPrivate::platformIntegration()->fontDatabase()->defaultFont()); new QFont(QGuiApplicationPrivate::platformIntegration()->fontDatabase()->defaultFont());
@ -1336,7 +1336,7 @@ QFont QGuiApplication::font()
void QGuiApplication::setFont(const QFont &font) void QGuiApplication::setFont(const QFont &font)
{ {
QMutexLocker locker(applicationFontMutex()); QMutexLocker locker(&applicationFontMutex);
if (!QGuiApplicationPrivate::app_font) if (!QGuiApplicationPrivate::app_font)
QGuiApplicationPrivate::app_font = new QFont(font); QGuiApplicationPrivate::app_font = new QFont(font);
else else

View File

@ -177,11 +177,11 @@ void QTouchDevice::setName(const QString &name)
typedef QList<QTouchDevice *> TouchDevices; typedef QList<QTouchDevice *> TouchDevices;
Q_GLOBAL_STATIC(TouchDevices, deviceList) Q_GLOBAL_STATIC(TouchDevices, deviceList)
Q_GLOBAL_STATIC(QMutex, devicesMutex) static QBasicMutex devicesMutex;
static void cleanupDevicesList() static void cleanupDevicesList()
{ {
QMutexLocker lock(devicesMutex()); QMutexLocker lock(&devicesMutex);
qDeleteAll(*deviceList()); qDeleteAll(*deviceList());
deviceList()->clear(); deviceList()->clear();
} }
@ -193,7 +193,7 @@ static void cleanupDevicesList()
*/ */
QList<const QTouchDevice *> QTouchDevice::devices() QList<const QTouchDevice *> QTouchDevice::devices()
{ {
QMutexLocker lock(devicesMutex()); QMutexLocker lock(&devicesMutex);
QList<QTouchDevice *> *devList = deviceList(); QList<QTouchDevice *> *devList = deviceList();
QList<const QTouchDevice *> constDevList; QList<const QTouchDevice *> constDevList;
for (int i = 0, count = devList->count(); i != count; ++i) for (int i = 0, count = devList->count(); i != count; ++i)
@ -206,7 +206,7 @@ QList<const QTouchDevice *> QTouchDevice::devices()
*/ */
bool QTouchDevicePrivate::isRegistered(QTouchDevice *dev) bool QTouchDevicePrivate::isRegistered(QTouchDevice *dev)
{ {
QMutexLocker lock(devicesMutex()); QMutexLocker lock(&devicesMutex);
return deviceList()->contains(dev); return deviceList()->contains(dev);
} }
@ -215,7 +215,7 @@ bool QTouchDevicePrivate::isRegistered(QTouchDevice *dev)
*/ */
void QTouchDevicePrivate::registerDevice(QTouchDevice *dev) void QTouchDevicePrivate::registerDevice(QTouchDevice *dev)
{ {
QMutexLocker lock(devicesMutex()); QMutexLocker lock(&devicesMutex);
if (deviceList()->isEmpty()) if (deviceList()->isEmpty())
qAddPostRoutine(cleanupDevicesList); qAddPostRoutine(cleanupDevicesList);
deviceList()->append(dev); deviceList()->append(dev);

View File

@ -55,7 +55,6 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
static QBasicAtomicPointer<QNetworkConfigurationManagerPrivate> connManager_ptr; static QBasicAtomicPointer<QNetworkConfigurationManagerPrivate> connManager_ptr;
Q_GLOBAL_STATIC(QMutex, connManager_mutex)
static void connManager_cleanup() static void connManager_cleanup()
{ {
@ -74,7 +73,8 @@ QNetworkConfigurationManagerPrivate *qNetworkConfigurationManagerPrivate()
{ {
QNetworkConfigurationManagerPrivate *ptr = connManager_ptr.loadAcquire(); QNetworkConfigurationManagerPrivate *ptr = connManager_ptr.loadAcquire();
if (!ptr) { if (!ptr) {
QMutexLocker locker(connManager_mutex()); static QBasicMutex connManager_mutex;
QMutexLocker locker(&connManager_mutex);
if (!(ptr = connManager_ptr.loadAcquire())) { if (!(ptr = connManager_ptr.loadAcquire())) {
ptr = new QNetworkConfigurationManagerPrivate; ptr = new QNetworkConfigurationManagerPrivate;

View File

@ -63,10 +63,7 @@
#endif #endif
#if defined (QT_NO_GETADDRINFO) #if defined (QT_NO_GETADDRINFO)
#include <qmutex.h> static QBasicMutex getHostByNameMutex;
QT_BEGIN_NAMESPACE
Q_GLOBAL_STATIC(QMutex, getHostByNameMutex)
QT_END_NAMESPACE
#endif #endif
QT_BEGIN_NAMESPACE 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 // reentrant on all platforms. For now this is okay since we only
// use one QHostInfoAgent, but if more agents are introduced, locking // use one QHostInfoAgent, but if more agents are introduced, locking
// must be provided. // must be provided.
QMutexLocker locker(::getHostByNameMutex()); QMutexLocker locker(&getHostByNameMutex);
hostent *result = gethostbyname(aceHostname.constData()); hostent *result = gethostbyname(aceHostname.constData());
if (result) { if (result) {
if (result->h_addrtype == AF_INET) { if (result->h_addrtype == AF_INET) {
@ -348,7 +345,7 @@ QString QHostInfo::localDomainName()
#if defined(QT_NO_GETADDRINFO) #if defined(QT_NO_GETADDRINFO)
// We have to call res_init to be sure that _res was initialized // 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 // So, for systems without getaddrinfo (which is thread-safe), we lock the mutex too
QMutexLocker locker(::getHostByNameMutex()); QMutexLocker locker(&getHostByNameMutex);
#endif #endif
local_res_init(); local_res_init();
QString domainName = QUrl::fromAce(local_res->defdname); QString domainName = QUrl::fromAce(local_res->defdname);

View File

@ -95,15 +95,11 @@ static void resolveLibrary()
#endif #endif
} }
#if defined(Q_OS_WINCE)
#include <qmutex.h>
Q_GLOBAL_STATIC(QMutex, qPrivCEMutex)
#endif
QHostInfo QHostInfoAgent::fromName(const QString &hostName) QHostInfo QHostInfoAgent::fromName(const QString &hostName)
{ {
#if defined(Q_OS_WINCE) #if defined(Q_OS_WINCE)
QMutexLocker locker(qPrivCEMutex()); static QBasicMutex qPrivCEMutex;
QMutexLocker locker(&qPrivCEMutex);
#endif #endif
QWindowsSockInit winSock; QWindowsSockInit winSock;