diff --git a/src/dbus/qdbusintegrator.cpp b/src/dbus/qdbusintegrator.cpp index 11c867613f..6061ba513b 100644 --- a/src/dbus/qdbusintegrator.cpp +++ b/src/dbus/qdbusintegrator.cpp @@ -101,8 +101,30 @@ void qdbusDefaultThreadDebug(int action, int condition, QDBusConnectionPrivate * qdbusThreadDebugFunc qdbusThreadDebug = nullptr; #endif -struct QDBusSpyHookList +class QDBusSpyHookList { +public: + void add(QDBusSpyCallEvent::Hook hook) + { + const auto locker = qt_scoped_lock(lock); + list.append(hook); + } + + void invoke(const QDBusMessage &msg) + { + // Create a copy of the hook list here, so that the hooks can be called + // without holding the lock. + QList hookListCopy; + { + const auto locker = qt_scoped_lock(lock); + hookListCopy = list; + } + + for (auto hook : std::as_const(hookListCopy)) + hook(msg); + } + +private: QBasicMutex lock; QList list; }; @@ -464,8 +486,7 @@ void qDBusAddSpyHook(QDBusSpyCallEvent::Hook hook) if (!hooks) return; - const auto locker = qt_scoped_lock(hooks->lock); - hooks->list.append(hook); + hooks->add(hook); } QDBusSpyCallEvent::~QDBusSpyCallEvent() @@ -488,17 +509,7 @@ inline void QDBusSpyCallEvent::invokeSpyHooks(const QDBusMessage &msg) if (!qDBusSpyHookList.exists()) return; - // Create a copy of the hook list here, so that the hooks can be called - // without holding the lock. - QList hookListCopy; - { - auto *hooks = qDBusSpyHookList(); - const auto locker = qt_scoped_lock(hooks->lock); - hookListCopy = hooks->list; - } - - for (auto hook : std::as_const(hookListCopy)) - hook(msg); + qDBusSpyHookList->invoke(msg); } extern "C" {