summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMorten Sørvig <morten.sorvig@qt.io>2023-09-11 15:25:50 +0200
committerTor Arne Vestbø <tor.arne.vestbo@qt.io>2023-09-13 17:51:47 +0000
commitbd79ebb6d070aa6e4f86d9cb4eba6a65239aecbe (patch)
tree809c89f2df3df771272267bb7227c31aa7c42a76
parent37dde96e5f1cdf8208b5ab9b4d1edacda416adc3 (diff)
wasm: simplify calling runOnMainThread()
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 <void> version of qstweb::runTaskOnMainThread, like before the 141f0ca33 change. Change-Id: Id8324a17c27ffce8db7acf235ad4c9e465790e0b Reviewed-by: Lorn Potter <lorn.potter@gmail.com> Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io> Reviewed-by: Piotr Wierciński <piotr.wiercinski@qt.io>
-rw-r--r--src/corelib/kernel/qeventdispatcher_wasm.cpp66
-rw-r--r--src/corelib/kernel/qeventdispatcher_wasm_p.h1
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<void>([] { 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<void>([] { 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<bool>([]() { 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<bool>(
- [] {
- 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<void>(
- [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<void(void)> fn)
fn();
}
+void QEventDispatcherWasm::runOnMainThread(std::function<void(void)> fn)
+{
+#if QT_CONFIG(thread)
+ qstdweb::runTaskOnMainThread<void>(fn, &g_proxyingQueue);
+#else
+ qstdweb::runTaskOnMainThread<void>(fn);
+#endif
+}
+
// Runs a function asynchronously. Main thread only.
void QEventDispatcherWasm::runAsync(std::function<void(void)> 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<void(void)> fn);
+ static void runOnMainThread(std::function<void(void)> fn);
static void runAsync(std::function<void(void)> fn);
static void runOnMainThreadAsync(std::function<void(void)> fn);