[qt6] Use RecursiveMutex instead of QMutex( QMutex::Recursive )

QMutex::Recursive will be gone.
The replacement only entered the stage with Qt 5.14
This commit is contained in:
Matthias Kuhn 2021-03-24 09:33:59 +01:00
parent f98ab57c1c
commit e94d9bc024
18 changed files with 207 additions and 16 deletions

View File

@ -99,7 +99,7 @@ Required build tools:
Required build dependencies:
* Qt >= 5.9.0
* Qt >= 5.12.0
* Proj >= 4.9.3
* GEOS >= 3.4
* Sqlite3 >= 3.0.0

View File

@ -10,6 +10,7 @@
class QgsAuthMethod : QObject
{
%Docstring(signature="appended")
@ -153,7 +154,6 @@ Construct a default authentication method
Non-public since this is an abstract base class
%End
static QString authMethodTag();
%Docstring
Tag signifying that this is an authentcation method (e.g. for use as title in message log panel output)
@ -173,7 +173,6 @@ Sets the support expansions (points in providers where the authentication is inj
Sets list of data providers this auth method supports
%End
};
QFlags<QgsAuthMethod::Expansion> operator|(QgsAuthMethod::Expansion f1, QFlags<QgsAuthMethod::Expansion> f2);

View File

@ -136,6 +136,7 @@ set(QGIS_CORE_SRCS
auth/qgsauthconfig.cpp
auth/qgsauthcrypto.cpp
auth/qgsauthmanager.cpp
auth/qgsauthmethod.cpp
auth/qgsauthmethodmetadata.cpp
auth/qgsauthmethodregistry.cpp

View File

@ -100,8 +100,13 @@ QgsAuthManager *QgsAuthManager::instance()
QgsAuthManager::QgsAuthManager()
{
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
mMutex.reset( new QMutex( QMutex::Recursive ) );
mMasterPasswordMutex.reset( new QMutex( QMutex::Recursive ) );
#else
mMutex = std::make_unique<QRecursiveMutex>();
mMasterPasswordMutex = std::make_unique<QRecursiveMutex>();
#endif
connect( this, &QgsAuthManager::messageOut,
this, &QgsAuthManager::writeToConsole );
}

View File

@ -20,7 +20,11 @@
#include "qgis_core.h"
#include "qgis_sip.h"
#include <QObject>
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
#include <QMutex>
#else
#include <QRecursiveMutex>
#endif
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QSqlDatabase>
@ -870,9 +874,13 @@ class CORE_EXPORT QgsAuthManager : public QObject
bool mScheduledDbEraseRequestEmitted = false;
int mScheduledDbEraseRequestCount = 0;
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
std::unique_ptr<QMutex> mMutex;
std::unique_ptr<QMutex> mMasterPasswordMutex;
#else
std::unique_ptr<QRecursiveMutex> mMutex;
std::unique_ptr<QRecursiveMutex> mMasterPasswordMutex;
#endif
#ifndef QT_NO_SSL
// mapping of sha1 digest and cert source and cert
// appending removes duplicates

View File

@ -0,0 +1,23 @@
/***************************************************************************
qgsauthmethod.cpp
---------------------
begin : March 2021
copyright : (C) 2021
author : Matthias Khn
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsauthmethod.h"
QgsAuthMethod::QgsAuthMethod()
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
: mMutex( QMutex::RecursionMode::Recursive )
#endif
{}

View File

@ -23,7 +23,12 @@
#include <QNetworkRequest>
#include <QStringList>
#include <QUrl>
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
#include <QMutex>
#else
#include <QRecursiveMutex>
#endif
#include "qgis_core.h"
@ -172,10 +177,7 @@ class CORE_EXPORT QgsAuthMethod : public QObject
* Construct a default authentication method
* \note Non-public since this is an abstract base class
*/
explicit QgsAuthMethod()
: mMutex( QMutex::RecursionMode::Recursive )
{}
explicit QgsAuthMethod();
//! Tag signifying that this is an authentcation method (e.g. for use as title in message log panel output)
static QString authMethodTag() { return QObject::tr( "Authentication method" ); }
@ -191,8 +193,11 @@ class CORE_EXPORT QgsAuthMethod : public QObject
QgsAuthMethod::Expansions mExpansions = QgsAuthMethod::Expansions();
QStringList mDataProviders;
int mVersion = 0;
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex mMutex;
#else
QRecursiveMutex mMutex;
#endif
};
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsAuthMethod::Expansions )

