summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qeventdispatcher_wasm.cpp
blob: f4fcdbb8b29153876fbf142517c9af93cd059222 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qeventdispatcher_wasm_p.h"

#include <QtCore/private/qabstracteventdispatcher_p.h> // for qGlobalPostedEventsCount()
#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>
#include <emscripten/threading.h>
#include <emscripten/val.h>

using namespace std::chrono;
using namespace std::chrono_literals;

QT_BEGIN_NAMESPACE

// using namespace emscripten;

Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher");
Q_LOGGING_CATEGORY(lcEventDispatcherTimers, "qt.eventdispatcher.timers");

#if QT_CONFIG(thread)
#define LOCK_GUARD(M) std::lock_guard<std::mutex> lock(M)
#else
#define LOCK_GUARD(M)
#endif

// Emscripten asyncify currently supports one level of suspend -
// recursion is not permitted. We track the suspend state here
// on order to fail (more) gracefully, but we can of course only
// track Qts own usage of asyncify.
static bool g_is_asyncify_suspended = false;

#if defined(QT_STATIC)

static bool useAsyncify()
{
    return qstdweb::haveAsyncify();
}

static bool useJspi()
{
    return qstdweb::haveJspi();
}

// clang-format off
EM_ASYNC_JS(void, qt_jspi_suspend_js, (), {
    ++Module.qtJspiSuspensionCounter;

    await new Promise(resolve => {
        Module.qtAsyncifyWakeUp.push(resolve);
    });
});

EM_JS(bool, qt_jspi_resume_js, (), {
    if (!Module.qtJspiSuspensionCounter)
        return false;

    --Module.qtJspiSuspensionCounter;

    setTimeout(() => {
        const wakeUp = (Module.qtAsyncifyWakeUp ?? []).pop();
        if (wakeUp) wakeUp();
    });
    return true;
});

EM_JS(bool, qt_jspi_can_resume_js, (), {
    return Module.qtJspiSuspensionCounter > 0;
});

EM_JS(void, init_jspi_support_js, (), {
    Module.qtAsyncifyWakeUp = [];
    Module.qtJspiSuspensionCounter = 0;
});
// clang-format on

void initJspiSupport() {
    init_jspi_support_js();
}

Q_CONSTRUCTOR_FUNCTION(initJspiSupport);

// clang-format off
EM_JS(void, qt_asyncify_suspend_js, (), {
    if (Module.qtSuspendId === undefined)
        Module.qtSuspendId = 0;
    let sleepFn = (wakeUp) => {
        Module.qtAsyncifyWakeUp = wakeUp;
    };
    ++Module.qtSuspendId;
    return Asyncify.handleSleep(sleepFn);
});

EM_JS(void, qt_asyncify_resume_js, (), {
    let wakeUp = Module.qtAsyncifyWakeUp;
    if (wakeUp == undefined)
        return;
    Module.qtAsyncifyWakeUp = undefined;
    const suspendId = Module.qtSuspendId;

    // Delayed wakeup with zero-timer. Workaround/fix for
    // https://github.com/emscripten-core/emscripten/issues/10515
    setTimeout(() => {
        // Another suspend occurred while the timeout was in queue.
        if (Module.qtSuspendId !== suspendId)
            return;
        wakeUp();
    });
});
// clang-format on

#else

// EM_JS is not supported for side modules; disable asyncify

static bool useAsyncify()
{
    return false;
}

static bool useJspi()
{
    return false;
}

void qt_jspi_suspend_js()
{
    Q_UNREACHABLE();
}

bool qt_jspi_resume_js()
{
    Q_UNREACHABLE();
    return false;
}

bool qt_jspi_can_resume_js()
{
    Q_UNREACHABLE();
    return false;
}

void qt_asyncify_suspend_js()
{
    Q_UNREACHABLE();
}

void qt_asyncify_resume_js()
{
    Q_UNREACHABLE();
}

#endif // defined(QT_STATIC)

// 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),
// if the thread was suspended.
bool qt_asyncify_suspend()
{
    if (g_is_asyncify_suspended)
        return false;
    g_is_asyncify_suspended = true;
    qt_asyncify_suspend_js();
    return true;
}

