diff --git a/src/solid/devices/backends/fstab/fstabhandling.cpp b/src/solid/devices/backends/fstab/fstabhandling.cpp --- a/src/solid/devices/backends/fstab/fstabhandling.cpp +++ b/src/solid/devices/backends/fstab/fstabhandling.cpp @@ -50,6 +50,7 @@ #include #endif #include +#include #endif #ifdef Q_OS_SOLARIS @@ -102,7 +103,7 @@ #define FSNAME(var) var.mnt_special #endif -Q_GLOBAL_STATIC(Solid::Backends::Fstab::FstabHandling, globalFstabCache) +Q_GLOBAL_STATIC(QThreadStorage, globalFstabCache) Solid::Backends::Fstab::FstabHandling::FstabHandling() : m_fstabCacheValid(false), @@ -143,12 +144,12 @@ void Solid::Backends::Fstab::FstabHandling::_k_updateFstabMountPointsCache() { - if (globalFstabCache->m_fstabCacheValid) { + if (globalFstabCache->localData().m_fstabCacheValid) { return; } - globalFstabCache->m_fstabCache.clear(); - globalFstabCache->m_fstabOptionsCache.clear(); + globalFstabCache->localData().m_fstabCache.clear(); + globalFstabCache->localData().m_fstabOptionsCache.clear(); #if HAVE_SETMNTENT @@ -167,10 +168,10 @@ const QString device = _k_deviceNameForMountpoint(fsname, fstype, mountpoint); QStringList options = QFile::decodeName(fe->mnt_opts).split(QLatin1Char(',')); - globalFstabCache->m_fstabCache.insert(device, mountpoint); - globalFstabCache->m_fstabFstypeCache.insert(device, fstype); + globalFstabCache->localData().m_fstabCache.insert(device, mountpoint); + globalFstabCache->localData().m_fstabFstypeCache.insert(device, fstype); while (!options.isEmpty()) { - globalFstabCache->m_fstabOptionsCache.insert(device, options.takeFirst()); + globalFstabCache->localData().m_fstabOptionsCache.insert(device, options.takeFirst()); } } } @@ -211,22 +212,22 @@ const QString device = items.at(0); const QString mountpoint = items.at(1); - globalFstabCache->m_fstabCache.insert(device, mountpoint); + globalFstabCache->localData().m_fstabCache.insert(device, mountpoint); } } fstab.close(); #endif - globalFstabCache->m_fstabCacheValid = true; + globalFstabCache->localData().m_fstabCacheValid = true; } QStringList Solid::Backends::Fstab::FstabHandling::deviceList() { _k_updateFstabMountPointsCache(); _k_updateMtabMountPointsCache(); - QStringList devices = globalFstabCache->m_fstabCache.keys(); - devices += globalFstabCache->m_mtabCache.keys(); + QStringList devices = globalFstabCache->localData().m_fstabCache.keys(); + devices += globalFstabCache->localData().m_mtabCache.keys(); devices.removeDuplicates(); return devices; } @@ -236,25 +237,25 @@ _k_updateFstabMountPointsCache(); _k_updateMtabMountPointsCache(); - QStringList mountpoints = globalFstabCache->m_fstabCache.values(device); - mountpoints += globalFstabCache->m_mtabCache.values(device); + QStringList mountpoints = globalFstabCache->localData().m_fstabCache.values(device); + mountpoints += globalFstabCache->localData().m_mtabCache.values(device); mountpoints.removeDuplicates(); return mountpoints; } QStringList Solid::Backends::Fstab::FstabHandling::options(const QString &device) { _k_updateFstabMountPointsCache(); - QStringList options = globalFstabCache->m_fstabOptionsCache.values(device); + QStringList options = globalFstabCache->localData().m_fstabOptionsCache.values(device); return options; } QString Solid::Backends::Fstab::FstabHandling::fstype(const QString &device) { _k_updateFstabMountPointsCache(); - return globalFstabCache->m_fstabFstypeCache.value(device); + return globalFstabCache->localData().m_fstabFstypeCache.value(device); } bool Solid::Backends::Fstab::FstabHandling::callSystemCommand(const QString &commandName, const QStringList &args, @@ -286,11 +287,11 @@ void Solid::Backends::Fstab::FstabHandling::_k_updateMtabMountPointsCache() { - if (globalFstabCache->m_mtabCacheValid) { + if (globalFstabCache->localData().m_mtabCacheValid) { return; } - globalFstabCache->m_mtabCache.clear(); + globalFstabCache->localData().m_mtabCache.clear(); #if HAVE_GETMNTINFO @@ -309,8 +310,8 @@ const QString fsname = QFile::decodeName(mounted[i].f_mntfromname); const QString mountpoint = QFile::decodeName(mounted[i].f_mntonname); const QString device = _k_deviceNameForMountpoint(fsname, type, mountpoint); - globalFstabCache->m_mtabCache.insert(device, mountpoint); - globalFstabCache->m_fstabFstypeCache.insert(device, type); + globalFstabCache->localData().m_mtabCache.insert(device, mountpoint); + globalFstabCache->localData().m_fstabFstypeCache.insert(device, type); } } @@ -328,28 +329,28 @@ const QString fsname = QFile::decodeName(FSNAME(fe)); const QString mountpoint = QFile::decodeName(MOUNTPOINT(fe)); const QString device = _k_deviceNameForMountpoint(fsname, type, mountpoint); - globalFstabCache->m_mtabCache.insert(device, mountpoint); - globalFstabCache->m_fstabFstypeCache.insert(device, type); + globalFstabCache->localData().m_mtabCache.insert(device, mountpoint); + globalFstabCache->localData().m_fstabFstypeCache.insert(device, type); } } ENDMNTENT(mnttab); #endif - globalFstabCache->m_mtabCacheValid = true; + globalFstabCache->localData().m_mtabCacheValid = true; } QStringList Solid::Backends::Fstab::FstabHandling::currentMountPoints(const QString &device) { _k_updateMtabMountPointsCache(); - return globalFstabCache->m_mtabCache.values(device); + return globalFstabCache->localData().m_mtabCache.values(device); } void Solid::Backends::Fstab::FstabHandling::flushMtabCache() { - globalFstabCache->m_mtabCacheValid = false; + globalFstabCache->localData().m_mtabCacheValid = false; } void Solid::Backends::Fstab::FstabHandling::flushFstabCache() { - globalFstabCache->m_fstabCacheValid = false; + globalFstabCache->localData().m_fstabCacheValid = false; } diff --git a/src/solid/devices/backends/hal/halfstabhandling.cpp b/src/solid/devices/backends/hal/halfstabhandling.cpp --- a/src/solid/devices/backends/hal/halfstabhandling.cpp +++ b/src/solid/devices/backends/hal/halfstabhandling.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -43,7 +44,17 @@ #endif typedef QMultiHash QStringMultiHash; -Q_GLOBAL_STATIC(QStringMultiHash, globalMountPointsCache) + +class MountPointsCache +{ +public: + QReadWriteLock lock; + bool firstCall = true; + QTime elapsedTime; + QMultiHash values; +}; + +Q_GLOBAL_STATIC(MountPointsCache, globalMountPointsCache) QString _k_resolveSymLink(const QString &filename) { @@ -72,19 +83,17 @@ void _k_updateMountPointsCache() { - static bool firstCall = true; - static QTime elapsedTime; - - if (firstCall) { - firstCall = false; - elapsedTime.start(); - } else if (elapsedTime.elapsed() > 10000) { - elapsedTime.restart(); + QWriteLocker locker(&globalMountPointsCache->lock); + if (globalMountPointsCache->firstCall) { + globalMountPointsCache->firstCall = false; + globalMountPointsCache->elapsedTime.start(); + } else if (globalMountPointsCache->elapsedTime.elapsed() > 10000) { + globalMountPointsCache->elapsedTime.restart(); } else { return; } - globalMountPointsCache->clear(); + globalMountPointsCache->values.clear(); #if HAVE_SETMNTENT @@ -99,7 +108,7 @@ const QString device = _k_resolveSymLink(QFile::decodeName(fe->mnt_fsname)); const QString mountpoint = _k_resolveSymLink(QFile::decodeName(fe->mnt_dir)); - globalMountPointsCache->insert(device, mountpoint); + globalMountPointsCache->values.insert(device, mountpoint); } } @@ -138,7 +147,7 @@ const QString device = _k_resolveSymLink(items.at(0)); const QString mountpoint = _k_resolveSymLink(items.at(1)); - globalMountPointsCache->insert(device, mountpoint); + globalMountPointsCache->values.insert(device, mountpoint); } } @@ -151,15 +160,17 @@ _k_updateMountPointsCache(); const QString deviceToFind = _k_resolveSymLink(device); - return globalMountPointsCache->contains(deviceToFind); + QReadLocker lock(&globalMountPointsCache->lock); + return globalMountPointsCache->values.contains(deviceToFind); } QStringList Solid::Backends::Hal::FstabHandling::possibleMountPoints(const QString &device) { _k_updateMountPointsCache(); const QString deviceToFind = _k_resolveSymLink(device); - return globalMountPointsCache->values(deviceToFind); + QReadLocker lock(&globalMountPointsCache->lock); + return globalMountPointsCache->values.values(deviceToFind); } QProcess *Solid::Backends::Hal::FstabHandling::callSystemCommand(const QString &commandName, diff --git a/src/solid/devices/backends/hal/halstorageaccess.cpp b/src/solid/devices/backends/hal/halstorageaccess.cpp --- a/src/solid/devices/backends/hal/halstorageaccess.cpp +++ b/src/solid/devices/backends/hal/halstorageaccess.cpp @@ -312,7 +312,7 @@ QString generateReturnObjectPath() { - static int number = 1; + static QAtomicInt number = 1; return "/org/kde/solid/HalStorageAccess_" + QString::number(number++); } diff --git a/src/solid/devices/backends/udisks2/udisksdevicebackend.h b/src/solid/devices/backends/udisks2/udisksdevicebackend.h --- a/src/solid/devices/backends/udisks2/udisksdevicebackend.h +++ b/src/solid/devices/backends/udisks2/udisksdevicebackend.h @@ -28,6 +28,7 @@ #include #include #include +#include #include "udisks2.h" @@ -78,7 +79,7 @@ QStringList m_interfaces; QString m_udi; - static QMap s_backends; + static QThreadStorage> s_backends; }; diff --git a/src/solid/devices/backends/udisks2/udisksdevicebackend.cpp b/src/solid/devices/backends/udisks2/udisksdevicebackend.cpp --- a/src/solid/devices/backends/udisks2/udisksdevicebackend.cpp +++ b/src/solid/devices/backends/udisks2/udisksdevicebackend.cpp @@ -33,27 +33,27 @@ using namespace Solid::Backends::UDisks2; /* Static cache for DeviceBackends for all UDIs */ -QMap DeviceBackend::s_backends; +QThreadStorage> DeviceBackend::s_backends; DeviceBackend *DeviceBackend::backendForUDI(const QString &udi, bool create) { DeviceBackend *backend = nullptr; if (udi.isEmpty()) { return backend; } - backend = s_backends.value(udi); + backend = s_backends.localData().value(udi); if (!backend && create) { backend = new DeviceBackend(udi); - s_backends.insert(udi, backend); + s_backends.localData().insert(udi, backend); } return backend; } void DeviceBackend::destroyBackend(const QString &udi) { - delete s_backends.take(udi); + delete s_backends.localData().take(udi); } DeviceBackend::DeviceBackend(const QString &udi) diff --git a/src/solid/devices/backends/udisks2/udisksopticaldisc.cpp b/src/solid/devices/backends/udisks2/udisksopticaldisc.cpp --- a/src/solid/devices/backends/udisks2/udisksopticaldisc.cpp +++ b/src/solid/devices/backends/udisks2/udisksopticaldisc.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include "../shared/udevqt.h" @@ -305,7 +306,7 @@ } }; -Q_GLOBAL_STATIC(SharedContentTypesCache, sharedContentTypesCache) +Q_GLOBAL_STATIC(QThreadStorage, sharedContentTypesCache) OpticalDisc::Identity::Identity() : m_detectTime(0), m_size(0), m_labelHash(0) { @@ -419,7 +420,7 @@ if (!(m_identity == newIdentity)) { QByteArray deviceFile(m_device->prop("Device").toByteArray()); m_cachedContent = - sharedContentTypesCache->getContent(newIdentity, deviceFile); + sharedContentTypesCache->localData().getContent(newIdentity, deviceFile); m_identity = newIdentity; } diff --git a/src/solid/devices/backends/udisks2/udisksstorageaccess.cpp b/src/solid/devices/backends/udisks2/udisksstorageaccess.cpp --- a/src/solid/devices/backends/udisks2/udisksstorageaccess.cpp +++ b/src/solid/devices/backends/udisks2/udisksstorageaccess.cpp @@ -306,7 +306,7 @@ QString StorageAccess::generateReturnObjectPath() { - static int number = 1; + static QAtomicInt number = 1; return "/org/kde/solid/UDisks2StorageAccess_" + QString::number(number++); }