summaryrefslogtreecommitdiffstats
path: root/src/dbus/qdbusconnection.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2014-12-29 11:35:36 -0200
committerThiago Macieira <thiago.macieira@intel.com>2015-09-15 02:08:36 +0000
commitc2049f67e4cfe5f09e1b033b910cb37d043a287e (patch)
tree72c9760dd86acdadbcd1261443cb923ce7ed9235 /src/dbus/qdbusconnection.cpp
parent939b7c630d3f51224eacb6596f0ea2267ca5bfe5 (diff)
Use a dedicated thread for handling incoming libdbus-1 events
Each application will have one thread dedicated for this, for all QDBusConnections. I wouldn't mind sharing such a thread with other uses in Qt, provided none of them ever block (the QProcessManager thread comes to mind, but it's going away soon). The cost associated with this change in this commit is so far rather minimal. All incoming D-Bus calls need to be handled after an event is posted anyway, to avoid deadlocking on reentering libdbus-1 functions that acquire locks still held. The cost is the one more thread running and the cost of synchronizing them when an event is posted. The benefits far outweigh that cost: no longer will we have problems of QtDBus failing to run if the main system or session connections are used before QCoreApplication is run. Moreover, events can be received and handled in aux threads even if the main thread is blocked on some operation. Note: this commit may not be testable (tst_qdbusconnection may fail) Task-number: QTBUG-43585 Change-Id: Ic5d393bfd36e48a193fcffff13b737556ccd11a8 Reviewed-by: Albert Astals Cid <aacid@kde.org> Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
Diffstat (limited to 'src/dbus/qdbusconnection.cpp')
-rw-r--r--src/dbus/qdbusconnection.cpp58
1 files changed, 33 insertions, 25 deletions
diff --git a/src/dbus/qdbusconnection.cpp b/src/dbus/qdbusconnection.cpp
index 08a21c5d02..47255992ae 100644
--- a/src/dbus/qdbusconnection.cpp
+++ b/src/dbus/qdbusconnection.cpp
@@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
+** Copyright (C) 2015 Intel Corporation.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtDBus module of the Qt Toolkit.
@@ -77,17 +78,16 @@ void QDBusConnectionManager::removeConnection(const QString &name)
// ### Output a warning if connections are being used after they have been removed.
}
+QDBusConnectionManager::QDBusConnectionManager()
+{
+ moveToThread(Q_NULLPTR); // we don't handle events
+ start();
+}
+
QDBusConnectionManager::~QDBusConnectionManager()
{
- for (QHash<QString, QDBusConnectionPrivate *>::const_iterator it = connectionHash.constBegin();
- it != connectionHash.constEnd(); ++it) {
- QDBusConnectionPrivate *d = it.value();
- if (!d->ref.deref())
- d->deleteYourself();
- else
- d->closeConnection();
- }
- connectionHash.clear();
+ quit();
+ wait();
}
QDBusConnectionManager* QDBusConnectionManager::instance()
@@ -106,6 +106,25 @@ void QDBusConnectionManager::setConnection(const QString &name, QDBusConnectionP
c->name = name;
}
+void QDBusConnectionManager::run()
+{
+ exec();
+
+ // cleanup:
+ QMutexLocker locker(&mutex);
+ for (QHash<QString, QDBusConnectionPrivate *>::const_iterator it = connectionHash.constBegin();
+ it != connectionHash.constEnd(); ++it) {
+ QDBusConnectionPrivate *d = it.value();
+ if (!d->ref.deref()) {
+ delete d;
+ } else {
+ d->closeConnection();
+ d->moveToThread(Q_NULLPTR); // allow it to be deleted in another thread
+ }
+ }
+ connectionHash.clear();
+}
+
/*!
\class QDBusConnection
\inmodule QtDBus
@@ -295,8 +314,6 @@ QDBusConnection &QDBusConnection::operator=(const QDBusConnection &other)
*/
QDBusConnection QDBusConnection::connectToBus(BusType type, const QString &name)
{
-// Q_ASSERT_X(QCoreApplication::instance(), "QDBusConnection::addConnection",
-// "Cannot create connection without a Q[Core]Application instance");
if (!qdbus_loadLibDBus()) {
QDBusConnectionPrivate *d = 0;
return QDBusConnection(d);
@@ -331,6 +348,7 @@ QDBusConnection QDBusConnection::connectToBus(BusType type, const QString &name)
// create the bus service
// will lock in QDBusConnectionPrivate::connectRelay()
d->setBusService(retval);
+ d->moveToThread(_q_manager());
return retval;
}
@@ -342,8 +360,6 @@ QDBusConnection QDBusConnection::connectToBus(BusType type, const QString &name)
QDBusConnection QDBusConnection::connectToBus(const QString &address,
const QString &name)
{
-// Q_ASSERT_X(QCoreApplication::instance(), "QDBusConnection::addConnection",
-// "Cannot create connection without a Q[Core]Application instance");
if (!qdbus_loadLibDBus()) {
QDBusConnectionPrivate *d = 0;
return QDBusConnection(d);
@@ -373,6 +389,7 @@ QDBusConnection QDBusConnection::connectToBus(const QString &address,
// create the bus service
// will lock in QDBusConnectionPrivate::connectRelay()
d->setBusService(retval);
+ d->moveToThread(_q_manager());
return retval;
}
@@ -385,8 +402,6 @@ QDBusConnection QDBusConnection::connectToBus(const QString &address,
QDBusConnection QDBusConnection::connectToPeer(const QString &address,
const QString &name)
{
-// Q_ASSERT_X(QCoreApplication::instance(), "QDBusConnection::addConnection",
-// "Cannot create connection without a Q[Core]Application instance");
if (!qdbus_loadLibDBus()) {
QDBusConnectionPrivate *d = 0;
return QDBusConnection(d);
@@ -405,6 +420,7 @@ QDBusConnection QDBusConnection::connectToPeer(const QString &address,
d->setPeer(c, error);
_q_manager()->setConnection(name, d);
+ d->moveToThread(_q_manager());
QDBusConnection retval(d);
@@ -1061,16 +1077,7 @@ class QDBusDefaultConnection: public QDBusConnection
public:
inline QDBusDefaultConnection(BusType type, const char *name)
: QDBusConnection(connectToBus(type, QString::fromLatin1(name))), ownName(name)
- {
- // make sure this connection is running on the main thread
- QCoreApplication *instance = QCoreApplication::instance();
- if (!instance) {
- qWarning("QDBusConnection: %s D-Bus connection created before QCoreApplication. Application may misbehave.",
- type == SessionBus ? "session" : type == SystemBus ? "system" : "generic");
- } else if (QDBusConnectionPrivate::d(*this)) {
- QDBusConnectionPrivate::d(*this)->moveToThread(instance->thread());
- }
- }
+ { }
inline ~QDBusDefaultConnection()
{ disconnectFromBus(QString::fromLatin1(ownName)); }
@@ -1125,6 +1132,7 @@ QDBusConnection QDBusConnection::sender()
*/
void QDBusConnectionPrivate::setBusService(const QDBusConnection &connection)
{
+ Q_ASSERT(mode == ClientMode);
busService = new QDBusConnectionInterface(connection, this);
ref.deref(); // busService has increased the refcounting to us
// avoid cyclic refcounting