Blob Blame History Raw
From ca1ba9162d28bd07c39238bbd0c891e54a46e4be Mon Sep 17 00:00:00 2001
From: Thiago Macieira <thiago.macieira@intel.com>
Date: Thu, 28 Apr 2016 15:00:58 -0700
Subject: [PATCH 1/2] Merge the QDBusMetaType's custom information to
 QDBusConnectionManager

This allows us to get rid of two Q_GLOBAL_STATIC in QtDBus, which means
fewer opportunities for screwing up the order of destruction. And since
QDBusConnectionManager now ensures that the types are initialized, we
don't need to re-initialize them everywhere.

The Q_GLOBAL_STATIC for QDBusConnectionManager ensures the proper
thread-safe locking, so we don't need to lock for every type that we're
trying to register. This should make things faster.

But as a side-effect, trying to register a D-Bus metatype will cause the
QDBusConnectionManager thread to start too.

Change-Id: Ifea6e497f11a461db432ffff1449a4e535234485
---
 src/dbus/qdbusconnection.cpp        |   1 +
 src/dbus/qdbusconnectionmanager_p.h |   3 +-
 src/dbus/qdbusintegrator.cpp        |   1 -
 src/dbus/qdbusmetatype.cpp          | 182 +++++++++++++++++++-----------------
 src/dbus/qdbusmetatype_p.h          |  27 +++++-
 5 files changed, 124 insertions(+), 90 deletions(-)

diff --git a/src/dbus/qdbusconnection.cpp b/src/dbus/qdbusconnection.cpp
index 34b3da7..4db6396 100644
--- a/src/dbus/qdbusconnection.cpp
+++ b/src/dbus/qdbusconnection.cpp
@@ -193,6 +193,7 @@ void QDBusConnectionManager::run()
         }
     }
     connectionHash.clear();
+    customTypes.clear();
 
     // allow deletion from any thread without warning
     moveToThread(Q_NULLPTR);
diff --git a/src/dbus/qdbusconnectionmanager_p.h b/src/dbus/qdbusconnectionmanager_p.h
index c0ab48e..97ecc74 100644
--- a/src/dbus/qdbusconnectionmanager_p.h
+++ b/src/dbus/qdbusconnectionmanager_p.h
@@ -48,13 +48,14 @@
 #define QDBUSCONNECTIONMANAGER_P_H
 
 #include "qdbusconnection_p.h"
+#include "qdbusmetatype_p.h"
 #include "private/qthread_p.h"
 
 #ifndef QT_NO_DBUS
 
 QT_BEGIN_NAMESPACE
 
