Skip to content

Commit c1a2f16

Browse files
committed
Decrelment keepalive counter in emscripten_clear_timeout
This change also cleans up the emscripten tests relating to the event loop. The browser tests were only being run in `PROXY_TO_PTHREAD` mode for some reason even though these tests should pass on the main thread. In test_other the `PROXY_TO_PTHREAD` flags were being passed as args (rather then emcc_args). Likely a copy/paste bug.
1 parent 88f9144 commit c1a2f16

14 files changed

+81
-83
lines changed

src/lib/libeventloop.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ LibraryJSEventLoop = {
2121
}, timeout);
2222
},
2323

24+
$safeClearTimeout: (id) => {
25+
// TODO: Track outstanding timeout IDs so that we can avoid calling
26+
// runtimeKeepalivePop trice for the same timeout.
27+
{{{ runtimeKeepalivePop() }}}
28+
clearTimeout(id);
29+
},
30+
2431
// Just like setImmediate but returns an i32 that can be passed back
2532
// to wasm rather than a JS object.
2633
$setImmediateWrapped: (func) => {
@@ -119,13 +126,7 @@ LibraryJSEventLoop = {
119126
emscripten_set_timeout: (cb, msecs, userData) =>
120127
safeSetTimeout(() => {{{ makeDynCall('vp', 'cb') }}}(userData), msecs),
121128

122-
#if AUDIO_WORKLET
123-
// Use a wrapper function here since simply aliasing `clearTimeout` would
124-
// cause the module to fail to load in the audio worklet context.
125-
emscripten_clear_timeout: (id) => clearTimeout(id),
126-
#else
127-
emscripten_clear_timeout: 'clearTimeout',
128-
#endif
129+
emscripten_clear_timeout: '$safeClearTimeout',
129130

130131
emscripten_set_timeout_loop__deps: ['$callUserCallback', 'emscripten_get_now'],
131132
emscripten_set_timeout_loop: (cb, msecs, userData) => {

src/lib/libsdl.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3579,9 +3579,10 @@ var LibrarySDL = {
35793579
() => {{{ makeDynCall('iip', 'callback') }}}(interval, param),
35803580
interval),
35813581

3582+
SDL_RemoveTimer__deps: ['$safeClearTimeout'],
35823583
SDL_RemoveTimer__proxy: 'sync',
35833584
SDL_RemoveTimer: (id) => {
3584-
clearTimeout(id);
3585+
safeClearTimeout(id);
35853586
return true;
35863587
},
35873588

test/emscripten_performance_now.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ void test(void *userData) {
1212
double now3 = emscripten_date_now();
1313
assert(now3 >= dateNow + 100);
1414

15-
#ifdef REPORT_RESULT
16-
REPORT_RESULT(0);
17-
#endif
15+
exit(0);
1816
}
1917

2018
int main() {
@@ -30,4 +28,5 @@ int main() {
3028
#else
3129
emscripten_set_timeout(test, 200, 0);
3230
#endif
31+
return 99;
3332
}

test/emscripten_set_interval.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
int funcExecuted = 0;
88

99
void testDone(void *userData) {
10+
printf("testDone\n");
1011
assert((long)userData == 2);
1112
assert(funcExecuted == 10);
1213
exit(0);
@@ -15,6 +16,7 @@ void testDone(void *userData) {
1516
long intervalId = 0;
1617

1718
void tick(void *userData) {
19+
printf("tick: %d\n", funcExecuted);
1820
assert((long)userData == 1);
1921
++funcExecuted;
2022
if (funcExecuted == 10) {
@@ -28,6 +30,5 @@ void tick(void *userData) {
2830

2931
int main() {
3032
intervalId = emscripten_set_interval(tick, 100, (void*)1);
31-
emscripten_exit_with_live_runtime();
3233
return 99;
3334
}

test/emscripten_set_timeout.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
#include <emscripten/emscripten.h>
22
#include <emscripten/html5.h>
3-
#include <emscripten/em_asm.h>
43
#include <assert.h>
54
#include <stdlib.h>
5+
#include <stdio.h>
66

77
int func1Executed = 0;
88
int func2Executed = 0;
99

1010
void func1(void *userData);
1111

1212
void func2(void *userData) {
13+
printf("func2: %d\n", func2Executed);
1314
assert((long)userData == 2);
1415
++func2Executed;
1516

16-
if (func2Executed == 1)
17-
{
17+
if (func2Executed == 1) {
1818
// Test canceling a setTimeout: register a callback but then cancel it immediately
1919
long id = emscripten_set_timeout(func1, 10, (void*)2);
2020
emscripten_clear_timeout(id);
2121

2222
emscripten_set_timeout(func2, 100, (void*)2);
2323
}
24-
if (func2Executed == 2)
25-
{
24+
25+
if (func2Executed == 2) {
2626
assert(func1Executed == 1);
2727
exit(0);
2828
}
2929
}
3030

3131
void func1(void *userData) {
32+
printf("func1\n");
3233
assert((long)userData == 1);
3334
++func1Executed;
3435

@@ -39,6 +40,5 @@ void func1(void *userData) {
3940

4041
int main() {
4142
emscripten_set_timeout(func1, 100, (void*)1);
42-
emscripten_exit_with_live_runtime();
4343
return 99;
4444
}

test/emscripten_set_timeout_loop.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
#include <emscripten/emscripten.h>
22
#include <emscripten/html5.h>
3-
#include <emscripten/em_asm.h>
43
#include <assert.h>
54
#include <stdlib.h>
5+
#include <stdio.h>
66

77
double previousSetTimeouTime = 0;
88
int funcExecuted = 0;
99

1010
void testDone(void *userData) {
11+
printf("testDone\n");
1112
assert((long)userData == 2);
1213
assert(funcExecuted == 10);
14+
emscripten_runtime_keepalive_pop();
1315
exit(0);
1416
}
1517

1618
bool tick(double time, void *userData) {
19+
printf("tick: %d\n", funcExecuted);
1720
assert(time >= previousSetTimeouTime);
1821
previousSetTimeouTime = time;
1922
assert((long)userData == 1);
2023
++funcExecuted;
21-
if (funcExecuted == 10)
22-
{
24+
if (funcExecuted == 10) {
2325
emscripten_set_timeout(testDone, 300, (void*)2);
2426
}
2527
return funcExecuted < 10;
2628
}
2729

2830
int main() {
2931
emscripten_set_timeout_loop(tick, 100, (void*)1);
30-
emscripten_exit_with_live_runtime();
32+
emscripten_runtime_keepalive_push();
3133
return 99;
3234
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7984
1+
7989
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
21327
1+
21344
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6330
1+
6336
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
16673
1+
16690

0 commit comments

Comments
 (0)