From bd79ebb6d070aa6e4f86d9cb4eba6a65239aecbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20S=C3=B8rvig?= Date: Mon, 11 Sep 2023 15:25:50 +0200 Subject: wasm: simplify calling runOnMainThread() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a static runOnMainThread() function to QEventDispatcherWasm which looks at QT_CONFIG(thread) and calls the correct qstdweb overload. Then, we don't have to use a #define to enable or disable the proxyingQueue argument. Use the version of qstweb::runTaskOnMainThread, like before the 141f0ca33 change. Change-Id: Id8324a17c27ffce8db7acf235ad4c9e465790e0b Reviewed-by: Lorn Potter Reviewed-by: Tor Arne Vestbø Reviewed-by: Piotr Wierciński --- src/corelib/kernel/qeventdispatcher_wasm.cpp | 66 +++++++++++++--------------- src/corelib/kernel/qeventdispatcher_wasm_p.h | 1 + 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/corelib/kernel/qeventdispatcher_wasm.cpp b/src/corelib/kernel/qeventdispatcher_wasm.cpp index 0245b0be61..0bf08eb8e5 100644 --- a/src/corelib/kernel/qeventdispatcher_wasm.cpp +++ b/src/corelib/kernel/qeventdispatcher_wasm.cpp @@ -16,12 +16,6 @@ using namespace std::chrono_literals; -#if QT_CONFIG(thread) -#define PROXYING_QUEUE_PARAM , &g_proxyingQueue -#else -#define PROXYING_QUEUE_PARAM -#endif // QT_CONFIG(thread) - QT_BEGIN_NAMESPACE // using namespace emscripten; @@ -326,10 +320,8 @@ void QEventDispatcherWasm::registerSocketNotifier(QSocketNotifier *notifier) bool wasEmpty = g_socketNotifiers.empty(); g_socketNotifiers.insert({notifier->socket(), notifier}); - if (wasEmpty) { - qstdweb::runTaskOnMainThread([] { setEmscriptenSocketCallbacks(); } - PROXYING_QUEUE_PARAM); - } + if (wasEmpty) + runOnMainThread([] { setEmscriptenSocketCallbacks(); }); } void QEventDispatcherWasm::unregisterSocketNotifier(QSocketNotifier *notifier) @@ -344,10 +336,8 @@ void QEventDispatcherWasm::unregisterSocketNotifier(QSocketNotifier *notifier) } } - if (g_socketNotifiers.empty()) { - qstdweb::runTaskOnMainThread([] { clearEmscriptenSocketCallbacks(); } - PROXYING_QUEUE_PARAM); - } + if (g_socketNotifiers.empty()) + runOnMainThread([] { clearEmscriptenSocketCallbacks(); }); } void QEventDispatcherWasm::registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object) @@ -543,16 +533,13 @@ bool QEventDispatcherWasm::wakeEventDispatcherThread() if (useJspi()) { if (!qt_jspi_can_resume_js()) return false; - return qstdweb::runTaskOnMainThread([]() { return qt_jspi_resume_js(); } - PROXYING_QUEUE_PARAM); + runOnMainThread([]() { qt_jspi_resume_js(); }); + } else { + if (!g_is_asyncify_suspended) + return false; + runOnMainThread([]() { qt_asyncify_resume(); }); } - return g_is_asyncify_suspended - && qstdweb::runTaskOnMainThread( - [] { - qt_asyncify_resume(); - return true; - } - PROXYING_QUEUE_PARAM); + return true; } // Process event activation callbacks for the main thread event dispatcher. @@ -637,19 +624,17 @@ void QEventDispatcherWasm::updateNativeTimer() // Update the native timer for this thread/dispatcher. This must be // done on the main thread where we have access to native API. - qstdweb::runTaskOnMainThread( - [this, maintainNativeTimer]() { - Q_ASSERT(emscripten_is_main_runtime_thread()); - - // "this" may have been deleted, or may be about to be deleted. - // Check if the pointer we have is still a valid event dispatcher, - // and keep the mutex locked while updating the native timer to - // prevent it from being deleted. - LOCK_GUARD(g_staticDataMutex); - if (isValidEventDispatcherPointer(this)) - maintainNativeTimer(); - } - PROXYING_QUEUE_PARAM); + runOnMainThread([this, maintainNativeTimer]() { + Q_ASSERT(emscripten_is_main_runtime_thread()); + + // "this" may have been deleted, or may be about to be deleted. + // Check if the pointer we have is still a valid event dispatcher, + // and keep the mutex locked while updating the native timer to + // prevent it from being deleted. + LOCK_GUARD(g_staticDataMutex); + if (isValidEventDispatcherPointer(this)) + maintainNativeTimer(); + }); } // Static timer activation callback. Must be called on the main thread @@ -909,6 +894,15 @@ void QEventDispatcherWasm::run(std::function fn) fn(); } +void QEventDispatcherWasm::runOnMainThread(std::function fn) +{ +#if QT_CONFIG(thread) + qstdweb::runTaskOnMainThread(fn, &g_proxyingQueue); +#else + qstdweb::runTaskOnMainThread(fn); +#endif +} + // Runs a function asynchronously. Main thread only. void QEventDispatcherWasm::runAsync(std::function fn) { diff --git a/src/corelib/kernel/qeventdispatcher_wasm_p.h b/src/corelib/kernel/qeventdispatcher_wasm_p.h index 598e15dc6b..27cf4552e9 100644 --- a/src/corelib/kernel/qeventdispatcher_wasm_p.h +++ b/src/corelib/kernel/qeventdispatcher_wasm_p.h @@ -88,6 +88,7 @@ private: bool *selectForRead, bool *selectForWrite, bool *socketDisconnect); static void run(std::function fn); + static void runOnMainThread(std::function fn); static void runAsync(std::function fn); static void runOnMainThreadAsync(std::function fn); -- cgit v1.2.3