// Wakes any currently suspended main thread. Returns true if the main
// thread was suspended, in which case it will now be asynchronously woken.
void qt_asyncify_resume()
{
    if (!g_is_asyncify_suspended)
        return;
    g_is_asyncify_suspended = false;
    qt_asyncify_resume_js();
}


Q_CONSTINIT QEventDispatcherWasm *QEventDispatcherWasm::g_mainThreadEventDispatcher = nullptr;
#if QT_CONFIG(thread)
Q_CONSTINIT QVector<QEventDispatcherWasm *> QEventDispatcherWasm::g_secondaryThreadEventDispatchers;
Q_CONSTINIT std::mutex QEventDispatcherWasm::g_staticDataMutex;
emscripten::ProxyingQueue QEventDispatcherWasm::g_proxyingQueue;
pthread_t QEventDispatcherWasm::g_mainThread;
#endif
// ### dynamic initialization:
std::multimap<int, QSocketNotifier *> QEventDispatcherWasm::g_socketNotifiers;
std::map<int, QEventDispatcherWasm::SocketReadyState> QEventDispatcherWasm::g_socketState;

QEventDispatcherWasm::QEventDispatcherWasm()
{
    // QEventDispatcherWasm operates in two main modes:
    // - On the main thread:
    //   The event dispatcher can process native events but can't
    //   block and wait for new events, unless asyncify is used.
    // - On a secondary thread:
    //   The event dispatcher can't process native events but can
    //   block and wait for new events.
    //
    // Which mode is determined by the calling thread: construct
    // the event dispatcher object on the thread where it will live.

    qCDebug(lcEventDispatcher) << "Creating QEventDispatcherWasm instance" << this
                               << "is main thread" << emscripten_is_main_runtime_thread();

    if (emscripten_is_main_runtime_thread()) {
        // There can be only one main thread event dispatcher at a time; in
        // addition the main instance is used by the secondary thread event
        // dispatchers so we set a global pointer to it.
        Q_ASSERT(g_mainThreadEventDispatcher == nullptr);
        g_mainThreadEventDispatcher = this;
#if QT_CONFIG(thread)
        g_mainThread = pthread_self();
#endif

        // Call the "onLoaded" JavaScript callback, unless startup tasks
        // have been registered which should complete first. Run async
        // to make sure event dispatcher construction (in particular any
        // subclass construction) has completed first.
        runAsync(callOnLoadedIfRequired);
    } else {
#if QT_CONFIG(thread)
        std::lock_guard<std::mutex> lock(g_staticDataMutex);
        g_secondaryThreadEventDispatchers.append(this);
#endif
    }
}

QEventDispatcherWasm::~QEventDispatcherWasm()
{
    qCDebug(lcEventDispatcher) << "Destroying QEventDispatcherWasm instance" << this;

    delete m_timerInfo;

#if QT_CONFIG(thread)
    if (isSecondaryThreadEventDispatcher()) {
        std::lock_guard<std::mutex> lock(g_staticDataMutex);
        g_secondaryThreadEventDispatchers.remove(g_secondaryThreadEventDispatchers.indexOf(this));
    } else
#endif
    {
        if (m_timerId > 0)
            emscripten_clear_timeout(m_timerId);
        if (!g_socketNotifiers.empty()) {
            qWarning("QEventDispatcherWasm: main thread event dispatcher deleted with active socket notifiers");
            clearEmscriptenSocketCallbacks();
            g_socketNotifiers.clear();
        }
        g_mainThreadEventDispatcher = nullptr;
        if (!g_socketNotifiers.empty()) {
            qWarning("QEventDispatcherWasm: main thread event dispatcher deleted with active socket notifiers");
            clearEmscriptenSocketCallbacks();
            g_socketNotifiers.clear();
        }

        g_socketState.clear();
    }
}

bool QEventDispatcherWasm::isMainThreadEventDispatcher()
{
    return this == g_mainThreadEventDispatcher;
}

bool QEventDispatcherWasm::isSecondaryThreadEventDispatcher()
{
    return this != g_mainThreadEventDispatcher;
}

