diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-11-26 16:54:26 +0100 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-12-08 08:48:50 +0100 |
commit | 59de6f6e6ed1d556d3f2d1bd9804fd237484d840 (patch) | |
tree | 5cffe4ce44c3dd8c6c29291b71951df9b53591f7 /sources | |
parent | b10dd95b7c48845ae49244ab8a264ab0f6192368 (diff) |
PySide6: Move the mutex for locking QObject allocation helpers to libpyside
Qml and Quick were using 2 different mutexes, which does not make
sense.
Task-number: PYSIDE-1709
Change-Id: Id0ec0f780c1d24e40a7f072dea62964ecf92e9d2
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources')
5 files changed, 16 insertions, 11 deletions
diff --git a/sources/pyside6/PySide6/QtQuick/pysidequickregistertype.cpp b/sources/pyside6/PySide6/QtQuick/pysidequickregistertype.cpp index 3ccbe69a4..edca30620 100644 --- a/sources/pyside6/PySide6/QtQuick/pysidequickregistertype.cpp +++ b/sources/pyside6/PySide6/QtQuick/pysidequickregistertype.cpp @@ -51,18 +51,15 @@ #include <QtCore/QMutex> -// Mutex used to avoid race condition on PySide::nextQObjectMemoryAddr. -static QMutex nextQmlElementMutex; - static void createQuickItem(void *memory, void *type) { - QMutexLocker locker(&nextQmlElementMutex); + QMutexLocker locker(&PySide::nextQObjectMemoryAddrMutex()); PySide::setNextQObjectMemoryAddr(memory); Shiboken::GilState state; PyObject *obj = PyObject_CallObject(reinterpret_cast<PyObject *>(type), 0); if (!obj || PyErr_Occurred()) PyErr_Print(); - PySide::setNextQObjectMemoryAddr(0); + PySide::setNextQObjectMemoryAddr(nullptr); } bool pyTypeObjectInheritsFromClass(PyTypeObject *pyObjType, QByteArray className) diff --git a/sources/pyside6/libpyside/pyside.cpp b/sources/pyside6/libpyside/pyside.cpp index 05c0e7486..b2bf0ed69 100644 --- a/sources/pyside6/libpyside/pyside.cpp +++ b/sources/pyside6/libpyside/pyside.cpp @@ -71,6 +71,7 @@ #include <QtCore/QCoreApplication> #include <QtCore/QDir> #include <QtCore/QFileInfo> +#include <QtCore/QMutex> #include <QtCore/QSharedPointer> #include <QtCore/QStack> #include <QtCore/QThread> @@ -387,6 +388,12 @@ bool inherits(PyTypeObject *objType, const char *class_name) return inherits(base, class_name); } +QMutex &nextQObjectMemoryAddrMutex() +{ + static QMutex mutex; + return mutex; +} + void *nextQObjectMemoryAddr() { return qobjectNextAddr; diff --git a/sources/pyside6/libpyside/pysideqobject.h b/sources/pyside6/libpyside/pysideqobject.h index 19e603bb7..538f6f024 100644 --- a/sources/pyside6/libpyside/pysideqobject.h +++ b/sources/pyside6/libpyside/pysideqobject.h @@ -48,6 +48,7 @@ QT_FORWARD_DECLARE_CLASS(QObject) QT_FORWARD_DECLARE_STRUCT(QMetaObject) +QT_FORWARD_DECLARE_CLASS(QMutex) namespace PySide { @@ -83,7 +84,10 @@ PYSIDE_API QObject *convertToQObject(PyObject *object, bool raiseError); /// attribute related with name PYSIDE_API PyObject *getMetaDataFromQObject(QObject *cppSelf, PyObject *self, PyObject *name); +/// Mutex for accessing QObject memory helpers from multiple threads +PYSIDE_API QMutex &nextQObjectMemoryAddrMutex(); PYSIDE_API void *nextQObjectMemoryAddr(); +/// Set the address where to allocate the next QObject (for QML) PYSIDE_API void setNextQObjectMemoryAddr(void *addr); PYSIDE_API PyObject *getWrapperForQObject(QObject *cppSelf, PyTypeObject *sbk_type); diff --git a/sources/pyside6/libpysideqml/pysideqmlregistertype.cpp b/sources/pyside6/libpysideqml/pysideqmlregistertype.cpp index cbacd5887..5f5d5fff8 100644 --- a/sources/pyside6/libpysideqml/pysideqmlregistertype.cpp +++ b/sources/pyside6/libpysideqml/pysideqmlregistertype.cpp @@ -59,20 +59,17 @@ #include <QtQml/QJSValue> #include <QtQml/QQmlListProperty> -// Mutex used to avoid race condition on PySide::nextQObjectMemoryAddr. -static QMutex nextQmlElementMutex; - static PySide::Qml::QuickRegisterItemFunction quickRegisterItemFunction = nullptr; static void createInto(void *memory, void *type) { - QMutexLocker locker(&nextQmlElementMutex); + QMutexLocker locker(&PySide::nextQObjectMemoryAddrMutex()); PySide::setNextQObjectMemoryAddr(memory); Shiboken::GilState state; PyObject *obj = PyObject_CallObject(reinterpret_cast<PyObject *>(type), 0); if (!obj || PyErr_Occurred()) PyErr_Print(); - PySide::setNextQObjectMemoryAddr(0); + PySide::setNextQObjectMemoryAddr(nullptr); } PyTypeObject *qObjectType() diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index e508b5855..f93024d55 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -3679,7 +3679,7 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr Indentation indent(uva); uva << "cptr = new (addr) ::" << ctorCall << ";\n" - << "PySide::setNextQObjectMemoryAddr(0);" + << "PySide::setNextQObjectMemoryAddr(nullptr);" << '\n'; } uva << "} else {\n"; |