Skip to content

Commit b4e0fbf

Browse files
authored
Improve SDL_Delay and add testing. NFC (#25973)
- Cleanup implemenation of SDL_Delay - Add testing of SDL_Timer/SDL_Delay under SDL1 - Add testing of SDL_Timer/SDL_Delay when running on pthread - Rename test_sdl2_timer.c -> test_sdl_timer.c since it works with both SDL1 and SDL2 - Avoid taking the address of stack locals in test_sdl_timer.c. Depends on #25971
1 parent bbf552e commit b4e0fbf

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

src/lib/libsdl.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,12 +1744,15 @@ var LibrarySDL = {
17441744
},
17451745

17461746
#if ASYNCIFY
1747-
SDL_Delay__deps: ['emscripten_sleep'],
1748-
SDL_Delay__async: true,
1749-
SDL_Delay: (delay) => _emscripten_sleep(delay),
1747+
SDL_Delay: 'emscripten_sleep',
17501748
#else
1749+
#if ASSERTIONS
1750+
SDL_Delay__deps: ['$warnOnce'],
1751+
#endif
17511752
SDL_Delay: (delay) => {
1752-
if (!ENVIRONMENT_IS_WORKER) abort('SDL_Delay called on the main thread! Potential infinite loop, quitting. (consider building with async support like ASYNCIFY)');
1753+
#if ASSERTIONS
1754+
if (!ENVIRONMENT_IS_WORKER) warnOnce('SDL_Delay called on the main thread! Potential infinite loop, quitting. (consider building with async support like ASYNCIFY)');
1755+
#endif
17531756
// horrible busy-wait, but in a worker it at least does not block rendering
17541757
var now = Date.now();
17551758
while (Date.now() - now < delay) {}
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
#include <assert.h>
99
#include <stdio.h>
1010
#include <emscripten.h>
11+
#include <emscripten/eventloop.h>
12+
#ifdef USE_SDL2
13+
#include <SDL2/SDL.h>
14+
#else
1115
#include <SDL/SDL.h>
16+
#endif
1217

1318
Uint32 start_time;
1419

@@ -25,7 +30,8 @@ Uint32 SDLCALL timer_callback(Uint32 interval, void *param) {
2530
emscripten_force_exit(0);
2631
}
2732

28-
void nop(void) {}
33+
int badret = 4;
34+
int goodret = 5;
2935

3036
int main(int argc, char** argv) {
3137
SDL_Init(SDL_INIT_TIMER);
@@ -36,13 +42,16 @@ int main(int argc, char** argv) {
3642
// not enough ticks from busy-wait
3743
assert(ticks2 >= ticks1 + 4);
3844

39-
int badret = 4;
40-
int goodret = 5;
41-
4245
start_time = SDL_GetTicks();
4346
SDL_TimerID badtimer = SDL_AddTimer(500, timer_callback, &badret);
4447
SDL_TimerID goodtimer = SDL_AddTimer(1000, timer_callback, &goodret);
4548
SDL_RemoveTimer(badtimer);
4649

50+
#if defined(USE_SDL2) && defined(_REENTRANT)
51+
// When threading is enabled SDL2 uses its generic thread-based timer
52+
// system. This does not keep the runtime alive like the non-threaded (or
53+
// SDL1) implementation based on emscripten_set_timeout.
54+
emscripten_runtime_keepalive_push();
55+
#endif
4756
return 99;
4857
}

test/codesize/test_codesize_hello_dylink_all.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"a.out.js": 245617,
2+
"a.out.js": 245482,
33
"a.out.nodebug.wasm": 573657,
4-
"total": 819274,
4+
"total": 819139,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

test/test_browser.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3055,8 +3055,13 @@ def test_sdl2_pumpevents(self):
30553055
# key events should be detected using SDL_PumpEvents
30563056
self.btest_exit('test_sdl2_pumpevents.c', cflags=['--pre-js', test_file('browser/fake_events.js'), '-sUSE_SDL=2'])
30573057

3058+
@also_with_proxy_to_pthread
3059+
def test_sdl_timer(self):
3060+
self.btest_exit('test_sdl_timer.c', cflags=['-sUSE_SDL'])
3061+
3062+
@also_with_proxy_to_pthread
30583063
def test_sdl2_timer(self):
3059-
self.btest_exit('test_sdl2_timer.c', cflags=['-sUSE_SDL=2'])
3064+
self.btest_exit('test_sdl_timer.c', cflags=['-sUSE_SDL=2', '-DUSE_SDL2'])
30603065

30613066
def test_sdl2_canvas_size(self):
30623067
self.btest_exit('test_sdl2_canvas_size.c', cflags=['-sUSE_SDL=2'])

0 commit comments

Comments
 (0)