bool QEventDispatcherWasm::isValidEventDispatcherPointer(QEventDispatcherWasm *eventDispatcher)
{
    if (eventDispatcher == g_mainThreadEventDispatcher)
        return true;
#if QT_CONFIG(thread)
    if (g_secondaryThreadEventDispatchers.contains(eventDispatcher))
        return true;
#endif
    return false;
}

bool QEventDispatcherWasm::processEvents(QEventLoop::ProcessEventsFlags flags)
{
    qCDebug(lcEventDispatcher) << "QEventDispatcherWasm::processEvents flags" << flags;

    emit awake();

    if (isMainThreadEventDispatcher()) {
        if (flags & QEventLoop::DialogExec)
            handleDialogExec();
        else if (flags & QEventLoop::ApplicationExec)
            handleApplicationExec();
    }

#if QT_CONFIG(thread)
    {
        // Reset wakeUp state: if wakeUp() was called at some point before
        // this then processPostedEvents() below will service that call.
        std::unique_lock<std::mutex> lock(m_mutex);
        m_wakeUpCalled = false;
    }
#endif

    processPostedEvents();

    // The processPostedEvents() call above may process an event which deletes the
    // application object and the event dispatcher; stop event processing in that case.
    if (!isValidEventDispatcherPointer(this))
        return false;

    if (m_interrupted) {
        m_interrupted = false;
        return false;
    }

    if (flags & QEventLoop::WaitForMoreEvents)
        wait();

    if (m_processTimers) {
        m_processTimers = false;
        processTimers();
    }

    return false;
}

void QEventDispatcherWasm::registerSocketNotifier(QSocketNotifier *notifier)
{
    LOCK_GUARD(g_staticDataMutex);

    bool wasEmpty = g_socketNotifiers.empty();
    g_socketNotifiers.insert({notifier->socket(), notifier});
    if (wasEmpty)
        runOnMainThread([] { setEmscriptenSocketCallbacks(); });
}

void QEventDispatcherWasm::unregisterSocketNotifier(QSocketNotifier *notifier)
{
    LOCK_GUARD(g_staticDataMutex);

    auto notifiers = g_socketNotifiers.equal_range(notifier->socket());
    for (auto it = notifiers.first; it != notifiers.second; ++it) {
        if (it->second == notifier) {
            g_socketNotifiers.erase(it);
            break;
        }
    }

    if (g_socketNotifiers.empty())
        runOnMainThread([] { clearEmscriptenSocketCallbacks(); });
}

void QEventDispatcherWasm::registerTimer(Qt::TimerId timerId, Duration interval, Qt::TimerType timerType, QObject *object)
{
#ifndef QT_NO_DEBUG
    if (qToUnderlying(timerId) < 1 || interval < 0ns || !object) {
        qWarning("QEventDispatcherWasm::registerTimer: invalid arguments");
        return;
    } else if (object->thread() != thread() || thread() != QThread::currentThread()) {
        qWarning("QEventDispatcherWasm::registerTimer: timers cannot be started from another "
                 "thread");
        return;
    }
#endif
    qCDebug(lcEventDispatcherTimers) << "registerTimer" << int(timerId) << interval << timerType << object;

    m_timerInfo->registerTimer(timerId, interval, timerType, object);
    updateNativeTimer();
}

bool QEventDispatcherWasm::unregisterTimer(Qt::TimerId timerId)
{
#ifndef QT_NO_DEBUG
    if (qToUnderlying(timerId) < 1) {
        qWarning("QEventDispatcherWasm::unregisterTimer: invalid argument");
        return false;
    } else if (thread() != QThread::currentThread()) {
        qWarning("QEventDispatcherWasm::unregisterTimer: timers cannot be stopped from another "
                 "thread");
        return false;
    }
#endif

    qCDebug(lcEventDispatcherTimers) << "unregisterTimer" << int(timerId);

    bool ans = m_timerInfo->unregisterTimer(timerId);
    updateNativeTimer();
    return ans;
}

