Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/pthread/emscripten_yield.c
6171 views
1
#include <stdbool.h>
2
#include <sched.h>
3
#include <threads.h>
4
5
#include <emscripten/threading.h>
6
7
#include "syscall.h"
8
#include "threading_internal.h"
9
10
static _Atomic pthread_t crashed_thread_id = NULL;
11
12
void _emscripten_thread_crashed() {
13
crashed_thread_id = pthread_self();
14
}
15
16
static void dummy(double now)
17
{
18
}
19
20
weak_alias(dummy, _emscripten_check_timers);
21
22
void _emscripten_yield(double now) {
23
int is_runtime_thread = emscripten_is_main_runtime_thread();
24
25
// When a secondary thread crashes, we need to be able to interrupt the main
26
// thread even if it's in a blocking/looping on a mutex. We want to avoid
27
// using the normal proxying mechanism to send this message since it can
28
// allocate (or otherwise itself crash) so use a low level atomic primitive
29
// for this signal.
30
if (is_runtime_thread) {
31
if (crashed_thread_id) {
32
// Mark the crashed thread as strongly referenced so that Node.js doesn't
33
// exit while the pthread is propagating the uncaught exception back to
34
// the main thread.
35
_emscripten_thread_set_strongref(crashed_thread_id);
36
// Return the event loop so we can handle the message from the crashed
37
// thread.
38
emscripten_exit_with_live_runtime();
39
}
40
41
// This is no-op in programs that don't include use of itimer/alarm.
42
_emscripten_check_timers(now);
43
44
// Assist other threads by executing proxied operations that are effectively
45
// singlethreaded.
46
emscripten_main_thread_process_queued_calls();
47
}
48
#ifdef EMSCRIPTEN_DYNAMIC_LINKING
49
else {
50
_emscripten_process_dlopen_queue();
51
}
52
#endif
53
}
54
55