Introduce centralized means of checking for asyncify

The new function has an advantage of not requring EM_JS.

Change-Id: Ib9ad0e6b59cfe2e6864697a14b5cfdb39f62af2d
Reviewed-by: David Skoland <david.skoland@qt.io>
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
This commit is contained in:
Mikolaj Boc 2022-09-16 16:09:53 +02:00
parent 20ae1cd486
commit b1b61636b3
5 changed files with 23 additions and 23 deletions

View File

@ -7,6 +7,7 @@
#include <QtCore/qcoreapplication.h>
#include <QtCore/qthread.h>
#include <QtCore/qsocketnotifier.h>
#include <QtCore/private/qstdweb_p.h>
#include "emscripten.h"
#include <emscripten/html5.h>
@ -34,9 +35,10 @@ static bool g_is_asyncify_suspended = false;
#if defined(QT_STATIC)
EM_JS(bool, qt_have_asyncify_js, (), {
return typeof Asyncify != "undefined";
});
static bool useAsyncify()
{
return qstdweb::haveAsyncify();
}
EM_JS(void, qt_asyncify_suspend_js, (), {
let sleepFn = (wakeUp) => {
@ -60,7 +62,7 @@ EM_JS(void, qt_asyncify_resume_js, (), {
// EM_JS is not supported for side modules; disable asyncify
bool qt_have_asyncify_js()
static bool useAsyncify()
{
return false;
}
@ -77,15 +79,6 @@ void qt_asyncify_resume_js()
#endif // defined(QT_STATIC)
// Returns true if asyncify is available.
bool qt_have_asyncify()
{
static bool have_asyncify = []{
return qt_have_asyncify_js();
}();
return have_asyncify;
}
// Suspends the main thread until qt_asyncify_resume() is called. Returns
// false immediately if Qt has already suspended the main thread (recursive
// suspend is not supported by Emscripten). Returns true (after resuming),
@ -391,7 +384,7 @@ void QEventDispatcherWasm::handleApplicationExec()
void QEventDispatcherWasm::handleDialogExec()
{
if (!qt_have_asyncify()) {
if (!useAsyncify()) {
qWarning() << "Warning: exec() is not supported on Qt for WebAssembly in this configuration. Please build"
<< "with asyncify support, or use an asynchronous API like QDialog::open()";
emscripten_sleep(1); // This call never returns
@ -420,7 +413,7 @@ bool QEventDispatcherWasm::wait(int timeout)
#endif
Q_ASSERT(emscripten_is_main_runtime_thread());
Q_ASSERT(isMainThreadEventDispatcher());
if (qt_have_asyncify()) {
if (useAsyncify()) {
if (timeout > 0)
qWarning() << "QEventDispatcherWasm asyncify wait with timeout is not supported; timeout will be ignored"; // FIXME

View File

@ -667,6 +667,12 @@ namespace Promise {
}
}
bool haveAsyncify()
{
static bool HaveAsyncify = !emscripten::val::global("Asyncify").isUndefined();
return HaveAsyncify;
}
} // namespace qstdweb
QT_END_NAMESPACE

View File

@ -189,6 +189,8 @@ namespace qstdweb {
static emscripten::val savedWindow = emscripten::val::global("window");
return savedWindow;
}
bool haveAsyncify();
}
QT_END_NAMESPACE

View File

@ -7,6 +7,7 @@ qt_internal_add_manual_test(eventloop_auto
../../qtwasmtestlib/qtwasmtestlib.cpp
LIBRARIES
Qt::Core
Qt::CorePrivate
)
add_custom_command(
@ -28,6 +29,7 @@ qt_internal_add_manual_test(eventloop_auto_asyncify
../../qtwasmtestlib/qtwasmtestlib.cpp
LIBRARIES
Qt::Core
Qt::CorePrivate
)
target_link_options(eventloop_auto_asyncify PRIVATE -sASYNCIFY -Os)

View File

@ -7,6 +7,7 @@
#include <QtCore/QObject>
#include <QtCore/QThread>
#include <QtCore/QTimer>
#include <QtCore/private/qstdweb_p.h>
#include <qtwasmtestlib.h>
@ -14,10 +15,6 @@
const int timerTimeout = 10;
EM_JS(bool, have_asyncify, (), {
return typeof Asyncify != "undefined";
});
class WasmEventDispatcherTest: public QObject
{
Q_OBJECT
@ -242,7 +239,7 @@ void WasmEventDispatcherTest::timerSecondaryThread()
// Post an event to the main thread and asyncify wait for it
void WasmEventDispatcherTest::postEventAsyncify()
{
if (!have_asyncify()) {
if (!qstdweb::haveAsyncify()) {
QtWasmTest::completeTestFunction(QtWasmTest::TestResult::Skip, "requires asyncify");
return;
}
@ -259,7 +256,7 @@ void WasmEventDispatcherTest::postEventAsyncify()
// Create a timer on the main thread and asyncify wait for it
void WasmEventDispatcherTest::timerAsyncify()
{
if (!have_asyncify()) {
if (!qstdweb::haveAsyncify()) {
QtWasmTest::completeTestFunction(QtWasmTest::TestResult::Skip, "requires asyncify");
return;
}
@ -276,7 +273,7 @@ void WasmEventDispatcherTest::timerAsyncify()
// Asyncify wait in a loop
void WasmEventDispatcherTest::postEventAsyncifyLoop()
{
if (!have_asyncify()) {
if (!qstdweb::haveAsyncify()) {
QtWasmTest::completeTestFunction(QtWasmTest::TestResult::Skip, "requires asyncify");
return;
}
@ -296,7 +293,7 @@ void WasmEventDispatcherTest::postEventAsyncifyLoop()
// Asyncify wait for QThread::wait() / pthread_join()
void WasmEventDispatcherTest::threadAsyncifyWait()
{
if (!have_asyncify())
if (!qstdweb::haveAsyncify())
QtWasmTest::completeTestFunction(QtWasmTest::TestResult::Skip, "requires asyncify");
const int threadCount = 15;