From 980f7e97e879e2f0493bfcdbe38a292eebbb9068 Mon Sep 17 00:00:00 2001 From: Marcus Eggenberger Date: Sat, 19 Jan 2008 15:02:14 +0000 Subject: [PATCH] Introducing SignalProxy NG! We're no longer using QMetaObject::invokeMethod() to deliver remote calls. Which should result in quite a performance increase. In addition the SignalProxy has been optimized to the latest changes in the core. This fixes certain instabilities in core and client. --- src/common/signalproxy.cpp | 100 ++++++++++++++++++++----------------- src/common/signalproxy.h | 22 +++----- version.inc | 4 +- 3 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/common/signalproxy.cpp b/src/common/signalproxy.cpp index 204c197e..2c443bbd 100644 --- a/src/common/signalproxy.cpp +++ b/src/common/signalproxy.cpp @@ -30,8 +30,8 @@ #include #include #include -#include #include +#include #include "syncableobject.h" #include "util.h" @@ -68,7 +68,7 @@ SignalRelay::SignalRelay(SignalProxy* parent, QObject* source) caller(source), _sync(false) { - QObject::connect(source, SIGNAL(destroyed()), parent, SLOT(detachSender()), Qt::DirectConnection); + QObject::connect(source, SIGNAL(destroyed()), parent, SLOT(detachSender())); } int SignalRelay::qt_metacall(QMetaObject::Call _c, int _id, void **_a) { @@ -273,7 +273,6 @@ void SignalProxy::objectRenamed(QString oldname, QString newname) { } void SignalProxy::objectRenamed(QByteArray classname, QString oldname, QString newname) { - QMutexLocker locker(&slaveMutex); if(_syncSlave.contains(classname) && _syncSlave[classname].contains(oldname) && oldname != newname) _syncSlave[classname][newname] = _syncSlave[classname].take(oldname); } @@ -395,12 +394,24 @@ const QHash &SignalProxy::syncMap(SyncableObject *obj) { return _classInfo[obj->metaObject()]->syncMap; } +void SignalProxy::setUpdatedRemotelyId(QObject *obj) { + const QMetaObject *meta = obj->metaObject(); + Q_ASSERT(_classInfo.contains(meta)); + _classInfo[meta]->updatedRemotelyId = meta->indexOfSignal("updatedRemotely()"); +} + +int SignalProxy::updatedRemotelyId(SyncableObject *obj) { + Q_ASSERT(_classInfo.contains(obj->metaObject())); + return _classInfo[obj->metaObject()]->updatedRemotelyId; +} + void SignalProxy::createClassInfo(QObject *obj) { if(_classInfo.contains(obj->metaObject())) return; ClassInfo *classInfo = new ClassInfo(); _classInfo[obj->metaObject()] = classInfo; + setUpdatedRemotelyId(obj); } bool SignalProxy::attachSignal(QObject* sender, const char* signal, const QByteArray& sigName) { @@ -440,7 +451,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()), Qt::DirectConnection); + QObject::connect(recv, SIGNAL(destroyed()), this, SLOT(detachSender())); return true; } @@ -462,7 +473,7 @@ void SignalProxy::synchronize(SyncableObject *obj) { if(proxyMode() == Server) { if(obj->metaObject()->indexOfSignal(QMetaObject::normalizedSignature("renameObject(QString, QString)")) != -1) - connect(obj, SIGNAL(renameObject(QString, QString)), this, SLOT(objectRenamed(QString, QString)), Qt::DirectConnection); + connect(obj, SIGNAL(renameObject(QString, QString)), this, SLOT(objectRenamed(QString, QString))); setInitialized(obj); } else { @@ -490,47 +501,22 @@ void SignalProxy::requestInit(SyncableObject *obj) { } void SignalProxy::detachSender() { - // 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) { - QMutexLocker locker(&slaveMutex); - _detachSignals(obj); - _detachSlots(obj); - SyncableObject *syncobj = qobject_cast(obj); - if(syncobj) _stopSync(syncobj); + detachSignals(obj); + detachSlots(obj); + stopSync(static_cast(obj)); } void SignalProxy::detachSignals(QObject* sender) { - QMutexLocker locker(&slaveMutex); - _detachSignals(sender); -} - -void SignalProxy::detachSlots(QObject* receiver) { - QMutexLocker locker(&slaveMutex); - _detachSlots(receiver); -} - -void SignalProxy::stopSync(SyncableObject* obj) { - QMutexLocker locker(&slaveMutex); - _stopSync(obj); -} - -void SignalProxy::_detachSignals(QObject* sender) { if(!_relayHash.contains(sender)) return; _relayHash.take(sender)->deleteLater(); } -void SignalProxy::_detachSlots(QObject* receiver) { +void SignalProxy::detachSlots(QObject* receiver) { SlotHash::iterator slotIter = _attachedSlots.begin(); while(slotIter != _attachedSlots.end()) { if(slotIter.value().first == receiver) { @@ -540,7 +526,7 @@ void SignalProxy::_detachSlots(QObject* receiver) { } } -void SignalProxy::_stopSync(SyncableObject* obj) { +void SignalProxy::stopSync(SyncableObject* obj) { if(_relayHash.contains(obj)) _relayHash[obj]->setSynchronize(false); @@ -579,11 +565,6 @@ void SignalProxy::receivePeerSignal(QIODevice *sender, const QVariant &packedFun if(call.type() != QVariant::Int) return handleSignal(call.toByteArray(), params); - // 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); - switch(call.toInt()) { case Sync: return handleSync(params); @@ -625,7 +606,7 @@ void SignalProxy::handleSync(QVariantList params) { qWarning("SignalProxy::handleSync(): invokeMethod for \"%s\" failed ", methodName(receiver, slotId).constData()); return; } - QMetaObject::invokeMethod(receiver, "updatedRemotely"); + invokeSlot(receiver, updatedRemotelyId(receiver)); } void SignalProxy::handleInitRequest(QIODevice *sender, const QVariantList ¶ms) { @@ -702,11 +683,38 @@ void SignalProxy::handleSignal(const QByteArray &funcName, const QVariantList &p } bool SignalProxy::invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms) { - int numParams = argTypes(receiver, methodId).count(); - QGenericArgument args[9]; - for(int i = 0; i < numParams; i++) - args[i] = QGenericArgument(params[i].typeName(), params[i].constData()); - return QMetaObject::invokeMethod(receiver, methodName(receiver, methodId), args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]); + const QList args = argTypes(receiver, methodId); + const int numArgs = args.count(); + + if(numArgs < params.count()) { + qWarning() << "SignalProxy::invokeSlot(): not enough params to invoke" << methodName(receiver, methodId); + return false; + } + + void *_a[numArgs+1]; + _a[0] = 0; + // check for argument compatibility and build params array + for(int i = 0; i < numArgs; i++) { + if(args[i] != QMetaType::type(params[i].typeName())) { + qWarning() << "SignalProxy::invokeSlot(): incompatible param types to invoke" << methodName(receiver, methodId); + return false; + } + _a[i+1] = const_cast(params[i].constData()); + } + + + Qt::ConnectionType type = QThread::currentThread() == receiver->thread() + ? Qt::DirectConnection + : Qt::QueuedConnection; + + if (type == Qt::DirectConnection) { + return receiver->qt_metacall(QMetaObject::InvokeMetaMethod, methodId, _a) < 0; + } else { + qWarning() << "Queued Connections are not implemented yet"; + // not to self: qmetaobject.cpp:990 ff + return false; + } + } void SignalProxy::dataAvailable() { diff --git a/src/common/signalproxy.h b/src/common/signalproxy.h index d9871a23..da9657fe 100644 --- a/src/common/signalproxy.h +++ b/src/common/signalproxy.h @@ -29,8 +29,6 @@ #include #include -#include - class SignalRelay; class SyncableObject; class QMetaObject; @@ -93,12 +91,14 @@ public: const QList &argTypes(QObject *obj, int methodId); const QByteArray &methodName(QObject *obj, int methodId); const QHash &syncMap(SyncableObject *obj); + int updatedRemotelyId(SyncableObject *obj); typedef QHash > ArgHash; typedef QHash MethodNameHash; struct ClassInfo { ArgHash argTypes; MethodNameHash methodNames; + int updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster QHash syncMap; }; @@ -125,6 +125,7 @@ private: void setArgTypes(QObject *obj, int methodId); void setMethodName(QObject *obj, int methodId); void setSyncMap(SyncableObject *obj); + void setUpdatedRemotelyId(QObject *obj); bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const; @@ -137,19 +138,15 @@ private: void handleInitData(QIODevice *sender, const QVariantList ¶ms); void handleSignal(const QByteArray &funcName, const QVariantList ¶ms); - bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms); + bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList()); QVariantMap initData(SyncableObject *obj) const; void setInitData(SyncableObject *obj, const QVariantMap &properties); - void _detachSignals(QObject *sender); - void _detachSlots(QObject *receiver); - void _stopSync(SyncableObject *obj); - - public: +public: void dumpSyncMap(SyncableObject *object); - private: - + +private: // Hash of used QIODevices QHash _peerByteCount; @@ -171,11 +168,6 @@ private: ProxyMode _proxyMode; - // the slaveMutex protects both containers: - // - _syncSlaves for sync and init calls - // - _attachedSlots - QMutex slaveMutex; - friend class SignalRelay; }; diff --git a/version.inc b/version.inc index a4744a84..a8ef6879 100644 --- a/version.inc +++ b/version.inc @@ -4,8 +4,8 @@ { using namespace Global; quasselVersion = "0.2.0-pre"; - quasselDate = "2008-01-18"; - quasselBuild = 356; + quasselDate = "2008-01-19"; + quasselBuild = 357; //! Minimum client build number the core needs clientBuildNeeded = 355; -- 2.20.1