Skip to content

Commit 14cb3a3

Browse files
authored
Fix maybeExit usage in browser main loop handling (#22438)
I think what happened here was that #18374 changed `maybeExit` to only be defined when `EXIT_RUNTIME` was set. This change was effectively reversed in #18782 but this location was not updated. Fixes: #22310
1 parent 37d578f commit 14cb3a3

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

src/library_browser.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ var LibraryBrowser = {
898898
#if OFFSCREEN_FRAMEBUFFER
899899
'emscripten_webgl_commit_frame',
900900
#endif
901-
#if EXIT_RUNTIME && !MINIMAL_RUNTIME
901+
#if !MINIMAL_RUNTIME
902902
'$maybeExit',
903903
#endif
904904
],
@@ -931,10 +931,10 @@ var LibraryBrowser = {
931931
function checkIsRunning() {
932932
if (thisMainLoopId < Browser.mainLoop.currentlyRunningMainloop) {
933933
#if RUNTIME_DEBUG
934-
dbg('main loop exiting..');
934+
dbg('main loop exiting');
935935
#endif
936936
{{{ runtimeKeepalivePop() }}}
937-
#if EXIT_RUNTIME && !MINIMAL_RUNTIME
937+
#if !MINIMAL_RUNTIME
938938
maybeExit();
939939
#endif
940940
return false;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <pthread.h>
4+
#include <stdbool.h>
5+
#include <emscripten.h>
6+
7+
_Atomic bool term = false;
8+
_Atomic bool loopthread_done = false;
9+
pthread_t thread;
10+
int loopcount = 0;
11+
12+
void loop() {
13+
printf("loop thread: %d\n", loopcount);
14+
15+
if (loopcount++ > 10) {
16+
// This should exit the thread and allow join to complete below, but it doesn't work
17+
emscripten_cancel_main_loop();
18+
printf("loop thread done\n");
19+
loopthread_done = true;
20+
}
21+
}
22+
23+
void* loopthread(void* arg) {
24+
emscripten_set_main_loop(loop, 0, false);
25+
return NULL;
26+
}
27+
28+
void mainloop() {
29+
if (loopthread_done) {
30+
printf("joinng..\n");
31+
pthread_join(thread, NULL);
32+
printf("join done\n");
33+
emscripten_cancel_main_loop();
34+
}
35+
}
36+
37+
int main() {
38+
pthread_create(&thread, NULL, loopthread, NULL);
39+
emscripten_set_main_loop(mainloop, 0, false);
40+
return 0;
41+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
loop thread: 0
2+
loop thread: 1
3+
loop thread: 2
4+
loop thread: 3
5+
loop thread: 4
6+
loop thread: 5
7+
loop thread: 6
8+
loop thread: 7
9+
loop thread: 8
10+
loop thread: 9
11+
loop thread: 10
12+
loop thread: 11
13+
loop thread done
14+
joinng..
15+
join done

test/test_other.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13424,6 +13424,10 @@ def test_pthread_icu(self):
1342413424
self.emcc_args.append('-pthread')
1342513425
self.do_other_test('test_pthread_icu.cpp')
1342613426

13427+
@node_pthreads
13428+
def test_pthread_set_main_loop(self):
13429+
self.do_other_test('test_pthread_set_main_loop.c')
13430+
1342713431
# unistd tests
1342813432

1342913433
def test_unistd_confstr(self):

0 commit comments

Comments
 (0)