diff --git a/src/corelib/kernel/qjnihelpers.cpp b/src/corelib/kernel/qjnihelpers.cpp index 712e8bbcab1..7d278c69f2c 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();