bool QEventDispatcherWasm::unregisterTimers(QObject *object)
{
#ifndef QT_NO_DEBUG
    if (!object) {
        qWarning("QEventDispatcherWasm::unregisterTimers: invalid argument");
        return false;
    } else if (object->thread() != thread() || thread() != QThread::currentThread()) {
        qWarning("QEventDispatcherWasm::unregisterTimers: timers cannot be stopped from another "
                 "thread");
        return false;
    }
#endif

    qCDebug(lcEventDispatcherTimers) << "registerTimer" << object;

    bool ans = m_timerInfo->unregisterTimers(object);
    updateNativeTimer();
    return ans;
}

QList<QAbstractEventDispatcher::TimerInfoV2>
QEventDispatcherWasm::timersForObject(QObject *object) const
{
#ifndef QT_NO_DEBUG
    if (!object) {
        qWarning("QEventDispatcherWasm:registeredTimers: invalid argument");
        return {};
    }
#endif

    return m_timerInfo->registeredTimers(object);
}

QEventDispatcherWasm::Duration QEventDispatcherWasm::remainingTime(Qt::TimerId timerId) const
{
    return m_timerInfo->remainingDuration(timerId);
}

void QEventDispatcherWasm::interrupt()
{
    m_interrupted = true;
    wakeUp();
}

void QEventDispatcherWasm::wakeUp()
{
    // The event dispatcher thread may be blocked or suspended by
    // wait(), or control may have been returned to the browser's
    // event loop. Make sure the thread is unblocked or make it
    // process events.
    bool wasBlocked = wakeEventDispatcherThread();
    // JSPI does not need a scheduled call to processPostedEvents, as the stack is not unwound
    // at startup.
    if (!qstdweb::haveJspi() && !wasBlocked && isMainThreadEventDispatcher()) {
        {
            LOCK_GUARD(m_mutex);
            if (m_pendingProcessEvents)
                return;
            m_pendingProcessEvents = true;
        }
        runOnMainThreadAsync([this](){
            QEventDispatcherWasm::callProcessPostedEvents(this);
        });
    }
}

void QEventDispatcherWasm::handleApplicationExec()
{
    // Start the main loop, and then stop it on the first callback. This
    // is done for the "simulateInfiniteLoop" functionality where
    // emscripten_set_main_loop() throws a JS exception which returns
    // control to the browser while preserving the C++ stack.
    //
    // Note that we don't use asyncify here: Emscripten supports one level of
    // asyncify only and we want to reserve that for dialog exec() instead of
    // using it for the one qApp exec().
    // When JSPI is used, awaited async calls are allowed to be nested, so we
    // proceed normally.
    if (!qstdweb::haveJspi()) {
        const bool simulateInfiniteLoop = true;
        emscripten_set_main_loop([](){
            emscripten_pause_main_loop();
        }, 0, simulateInfiniteLoop);
    }
}

void QEventDispatcherWasm::handleDialogExec()
{
    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
    }
    // For the asyncify case we do nothing here and wait for events in wait()
}

// Blocks/suspends the calling thread. This is possible in two cases:
// - Caller is a secondary thread: block on m_moreEvents
// - Caller is the main thread and asyncify is enabled: suspend using qt_asyncify_suspend()
// Returns false if the wait timed out.
bool QEventDispatcherWasm::wait(int timeout)
{
#if QT_CONFIG(thread)
    using namespace std::chrono_literals;
    Q_ASSERT(QThread::currentThread() == thread());

    if (isSecondaryThreadEventDispatcher()) {
        std::unique_lock<std::mutex> lock(m_mutex);

        // If wakeUp() was called there might be pending events in the event
        // queue which should be processed. Don't block, instead return
        // so that the event loop can spin and call processEvents() again.
        if (m_wakeUpCalled)
            return true;

        auto wait_time = timeout > 0 ? timeout * 1ms : std::chrono::duration<int, std::micro>::max();
        bool wakeUpCalled = m_moreEvents.wait_for(lock, wait_time, [=] { return m_wakeUpCalled; });
        return wakeUpCalled;
    }
#endif
    Q_ASSERT(emscripten_is_main_runtime_thread());
    Q_ASSERT(isMainThreadEventDispatcher());
    if (useAsyncify()) {
        if (timeout > 0)
            qWarning() << "QEventDispatcherWasm asyncify wait with timeout is not supported; timeout will be ignored"; // FIXME

        if (useJspi()) {
            qt_jspi_suspend_js();
        } else {
            bool didSuspend = qt_asyncify_suspend();
            if (!didSuspend) {
                qWarning("QEventDispatcherWasm: current thread is already suspended; could not asyncify wait for events");
                return false;
            }
        }
        return true;
    } else {
        qWarning("QEventLoop::WaitForMoreEvents is not supported on the main thread without asyncify");
        Q_UNUSED(timeout);
    }
    return false;
}

