Skip to content

Commit

Permalink
Add mutex locker when accessing to attribute member
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 committed Aug 16, 2019
1 parent 883f365 commit 35c2658
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -366,8 +366,15 @@ QgsPostgresConn::~QgsPostgresConn()
mConn = nullptr;
}

void QgsPostgresConn::ref()
{
QMutexLocker locker( &mLock );
++mRef;
}

void QgsPostgresConn::unref()
{
QMutexLocker locker( &mLock );
if ( --mRef > 0 )
return;

Expand All @@ -381,6 +388,8 @@ void QgsPostgresConn::unref()
connections.remove( key );
}

// to avoid destroying locked mutex
locker.unlock();
delete this;
}

Expand Down Expand Up @@ -424,6 +433,7 @@ void QgsPostgresConn::addColumnInfo( QgsPostgresLayerProperty &layerProperty, co

bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchPublicOnly, bool allowGeometrylessTables, const QString &schema )
{
QMutexLocker locker( &mLock );
int nColumns = 0;
int foundInTables = 0;
QgsPostgresResult result;
Expand Down Expand Up @@ -800,6 +810,8 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP

bool QgsPostgresConn::supportedLayers( QVector<QgsPostgresLayerProperty> &layers, bool searchGeometryColumnsOnly, bool searchPublicOnly, bool allowGeometrylessTables, const QString &schema )
{
QMutexLocker locker( &mLock );

// Get the list of supported tables
if ( !getTableInfo( searchGeometryColumnsOnly, searchPublicOnly, allowGeometrylessTables, schema ) )
{
Expand Down Expand Up @@ -872,6 +884,7 @@ bool QgsPostgresConn::hasPointcloud()
/* Functions for determining available features in postGIS */
QString QgsPostgresConn::postgisVersion()
{
QMutexLocker locker( &mLock );
if ( mGotPostgisVersion )
return mPostgisVersionInfo;

Expand Down Expand Up @@ -1276,12 +1289,14 @@ QString QgsPostgresConn::PQerrorMessage() const

int QgsPostgresConn::PQsendQuery( const QString &query )
{
QMutexLocker locker( &mLock );
Q_ASSERT( mConn );
return ::PQsendQuery( mConn, query.toUtf8() );
}

bool QgsPostgresConn::begin()
{
QMutexLocker locker( &mLock );
if ( mTransaction )
{
return PQexecNR( QStringLiteral( "SAVEPOINT transaction_savepoint" ) );
Expand All @@ -1294,6 +1309,7 @@ bool QgsPostgresConn::begin()

bool QgsPostgresConn::commit()
{
QMutexLocker locker( &mLock );
if ( mTransaction )
{
return PQexecNR( QStringLiteral( "RELEASE SAVEPOINT transaction_savepoint" ) );
Expand All @@ -1306,6 +1322,7 @@ bool QgsPostgresConn::commit()

bool QgsPostgresConn::rollback()
{
QMutexLocker locker( &mLock );
if ( mTransaction )
{
return PQexecNR( QStringLiteral( "ROLLBACK TO SAVEPOINT transaction_savepoint" ) )
Expand All @@ -1319,6 +1336,7 @@ bool QgsPostgresConn::rollback()

qint64 QgsPostgresConn::getBinaryInt( QgsPostgresResult &queryResult, int row, int col )
{
QMutexLocker locker( &mLock );
quint64 oid;
char *p = PQgetvalue( queryResult.result(), row, col );
size_t s = PQgetlength( queryResult.result(), row, col );
Expand Down Expand Up @@ -1438,6 +1456,7 @@ QString QgsPostgresConn::fieldExpression( const QgsField &fld, QString expr )

void QgsPostgresConn::deduceEndian()
{
QMutexLocker locker( &mLock );
// need to store the PostgreSQL endian format used in binary cursors
// since it appears that starting with
// version 7.4, binary cursors return data in XDR whereas previous versions
Expand Down Expand Up @@ -1911,6 +1930,7 @@ void QgsPostgresConn::deleteConnection( const QString &connName )

bool QgsPostgresConn::cancel()
{
QMutexLocker locker( &mLock );
PGcancel *c = ::PQgetCancel( mConn );
if ( !c )
{
Expand All @@ -1931,6 +1951,7 @@ bool QgsPostgresConn::cancel()

QString QgsPostgresConn::currentDatabase() const
{
QMutexLocker locker( &mLock );
QString database;
QString sql = "SELECT current_database()";
QgsPostgresResult res( PQexec( sql ) );
Expand Down
2 changes: 1 addition & 1 deletion src/providers/postgres/qgspostgresconn.h
Expand Up @@ -193,7 +193,7 @@ class QgsPostgresConn : public QObject
*/
static QgsPostgresConn *connectDb( const QString &connInfo, bool readOnly, bool shared = true, bool transaction = false );

void ref() { ++mRef; }
void ref();
void unref();

//! Gets postgis version string
Expand Down

0 comments on commit 35c2658

Please sign in to comment.