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>
This commit is contained in:
Morten Sørvig 2023-09-11 15:25:50 +02:00 committed by Tor Arne Vestbø
parent 37dde96e5f
commit bd79ebb6d0
2 changed files with 30 additions and 35 deletions

View File

@ -16,12 +16,6 @@
using namespace std::chrono_literals; 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 QT_BEGIN_NAMESPACE
// using namespace emscripten; // using namespace emscripten;
@ -326,10 +320,8 @@ void QEventDispatcherWasm::registerSocketNotifier(QSocketNotifier *notifier)
bool wasEmpty = g_socketNotifiers.empty(); bool wasEmpty = g_socketNotifiers.empty();
g_socketNotifiers.insert({notifier->socket(), notifier}); g_socketNotifiers.insert({notifier->socket(), notifier});
if (wasEmpty) { if (wasEmpty)
qstdweb::runTaskOnMainThread<void>([] { setEmscriptenSocketCallbacks(); } runOnMainThread([] { setEmscriptenSocketCallbacks(); });
PROXYING_QUEUE_PARAM);
}
} }
void QEventDispatcherWasm::unregisterSocketNotifier(QSocketNotifier *notifier) void QEventDispatcherWasm::unregisterSocketNotifier(QSocketNotifier *notifier)
@ -344,10 +336,8 @@ void QEventDispatcherWasm::unregisterSocketNotifier(QSocketNotifier *notifier)
} }
} }
if (g_socketNotifiers.empty()) { if (g_socketNotifiers.empty())
qstdweb::runTaskOnMainThread<void>([] { clearEmscriptenSocketCallbacks(); } runOnMainThread([] { clearEmscriptenSocketCallbacks(); });
PROXYING_QUEUE_PARAM);
}
} }
void QEventDispatcherWasm::registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object) void QEventDispatcherWasm::registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object)
@ -543,16 +533,13 @@ bool QEventDispatcherWasm::wakeEventDispatcherThread()
if (useJspi()) { if (useJspi()) {
if (!qt_jspi_can_resume_js()) if (!qt_jspi_can_resume_js())
return false; return false;
return qstdweb::runTaskOnMainThread<bool>([]() { return qt_jspi_resume_js(); } runOnMainThread([]() { qt_jspi_resume_js(); });
PROXYING_QUEUE_PARAM); } 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; return true;
}
PROXYING_QUEUE_PARAM);
} }
// Process event activation callbacks for the main thread event dispatcher. // Process event activation callbacks for the main thread event dispatcher.
@ -637,8 +624,7 @@ void QEventDispatcherWasm::updateNativeTimer()
// Update the native timer for this thread/dispatcher. This must be // Update the native timer for this thread/dispatcher. This must be
// done on the main thread where we have access to native API. // done on the main thread where we have access to native API.
qstdweb::runTaskOnMainThread<void>( runOnMainThread([this, maintainNativeTimer]() {
[this, maintainNativeTimer]() {
Q_ASSERT(emscripten_is_main_runtime_thread()); Q_ASSERT(emscripten_is_main_runtime_thread());
// "this" may have been deleted, or may be about to be deleted. // "this" may have been deleted, or may be about to be deleted.
@ -648,8 +634,7 @@ void QEventDispatcherWasm::updateNativeTimer()
LOCK_GUARD(g_staticDataMutex); LOCK_GUARD(g_staticDataMutex);
if (isValidEventDispatcherPointer(this)) if (isValidEventDispatcherPointer(this))
maintainNativeTimer(); maintainNativeTimer();
} });
PROXYING_QUEUE_PARAM);
} }
// Static timer activation callback. Must be called on the main thread // Static timer activation callback. Must be called on the main thread
@ -909,6 +894,15 @@ void QEventDispatcherWasm::run(std::function<void(void)> fn)
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. // Runs a function asynchronously. Main thread only.
void QEventDispatcherWasm::runAsync(std::function<void(void)> fn) void QEventDispatcherWasm::runAsync(std::function<void(void)> fn)
{ {

View File

@ -88,6 +88,7 @@ private:
bool *selectForRead, bool *selectForWrite, bool *socketDisconnect); bool *selectForRead, bool *selectForWrite, bool *socketDisconnect);
static void run(std::function<void(void)> fn); 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 runAsync(std::function<void(void)> fn);
static void runOnMainThreadAsync(std::function<void(void)> fn); static void runOnMainThreadAsync(std::function<void(void)> fn);