// Wakes a blocked/suspended event dispatcher thread. Returns true if the
// thread is unblocked or was resumed, false if the thread state could not
// be determined.
bool QEventDispatcherWasm::wakeEventDispatcherThread()
{
#if QT_CONFIG(thread)
    if (isSecondaryThreadEventDispatcher()) {
        std::lock_guard<std::mutex> lock(m_mutex);
        m_wakeUpCalled = true;
        m_moreEvents.notify_one();
        return true;
    }
#endif
    Q_ASSERT(isMainThreadEventDispatcher());
    if (useJspi()) {

#if QT_CONFIG(thread)
        return qstdweb::runTaskOnMainThread<bool>(
                []() { return qt_jspi_can_resume_js() && qt_jspi_resume_js(); }, &g_proxyingQueue);
#else
        return qstdweb::runTaskOnMainThread<bool>(
                []() { return qt_jspi_can_resume_js() && qt_jspi_resume_js(); });
#endif

    } else {
        if (!g_is_asyncify_suspended)
            return false;
        runOnMainThread([]() { qt_asyncify_resume(); });
    }
    return true;
}

// Process event activation callbacks for the main thread event dispatcher.
// Must be called on the main thread.
void QEventDispatcherWasm::callProcessPostedEvents(void *context)
{
    Q_ASSERT(emscripten_is_main_runtime_thread());

    // Bail out if Qt has been shut down.
    if (!g_mainThreadEventDispatcher)
        return;

    // In the unlikely event that we get a callProcessPostedEvents() call for
    // a previous main thread event dispatcher (i.e. the QApplication
    // object was deleted and created again): just ignore it and return.
    if (context != g_mainThreadEventDispatcher)
        return;

    {
        LOCK_GUARD(g_mainThreadEventDispatcher->m_mutex);
        g_mainThreadEventDispatcher->m_pendingProcessEvents = false;
    }

    g_mainThreadEventDispatcher->processPostedEvents();
}

bool QEventDispatcherWasm::processPostedEvents()
{
    QCoreApplication::sendPostedEvents();
    return false;
}

void QEventDispatcherWasm::processTimers()
{
    m_timerInfo->activateTimers();
    updateNativeTimer(); // schedule next native timer, if any
}