View File

@ -6328,8 +6328,13 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
// crashes in the WFS provider may occur, since it can parse expressions
// in parallel.
// The mutex needs to be recursive.
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
static QMutex sFunctionsMutex( QMutex::Recursive );
QMutexLocker locker( &sFunctionsMutex );
#else
static QRecursiveMutex sFunctionsMutex;
QMutexLocker locker( &sFunctionsMutex );
#endif
QList<QgsExpressionFunction *> &functions = *sFunctions();

View File

@ -71,7 +71,11 @@
#define PROVIDER_DESCRIPTION QStringLiteral( "GDAL data provider" )
// To avoid potential races when destroying related instances ("main" and clones)
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
Q_GLOBAL_STATIC_WITH_ARGS( QMutex, sGdalProviderMutex, ( QMutex::Recursive ) )
#else
Q_GLOBAL_STATIC( QRecursiveMutex, sGdalProviderMutex )
#endif
QHash< QgsGdalProvider *, QVector<QgsGdalProvider::DatasetPair> > QgsGdalProvider::mgDatasetCache;
@ -146,7 +150,11 @@ QgsGdalProvider::QgsGdalProvider( const QString &uri, const QgsError &error )
QgsGdalProvider::QgsGdalProvider( const QString &uri, const ProviderOptions &options, bool update, GDALDatasetH dataset )
: QgsRasterDataProvider( uri, options )
, mpRefCounter( new QAtomicInt( 1 ) )
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
, mpMutex( new QMutex( QMutex::Recursive ) )
#else
, mpMutex( new QRecursiveMutex() )
#endif
, mpParent( new QgsGdalProvider * ( this ) )
, mpLightRefCounter( new QAtomicInt( 1 ) )
, mUpdate( update )
@ -238,7 +246,12 @@ QgsGdalProvider::QgsGdalProvider( const QgsGdalProvider &other )
mpRefCounter = new QAtomicInt( 1 );
mpLightRefCounter = other.mpLightRefCounter;
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
mpMutex = new QMutex( QMutex::Recursive );
#else
mpMutex = new QRecursiveMutex();
#endif
mpParent = other.mpParent;
if ( getCachedGdalHandles( const_cast<QgsGdalProvider *>( &other ), mGdalBaseDataset, mGdalDataset ) )

View File

@ -235,7 +235,12 @@ class QgsGdalProvider final: public QgsRasterDataProvider, QgsGdalProviderBase
QAtomicInt *mpRefCounter = nullptr;
// mutex to protect access to mGdalDataset among main and shared provider instances
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex *mpMutex = nullptr;
#else
QRecursiveMutex *mpMutex = nullptr;
#endif
// pointer to a QgsGdalProvider* that is the parent. Note when *mpParent == this, we are the parent.
QgsGdalProvider **mpParent = nullptr;

View File