-class QDBusConnectionManager : public QDaemonThread
+class QDBusConnectionManager : public QDaemonThread, public QDBusMetaTypeId
 {
     Q_OBJECT
     struct ConnectionRequestData;
diff --git a/src/dbus/qdbusintegrator.cpp b/src/dbus/qdbusintegrator.cpp
index 3be775d..37d2ae1 100644
--- a/src/dbus/qdbusintegrator.cpp
+++ b/src/dbus/qdbusintegrator.cpp
@@ -1019,7 +1019,6 @@ QDBusConnectionPrivate::QDBusConnectionPrivate(QObject *p)
         qdbusThreadDebug = qdbusDefaultThreadDebug;
 #endif
 
-    QDBusMetaTypeId::init();
     connect(this, &QDBusConnectionPrivate::dispatchStatusChanged,
             this, &QDBusConnectionPrivate::doDispatch, Qt::QueuedConnection);
     connect(this, &QDBusConnectionPrivate::spyHooksFinished,
diff --git a/src/dbus/qdbusmetatype.cpp b/src/dbus/qdbusmetatype.cpp
index 2aaf0e2..1774b85 100644
--- a/src/dbus/qdbusmetatype.cpp
+++ b/src/dbus/qdbusmetatype.cpp
@@ -1,6 +1,7 @@
 /****************************************************************************
 **
 ** Copyright (C) 2015 The Qt Company Ltd.
+** Copyright (C) 2016 Intel Corporation.
 ** Contact: http://www.qt.io/licensing/
 **
 ** This file is part of the QtDBus module of the Qt Toolkit.
@@ -33,19 +34,15 @@
 
 #include "qdbusmetatype.h"
 #include "qdbusmetatype_p.h"
-
-#include <string.h>
 #include "qdbus_symbols_p.h"
 
-#include <qbytearray.h>
-#include <qglobal.h>
-#include <qreadwritelock.h>
-#include <qvector.h>
+#include <string.h>
 
 #include "qdbusargument_p.h"
 #include "qdbusutil_p.h"
 #include "qdbusunixfiledescriptor.h"
 #ifndef QT_BOOTSTRAPPED
+#include "qdbusconnectionmanager_p.h"
 #include "qdbusmessage.h"
 #endif
 
@@ -58,82 +55,72 @@
 
 QT_BEGIN_NAMESPACE
 
-class QDBusCustomTypeInfo
-{
-public:
-    QDBusCustomTypeInfo() : signature(), marshall(0), demarshall(0)
-    { }
-
-    // Suggestion:
-    // change 'signature' to char* and make QDBusCustomTypeInfo a Movable type
-    QByteArray signature;
-    QDBusMetaType::MarshallFunction marshall;
-    QDBusMetaType::DemarshallFunction demarshall;
-};
+static void registerMarshallOperatorsNoLock(QVector<QDBusCustomTypeInfo> &ct, int id,
+                                            QDBusMetaType::MarshallFunction mf,
+                                            QDBusMetaType::DemarshallFunction df);
 
 template<typename T>
-inline static void registerHelper(T * = 0)
+inline static void registerHelper(QVector<QDBusCustomTypeInfo> &ct)
 {
     void (*mf)(QDBusArgument &, const T *) = qDBusMarshallHelper<T>;
     void (*df)(const QDBusArgument &, T *) = qDBusDemarshallHelper<T>;
-    QDBusMetaType::registerMarshallOperators(qMetaTypeId<T>(),
+    registerMarshallOperatorsNoLock(ct, qMetaTypeId<T>(),
         reinterpret_cast<QDBusMetaType::MarshallFunction>(mf),
         reinterpret_cast<QDBusMetaType::DemarshallFunction>(df));
 }
 
-void QDBusMetaTypeId::init()
+QDBusMetaTypeId *QDBusMetaTypeId::instance()
+{
+#ifdef QT_BOOTSTRAPPED
+    static QDBusMetaTypeId self;
+    return &self;
+#else
+    return QDBusConnectionManager::instance();
+#endif
+}
+
+QDBusMetaTypeId::QDBusMetaTypeId()
 {
-    static QBasicAtomicInt initialized = Q_BASIC_ATOMIC_INITIALIZER(false);
-
-    // reentrancy is not a problem since everything else is locked on their own
-    // set the guard variable at the end
-    if (!initialized.load()) {
-        // register our types with Qt Core (calling qMetaTypeId<T>() does this implicitly)
-        (void)message();
-        (void)argument();
-        (void)variant();
-        (void)objectpath();
-        (void)signature();
-        (void)error();
-        (void)unixfd();
+    // register our types with Qt Core (calling qMetaTypeId<T>() does this implicitly)
+    (void)message();
+    (void)argument();
+    (void)variant();
+    (void)objectpath();
+    (void)signature();
+    (void)error();
+    (void)unixfd();
 
 #ifndef QDBUS_NO_SPECIALTYPES
-        // and register Qt Core's with us
-        registerHelper<QDate>();
-        registerHelper<QTime>();
-        registerHelper<QDateTime>();
-        registerHelper<QRect>();
-        registerHelper<QRectF>();
-        registerHelper<QSize>();
-        registerHelper<QSizeF>();
-        registerHelper<QPoint>();
-        registerHelper<QPointF>();
-        registerHelper<QLine>();
-        registerHelper<QLineF>();
-        registerHelper<QVariantList>();
-        registerHelper<QVariantMap>();
-        registerHelper<QVariantHash>();
-
-        qDBusRegisterMetaType<QList<bool> >();
-        qDBusRegisterMetaType<QList<short> >();
-        qDBusRegisterMetaType<QList<ushort> >();
-        qDBusRegisterMetaType<QList<int> >();
-        qDBusRegisterMetaType<QList<uint> >();
-        qDBusRegisterMetaType<QList<qlonglong> >();
-        qDBusRegisterMetaType<QList<qulonglong> >();
-        qDBusRegisterMetaType<QList<double> >();
-        qDBusRegisterMetaType<QList<QDBusObjectPath> >();
-        qDBusRegisterMetaType<QList<QDBusSignature> >();
-        qDBusRegisterMetaType<QList<QDBusUnixFileDescriptor> >();
+    // and register Qt Core's with us
+    registerHelper<QDate>(customTypes);
+    registerHelper<QTime>(customTypes);
+    registerHelper<QDateTime>(customTypes);
+    registerHelper<QRect>(customTypes);
+    registerHelper<QRectF>(customTypes);
+    registerHelper<QSize>(customTypes);
+    registerHelper<QSizeF>(customTypes);
+    registerHelper<QPoint>(customTypes);
+    registerHelper<QPointF>(customTypes);
+    registerHelper<QLine>(customTypes);
+    registerHelper<QLineF>(customTypes);
+    registerHelper<QVariantList>(customTypes);
+    registerHelper<QVariantMap>(customTypes);
+    registerHelper<QVariantHash>(customTypes);
+
+    registerHelper<QList<bool> >(customTypes);
+    registerHelper<QList<short> >(customTypes);
+    registerHelper<QList<ushort> >(customTypes);
+    registerHelper<QList<int> >(customTypes);
+    registerHelper<QList<uint> >(customTypes);
+    registerHelper<QList<qlonglong> >(customTypes);
+    registerHelper<QList<qulonglong> >(customTypes);
+    registerHelper<QList<double> >(customTypes);
+    registerHelper<QList<QDBusObjectPath> >(customTypes);
+    registerHelper<QList<QDBusSignature> >(customTypes);
+    registerHelper<QList<QDBusUnixFileDescriptor> >(customTypes);
 #endif
-
-        initialized.store(true);
-    }
 }
 
-Q_GLOBAL_STATIC(QVector<QDBusCustomTypeInfo>, customTypes)
-Q_GLOBAL_STATIC(QReadWriteLock, customTypesLock)
-
 /*!
     \class QDBusMetaType
     \inmodule QtDBus
@@ -203,14 +190,22 @@ void QDBusMetaType::registerMarshallOperators(int id, MarshallFunction mf,
                                               DemarshallFunction df)
 {
     QByteArray var;
-    QVector<QDBusCustomTypeInfo> *ct = customTypes();
-    if (id < 0 || !mf || !df || !ct)
+    QDBusMetaTypeId *mgr = QDBusMetaTypeId::instance();
+    if (id < 0 || !mf || !df || !mgr)
         return;                 // error!
 
-    QWriteLocker locker(customTypesLock());
-    if (id >= ct->size())
-        ct->resize(id + 1);
-    QDBusCustomTypeInfo &info = (*ct)[id];
+    QWriteLocker locker(&mgr->customTypesLock);
+    QVector<QDBusCustomTypeInfo> &ct = mgr->customTypes;
+    registerMarshallOperatorsNoLock(ct, id, mf, df);
+}
+
+static void registerMarshallOperatorsNoLock(QVector<QDBusCustomTypeInfo> &ct, int id,
+                                            QDBusMetaType::MarshallFunction mf,
+                                            QDBusMetaType::DemarshallFunction df)
+{
+    if (id >= ct.size())
+        ct.resize(id + 1);
+    QDBusCustomTypeInfo &info = ct[id];
     info.marshall = mf;
     info.demarshall = df;
 }
@@ -227,12 +222,16 @@ bool QDBusMetaType::marshall(QDBusArgument &arg, int id, const void *data)
 
     MarshallFunction mf;
     {
-        QReadLocker locker(customTypesLock());
-        QVector<QDBusCustomTypeInfo> *ct = customTypes();
-        if (id >= ct->size())
+        const QDBusMetaTypeId *mgr = QDBusMetaTypeId::instance();
+        if (!mgr)
+            return false;       // shutting down
+
+        QReadLocker locker(&mgr->customTypesLock);
+        const QVector<QDBusCustomTypeInfo> &ct = mgr->customTypes;
+        if (id >= ct.size())
             return false;       // non-existent
 
-        const QDBusCustomTypeInfo &info = (*ct).at(id);
+        const QDBusCustomTypeInfo &info = ct.at(id);
         if (!info.marshall) {
             mf = 0;             // make gcc happy
             return false;
@@ -256,12 +255,16 @@ bool QDBusMetaType::demarshall(const QDBusArgument &arg, int id, void *data)
 
     DemarshallFunction df;
     {
-        QReadLocker locker(customTypesLock());
-        QVector<QDBusCustomTypeInfo> *ct = customTypes();
-        if (id >= ct->size())
+        const QDBusMetaTypeId *mgr = QDBusMetaTypeId::instance();
+        if (!mgr)
+            return false;       // shutting down
+
+        QReadLocker locker(&mgr->customTypesLock);
+        const QVector<QDBusCustomTypeInfo> &ct = mgr->customTypes;
+        if (id >= ct.size())
             return false;       // non-existent
 
-        const QDBusCustomTypeInfo &info = (*ct).at(id);
+        const QDBusCustomTypeInfo &info = ct.at(id);
         if (!info.demarshall) {
             df = 0;             // make gcc happy
             return false;
@@ -431,13 +434,17 @@ const char *QDBusMetaType::typeToSignature(int type)
         return DBUS_TYPE_UNIX_FD_AS_STRING;
 
     // try the database
-    QVector<QDBusCustomTypeInfo> *ct = customTypes();
+    QDBusMetaTypeId *mgr = QDBusMetaTypeId::instance();
+    if (!mgr)
+        return Q_NULLPTR;       // shutting down
+
     {
-        QReadLocker locker(customTypesLock());
-        if (type >= ct->size())
+        QReadLocker locker(&mgr->customTypesLock);
+        const QVector<QDBusCustomTypeInfo> &ct = mgr->customTypes;
+        if (type >= ct.size())
             return 0;           // type not registered with us
 
-        const QDBusCustomTypeInfo &info = (*ct).at(type);
+        const QDBusCustomTypeInfo &info = ct.at(type);
 
         if (!info.signature.isNull())
             return info.signature;
@@ -454,8 +461,9 @@ const char *QDBusMetaType::typeToSignature(int type)
         QByteArray signature = QDBusArgumentPrivate::createSignature(type);
 
         // re-acquire lock
-        QWriteLocker locker(customTypesLock());
-        info = &(*ct)[type];
+        QWriteLocker locker(&mgr->customTypesLock);
+        QVector<QDBusCustomTypeInfo> &ct = mgr->customTypes;
+        info = &ct[type];
         info->signature = signature;
     }
     return info->signature;
diff --git a/src/dbus/qdbusmetatype_p.h b/src/dbus/qdbusmetatype_p.h
index 348fc6c..24c8326 100644
--- a/src/dbus/qdbusmetatype_p.h
+++ b/src/dbus/qdbusmetatype_p.h
@@ -1,6 +1,7 @@
 /****************************************************************************
 **
 ** Copyright (C) 2015 The Qt Company Ltd.
+** Copyright (C) 2016 Intel Corporation.
 ** Contact: http://www.qt.io/licensing/
 **
 ** This file is part of the QtDBus module of the Qt Toolkit.
@@ -53,10 +54,27 @@
 #include <qdbuserror.h>
 #include <qdbusunixfiledescriptor.h>
 
+#include <qbytearray.h>
+#include <qreadwritelock.h>
+#include <qvector.h>
+
 #ifndef QT_NO_DBUS
 
 QT_BEGIN_NAMESPACE
 
+class QDBusCustomTypeInfo
+{
+public:
+    QDBusCustomTypeInfo() : signature(), marshall(0), demarshall(0)
+    { }
+
+    // Suggestion:
+    // change 'signature' to char* and make QDBusCustomTypeInfo a Movable type
+    QByteArray signature;
+    QDBusMetaType::MarshallFunction marshall;
+    QDBusMetaType::DemarshallFunction demarshall;
+};
+
 struct QDBusMetaTypeId
 {
     static int message();         // QDBusMessage
@@ -67,7 +85,14 @@ struct QDBusMetaTypeId
     static int error();           // QDBusError
     static int unixfd();          // QDBusUnixFileDescriptor
 
-    static void init();
+    static void init() { instance(); }
+    static QDBusMetaTypeId *instance();
+
+    mutable QReadWriteLock customTypesLock;
+    QVector<QDBusCustomTypeInfo> customTypes;
+
+protected:
+    QDBusMetaTypeId();
 };
 
 inline int QDBusMetaTypeId::message()
-- 
2.9.3