LCOV - code coverage report
Current view: top level - shared-main-lib - watchdog.cpp (source / functions) Coverage Total Hit
Test: QtApplicationManager Lines: 27.4 % 424 116
Test Date: 2024-06-22 10:03:32 Functions: 55.9 % 34 19
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 8.1 % 725 59

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2024 The Qt Company Ltd.
       2                 :             : // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
       3                 :             : 
       4                 :             : #include <QCoreApplication>
       5                 :             : #include <QThread>
       6                 :             : 
       7                 :             : #include "logging.h"
       8                 :             : #include "utilities.h"
       9                 :             : #include "watchdog.h"
      10                 :             : #include "watchdog_p.h"
      11                 :             : #include "qtappman_common-config_p.h"
      12                 :             : 
      13                 :             : #if defined(Q_OS_UNIX)
      14                 :             : #  include <pthread.h>
      15                 :             : #  include <csignal>
      16                 :             : #  if QT_CONFIG(am_systemd_watchdog)
      17                 :             : #    include <systemd/sd-daemon.h>
      18                 :             : #  endif
      19                 :             : #elif defined(Q_OS_WINDOWS)
      20                 :             : #  include <windows.h>
      21                 :             : #endif
      22                 :             : 
      23                 :             : using namespace Qt::StringLiterals;
      24                 :             : using namespace std::chrono_literals;
      25                 :             : 
      26                 :             : 
      27                 :             : QT_BEGIN_NAMESPACE_AM
      28                 :             : 
      29                 :           0 : static const char *renderStateName(WatchdogPrivate::RenderState rs)
      30                 :             : {
      31   [ #  #  #  #  :           0 :     switch (rs) {
                      # ]
      32                 :             :     case WatchdogPrivate::Idle:   return "Idle";
      33                 :           0 :     case WatchdogPrivate::Sync:   return "Syncing";
      34                 :           0 :     case WatchdogPrivate::Render: return "Rendering";
      35                 :           0 :     case WatchdogPrivate::Swap:   return "Swapping";
      36                 :             :     }
      37                 :           0 :     return "";
      38                 :             : }
      39                 :             : 
      40                 :           0 : QDebug &operator<<(QDebug &dbg, WatchdogPrivate::RenderState rs)
      41                 :             : {
      42                 :           0 :     dbg << renderStateName(rs);
      43                 :           0 :     return dbg;
      44                 :             : }
      45                 :             : 
      46                 :           0 : static quintptr currentThreadHandle()
      47                 :             : {
      48                 :             : #if defined(Q_OS_DARWIN)
      49                 :             :     return reinterpret_cast<quintptr>(::pthread_self());  // pointer
      50                 :             : #elif defined(Q_OS_UNIX)
      51                 :           0 :     return static_cast<quintptr>(::pthread_self());  // ulong
      52                 :             : #elif defined(Q_OS_WINDOWS)
      53                 :             :     return static_cast<quintptr>(::GetCurrentThreadId());  // DWORD
      54                 :             : #else
      55                 :             :     return 0;
      56                 :             : #endif
      57                 :             : }
      58                 :             : 
      59                 :           0 : static void killThread(quintptr threadHandle)
      60                 :             : {
      61                 :             : #if defined(Q_OS_DARWIN)
      62                 :             :     ::pthread_kill(reinterpret_cast<pthread_t>(threadHandle), SIGABRT);
      63                 :             : #elif defined(Q_OS_UNIX)
      64                 :           0 :     ::pthread_kill(static_cast<pthread_t>(threadHandle), SIGABRT);
      65                 :             : #elif defined(Q_OS_WINDOWS)
      66                 :             :     auto winId = static_cast<DWORD>(threadHandle);
      67                 :             : 
      68                 :             :     if (::GetCurrentThreadId() == winId) {
      69                 :             :         ::abort();
      70                 :             :     } else {
      71                 :             :         bool ok = false;
      72                 :             :         auto winHandle = ::OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_SET_CONTEXT,
      73                 :             :                                       false, winId);
      74                 :             :         // There's no built-in way on Windows to abort a specific thread
      75                 :             :         // We try to set the single-step trap on the thread and expect it to abort
      76                 :             :         if (winHandle) {
      77                 :             :             CONTEXT context = { };
      78                 :             :             context.ContextFlags = CONTEXT_CONTROL;
      79                 :             :             if (::SuspendThread(winHandle) == 0) { // 0: not suspended before
      80                 :             :                 if (::GetThreadContext(winHandle, &context) > 0) {
      81                 :             :                     context.ContextFlags = CONTEXT_CONTROL;
      82                 :             : #  if defined(Q_PROCESSOR_ARM_64)
      83                 :             :                     context.Cpsr |= 0x200000;  // single-step trap
      84                 :             : #  elif defined(Q_PROCESSOR_X86_64)
      85                 :             :                     context.EFlags |= 0x100; // single-step trap
      86                 :             : #  else
      87                 :             :                     static_assert(false, "This architecture is not supported.");
      88                 :             : #  endif
      89                 :             :                     if (::SetThreadContext(winHandle, &context) > 0) {
      90                 :             :                         if (::ResumeThread(winHandle) == 1) // 1: suspended once
      91                 :             :                             ok = true;
      92                 :             :                     }
      93                 :             :                 }
      94                 :             :             }
      95                 :             :             ::CloseHandle(winHandle);
      96                 :             :         }
      97                 :             :         if (!ok) {
      98                 :             :             qCCritical(LogWatchdogStat).nospace()
      99                 :             :                 << "Failed to kill thread " << winId << ". Aborting process instead";
     100                 :             :             ::abort();
     101                 :             :         }
     102                 :             :     }
     103                 :             : #else
     104                 :             :     Q_UNUSED(threadHandle)
     105                 :             : #endif
     106                 :           0 : }
     107                 :             : 
     108                 :             : 
     109                 :             : ///////////////////////////////////////////////////////////////////////////////////////////////////
     110                 :             : // WatchdogPrivate
     111                 :             : ///////////////////////////////////////////////////////////////////////////////////////////////////
     112                 :             : 
     113                 :             : Watchdog *WatchdogPrivate::s_instance = nullptr;
     114                 :             : QAtomicPointer<QTimer> WatchdogPrivate::s_mainThreadTimer;
     115                 :             : 
     116                 :          88 : WatchdogPrivate::WatchdogPrivate(Watchdog *q)
     117                 :             :     : QObject(nullptr)
     118   [ +  -  +  - ]:          88 :     , m_wdThread(new QThread(q))
     119   [ +  -  +  - ]:          88 :     , m_quickWindowCheck(new QTimer(this))
     120   [ +  -  +  -  :         176 :     , m_eventLoopCheck(new QTimer(this))
                   +  - ]
     121                 :             : {
     122                 :             :     // we're on the ui thread
     123                 :          88 :     Q_ASSERT(QThread::currentThread() == qApp->thread());
     124                 :             : 
     125         [ +  - ]:          88 :     m_wdThread->setObjectName("QtAM-Watchdog");
     126         [ +  - ]:          88 :     moveToThread(m_wdThread);
     127   [ +  -  +  - ]:         171 :     connect(m_wdThread, &QThread::finished, this, [this]() { delete this; }, Qt::DirectConnection);
     128                 :             : 
     129                 :             :     // "this" is now on the wd thread
     130                 :          88 :     Q_ASSERT(thread() == m_wdThread);
     131                 :          88 :     Q_ASSERT(m_quickWindowCheck->thread() == m_wdThread);
     132                 :             : 
     133                 :          88 :     QElapsedTimer et;
     134                 :          88 :     et.start();
     135                 :          88 :     Q_ASSERT(et.isMonotonic());
     136                 :          88 :     m_referenceTime = et.msecsSinceReference();
     137                 :             : 
     138                 :             :     //TODO: detect overflow on the 30 bit elapsed timer and reset the reference time
     139                 :             :     //      needs to < ~12days, maybe default to 1day
     140                 :             : 
     141                 :          88 :     connect(m_quickWindowCheck, &QTimer::timeout,
     142         [ +  - ]:          88 :             this, &WatchdogPrivate::quickWindowCheck);
     143                 :             : 
     144                 :          88 :     connect(m_eventLoopCheck, &QTimer::timeout,
     145         [ +  - ]:          88 :             this, &WatchdogPrivate::eventLoopCheck);
     146                 :             : 
     147         [ +  - ]:          88 :     QMetaObject::invokeMethod(this, &WatchdogPrivate::setupSystemdWatchdog, Qt::QueuedConnection);
     148                 :          88 : }
     149                 :             : 
     150                 :         166 : WatchdogPrivate::~WatchdogPrivate()
     151                 :             : {
     152                 :          83 :     Q_ASSERT(QThread::currentThread() == m_wdThread);
     153         [ -  + ]:          83 :     for (const auto *eld : std::as_const(m_eventLoops)) {
     154         [ #  # ]:           0 :         if (eld->m_isMainThread) {
     155         [ #  # ]:           0 :             qCInfo(LogWatchdogStat) << "Event loop of thread" << static_cast<void *>(eld->m_thread)
     156                 :           0 :                                     << "/ the main GUI thread has finished and is not being watched anymore";
     157                 :           0 :             s_mainThreadTimer = eld->m_timer; // cannot delete from this thread
     158                 :           0 :             m_watchingMainEventLoop = false;
     159                 :             :         }
     160                 :           0 :         delete eld;
     161                 :             :     }
     162         [ -  + ]:          83 :     for (const auto *qwd : std::as_const(m_quickWindows))
     163         [ #  # ]:           0 :         delete qwd;
     164                 :         166 : }
     165                 :             : 
     166                 :          88 : void WatchdogPrivate::setupSystemdWatchdog()
     167                 :             : {
     168                 :             : #if QT_CONFIG(am_systemd_watchdog)
     169                 :             :     // we're on the wd thread
     170                 :             :     Q_ASSERT(QThread::currentThread() == m_wdThread);
     171                 :             : 
     172                 :             :     uint64_t wdTimeoutUsec = 0;
     173                 :             :     if (::sd_watchdog_enabled(1, &wdTimeoutUsec) <= 0)
     174                 :             :         wdTimeoutUsec = 0;
     175                 :             : 
     176                 :             :     if (wdTimeoutUsec > 0) {
     177                 :             :         static auto sdTrigger = []() { ::sd_notify(0, "WATCHDOG=1"); };
     178                 :             :         auto sdTimer = new QTimer(this);
     179                 :             :         sdTimer->setInterval(wdTimeoutUsec / 1000 / 2);
     180                 :             :         // the timer is triggered on the wd thread
     181                 :             :         connect(sdTimer, &QTimer::timeout, this, sdTrigger);
     182                 :             :         sdTrigger();
     183                 :             :         sdTimer->start();
     184                 :             : 
     185                 :             :         qCInfo(LogWatchdogStat).nospace() << "Systemd watchdog enabled (timeout is "
     186                 :             :                                           << (wdTimeoutUsec / 1000) << "ms)";
     187                 :             :     }
     188                 :             : #endif
     189                 :          88 : }
     190                 :             : 
     191                 :             : 
     192                 :             : ///////////////////////////////////////////////////////////////////////////////////////////////////
     193                 :             : // WatchdogPrivate   /   EventLoop
     194                 :             : ///////////////////////////////////////////////////////////////////////////////////////////////////
     195                 :             : 
     196                 :             : 
     197                 :          88 : void WatchdogPrivate::setEventLoopTimeouts(std::chrono::milliseconds check, std::chrono::milliseconds warn,
     198                 :             :                                            std::chrono::milliseconds kill)
     199                 :             : {
     200         [ -  + ]:          88 :     m_eventLoopCheckInterval = std::max(0ms, check);
     201                 :          88 :     m_eventLoopCheck->setInterval(m_eventLoopCheckInterval);
     202         [ -  + ]:          88 :     if (m_eventLoopCheckInterval > 0ms)
     203                 :           0 :         m_eventLoopCheck->start();
     204                 :             :     else
     205                 :          88 :         m_eventLoopCheck->stop();
     206                 :             : 
     207                 :             :     // these will be picked up automatically
     208         [ -  + ]:          88 :     m_warnEventLoopTime = std::max(0ms, warn);
     209         [ -  + ]:          88 :     m_killEventLoopTime = std::max(0ms, kill);
     210                 :             : 
     211         [ +  - ]:          88 :     if (m_warnEventLoopTime == m_killEventLoopTime)
     212                 :          88 :         m_warnEventLoopTime = 0ms;
     213                 :             : 
     214         [ -  + ]:          88 :     if (m_warnEventLoopTime > m_killEventLoopTime) {
     215         [ #  # ]:           0 :         qCWarning(LogWatchdogStat).nospace()
     216   [ #  #  #  # ]:           0 :             << "Event loop warning timeout (" << m_warnEventLoopTime
     217   [ #  #  #  #  :           0 :             << ") is greater than kill timeout (" << m_killEventLoopTime << ")";
                   #  # ]
     218                 :             :     }
     219                 :             : 
     220                 :             :     // watch the main event loop
     221         [ +  - ]:          88 :     if (!m_watchingMainEventLoop)
     222                 :          88 :         watchEventLoop(qApp->thread());
     223                 :          88 : }
     224                 :             : 
     225                 :          88 : bool WatchdogPrivate::isEventLoopWatchingEnabled() const
     226                 :             : {
     227         [ #  # ]:           0 :     if (m_eventLoopCheckInterval <= 0ms)
     228                 :             :         return false;
     229                 :             :     else
     230   [ #  #  #  #  :           0 :         return (m_warnEventLoopTime > 0ms) || (m_killEventLoopTime > 0ms);
             #  #  #  # ]
     231                 :             : }
     232                 :             : 
     233                 :          88 : void WatchdogPrivate::watchEventLoop(QThread *thread)
     234                 :             : {
     235                 :             :     // we're on the wd thread
     236                 :          88 :     Q_ASSERT(QThread::currentThread() == m_wdThread);
     237                 :             : 
     238         [ +  - ]:          88 :     if (!thread)
     239                 :          88 :         return;
     240                 :             : 
     241         [ -  + ]:          88 :     if (!isEventLoopWatchingEnabled())
     242                 :             :         return;
     243                 :             : 
     244                 :           0 :     QString info;
     245   [ #  #  #  # ]:           0 :     const auto className = thread->metaObject()->className();
     246         [ #  # ]:           0 :     const auto objectName = thread->objectName();
     247   [ #  #  #  #  :           0 :     bool isMainThread = qApp && (thread == qApp->thread());
                   #  # ]
     248   [ #  #  #  #  :           0 :     if (className && qstrcmp(className, "QThread"))
                   #  # ]
     249   [ #  #  #  # ]:           0 :         info = info + u" / class: " + QString::fromLatin1(className);
     250         [ #  # ]:           0 :     if (!objectName.isEmpty())
     251         [ #  # ]:           0 :         info = info + u" / name: " + objectName;
     252         [ #  # ]:           0 :     if (isMainThread)
     253         [ #  # ]:           0 :         info = info + u" / the main GUI thread";
     254                 :             : 
     255   [ #  #  #  # ]:           0 :     if (!thread->eventDispatcher()) {
     256   [ #  #  #  #  :           0 :         qCWarning(LogWatchdogStat).nospace().noquote()
             #  #  #  # ]
     257   [ #  #  #  #  :           0 :             << "Event loop of thread " << static_cast<void *>(thread) << info
                   #  # ]
     258         [ #  # ]:           0 :             << " cannot be watched, because the thread has no event dispatcher installed";
     259                 :             :         return;
     260                 :             :     }
     261                 :             : 
     262         [ #  # ]:           0 :     for (const auto *eld : std::as_const(m_eventLoops)) {
     263         [ #  # ]:           0 :         if (eld->m_thread == thread)
     264                 :           0 :             return;
     265                 :             :     }
     266                 :             : 
     267   [ #  #  #  # ]:           0 :     auto *eld = new EventLoopData;
     268                 :           0 :     static quint64 uniqueCounter = 0;
     269         [ #  # ]:           0 :     eld->m_thread = thread;
     270                 :           0 :     eld->m_uniqueCounter = ++uniqueCounter;
     271   [ #  #  #  # ]:           0 :     eld->m_timer = new QTimer;
     272                 :           0 :     eld->m_isMainThread = isMainThread;
     273         [ #  # ]:           0 :     m_eventLoops << eld;
     274                 :             : 
     275         [ #  # ]:           0 :     if (isMainThread)
     276                 :           0 :         m_watchingMainEventLoop = true;
     277                 :             : 
     278   [ #  #  #  # ]:           0 :     if (!m_eventLoopCheck->isActive())
     279         [ #  # ]:           0 :         m_eventLoopCheck->start();
     280                 :             : 
     281   [ #  #  #  #  :           0 :     qCInfo(LogWatchdogStat).nospace().noquote()
             #  #  #  # ]
     282   [ #  #  #  #  :           0 :         << "Event loop of thread " << static_cast<void *>(thread) << info << " is being watched now "
             #  #  #  # ]
     283   [ #  #  #  #  :           0 :         << "(check every " << m_eventLoopCheckInterval << ", warn/kill after "
                   #  # ]
     284   [ #  #  #  #  :           0 :         << m_warnEventLoopTime << "/" << m_killEventLoopTime << ")";
             #  #  #  # ]
     285                 :             : 
     286   [ #  #  #  # ]:           0 :     eld->m_timer->setInterval((m_warnEventLoopTime > 0ms ? m_warnEventLoopTime : m_killEventLoopTime) / 2);
     287         [ #  # ]:           0 :     eld->m_timer->start();
     288         [ #  # ]:           0 :     eld->m_timer->moveToThread(thread);
     289         [ #  # ]:           0 :     connect(eld->m_timer, &QTimer::timeout, eld->m_timer, [this, eld]() {
     290                 :             :         // we're on the watched thread
     291                 :           0 :         Q_ASSERT(QThread::currentThread() == eld->m_thread);
     292                 :             : 
     293         [ #  # ]:           0 :         if (!eld->m_threadHandle)
     294                 :           0 :             eld->m_threadHandle = currentThreadHandle();
     295                 :             : 
     296                 :           0 :         AtomicState as;
     297                 :           0 :         as.pod = eld->m_state;
     298                 :             : 
     299                 :           0 :         QElapsedTimer et;
     300                 :           0 :         et.start();
     301         [ #  # ]:           0 :         quint64 now = quint64(et.msecsSinceReference() - m_referenceTime);
     302                 :           0 :         quint64 expectedAt = as.eventLoopBits.expectedAt;
     303         [ #  # ]:           0 :         quint64 elapsed = (!expectedAt || (now < expectedAt)) ? 0 : (now - expectedAt);
     304                 :             : 
     305                 :           0 :         bool wasStuck = false;
     306         [ #  # ]:           0 :         quint64 maxTime = quint64(m_warnEventLoopTime.count());
     307                 :             : 
     308   [ #  #  #  # ]:           0 :         if (maxTime && (elapsed > maxTime)) {
     309                 :           0 :             wasStuck = true;
     310                 :           0 :             as.eventLoopBits.stuckCounter = std::clamp(as.eventLoopBits.stuckCounter + 1ULL, 0ULL, (1ULL << 4) - 1);
     311                 :             :         }
     312         [ #  # ]:           0 :         if (wasStuck && (elapsed > as.eventLoopBits.longestStuckDuration))
     313                 :           0 :             as.eventLoopBits.longestStuckDuration = std::clamp(elapsed, 0ULL, (1ULL << 20) - 1);
     314                 :             : 
     315         [ #  # ]:           0 :         if (wasStuck) {
     316         [ #  # ]:           0 :             qCWarning(LogWatchdogLive).nospace()
     317   [ #  #  #  # ]:           0 :                 << "Event loop of thread " << static_cast<void *>(eld->m_thread.get())
     318   [ #  #  #  #  :           0 :                 << " was stuck for " << elapsed << "ms, but then continued (the warn threshold is "
                   #  # ]
     319   [ #  #  #  # ]:           0 :                 << maxTime << "ms)";
     320                 :             :         }
     321                 :             : 
     322                 :           0 :         as.eventLoopBits.expectedAt = now + maxTime / 2;
     323                 :           0 :         eld->m_state = as.pod;
     324                 :             : 
     325         [ #  # ]:           0 :         auto nextDuration = (m_warnEventLoopTime > 0ms ? m_warnEventLoopTime : m_killEventLoopTime) / 2;
     326         [ #  # ]:           0 :         if (eld->m_timer->intervalAsDuration() != nextDuration)
     327                 :           0 :             eld->m_timer->setInterval(nextDuration);
     328                 :           0 :     });
     329                 :             : 
     330                 :             :     // no finished signal for the main thread - only the destructor
     331         [ #  # ]:           0 :     if (!eld->m_isMainThread) {
     332         [ #  # ]:           0 :         connect(thread, &QThread::finished, this, [eld]() {
     333                 :             :                 // we're on the watched thread
     334                 :           0 :                 Q_ASSERT(QThread::currentThread() == eld->m_thread);
     335                 :             : 
     336         [ #  # ]:           0 :                 delete eld->m_timer;
     337                 :           0 :                 eld->m_timer = nullptr;
     338                 :           0 :             }, Qt::DirectConnection);
     339                 :             : 
     340         [ #  # ]:           0 :         connect(thread, &QThread::finished, this, [this, eld]() {
     341                 :             :                 // we're on the wd thread
     342                 :           0 :                 Q_ASSERT(QThread::currentThread() == m_wdThread);
     343                 :             : 
     344   [ #  #  #  #  :           0 :                 qCInfo(LogWatchdogStat) << "Event loop of thread" << static_cast<void *>(eld->m_thread)
                   #  # ]
     345         [ #  # ]:           0 :                                         << "has finished and is not being watched anymore";
     346                 :             : 
     347                 :           0 :                 m_eventLoops.removeOne(eld);
     348         [ #  # ]:           0 :                 delete eld;
     349                 :             : 
     350   [ #  #  #  # ]:           0 :                 if (m_eventLoops.isEmpty() && m_eventLoopCheck->isActive())
     351                 :           0 :                     m_eventLoopCheck->stop();
     352                 :           0 :             }, Qt::QueuedConnection);
     353                 :             :     }
     354                 :          88 : }
     355                 :             : 
     356                 :           0 : void WatchdogPrivate::eventLoopCheck()
     357                 :             : {
     358                 :             :     // we're on the wd thread
     359                 :           0 :     Q_ASSERT(QThread::currentThread() == m_wdThread);
     360                 :             : 
     361                 :             :     // this is the "print statistics" and "kill thread" timer
     362                 :             : 
     363                 :             :     // no point in spamming the log, while libbacktrace takes its time
     364         [ #  # ]:           0 :     if (m_threadIsBeingKilled)
     365                 :             :         return;
     366                 :             : 
     367         [ #  # ]:           0 :     for (auto *eld : std::as_const(m_eventLoops)) {
     368   [ #  #  #  # ]:           0 :         if (!eld->m_thread || m_threadIsBeingKilled)
     369                 :           0 :             continue;
     370                 :             : 
     371         [ #  # ]:           0 :         AtomicState as;
     372         [ #  # ]:           0 :         as.pod = eld->m_state;
     373                 :             : 
     374         [ #  # ]:           0 :         if (as.eventLoopBits.stuckCounter > eld->m_lastState.eventLoopBits.stuckCounter) {
     375                 :           0 :             static constexpr quint64 maxCounter = (1ULL << 4) - 2;
     376                 :           0 :             QByteArray stuckCounterString;
     377                 :           0 :             quint64 counter = as.eventLoopBits.stuckCounter;
     378                 :             : 
     379                 :           0 :             if (counter) {
     380         [ #  # ]:           0 :                 if (counter > maxCounter)
     381         [ #  # ]:           0 :                     stuckCounterString += "more than ";
     382   [ #  #  #  # ]:           0 :                 stuckCounterString = stuckCounterString + QByteArray::number(counter)
     383   [ #  #  #  # ]:           0 :                                      + ((counter == 1) ? " time" : " times");
     384                 :             :             }
     385                 :             : 
     386   [ #  #  #  #  :           0 :             qCWarning(LogWatchdogStat).nospace()
                   #  # ]
     387   [ #  #  #  # ]:           0 :                 << "Event loop of thread " << static_cast<void *>(eld->m_thread.get())
     388   [ #  #  #  #  :           0 :                 << " was stuck " << stuckCounterString.constData() << ". "
             #  #  #  # ]
     389   [ #  #  #  #  :           0 :                 << "The longest period was " << as.eventLoopBits.longestStuckDuration << "ms";
                   #  # ]
     390                 :           0 :         }
     391                 :           0 :         eld->m_lastState.pod = as.pod;
     392                 :             : 
     393                 :           0 :         QElapsedTimer et;
     394                 :           0 :         et.start();
     395         [ #  # ]:           0 :         quint64 now = quint64(et.msecsSinceReference() - m_referenceTime);
     396                 :           0 :         quint64 expectedAt = as.eventLoopBits.expectedAt;
     397         [ #  # ]:           0 :         quint64 elapsed = (!expectedAt || (now < expectedAt)) ? 0 : (now - expectedAt);
     398         [ #  # ]:           0 :         quint64 warnTime = quint64(m_warnEventLoopTime.count());
     399                 :           0 :         quint64 killTime = quint64(m_killEventLoopTime.count());
     400                 :             : 
     401   [ #  #  #  # ]:           0 :         if (killTime && (elapsed > killTime) && eld->m_thread) {
     402         [ #  # ]:           0 :             qCCritical(LogWatchdogStat).nospace()
     403   [ #  #  #  # ]:           0 :                 << "Event loop of thread " << static_cast<void *>(eld->m_thread.get())
     404   [ #  #  #  # ]:           0 :                 << " is getting killed, because it is now stuck for over " << elapsed
     405   [ #  #  #  #  :           0 :                 << "ms (the kill threshold is " << killTime << "ms)";
                   #  # ]
     406                 :             : 
     407                 :             :             // avoid multiple messages, until the thread is actually killed
     408                 :           0 :             m_threadIsBeingKilled = 1;
     409                 :           0 :             killThread(eld->m_threadHandle);
     410         [ #  # ]:           0 :         } else if (warnTime && (elapsed > (warnTime * 2))
     411   [ #  #  #  # ]:           0 :                    && (elapsed > (quint64(m_eventLoopCheckInterval.count()) / 2))) {
     412         [ #  # ]:           0 :             qCWarning(LogWatchdogStat).nospace()
     413   [ #  #  #  # ]:           0 :                 << "Event loop of thread " << static_cast<void *>(eld->m_thread.get())
     414   [ #  #  #  # ]:           0 :                 << " is currently stuck for over " << elapsed
     415   [ #  #  #  #  :           0 :                 << "ms (the warn threshold is " << warnTime << "ms)";
                   #  # ]
     416                 :             :         }
     417                 :             :     }
     418                 :             : }
     419                 :             : 
     420                 :             : 
     421                 :             : ///////////////////////////////////////////////////////////////////////////////////////////////////
     422                 :             : // WatchdogPrivate   /   QuickWindow
     423                 :             : ///////////////////////////////////////////////////////////////////////////////////////////////////
     424                 :             : 
     425                 :             : 
     426                 :          88 : void WatchdogPrivate::setQuickWindowTimeouts(std::chrono::milliseconds check,
     427                 :             :                                              std::chrono::milliseconds warnSync, std::chrono::milliseconds killSync,
     428                 :             :                                              std::chrono::milliseconds warnRender, std::chrono::milliseconds killRender,
     429                 :             :                                              std::chrono::milliseconds warnSwap, std::chrono::milliseconds killSwap)
     430                 :             : {
     431         [ -  + ]:          88 :     m_quickWindowCheckInterval = std::max(0ms, check);
     432                 :          88 :     m_quickWindowCheck->setInterval(m_quickWindowCheckInterval);
     433         [ -  + ]:          88 :     if (m_quickWindowCheckInterval > 0ms)
     434                 :           0 :         m_quickWindowCheck->start();
     435                 :             :     else
     436                 :          88 :         m_quickWindowCheck->stop();
     437                 :             : 
     438                 :             :     // these will be picked up automatically
     439         [ -  + ]:          88 :     m_warnRenderStateTime[Sync] = std::max(0ms, warnSync);
     440         [ -  + ]:          88 :     m_warnRenderStateTime[Render] = std::max(0ms, warnRender);
     441         [ -  + ]:          88 :     m_warnRenderStateTime[Swap] = std::max(0ms, warnSwap);
     442         [ -  + ]:          88 :     m_killRenderStateTime[Sync] = std::max(0ms, killSync);
     443         [ -  + ]:          88 :     m_killRenderStateTime[Render] = std::max(0ms, killRender);
     444         [ -  + ]:          88 :     m_killRenderStateTime[Swap] = std::max(0ms, killSwap);
     445                 :             : 
     446         [ +  + ]:         352 :     for (auto rs : { RenderState::Sync, RenderState::Render, RenderState::Swap }) {
     447         [ -  + ]:         264 :         if (m_warnRenderStateTime[rs] > m_killRenderStateTime[rs]) {
     448         [ #  # ]:           0 :             qCWarning(LogWatchdogStat).nospace()
     449   [ #  #  #  #  :           0 :                 << "Quick window " << rs << " warning timeout (" << m_warnRenderStateTime[rs]
             #  #  #  # ]
     450   [ #  #  #  #  :           0 :                 << ") is greater than kill timeout (" << m_killRenderStateTime[rs] << ")";
                   #  # ]
     451                 :             :         }
     452                 :             :     }
     453                 :          88 : }
     454                 :             : 
     455                 :          84 : bool WatchdogPrivate::isQuickWindowWatchingEnabled() const
     456                 :             : {
     457         [ -  + ]:          84 :     if (m_quickWindowCheckInterval <= 0ms)
     458                 :             :         return false;
     459                 :           0 :     bool enabled = false;
     460         [ #  # ]:           0 :     for (auto rs : { RenderState::Sync, RenderState::Render, RenderState::Swap })
     461   [ #  #  #  #  :           0 :         enabled = enabled || (m_warnRenderStateTime[rs] > 0ms) || (m_killRenderStateTime[rs] > 0ms);
                   #  # ]
     462                 :           0 :     return enabled;
     463                 :             : }
     464                 :             : 
     465                 :          84 : void WatchdogPrivate::watchQuickWindow(QQuickWindow *quickWindow)
     466                 :             : {
     467                 :             :     // we're on the wd thread
     468                 :          84 :     Q_ASSERT(QThread::currentThread() == m_wdThread);
     469                 :             : 
     470         [ +  - ]:          84 :     if (!quickWindow)
     471                 :          84 :         return;
     472                 :             : 
     473         [ -  + ]:          84 :     if (!isQuickWindowWatchingEnabled())
     474                 :             :         return;
     475                 :             : 
     476         [ #  # ]:           0 :     for (const auto *qwd : std::as_const(m_quickWindows)) {
     477         [ #  # ]:           0 :         if (qwd->m_window == quickWindow)
     478                 :          84 :             return;
     479                 :             :     }
     480                 :             : 
     481                 :           0 :     auto *qwd = new QuickWindowData;
     482                 :           0 :     static quint64 uniqueCounter = 0;
     483                 :           0 :     qwd->m_window = quickWindow;
     484                 :           0 :     qwd->m_uniqueCounter = ++uniqueCounter;
     485                 :           0 :     m_quickWindows << qwd;
     486                 :             : 
     487         [ #  # ]:           0 :     if (!m_quickWindowCheck->isActive())
     488                 :           0 :         m_quickWindowCheck->start();
     489                 :             : 
     490                 :           0 :     QString info;
     491   [ #  #  #  # ]:           0 :     const auto className = quickWindow->metaObject()->className();
     492         [ #  # ]:           0 :     const auto objectName = quickWindow->objectName();
     493         [ #  # ]:           0 :     const auto title = quickWindow->title();
     494   [ #  #  #  #  :           0 :     if (className && qstrcmp(className, "QQuickWindowQmlImpl"))
                   #  # ]
     495   [ #  #  #  # ]:           0 :         info = info + u" / class: " + QString::fromLatin1(className);
     496         [ #  # ]:           0 :     if (!objectName.isEmpty())
     497         [ #  # ]:           0 :         info = info + u" / name: \"" + objectName + u'"';
     498         [ #  # ]:           0 :     if (!title.isEmpty())
     499         [ #  # ]:           0 :         info = info + u" / title: \"" + title + u'"';
     500                 :             : 
     501   [ #  #  #  #  :           0 :     qCInfo(LogWatchdogStat).nospace().noquote()
             #  #  #  # ]
     502   [ #  #  #  #  :           0 :         << "Window " << static_cast<void *>(quickWindow) << info << " is being watched now "
             #  #  #  # ]
     503   [ #  #  #  #  :           0 :         << "(check every " << m_quickWindowCheckInterval << ", sync, render, swap warn/kill after "
                   #  # ]
     504   [ #  #  #  #  :           0 :         << m_warnRenderStateTime[RenderState::Sync] << "/" << m_killRenderStateTime[RenderState::Sync] << ", "
             #  #  #  # ]
     505   [ #  #  #  #  :           0 :         << m_warnRenderStateTime[RenderState::Render] << "/" << m_killRenderStateTime[RenderState::Render] << ", "
             #  #  #  # ]
     506   [ #  #  #  #  :           0 :         << m_warnRenderStateTime[RenderState::Swap] << "/" << m_killRenderStateTime[RenderState::Swap] << ")";
             #  #  #  # ]
     507                 :             : 
     508         [ #  # ]:           0 :     connect(quickWindow, &QObject::destroyed, this, [this, qwd](QObject *o) {
     509                 :             :         // we're on wd thread
     510                 :           0 :         Q_ASSERT(QThread::currentThread() == m_wdThread);
     511                 :             : 
     512   [ #  #  #  #  :           0 :         qCInfo(LogWatchdogStat) << "Window" << static_cast<void *>(o)
                   #  # ]
     513         [ #  # ]:           0 :                                 << "has been destroyed and is not being watched anymore";
     514                 :           0 :         m_quickWindows.removeOne(qwd);
     515         [ #  # ]:           0 :         delete qwd;
     516                 :             : 
     517   [ #  #  #  # ]:           0 :         if (m_quickWindows.isEmpty() && m_quickWindowCheck->isActive())
     518                 :           0 :             m_quickWindowCheck->stop();
     519                 :           0 :     });
     520                 :             : 
     521                 :           0 :     auto changeState = [this](QuickWindowData *qwd, RenderState fromState, RenderState toState)
     522                 :             :     {
     523                 :             :         // we're on the render thread
     524                 :           0 :         Q_ASSERT(QThread::currentThread() != m_wdThread);
     525                 :           0 :         Q_ASSERT(QThread::currentThread() != qApp->thread());
     526                 :             : 
     527                 :             :         // this function is never called on the same wd concurrently!
     528                 :             : 
     529         [ #  # ]:           0 :         if (!qwd->m_renderThreadSet) {
     530                 :           0 :             qwd->m_renderThread = QThread::currentThread();
     531                 :           0 :             qwd->m_renderThreadHandle = currentThreadHandle();
     532                 :           0 :             qwd->m_renderThreadSet = 1;
     533                 :             :         }
     534                 :             : 
     535         [ #  # ]:           0 :         AtomicState as;
     536         [ #  # ]:           0 :         as.pod = qwd->m_state;
     537                 :             : 
     538         [ #  # ]:           0 :         if (as.quickWindowBits.state != quint64(fromState))
     539   [ #  #  #  #  :           0 :             qCWarning(LogWatchdogLive) << "Window" << static_cast<void *>(qwd->m_window.get())
                   #  # ]
     540   [ #  #  #  # ]:           0 :                                        << "state mismatch: expected" << fromState
     541   [ #  #  #  # ]:           0 :                                        << "but got" << RenderState(as.quickWindowBits.state);
     542                 :             : 
     543                 :           0 :         as.quickWindowBits.state = quint64(toState);
     544                 :           0 :         QElapsedTimer et;
     545                 :           0 :         et.start();
     546         [ #  # ]:           0 :         quint64 now = quint64(et.msecsSinceReference() - m_referenceTime);
     547                 :           0 :         quint64 startedAt = as.quickWindowBits.startedAt;
     548         [ #  # ]:           0 :         quint64 elapsed = (!startedAt || (now < startedAt)) ? 0 : (now - startedAt);
     549                 :           0 :         as.quickWindowBits.startedAt =  now;
     550                 :             : 
     551                 :           0 :         bool wasStuck = false;
     552         [ #  # ]:           0 :         quint64 maxTime = quint64(m_warnRenderStateTime[fromState].count());
     553                 :             : 
     554   [ #  #  #  # ]:           0 :         if (maxTime && (elapsed > maxTime)) {
     555                 :           0 :             wasStuck = true;
     556                 :             : 
     557         [ #  # ]:           0 :             if ((fromState == Sync) && (toState == Render))
     558                 :           0 :                 as.quickWindowBits.stuckCounterSync = std::clamp(as.quickWindowBits.stuckCounterSync + 1ULL, 0ULL, (1ULL << 4) - 1);
     559         [ #  # ]:           0 :             else if ((fromState == Render) && (toState == Swap))
     560                 :           0 :                 as.quickWindowBits.stuckCounterRender = std::clamp(as.quickWindowBits.stuckCounterRender + 1ULL, 0ULL, (1ULL << 4) - 1);
     561         [ #  # ]:           0 :             else if ((fromState == Swap) && (toState == Idle))
     562                 :           0 :                 as.quickWindowBits.stuckCounterSwap = std::clamp(as.quickWindowBits.stuckCounterSwap + 1ULL, 0ULL, (1ULL << 4) - 1);
     563                 :             :         }
     564         [ #  # ]:           0 :         if (wasStuck && (elapsed > as.quickWindowBits.longestStuckDuration)) {
     565                 :           0 :             as.quickWindowBits.longestStuckType = quint64(fromState);
     566                 :           0 :             as.quickWindowBits.longestStuckDuration = std::clamp(elapsed, 0ULL, (1ULL << 18) - 1);
     567                 :             :         }
     568                 :             : 
     569                 :           0 :         qwd->m_state = as.pod;
     570                 :             : 
     571         [ #  # ]:           0 :         if (wasStuck) {
     572         [ #  # ]:           0 :             qCWarning(LogWatchdogLive).nospace()
     573   [ #  #  #  # ]:           0 :                 << "Window " << static_cast<void *>(qwd->m_window.get())
     574   [ #  #  #  #  :           0 :                 << " was stuck in state " << fromState << " for " << elapsed
             #  #  #  # ]
     575   [ #  #  #  #  :           0 :                 << "ms, but then continued (the warn threshold is " << maxTime << "ms)";
                   #  # ]
     576                 :             :         }
     577                 :           0 :     };
     578                 :             : 
     579         [ #  # ]:           0 :     connect(quickWindow, &QQuickWindow::beforeSynchronizing, this, [=]() {
     580                 :           0 :             changeState(qwd, Idle, Sync);
     581                 :           0 :         }, Qt::DirectConnection);
     582         [ #  # ]:           0 :     connect(quickWindow, &QQuickWindow::beforeRendering, this, [=]() {
     583                 :           0 :             changeState(qwd, Sync, Render);
     584                 :           0 :         }, Qt::DirectConnection);
     585         [ #  # ]:           0 :     connect(quickWindow, &QQuickWindow::afterRendering, this, [=]() {
     586                 :           0 :             changeState(qwd, Render, Swap);
     587                 :           0 :         }, Qt::DirectConnection);
     588         [ #  # ]:           0 :     connect(quickWindow, &QQuickWindow::frameSwapped, this, [=]() {
     589                 :           0 :             changeState(qwd, Swap, Idle);
     590                 :           0 :         }, Qt::DirectConnection);
     591         [ #  # ]:           0 :     connect(quickWindow, &QQuickWindow::sceneGraphAboutToStop, this, [=]() {
     592                 :           0 :             AtomicState as = { 0 };
     593                 :           0 :             as.quickWindowBits.state = quint64(Idle);
     594                 :           0 :             qwd->m_state = as.pod;
     595                 :             :         }, Qt::DirectConnection);
     596                 :           0 : }
     597                 :             : 
     598                 :           0 : void WatchdogPrivate::quickWindowCheck()
     599                 :             : {
     600                 :             :     // we're on the wd thread
     601                 :           0 :     Q_ASSERT(QThread::currentThread() == m_wdThread);
     602                 :             : 
     603                 :             :     // this is the "print statistics" and "kill thread" timer
     604                 :             : 
     605                 :             :     // no point in spamming the log, while libbacktrace takes its time
     606         [ #  # ]:           0 :     if (m_threadIsBeingKilled)
     607                 :             :         return;
     608                 :             : 
     609         [ #  # ]:           0 :     for (auto *qwd : std::as_const(m_quickWindows)) {
     610         [ #  # ]:           0 :         if (!qwd->m_window)
     611                 :           0 :             continue;
     612                 :             : 
     613         [ #  # ]:           0 :         AtomicState as;
     614         [ #  # ]:           0 :         as.pod = qwd->m_state;
     615                 :             : 
     616                 :             :         // only print the stats, if any "stuck" counter changed since last time
     617         [ #  # ]:           0 :         if ((as.quickWindowBits.stuckCounterSync > qwd->m_lastState.quickWindowBits.stuckCounterSync)
     618         [ #  # ]:           0 :             || (as.quickWindowBits.stuckCounterRender > qwd->m_lastState.quickWindowBits.stuckCounterRender)
     619         [ #  # ]:           0 :             || (as.quickWindowBits.stuckCounterSwap > qwd->m_lastState.quickWindowBits.stuckCounterSwap)) {
     620                 :           0 :             static constexpr quint64 maxCounter = (1ULL << 4) - 2;
     621                 :           0 :             QByteArray stuckCounterString;
     622                 :             : 
     623                 :           0 :             auto appendStuckCounter = [&](quint64 counter, RenderState rs) {
     624         [ #  # ]:           0 :                 if (counter) {
     625   [ #  #  #  # ]:           0 :                     if (!stuckCounterString.isEmpty())
     626                 :           0 :                         stuckCounterString += ", ";
     627         [ #  # ]:           0 :                     if (counter > maxCounter)
     628                 :           0 :                         stuckCounterString += "more than ";
     629         [ #  # ]:           0 :                     stuckCounterString = stuckCounterString + QByteArray::number(counter)
     630                 :           0 :                                          + ((counter == 1) ? " time" : " times")
     631   [ #  #  #  # ]:           0 :                                          + " in state " + renderStateName(rs);
     632                 :             :                 }
     633                 :           0 :             };
     634                 :             : 
     635         [ #  # ]:           0 :             appendStuckCounter(as.quickWindowBits.stuckCounterSync, RenderState::Sync);
     636         [ #  # ]:           0 :             appendStuckCounter(as.quickWindowBits.stuckCounterRender, RenderState::Render);
     637         [ #  # ]:           0 :             appendStuckCounter(as.quickWindowBits.stuckCounterSwap, RenderState::Swap);
     638                 :             : 
     639   [ #  #  #  #  :           0 :             qCWarning(LogWatchdogStat).nospace()
                   #  # ]
     640   [ #  #  #  #  :           0 :                 << "Window " << static_cast<void *>(qwd->m_window.get()) << " was stuck "
             #  #  #  # ]
     641   [ #  #  #  # ]:           0 :                 << stuckCounterString.constData() << ". The longest period was "
     642         [ #  # ]:           0 :                 << as.quickWindowBits.longestStuckDuration << "ms in state "
     643   [ #  #  #  # ]:           0 :                 << RenderState(as.quickWindowBits.longestStuckType);
     644                 :           0 :         }
     645                 :           0 :         qwd->m_lastState.pod = as.pod;
     646                 :             : 
     647         [ #  # ]:           0 :         if (as.quickWindowBits.state == quint64(Idle))
     648                 :           0 :             continue;
     649                 :             : 
     650                 :           0 :         QElapsedTimer et;
     651                 :           0 :         et.start();
     652         [ #  # ]:           0 :         quint64 now = quint64(et.msecsSinceReference() - m_referenceTime);
     653                 :           0 :         quint64 startedAt = as.quickWindowBits.startedAt;
     654         [ #  # ]:           0 :         quint64 elapsed = (!startedAt || (now < startedAt)) ? 0 : (now - startedAt);
     655         [ #  # ]:           0 :         quint64 warnTime = quint64(m_warnRenderStateTime[as.quickWindowBits.state].count());
     656                 :           0 :         quint64 killTime = quint64(m_killRenderStateTime[as.quickWindowBits.state].count());
     657                 :             : 
     658   [ #  #  #  # ]:           0 :         if (killTime && (elapsed > killTime) && qwd->m_renderThread) {
     659         [ #  # ]:           0 :             qCCritical(LogWatchdogStat).nospace()
     660   [ #  #  #  # ]:           0 :                 << "Window " << static_cast<void *>(qwd->m_window.get())
     661   [ #  #  #  # ]:           0 :                 << " is getting its render thread (" << static_cast<void *>(qwd->m_renderThread.get())
     662                 :             :                 << ") killed, because it is now stuck in state "
     663   [ #  #  #  #  :           0 :                 << RenderState(as.quickWindowBits.state) << " for over " << elapsed
             #  #  #  # ]
     664   [ #  #  #  #  :           0 :                 << "ms (the kill threshold is " << killTime << "ms)";
                   #  # ]
     665                 :             : 
     666                 :             :             // avoid multiple messages, until the thread is actually killed
     667                 :           0 :             m_threadIsBeingKilled = 1;
     668                 :           0 :             killThread(qwd->m_renderThreadHandle);
     669         [ #  # ]:           0 :         } else if (warnTime && (elapsed > (warnTime * 2))
     670   [ #  #  #  # ]:           0 :                    && (elapsed > (quint64(m_quickWindowCheckInterval.count()) / 2))) {
     671         [ #  # ]:           0 :             qCWarning(LogWatchdogStat).nospace()
     672   [ #  #  #  # ]:           0 :                 << "Window " << static_cast<void *>(qwd->m_window.get())
     673   [ #  #  #  # ]:           0 :                 << " is currently stuck in state " << RenderState(as.quickWindowBits.state)
     674   [ #  #  #  #  :           0 :                 << " for over " << elapsed << "ms (the warn threshold is " << warnTime << "ms)";
          #  #  #  #  #  
                      # ]
     675                 :             :         }
     676                 :             :     }
     677                 :             : }
     678                 :             : 
     679                 :             : 
     680                 :             : ///////////////////////////////////////////////////////////////////////////////////////////////////
     681                 :             : // Watchdog
     682                 :             : ///////////////////////////////////////////////////////////////////////////////////////////////////
     683                 :             : 
     684                 :             : 
     685                 :          88 : Watchdog::Watchdog()
     686                 :             :     : QObject(qApp)
     687   [ +  -  +  - ]:          88 :     , d(new WatchdogPrivate(this))
     688                 :             : {
     689                 :             :     // automatically watch any QQuickWindows that are created
     690         [ +  - ]:          88 :     qApp->installEventFilter(this);
     691                 :             : 
     692                 :             :     // we need to stop event handling in the WD thread *before* ~QCoreApplication,
     693                 :             :     // otherwise we run into a data-race on the qApp vptr:
     694                 :             :     // https://github.com/google/sanitizers/wiki/ThreadSanitizerPopularDataRaces#data-race-on-vptr
     695         [ +  - ]:          88 :     connect(qApp, &QCoreApplication::aboutToQuit, this, [this]() {
     696                 :          83 :         shutdown();
     697                 :          83 :         m_cleanShutdown = true;
     698                 :             :     });
     699         [ +  - ]:          88 :     d->m_wdThread->start();
     700                 :          88 : }
     701                 :             : 
     702                 :          88 : Watchdog *Watchdog::create()
     703                 :             : {
     704                 :          88 :     Q_ASSERT(qApp);
     705                 :          88 :     Q_ASSERT(QThread::currentThread() == qApp->thread());
     706                 :             : 
     707         [ +  - ]:          88 :     if (!WatchdogPrivate::s_instance)
     708         [ +  - ]:          88 :         WatchdogPrivate::s_instance = new Watchdog;
     709                 :          88 :     return WatchdogPrivate::s_instance;
     710                 :             : }
     711                 :             : 
     712                 :         166 : Watchdog::~Watchdog()
     713                 :             : {
     714         [ -  + ]:          83 :     if (!m_cleanShutdown) {
     715         [ #  # ]:           0 :         qCCritical(LogWatchdogStat) << "The watchdog could not properly shutdown, as no "
     716                 :             :                                        "QCoreApplication::aboutToQuit signal was received "
     717                 :           0 :                                        "(this will result in TSAN warnings).";
     718                 :           0 :         shutdown();
     719                 :             :     }
     720                 :          83 :     WatchdogPrivate::s_instance = nullptr;
     721                 :             : 
     722                 :             :     // the finished() signal of the thread will auto-delete d from within that thread
     723                 :         166 : }
     724                 :             : 
     725                 :          83 : void Watchdog::shutdown()
     726                 :             : {
     727                 :          83 :     auto wdThread = d->m_wdThread; // 'd' is dead after quit()
     728                 :          83 :     wdThread->quit();
     729                 :          83 :     wdThread->wait();
     730                 :             : 
     731         [ +  - ]:          83 :     if (qApp)
     732                 :          83 :         qApp->removeEventFilter(this);
     733                 :             : 
     734                 :             :     // the main thread timer must be deleted from the main thread
     735         [ -  + ]:          83 :     if (auto *timer = WatchdogPrivate::s_mainThreadTimer.fetchAndStoreOrdered(nullptr))
     736                 :           0 :         delete timer;
     737                 :          83 : }
     738                 :             : 
     739                 :          88 : void Watchdog::setEventLoopTimeouts(std::chrono::milliseconds check,
     740                 :             :                                     std::chrono::milliseconds warn, std::chrono::milliseconds kill)
     741                 :             : {
     742                 :          88 :     QMetaObject::invokeMethod(d, [this, check, warn, kill]() {
     743                 :          88 :             d->setEventLoopTimeouts(check, warn, kill);
     744                 :          88 :         }, Qt::QueuedConnection);
     745                 :          88 : }
     746                 :             : 
     747                 :          88 : void Watchdog::setQuickWindowTimeouts(std::chrono::milliseconds check,
     748                 :             :                                       std::chrono::milliseconds warnSync, std::chrono::milliseconds killSync,
     749                 :             :                                       std::chrono::milliseconds warnRender, std::chrono::milliseconds killRender,
     750                 :             :                                       std::chrono::milliseconds warnSwap, std::chrono::milliseconds killSwap)
     751                 :             : {
     752                 :          88 :     QMetaObject::invokeMethod(d, [this, check, warnSync, killSync, warnRender, killRender, warnSwap, killSwap]() {
     753                 :          88 :             d->setQuickWindowTimeouts(check, warnSync, killSync, warnRender, killRender, warnSwap, killSwap);
     754                 :          88 :         }, Qt::QueuedConnection);
     755                 :          88 : }
     756                 :             : 
     757                 :        5824 : bool Watchdog::eventFilter(QObject *watched, QEvent *event)
     758                 :             : {
     759         [ +  + ]:        5824 :     if (event->type() == QEvent::PlatformSurface) {
     760         [ +  + ]:         139 :         auto surfaceEventType = static_cast<QPlatformSurfaceEvent *>(event)->surfaceEventType();
     761         [ +  + ]:         139 :         if (surfaceEventType == QPlatformSurfaceEvent::SurfaceCreated) {
     762         [ +  + ]:         128 :             if (auto *quickWindow = qobject_cast<QQuickWindow *>(watched)) {
     763                 :          84 :                 QPointer p(quickWindow);
     764                 :             :                 // We need a blocking invoke here to ensure that quickWindow is still valid
     765                 :             :                 // on the watchdog thread when calling watchQuickWindow().
     766                 :             :                 // Otherwise, the window could be destroyed prematurely and - even worse - we could
     767                 :             :                 // run into an ABA problem on quickWindow.
     768   [ +  -  +  - ]:         336 :                 QMetaObject::invokeMethod(d, [this, p]() {
     769                 :          84 :                         d->watchQuickWindow(p.get());
     770                 :          84 :                     }, Qt::BlockingQueuedConnection);
     771                 :          84 :             }
     772                 :             :         }
     773                 :             :     }
     774                 :        5824 :     return QObject::eventFilter(watched, event);
     775                 :             : }
     776                 :             : 
     777                 :             : QT_END_NAMESPACE_AM
        

Generated by: LCOV version 2.0-1