Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Avoid thread violation warnings spamming console by limiting to one w…
…arning per code location
  • Loading branch information
nyalldawson committed Apr 27, 2023
1 parent f21d7cc commit 85074b1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -532,6 +532,7 @@ set(QGIS_CORE_SRCS
qgstemporalrangeobject.cpp
qgstemporalutils.cpp
qgstessellator.cpp
qgsthreadingutils.cpp
qgstilecache.cpp
qgstiledownloadmanager.cpp
qgstiles.cpp
Expand Down
19 changes: 19 additions & 0 deletions src/core/qgsthreadingutils.cpp
@@ -0,0 +1,19 @@
/***************************************************************************
qgsthreadingutils.cpp
--------------------------------------
Date : April 2023
Copyright : (C) 2023 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* 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 "qgsthreadingutils.h"

QSet< QString > QgsThreadingUtils::sEmittedWarnings;
QMutex QgsThreadingUtils::sEmittedWarningMutex;
7 changes: 6 additions & 1 deletion src/core/qgsthreadingutils.h
Expand Up @@ -26,6 +26,7 @@
#include <QThread>
#if defined(QGISDEBUG) || defined(AGGRESSIVE_SAFE_MODE)
#include <QDebug>
#include <QMutex>
#endif
#include <QSemaphore>
#include <QCoreApplication>
Expand All @@ -46,7 +47,7 @@
#ifdef __clang_analyzer__
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS_NON_FATAL do {} while(false);
#elif defined(QGISDEBUG)
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS_NON_FATAL if ( QThread::currentThread() != thread() ) {qWarning() << QStringLiteral("%2 (%1:%3) is run from a different thread than the object %4 lives in [0x%5 vs 0x%6]" ).arg( QString( __FILE__ ), QString( __FUNCTION__ ), QString::number( __LINE__ ), objectName() ).arg( reinterpret_cast< qint64 >( QThread::currentThread() ), 0, 16 ).arg( reinterpret_cast< qint64 >( thread() ), 0, 16 ).toLocal8Bit().constData(); }
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS_NON_FATAL if ( QThread::currentThread() != thread() ) { const QString location = QStringLiteral("%1 (%2:%3)").arg( QString( __FUNCTION__ ) ,QString( __FILE__ ), QString::number( __LINE__ ) ); QgsThreadingUtils::sEmittedWarningMutex.lock(); if ( !QgsThreadingUtils::sEmittedWarnings.contains( location ) ) { qWarning() << QStringLiteral("%1 is run from a different thread than the object %2 lives in [0x%3 vs 0x%4]" ).arg( location, objectName() ).arg( reinterpret_cast< qint64 >( QThread::currentThread() ), 0, 16 ).arg( reinterpret_cast< qint64 >( thread() ), 0, 16 ).toLocal8Bit().constData(); QgsThreadingUtils::sEmittedWarnings.insert( location ); } QgsThreadingUtils::sEmittedWarningMutex.unlock(); }
#else
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS_NON_FATAL do {} while(false);
#endif
Expand Down Expand Up @@ -153,6 +154,10 @@ class CORE_EXPORT QgsThreadingUtils
return true;
}
}
#if defined(QGISDEBUG)
static QSet< QString > sEmittedWarnings;
static QMutex sEmittedWarningMutex;
#endif

};

Expand Down

0 comments on commit 85074b1

Please sign in to comment.