runOnAndroidMainThread(): Don't block QThreadPool::globalInstance() with timout awaiters

A waiting task on QThreadPool::globalInstance() will block the worker
thread that it was scheduled on, making it unavailable for productive
work. That's why one should only put CPU-bound tasks onto
QThreadPool::globalInstance(). When blocking nonetheless, use the
releaseThread()/reserveThread() trick to avoid deadlocks caused by the
pool running out of workers.

So, do that here.

Task-number: QTBUG-109586
Pick-to: 6.5 6.4 6.2
Change-Id: Ia2660c69e1f23b5df0c308576301aac6e05d4725
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
This commit is contained in:
Marc Mutz 2022-12-22 11:58:17 +01:00
parent 26ea6157e6
commit c049601348

View File

@ -176,6 +176,14 @@ QFuture<QVariant> QNativeInterface::QAndroidApplication::runOnAndroidMainThread(
loop.quit();
});
watcher.setFuture(future);
// we're going to sleep, make sure we don't block
// QThreadPool::globalInstance():
QThreadPool::globalInstance()->releaseThread();
const auto sg = qScopeGuard([] {
QThreadPool::globalInstance()->reserveThread();
});
loop.exec();
});
}