From 628692a2f2950b7c25dfa45637d7d063792188cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20S=C3=B8rvig?= Date: Wed, 13 Sep 2023 16:06:27 +0200 Subject: [PATCH] wasm: make haveAsyncify() return true for any asyncify MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit User code usually don't need to differentiate between asyncify 1 or 2 (JSPI), since the differences are abstracted over by the wasm event dispatcher. haveJspi() returns true for JSPI only as before, and can be used to differentiate between the two. Add canBlockCallingThread(), which returns true also for secondary threads (which don't need asyncify to block). Change-Id: Ia37513f2d4c56ef6351c950b5fc31ad15fa389d9 Reviewed-by: Morten Johan Sørvig --- src/corelib/platform/wasm/qstdweb.cpp | 29 +++++++++++++++++++++------ src/corelib/platform/wasm/qstdweb_p.h | 1 + 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/corelib/platform/wasm/qstdweb.cpp b/src/corelib/platform/wasm/qstdweb.cpp index 05121ba277..5d17069eec 100644 --- a/src/corelib/platform/wasm/qstdweb.cpp +++ b/src/corelib/platform/wasm/qstdweb.cpp @@ -10,6 +10,8 @@ #include #include #include +#include + #include #include @@ -854,18 +856,33 @@ namespace Promise { } } -bool haveAsyncify() -{ - static bool HaveAsyncify = jsHaveAsyncify(); - return HaveAsyncify; -} - +// Asyncify and thread blocking: Normally, it's not possible to block the main +// thread, except if asyncify is enabled. Secondary threads can always block. +// +// haveAsyncify(): returns true if the main thread can block on QEventLoop::exec(), +// if either asyncify 1 or 2 (JSPI) is available. +// +// haveJspi(): returns true if asyncify 2 (JSPI) is available. +// +// canBlockCallingThread(): returns true if the calling thread can block on +// QEventLoop::exec(), using either asyncify or as a seconarday thread. bool haveJspi() { static bool HaveJspi = jsHaveJspi(); return HaveJspi; } +bool haveAsyncify() +{ + static bool HaveAsyncify = jsHaveAsyncify() || haveJspi(); + return HaveAsyncify; +} + +bool canBlockCallingThread() +{ + return haveAsyncify() || !emscripten_is_main_runtime_thread(); +} + std::shared_ptr readDataTransfer(emscripten::val webDataTransfer, std::function imageReader, std::function)> onDone) diff --git a/src/corelib/platform/wasm/qstdweb_p.h b/src/corelib/platform/wasm/qstdweb_p.h index a7a98ca042..7e94821272 100644 --- a/src/corelib/platform/wasm/qstdweb_p.h +++ b/src/corelib/platform/wasm/qstdweb_p.h @@ -215,6 +215,7 @@ namespace qstdweb { bool haveAsyncify(); bool haveJspi(); + bool canBlockCallingThread(); struct CancellationFlag {