// Updates the native timer based on currently registered Qt timers.
// Must be called on the event dispatcher thread.
void QEventDispatcherWasm::updateNativeTimer()
{
#if QT_CONFIG(thread)
    Q_ASSERT(QThread::currentThread() == thread());
#endif

    // Multiplex Qt timers down to a single native timer, maintained
    // to have a timeout corresponding to the shortest Qt timer. This
    // is done in two steps: first determine the target wakeup time
    // on the event dispatcher thread (since this thread has exclusive
    // access to m_timerInfo), and then call native API to set the new
    // wakeup time on the main thread.

    const std::optional<std::chrono::nanoseconds> wait = m_timerInfo->timerWait();
    const auto toWaitDuration = duration_cast<milliseconds>(wait.value_or(0ms));
    const auto newTargetTimePoint = m_timerInfo->currentTime + toWaitDuration;
    auto epochNsecs = newTargetTimePoint.time_since_epoch();
    auto newTargetTime = std::chrono::duration_cast<std::chrono::milliseconds>(epochNsecs);
    auto maintainNativeTimer = [this, wait, toWaitDuration, newTargetTime]() {
        Q_ASSERT(emscripten_is_main_runtime_thread());

        if (!wait) {
            if (m_timerId > 0) {
                emscripten_clear_timeout(m_timerId);
                m_timerId = 0;
                m_timerTargetTime = 0ms;
            }
            return;
        }

        if (m_timerTargetTime != 0ms && newTargetTime >= m_timerTargetTime)
            return; // existing timer is good

        qCDebug(lcEventDispatcherTimers)
                << "Created new native timer with wait" << toWaitDuration.count() << "ms"
                << "timeout" << newTargetTime.count() << "ms";
        emscripten_clear_timeout(m_timerId);
        m_timerId = emscripten_set_timeout(&QEventDispatcherWasm::callProcessTimers,
                                           toWaitDuration.count(), this);
        m_timerTargetTime = newTargetTime;
    };

    // Update the native timer for this thread/dispatcher. This must be
    // done on the main thread where we have access to native API.
    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
// and will then either process timers on the main thread or wake and
// process timers on a secondary thread.
void QEventDispatcherWasm::callProcessTimers(void *context)
{
    Q_ASSERT(emscripten_is_main_runtime_thread());

    // Note: "context" may be a stale pointer here,
    // take care before casting and dereferencing!

    // Process timers on this thread if this is the main event dispatcher
    if (reinterpret_cast<QEventDispatcherWasm *>(context) == g_mainThreadEventDispatcher) {
        g_mainThreadEventDispatcher->m_timerTargetTime = 0ms;
        g_mainThreadEventDispatcher->processTimers();
        return;
    }

    // Wake and process timers on the secondary thread if this a secondary thread dispatcher
#if QT_CONFIG(thread)
    std::lock_guard<std::mutex> lock(g_staticDataMutex);
    if (g_secondaryThreadEventDispatchers.contains(context)) {
        QEventDispatcherWasm *eventDispatcher = reinterpret_cast<QEventDispatcherWasm *>(context);
        eventDispatcher->m_timerTargetTime = 0ms;
        eventDispatcher->m_processTimers = true;
        eventDispatcher->wakeUp();
    }
#endif
}

void QEventDispatcherWasm::setEmscriptenSocketCallbacks()
{
    qCDebug(lcEventDispatcher) << "setEmscriptenSocketCallbacks";

    emscripten_set_socket_error_callback(nullptr, QEventDispatcherWasm::socketError);
    emscripten_set_socket_open_callback(nullptr, QEventDispatcherWasm::socketOpen);
    emscripten_set_socket_listen_callback(nullptr, QEventDispatcherWasm::socketListen);
    emscripten_set_socket_connection_callback(nullptr, QEventDispatcherWasm::socketConnection);
    emscripten_set_socket_message_callback(nullptr, QEventDispatcherWasm::socketMessage);
    emscripten_set_socket_close_callback(nullptr, QEventDispatcherWasm::socketClose);
}

void QEventDispatcherWasm::clearEmscriptenSocketCallbacks()
{
    qCDebug(lcEventDispatcher) << "clearEmscriptenSocketCallbacks";

    emscripten_set_socket_error_callback(nullptr, nullptr);
    emscripten_set_socket_open_callback(nullptr, nullptr);
    emscripten_set_socket_listen_callback(nullptr, nullptr);
    emscripten_set_socket_connection_callback(nullptr, nullptr);
    emscripten_set_socket_message_callback(nullptr, nullptr);
    emscripten_set_socket_close_callback(nullptr, nullptr);
}

void QEventDispatcherWasm::socketError(int socket, int err, const char* msg, void *context)
{
    Q_UNUSED(err);
    Q_UNUSED(msg);
    Q_UNUSED(context);

    // Emscripten makes socket callbacks while the main thread is busy-waiting for a mutex,
    // which can cause deadlocks if the callback code also tries to lock the same mutex.
    // This is most easily reproducible by adding print statements, where each print requires
    // taking a mutex lock. Work around this by running the callback asynchronously, i.e. by using
    // a native zero-timer, to make sure the main thread stack is completely unwond before calling
    // the Qt handler.
    // It is currently unclear if this problem is caused by code in Qt or in Emscripten, or
    // if this completely fixes the problem.
    runAsync([socket](){
        auto notifiersRange = g_socketNotifiers.equal_range(socket);
        std::vector<std::pair<int, QSocketNotifier *>> notifiers(notifiersRange.first, notifiersRange.second);
        for (auto [_, notifier]: notifiers) {
            QCoreApplication::postEvent(notifier, new QEvent(QEvent::SockAct));
        }
        setSocketState(socket, true, true);
    });
}

void QEventDispatcherWasm::socketOpen(int socket, void *context)
{
    Q_UNUSED(context);

    runAsync([socket](){
        auto notifiersRange = g_socketNotifiers.equal_range(socket);
        std::vector<std::pair<int, QSocketNotifier *>> notifiers(notifiersRange.first, notifiersRange.second);
        for (auto [_, notifier]: notifiers) {
            if (notifier->type() == QSocketNotifier::Write) {
                QCoreApplication::postEvent(notifier, new QEvent(QEvent::SockAct));
            }
        }
        setSocketState(socket, false, true);
    });
}

void QEventDispatcherWasm::socketListen(int socket, void *context)
{
    Q_UNUSED(socket);
    Q_UNUSED(context);
}

void QEventDispatcherWasm::socketConnection(int socket, void *context)
{
    Q_UNUSED(socket);
    Q_UNUSED(context);
}

void QEventDispatcherWasm::socketMessage(int socket, void *context)
{
    Q_UNUSED(context);

    runAsync([socket](){
        auto notifiersRange = g_socketNotifiers.equal_range(socket);
        std::vector<std::pair<int, QSocketNotifier *>> notifiers(notifiersRange.first, notifiersRange.second);
        for (auto [_, notifier]: notifiers) {
            if (notifier->type() == QSocketNotifier::Read) {
                QCoreApplication::postEvent(notifier, new QEvent(QEvent::SockAct));
            }
        }
        setSocketState(socket, true, false);
    });
}

void QEventDispatcherWasm::socketClose(int socket, void *context)
{
    Q_UNUSED(context);

    // Emscripten makes emscripten_set_socket_close_callback() calls to socket 0,
    // which is not a valid socket. see https://github.com/emscripten-core/emscripten/issues/6596
    if (socket == 0)
        return;

    runAsync([socket](){
        auto notifiersRange = g_socketNotifiers.equal_range(socket);
        std::vector<std::pair<int, QSocketNotifier *>> notifiers(notifiersRange.first, notifiersRange.second);
        for (auto [_, notifier]: notifiers)
            QCoreApplication::postEvent(notifier, new QEvent(QEvent::SockClose));

        setSocketState(socket, true, true);
        clearSocketState(socket);
    });
}

void QEventDispatcherWasm::setSocketState(int socket, bool setReadyRead, bool setReadyWrite)
{
    LOCK_GUARD(g_staticDataMutex);
    SocketReadyState &state = g_socketState[socket];

    // Additively update socket ready state, e.g. if it
    // was already ready read then it stays ready read.
    state.readyRead |= setReadyRead;
    state.readyWrite |= setReadyWrite;

    // Wake any waiters for the given readiness. The waiter consumes
    // the ready state, returning the socket to not-ready.
    if (QEventDispatcherWasm *waiter = state.waiter)
        if ((state.readyRead && state.waitForReadyRead) || (state.readyWrite && state.waitForReadyWrite))
            waiter->wakeEventDispatcherThread();
}

void QEventDispatcherWasm::clearSocketState(int socket)
{
    LOCK_GUARD(g_staticDataMutex);
    g_socketState.erase(socket);
}

void QEventDispatcherWasm::waitForSocketState(int timeout, int socket, bool checkRead, bool checkWrite,
                                              bool *selectForRead, bool *selectForWrite, bool *socketDisconnect)
{
    // Loop until the socket becomes readyRead or readyWrite. Wait for
    // socket activity if it currently is neither.
    while (true) {
        *selectForRead = false;
        *selectForWrite = false;

        {
            LOCK_GUARD(g_staticDataMutex);

            // Access or create socket state: we want to register that a thread is waitng
            // even if we have not received any socket callbacks yet.
            SocketReadyState &state = g_socketState[socket];
            if (state.waiter) {
                qWarning() << "QEventDispatcherWasm::waitForSocketState: a thread is already waiting";
                break;
            }

            bool shouldWait = true;
            if (checkRead && state.readyRead) {
                shouldWait = false;
                state.readyRead = false;
                *selectForRead = true;
            }
            if (checkWrite && state.readyWrite) {
                shouldWait = false;
                state.readyWrite = false;
                *selectForRead = true;
            }
            if (!shouldWait)
                break;

            state.waiter = this;
            state.waitForReadyRead = checkRead;
            state.waitForReadyWrite = checkWrite;
        }

        bool didTimeOut = !wait(timeout);
        {
            LOCK_GUARD(g_staticDataMutex);

            // Missing socket state after a wakeup means that the socket has been closed.
            auto it = g_socketState.find(socket);
            if (it == g_socketState.end()) {
                *socketDisconnect = true;
                break;
            }
            it->second.waiter = nullptr;
            it->second.waitForReadyRead = false;
            it->second.waitForReadyWrite = false;
        }

        if (didTimeOut)
            break;
    }
}

void QEventDispatcherWasm::socketSelect(int timeout, int socket, bool waitForRead, bool waitForWrite,
                                        bool *selectForRead, bool *selectForWrite, bool *socketDisconnect)
{
    QEventDispatcherWasm *eventDispatcher = static_cast<QEventDispatcherWasm *>(
        QAbstractEventDispatcher::instance(QThread::currentThread()));

    if (!eventDispatcher) {
        qWarning("QEventDispatcherWasm::socketSelect called without eventdispatcher instance");
        return;
    }

    eventDispatcher->waitForSocketState(timeout, socket, waitForRead, waitForWrite,
                                        selectForRead, selectForWrite, socketDisconnect);
}

namespace {
    int g_startupTasks = 0;
}

// The following functions manages sending the "qtLoaded" event/callback
// from qtloader.js on startup, once Qt initialization has been completed
// and the application is ready to display the first frame. This can be
// either as soon as the event loop is running, or later, if additional
// startup tasks (e.g. local font loading) have been registered.

void QEventDispatcherWasm::registerStartupTask()
{
    ++g_startupTasks;
}

void QEventDispatcherWasm::completeStarupTask()
{
    --g_startupTasks;
    callOnLoadedIfRequired();
}

void QEventDispatcherWasm::callOnLoadedIfRequired()
{
    if (g_startupTasks > 0)
        return;

    static bool qtLoadedCalled = false;
    if (qtLoadedCalled)
        return;
    qtLoadedCalled = true;

    Q_ASSERT(g_mainThreadEventDispatcher);
    g_mainThreadEventDispatcher->onLoaded();
}

void QEventDispatcherWasm::onLoaded()
{
    emscripten::val qt = emscripten::val::module_property("qt");
    if (qt.isUndefined())
        return;
    qt.call<void>("onLoaded");
}

namespace {
    void trampoline(void *context) {

        auto async_fn = [](void *context){
            std::function<void(void)> *fn = reinterpret_cast<std::function<void(void)> *>(context);
            (*fn)();
            delete fn;
        };

        emscripten_async_call(async_fn, context, 0);
    }
}

// Runs a function right away
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)
{
    trampoline(new std::function<void(void)>(fn));
}

// Runs a function on the main thread. The function always runs asynchronously,
// also if the calling thread is the main thread.
void QEventDispatcherWasm::runOnMainThreadAsync(std::function<void(void)> fn)
{
    void *context = new std::function<void(void)>(fn);
#if QT_CONFIG(thread)
    if (!emscripten_is_main_runtime_thread()) {
        g_proxyingQueue.proxyAsync(g_mainThread, [context]{
            trampoline(context);
        });
        return;
    }
#endif
    trampoline(context);
}

QT_END_NAMESPACE

#include "moc_qeventdispatcher_wasm_p.cpp"