From 9ca81260e9293f6ef9a8e8da08eda7bdbdb90a7d Mon Sep 17 00:00:00 2001 From: BogDan Vatra Date: Tue, 16 Apr 2019 17:12:21 +0300 Subject: 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 --- src/corelib/kernel/qjnihelpers.cpp | 16 ++++++++++++++-- 1 file 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 #include @@ -474,6 +475,17 @@ void QtAndroidPrivate::runOnAndroidThread(const QtAndroidPrivate::Runnable &runn env->CallStaticVoidMethod(g_jNativeClass, g_runPendingCppRunnablesMethodID); } +static bool waitForSemaphore(int timeoutMs, QSharedPointer 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 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(); -- cgit v1.2.3