From a8df9982901a8e9d01cce06c9ffe2b34e6ea23a7 Mon Sep 17 00:00:00 2001
From: Roland Winklmeier <Roland.M.Winklmeier@gmail.com>
Date: Mon, 27 Oct 2014 23:09:06 +0100
Subject: [PATCH] Make QTestEventLoop::exitLoop() thread-safe

QTestEventLoop::exitLoop() is used by QSignalSpy to stop event
processing when the connected signal has been received. The design
of QSignalSpy requires QTestEventLoop::exitLoop() to be
thread-safe, which it wasn't. When QSignalSpy is connected
to a signal in a different thread, exitLoop() was called from
the thread which emitted the signal and not the one in which
QTestEventLoop is running. This caused troubles when killing
the internal timer.

This patch adds a check in the beginning of exitLoop(). If
it is called from a different thread, it will post an event
into the message queue in which QTestEventLoop is running
and execute it there.

Change-Id: Icb8c8ff2f5344800ee6c6125b98c677c7a196c32
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
---
 src/testlib/qtesteventloop.h | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/testlib/qtesteventloop.h b/src/testlib/qtesteventloop.h
index 9e738d2c474..034655cc501 100644
--- a/src/testlib/qtesteventloop.h
+++ b/src/testlib/qtesteventloop.h
@@ -40,6 +40,7 @@
 #include <QtCore/qeventloop.h>
 #include <QtCore/qobject.h>
 #include <QtCore/qpointer.h>
+#include <QtCore/qthread.h>
 
 QT_BEGIN_NAMESPACE
 
@@ -101,6 +102,12 @@ inline void QTestEventLoop::enterLoopMSecs(int ms)
 
 inline void QTestEventLoop::exitLoop()
 {
+    if (thread() != QThread::currentThread())
+    {
+        QMetaObject::invokeMethod(this, "exitLoop", Qt::QueuedConnection);
+        return;
+    }
+
     if (timerId != -1)
         killTimer(timerId);
     timerId = -1;
-- 
GitLab