From fe1fec5e5f0799e03f6a3473dc56511bc00f26eb Mon Sep 17 00:00:00 2001 From: Marcus Eggenberger Date: Fri, 28 Dec 2007 17:18:28 +0000 Subject: [PATCH] Well this is my deferred christmas present: signalproxy should be threadsafe now! YATTAAA! :) --- src/common/signalproxy.cpp | 59 ++++++++++++++++++++++++++++---------- src/common/signalproxy.h | 18 +++++++++--- 2 files changed, 58 insertions(+), 19 deletions(-) diff --git a/src/common/signalproxy.cpp b/src/common/signalproxy.cpp index c30fe4f1..a6e16faa 100644 --- a/src/common/signalproxy.cpp +++ b/src/common/signalproxy.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include "util.h" @@ -62,7 +63,7 @@ SignalRelay::SignalRelay(SignalProxy* parent, QObject* source) caller(source), _sync(false) { - QObject::connect(source, SIGNAL(destroyed()), parent, SLOT(detachSender())); + QObject::connect(source, SIGNAL(destroyed()), parent, SLOT(detachSender()), Qt::DirectConnection); } int SignalRelay::qt_metacall(QMetaObject::Call _c, int _id, void **_a) { @@ -409,7 +410,7 @@ bool SignalProxy::attachSlot(const QByteArray& sigName, QObject* recv, const cha _attachedSlots.insert(funcName, qMakePair(recv, methodId)); QObject::disconnect(recv, SIGNAL(destroyed()), this, SLOT(detachSender())); - QObject::connect(recv, SIGNAL(destroyed()), this, SLOT(detachSender())); + QObject::connect(recv, SIGNAL(destroyed()), this, SLOT(detachSender()), Qt::DirectConnection); return true; } @@ -471,30 +472,37 @@ void SignalProxy::requestInit(QObject *obj) { } void SignalProxy::detachSender() { - // this is a slot so we can bypass the QueuedConnection - // and if someone is forcing direct connection in a multithreaded environment he's just nuts... - _detachSignals(sender()); - _detachSlots(sender()); + // this is a private slot. the reason for that is, that we enfoce Qt::DirectConnection. + // the result is, that we can be sure, that a registered receiver is removed immediately when it's destroyed. + // since we're guarding the the internal datastructures with mutexes this means, + // that the object destruction is deffered until we're finished delivering our sync/init/whatever request. + // all in all: thread safety! *sigh* + detachObject(sender()); } // detachObject/Signals/Slots() can be called as a result of an incoming call // this might destroy our the iterator used for delivery // thus we wrap the actual disconnection by using QueuedConnections void SignalProxy::detachObject(QObject* obj) { - detachSignals(obj); - detachSlots(obj); + QMutexLocker locker(&slaveMutex); + _detachSignals(obj); + _detachSlots(obj); + _stopSync(obj); } void SignalProxy::detachSignals(QObject* sender) { - QMetaObject::invokeMethod(this, "_detachSignals", - Qt::QueuedConnection, - Q_ARG(QObject*, sender)); + QMutexLocker locker(&slaveMutex); + _detachSignals(sender); } void SignalProxy::detachSlots(QObject* receiver) { - QMetaObject::invokeMethod(this, "_detachSlots", - Qt::QueuedConnection, - Q_ARG(QObject*, receiver)); + QMutexLocker locker(&slaveMutex); + _detachSlots(receiver); +} + +void SignalProxy::stopSync(QObject* obj) { + QMutexLocker locker(&slaveMutex); + _stopSync(obj); } void SignalProxy::_detachSignals(QObject* sender) { @@ -513,6 +521,22 @@ void SignalProxy::_detachSlots(QObject* receiver) { } } +void SignalProxy::_stopSync(QObject* obj) { + if(_relayHash.contains(obj)) + _relayHash[obj]->setSynchronize(false); + + // we can't use a className here, since it might be effed up, if we receive the call as a result of a decon + // gladly the objectName() is still valid. So we have only to iterate over the classes not each instance! *sigh* + QHash::iterator classIter = _syncSlave.begin(); + while(classIter != _syncSlave.end()) { + if(classIter->contains(obj->objectName())) { + classIter->remove(obj->objectName()); + break; + } + classIter++; + } +} + void SignalProxy::dispatchSignal(QIODevice *receiver, const QVariant &identifier, const QVariantList ¶ms) { QVariantList packedFunc; packedFunc << identifier; @@ -532,6 +556,11 @@ void SignalProxy::dispatchSignal(const QVariant &identifier, const QVariantList void SignalProxy::receivePeerSignal(QIODevice *sender, const QVariant &packedFunc) { QVariantList params(packedFunc.toList()); + // well yes we are locking code here and not only data. + // otherwise each handler would have to lock the data anyway. this leaves us on the safe side + // unable to forget a lock + QMutexLocker locker(&slaveMutex); + QVariant call = params.takeFirst(); if(call.type() != QVariant::Int) return handleSignal(call.toByteArray(), params); @@ -555,7 +584,7 @@ void SignalProxy::handleSync(QVariantList params) { return; } - QByteArray className =params.takeFirst().toByteArray(); + QByteArray className = params.takeFirst().toByteArray(); QString objectName = params.takeFirst().toString(); int signalId = params.takeFirst().toInt(); diff --git a/src/common/signalproxy.h b/src/common/signalproxy.h index be2c9ffd..6204573d 100644 --- a/src/common/signalproxy.h +++ b/src/common/signalproxy.h @@ -29,6 +29,8 @@ #include #include +#include + class SignalRelay; class QMetaObject; @@ -68,10 +70,11 @@ public: void setInitialized(QObject *obj); bool initialized(QObject *obj); void requestInit(QObject *obj); - + void detachObject(QObject *obj); void detachSignals(QObject *sender); void detachSlots(QObject *receiver); + void stopSync(QObject *obj); //! Writes a QVariant to a device. /** The data item is prefixed with the resulting blocksize, @@ -142,9 +145,13 @@ private: void _detachSignals(QObject *sender); void _detachSlots(QObject *receiver); + void _stopSync(QObject *obj); - void dumpSyncMap(QObject *object); + void dumpSyncMap(QObject *object); + // Hash of used QIODevices + QHash _peerByteCount; + // containg a list of argtypes for fast access QHash _classInfo; @@ -160,11 +167,14 @@ private: typedef QHash ObjectId; QHash _syncSlave; - // Hash of used QIODevices - QHash _peerByteCount; ProxyMode _proxyMode; + // the slaveMutex protects both containers: + // - _syncSlaves for sync and init calls + // - _attachedSlots + QMutex slaveMutex; + friend class SignalRelay; }; -- 2.20.1