@ -99,7 +99,12 @@ static OGRwkbGeometryType ogrWkbGeometryTypeFromName( const QString &typeName );
static bool IsLocalFile( const QString &path );
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
Q_GLOBAL_STATIC_WITH_ARGS( QMutex, sGlobalMutex, ( QMutex::Recursive ) )
#else
Q_GLOBAL_STATIC( QRecursiveMutex, sGlobalMutex )
#endif
//! Map a dataset name to the number of opened GDAL dataset objects on it (if opened with GDALOpenWrapper, only for GPKG)
typedef QMap< QString, int > OpenedDsCountMap;
@ -603,7 +608,11 @@ QgsOgrProvider::QgsOgrProvider( QString const &uri, const ProviderOptions &optio
mLayerMetadata.setType( QStringLiteral( "dataset" ) );
if ( mOgrOrigLayer )
{
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex *mutex = nullptr;
#else
QRecursiveMutex *mutex = nullptr;
#endif
OGRLayerH layer = mOgrOrigLayer->getHandleAndMutex( mutex );
QMutexLocker locker( mutex );
const QString identifier = GDALGetMetadataItem( layer, "IDENTIFIER", nullptr );
@ -1104,7 +1113,11 @@ void QgsOgrProvider::loadFields()
}
else
{
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex *mutex = nullptr;
#else
QRecursiveMutex *mutex = nullptr;
#endif
OGRLayerH ogrLayer = mOgrLayer->getHandleAndMutex( mutex );
QMutexLocker locker( mutex );
mOGRGeomType = getOgrGeomType( mGDALDriverName, ogrLayer );
@ -1314,7 +1327,11 @@ QString QgsOgrProvider::storageType() const
void QgsOgrProvider::setRelevantFields( bool fetchGeometry, const QgsAttributeList &fetchAttributes ) const
{
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex *mutex = nullptr;
#else
QRecursiveMutex *mutex = nullptr;
#endif
OGRLayerH ogrLayer = mOgrLayer->getHandleAndMutex( mutex );
QMutexLocker locker( mutex );
QgsOgrProviderUtils::setRelevantFields( ogrLayer, mAttributeFields.count(), fetchGeometry, fetchAttributes, mFirstFieldIsFid, mSubsetString );
@ -1904,7 +1921,11 @@ bool QgsOgrProvider::addFeatures( QgsFeatureList &flist, Flags flags )
if ( !( flags & QgsFeatureSink::FastInsert ) &&
( mGDALDriverName == QLatin1String( "CSV" ) || mGDALDriverName == QLatin1String( "XLSX" ) || mGDALDriverName == QLatin1String( "ODS" ) ) )
{
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex *mutex = nullptr;
#else
QRecursiveMutex *mutex = nullptr;
#endif
OGRLayerH layer = mOgrOrigLayer->getHandleAndMutex( mutex );
{
QMutexLocker locker( mutex );
@ -2277,7 +2298,11 @@ bool QgsOgrProvider::_setSubsetString( const QString &theSQL, bool updateFeature
if ( !theSQL.isEmpty() )
{
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex *mutex = nullptr;
#else
QRecursiveMutex *mutex = nullptr;
#endif
OGRLayerH layer = mOgrOrigLayer->getHandleAndMutex( mutex );
GDALDatasetH ds = mOgrOrigLayer->getDatasetHandleAndMutex( mutex );
OGRLayerH subsetLayerH;
@ -2306,7 +2331,11 @@ bool QgsOgrProvider::_setSubsetString( const QString &theSQL, bool updateFeature
{
mOgrSqlLayer.reset();
mOgrLayer = mOgrOrigLayer.get();
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex *mutex = nullptr;
#else
QRecursiveMutex *mutex = nullptr;
#endif
OGRLayerH layer = mOgrOrigLayer->getHandleAndMutex( mutex );
{
QMutexLocker locker( mutex );
@ -2717,7 +2746,11 @@ bool QgsOgrProvider::createSpatialIndex()
else if ( mGDALDriverName == QLatin1String( "GPKG" ) ||
mGDALDriverName == QLatin1String( "SQLite" ) )
{
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex *mutex = nullptr;
#else
QRecursiveMutex *mutex = nullptr;
#endif
OGRLayerH layer = mOgrOrigLayer->getHandleAndMutex( mutex );
QByteArray sql = QByteArray( "SELECT CreateSpatialIndex(" + quotedIdentifier( layerName ) + ","
+ quotedIdentifier( OGR_L_GetGeometryColumn( layer ) ) + ") " ); // quote the layer name so spaces are handled
@ -6205,13 +6238,21 @@ void QgsOgrLayer::SetSpatialFilter( OGRGeometryH hGeometry )
OGR_L_SetSpatialFilter( hLayer, hGeometry );
}
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
GDALDatasetH QgsOgrLayer::getDatasetHandleAndMutex( QMutex *&mutex )
#else
GDALDatasetH QgsOgrLayer::getDatasetHandleAndMutex( QRecursiveMutex *&mutex )
#endif
{
mutex = &( ds->mutex );
return ds->hDS;
}
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
OGRLayerH QgsOgrLayer::getHandleAndMutex( QMutex *&mutex )
#else
OGRLayerH QgsOgrLayer::getHandleAndMutex( QRecursiveMutex *&mutex )
#endif
{
mutex = &( ds->mutex );
return hLayer;
@ -6334,7 +6375,11 @@ QString QgsOgrLayer::GetMetadataItem( const QString &key, const QString &domain
domain.toUtf8().constData() );
}
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex &QgsOgrFeatureDefn::mutex()
#else
QRecursiveMutex &QgsOgrFeatureDefn::mutex()
#endif
{
return layer->mutex();
}
@ -6432,7 +6477,11 @@ bool QgsOgrProviderMetadata::saveStyle(
if ( !userLayer )
return false;
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex *mutex = nullptr;
#else
QRecursiveMutex *mutex = nullptr;
#endif
OGRLayerH hUserLayer = userLayer->getHandleAndMutex( mutex );
GDALDatasetH hDS = userLayer->getDatasetHandleAndMutex( mutex );
QMutexLocker locker( mutex );
@ -6644,7 +6693,11 @@ bool QgsOgrProviderMetadata::deleteStyleById( const QString &uri, QString styleI
if ( !userLayer )
return false;
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex *mutex = nullptr;
#else
QRecursiveMutex *mutex = nullptr;
#endif
GDALDatasetH hDS = userLayer->getDatasetHandleAndMutex( mutex );
QMutexLocker locker( mutex );
@ -6725,9 +6778,14 @@ QString QgsOgrProviderMetadata::loadStyle( const QString &uri, QString &errCause
return QString();
}
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex *mutex1 = nullptr;
OGRLayerH hLayer = layerStyles->getHandleAndMutex( mutex1 );
QMutex *mutex2 = nullptr;
#else
QRecursiveMutex *mutex1 = nullptr;
QRecursiveMutex *mutex2 = nullptr;
#endif
OGRLayerH hLayer = layerStyles->getHandleAndMutex( mutex1 );
OGRLayerH hUserLayer = userLayer->getHandleAndMutex( mutex2 );
QMutexLocker lock1( mutex1 );
QMutexLocker lock2( mutex2 );
@ -6814,10 +6872,16 @@ int QgsOgrProviderMetadata::listStyles(
return 0;
}
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex *mutex1 = nullptr;
QMutex *mutex2 = nullptr;
#else
QRecursiveMutex *mutex1 = nullptr;
QRecursiveMutex *mutex2 = nullptr;
#endif
OGRLayerH hLayer = layerStyles->getHandleAndMutex( mutex1 );
QMutexLocker lock1( mutex1 );
QMutex *mutex2 = nullptr;
OGRLayerH hUserLayer = userLayer->getHandleAndMutex( mutex2 );
QMutexLocker lock2( mutex2 );
@ -6908,7 +6972,12 @@ QString QgsOgrProviderMetadata::getStyleById( const QString &uri, QString styleI
return QString();
}
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex *mutex1 = nullptr;
#else
QRecursiveMutex *mutex1 = nullptr;
#endif
OGRLayerH hLayer = layerStyles->getHandleAndMutex( mutex1 );
QMutexLocker lock1( mutex1 );

View File

@ -397,13 +397,21 @@ class CORE_EXPORT QgsOgrProviderUtils
class DatasetWithLayers
{
public:
QMutex mutex;
GDALDatasetH hDS = nullptr;
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex mutex;
#else
QRecursiveMutex mutex;
#endif
GDALDatasetH hDS = nullptr;
QMap<QString, QgsOgrLayer *> setLayers;
int refCount = 0;
bool canBeShared = true;
DatasetWithLayers(): mutex( QMutex::Recursive ) {}
DatasetWithLayers()
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
: mutex( QMutex::Recursive )
#endif
{}
};
//! Map dataset identification to a list of corresponding DatasetWithLayers*
@ -559,8 +567,11 @@ class QgsOgrDataset
static QgsOgrDatasetSharedPtr create( const QgsOgrProviderUtils::DatasetIdentification &ident,
QgsOgrProviderUtils::DatasetWithLayers *ds );
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex &mutex() { return mDs->mutex; }
#else
QRecursiveMutex &mutex() { return mDs->mutex; }
#endif
bool executeSQLNoReturn( const QString &sql );
@ -585,7 +596,11 @@ class QgsOgrFeatureDefn
~QgsOgrFeatureDefn() = default;
OGRFeatureDefnH get();
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex &mutex();
#else
QRecursiveMutex &mutex();
#endif
public:
@ -644,7 +659,11 @@ class QgsOgrLayer
QgsOgrProviderUtils::DatasetWithLayers *ds,
OGRLayerH hLayer );
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex &mutex() { return ds->mutex; }
#else
QRecursiveMutex &mutex() { return ds->mutex; }
#endif
public:
@ -735,11 +754,20 @@ class QgsOgrLayer
//! Wrapper of OGR_L_GetLayerCount
void SetSpatialFilter( OGRGeometryH );
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
//! Returns native GDALDatasetH object with the mutex to lock when using it
GDALDatasetH getDatasetHandleAndMutex( QMutex *&mutex );
//! Returns native OGRLayerH object with the mutex to lock when using it
OGRLayerH getHandleAndMutex( QMutex *&mutex );
#else
//! Returns native GDALDatasetH object with the mutex to lock when using it
GDALDatasetH getDatasetHandleAndMutex( QRecursiveMutex *&mutex );
//! Returns native OGRLayerH object with the mutex to lock when using it
OGRLayerH getHandleAndMutex( QRecursiveMutex *&mutex );
#endif
//! Wrapper of GDALDatasetReleaseResultSet( GDALDatasetExecuteSQL( ... ) )
void ExecuteSQLNoReturn( const QByteArray &sql );

View File

@ -27,7 +27,11 @@
#include "qgsnetworkcontentfetchertask.h"
#include <QObject>
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
#include <QMutex>
#else
#include <QRecursiveMutex>
#endif
#include <QCache>
#include <QSet>
#include <QDateTime>
@ -210,7 +214,9 @@ class CORE_EXPORT QgsAbstractContentCache : public QgsAbstractContentCacheBase
long maxCacheSize = 20000000,
int fileModifiedCheckTimeout = 30000 )
: QgsAbstractContentCacheBase( parent )
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
, mMutex( QMutex::Recursive )
#endif
, mMaxCacheSize( maxCacheSize )
, mFileModifiedCheckTimeout( fileModifiedCheckTimeout )
, mTypeString( typeString.isEmpty() ? QObject::tr( "Content" ) : typeString )
@ -548,8 +554,11 @@ class CORE_EXPORT QgsAbstractContentCache : public QgsAbstractContentCacheBase
return currentEntry;
}
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
mutable QMutex mMutex;
#else
mutable QRecursiveMutex mMutex;
#endif
//! Estimated total size of all cached content
long mTotalSize = 0;

View File

@ -102,6 +102,9 @@
#include <QRegularExpression>
#include <QTextStream>
#include <QScreen>
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
#include <QRecursiveMutex>
#endif
#ifndef Q_OS_WIN
#include <netinet/in.h>
@ -2570,7 +2573,11 @@ QgsApplication::ApplicationMembers *QgsApplication::members()
}
else
{
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
static QMutex sMemberMutex( QMutex::Recursive );
#else
static QRecursiveMutex sMemberMutex;
#endif
QMutexLocker lock( &sMemberMutex );
if ( !sApplicationMembers )
sApplicationMembers = new ApplicationMembers();

View File

@ -389,7 +389,11 @@ class QgsTaskRunnableWrapper : public QRunnable
QgsTaskManager::QgsTaskManager( QObject *parent )
: QObject( parent )
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
, mTaskMutex( new QMutex( QMutex::Recursive ) )
#else
, mTaskMutex( new QRecursiveMutex() )
#endif
{
}

View File

@ -597,7 +597,11 @@ class CORE_EXPORT QgsTaskManager : public QObject
bool mInitialized = false;
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
mutable QMutex *mTaskMutex;
#else
mutable QRecursiveMutex *mTaskMutex;
#endif
QMap< long, TaskInfo > mTasks;
QMap< long, QgsTaskList > mTaskDependencies;

View File

@ -144,7 +144,9 @@ void QgsTileDownloadManagerReplyWorkerObject::replyFinished()
QgsTileDownloadManager::QgsTileDownloadManager()
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
: mMutex( QMutex::Recursive )
#endif
{
}

View File

@ -281,7 +281,11 @@ class CORE_EXPORT QgsTileDownloadManager
QList<QueueEntry> mQueue;
bool mShuttingDown = false;
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
mutable QMutex mMutex;
#else
mutable QRecursiveMutex mMutex;
#endif
QThread *mWorkerThread = nullptr;
QgsTileDownloadManagerWorker *mWorker = nullptr;
Stats mStats;