summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel
diff options
context:
space:
mode:
authorBogDan Vatra <bogdan@kde.org>2019-04-16 17:12:21 +0300
committerBogDan Vatra <bogdan@kdab.com>2019-04-25 11:14:28 +0000
commit9ca81260e9293f6ef9a8e8da08eda7bdbdb90a7d (patch)
tree97819ab3461384d0452fc6a3da570f8b94a5f43b /src/corelib/kernel
parent1269f9cd16485d0081f5027ffec914bcb696f658 (diff)
Android: Fix hang in runOnAndroidThreadSync and requestPermissionsSync
Keep spinning the main event loop if we can't acquire the semaphore, this way the Android UI thread can post events on it. Fixes: QTBUG-74076 Change-Id: Ia87e0535f94c67728176918ab928ff5ce8b00f8e Reviewed-by: VaL Doroshchuk <valentyn.doroshchuk@qt.io>
Diffstat (limited to 'src/corelib/kernel')
-rw-r--r--src/corelib/kernel/qjnihelpers.cpp16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/corelib/kernel/qjnihelpers.cpp b/src/corelib/kernel/qjnihelpers.cpp
index 712e8bbcab..7d278c69f2 100644
--- a/src/corelib/kernel/qjnihelpers.cpp
+++ b/src/corelib/kernel/qjnihelpers.cpp
@@ -45,6 +45,7 @@
#include "qsharedpointer.h"
#include "qvector.h"
#include "qthread.h"
+#include "qcoreapplication.h"
#include <QtCore/qrunnable.h>
#include <deque>
@@ -474,6 +475,17 @@ void QtAndroidPrivate::runOnAndroidThread(const QtAndroidPrivate::Runnable &runn
env->CallStaticVoidMethod(g_jNativeClass, g_runPendingCppRunnablesMethodID);
}
+static bool waitForSemaphore(int timeoutMs, QSharedPointer<QSemaphore> sem)
+{
+ while (timeoutMs > 0) {
+ if (sem->tryAcquire(1, 10))
+ return true;
+ timeoutMs -= 10;
+ QCoreApplication::processEvents();
+ }
+ return false;
+}
+
void QtAndroidPrivate::runOnAndroidThreadSync(const QtAndroidPrivate::Runnable &runnable, JNIEnv *env, int timeoutMs)
{
QSharedPointer<QSemaphore> sem(new QSemaphore);
@@ -481,7 +493,7 @@ void QtAndroidPrivate::runOnAndroidThreadSync(const QtAndroidPrivate::Runnable &
runnable();
sem->release();
}, env);
- sem->tryAcquire(1, timeoutMs);
+ waitForSemaphore(timeoutMs, sem);
}
void QtAndroidPrivate::requestPermissions(JNIEnv *env, const QStringList &permissions, const QtAndroidPrivate::PermissionsResultFunc &callbackFunc, bool directCall)
@@ -524,7 +536,7 @@ QtAndroidPrivate::PermissionsHash QtAndroidPrivate::requestPermissionsSync(JNIEn
*res = result;
sem->release();
}, true);
- if (sem->tryAcquire(1, timeoutMs))
+ if (waitForSemaphore(timeoutMs, sem))
return std::move(*res);
else // mustn't touch *res
return QHash<QString, QtAndroidPrivate::PermissionsResult>();