Add QGIS_PROTECT_QOBJECT_THREAD_ACCESS macro

Either shows a warning when a qobject function is accessed
from a thread which the object doesn't have affinity with,
OR causes a fatal error if the AGGRESSIVE_SAFE_MODE cmake flag
is set.

We should ensure this macro is present for all qobject methods
which may potentially be called from threads
This commit is contained in:
Nyall Dawson 2022-12-13 10:34:45 +10:00
parent 6a54c9a554
commit 6572c9bd46
4 changed files with 448 additions and 5 deletions

View File

@ -692,6 +692,8 @@ else()
set(QGISDEBUG FALSE)
endif()
set (AGGRESSIVE_SAFE_MODE FALSE CACHE BOOL "Forces a aggressive safe mode where issues like unsafe thread access will resort in fatal exceptions")
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8 /std:c++17")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /utf-8 /std:c++17")

View File

@ -90,6 +90,8 @@
#cmakedefine QGISDEBUG
#cmakedefine AGGRESSIVE_SAFE_MODE
#cmakedefine HAVE_QUICK
#cmakedefine HAVE_QT5SERIALPORT

View File

@ -19,6 +19,7 @@
#define SIP_NO_FILE
#include "qgis_core.h"
#include "qgsconfig.h"
#include "qgsfeedback.h"
@ -26,6 +27,15 @@
#include <QSemaphore>
#include <memory>
#ifdef AGGRESSIVE_SAFE_MODE
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS if ( QThread::currentThread() != thread() ) {qFatal( "%s", 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() ); }
#elif defined(QGISDEBUG)
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS 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(); }
#else
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS do {} while(false);
#endif
/**
* \ingroup core
* \brief Provides threading utilities for QGIS.

File diff suppressed because it is too large Load Diff