From 5eed6fbe38266767a7080d0f16abc16de97afaf8 Mon Sep 17 00:00:00 2001 From: Kai Koehne Date: Thu, 6 Oct 2011 16:33:47 +0200 Subject: [PATCH] Debugger: Fix lock when profiling Parts of the loading of data is now in a separate thread, which was already trying to log trace data while the QDeclarativeDebugTrace constructor was still blocking. Avoid this by calling QDeclarativeDebugTrace constructor before the actual execution begins (addEngine()). Also make sure that the logging methods are reentrant with a mutex. Change-Id: I5c2c1d14763fd9c7cb6fc93c6dff22d00d8737f1 Reviewed-on: http://codereview.qt-project.org/6169 Reviewed-by: Christiaan Janssen Sanity-Review: Qt Sanity Bot --- .../debugger/qdeclarativedebugtrace.cpp | 17 ++++++++++++++++- .../debugger/qdeclarativedebugtrace_p.h | 7 +++++++ src/declarative/qml/qdeclarativeengine.cpp | 2 ++ 3 files changed, 25 insertions(+), 1 deletions(-) diff --git a/src/declarative/debugger/qdeclarativedebugtrace.cpp b/src/declarative/debugger/qdeclarativedebugtrace.cpp index befc3ea..76e4e23 100644 --- a/src/declarative/debugger/qdeclarativedebugtrace.cpp +++ b/src/declarative/debugger/qdeclarativedebugtrace.cpp @@ -44,6 +44,8 @@ #include #include #include +#include +#include QT_BEGIN_NAMESPACE @@ -77,6 +79,16 @@ QDeclarativeDebugTrace::QDeclarativeDebugTrace() } } +void QDeclarativeDebugTrace::addEngine(QDeclarativeEngine * /*engine*/) +{ + // just make sure that the service is properly registered + traceInstance(); +} + +void QDeclarativeDebugTrace::removeEngine(QDeclarativeEngine */*engine*/) +{ +} + void QDeclarativeDebugTrace::addEvent(EventType t) { if (QDeclarativeDebugService::isDebuggingEnabled()) @@ -188,7 +200,9 @@ void QDeclarativeDebugTrace::endRangeImpl(RangeType range) */ void QDeclarativeDebugTrace::processMessage(const QDeclarativeDebugData &message) { - if (m_deferredSend) + QMutexLocker locker(&m_mutex); + if (m_deferredSend + || (QThread::currentThread() != QCoreApplication::instance()->thread())) m_data.append(message); else sendMessage(message.toByteArray()); @@ -200,6 +214,7 @@ void QDeclarativeDebugTrace::processMessage(const QDeclarativeDebugData &message void QDeclarativeDebugTrace::sendMessages() { if (m_deferredSend) { + QMutexLocker locker(&m_mutex); //### this is a suboptimal way to send batched messages for (int i = 0; i < m_data.count(); ++i) sendMessage(m_data.at(i).toByteArray()); diff --git a/src/declarative/debugger/qdeclarativedebugtrace_p.h b/src/declarative/debugger/qdeclarativedebugtrace_p.h index fb2ef53..06c4311 100644 --- a/src/declarative/debugger/qdeclarativedebugtrace_p.h +++ b/src/declarative/debugger/qdeclarativedebugtrace_p.h @@ -55,6 +55,7 @@ #include #include +#include QT_BEGIN_HEADER @@ -74,6 +75,8 @@ struct QDeclarativeDebugData }; class QUrl; +class QDeclarativeEngine; + class Q_DECLARATIVE_EXPORT QDeclarativeDebugTrace : public QDeclarativeDebugService { public: @@ -106,6 +109,9 @@ public: MaximumRangeType }; + static void addEngine(QDeclarativeEngine *engine); + static void removeEngine(QDeclarativeEngine *engine); + static void addEvent(EventType); static void startRange(RangeType); @@ -133,6 +139,7 @@ private: bool m_deferredSend; bool m_messageReceived; QList m_data; + QMutex m_mutex; }; QT_END_NAMESPACE diff --git a/src/declarative/qml/qdeclarativeengine.cpp b/src/declarative/qml/qdeclarativeengine.cpp index 2b76f1f..1794dbe 100644 --- a/src/declarative/qml/qdeclarativeengine.cpp +++ b/src/declarative/qml/qdeclarativeengine.cpp @@ -438,6 +438,7 @@ void QDeclarativeEnginePrivate::init() QDeclarativeEngineDebugService::instance()->addEngine(q); QV8DebugService::instance()->addEngine(q); QV8ProfilerService::instance()->addEngine(q); + QDeclarativeDebugTrace::addEngine(q); } } @@ -504,6 +505,7 @@ QDeclarativeEngine::~QDeclarativeEngine() QDeclarativeEngineDebugService::instance()->remEngine(this); QV8DebugService::instance()->removeEngine(this); QV8ProfilerService::instance()->removeEngine(this); + QDeclarativeDebugTrace::removeEngine(this); } // if we are the parent of any of the qobject module api instances, -- 1.7.2.5