We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4997

Summary: Emscripten: Use emscripten_sleep() call under SDL_Delay() to allow use Asyncify
Product: SDL Reporter: Vitaly Novichkov <admin>
Component: timerAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 Keywords: target-2.0.14
Version: HG 2.1   
Hardware: Other   
OS: Other   
Attachments: Very simple test of SDL_Delay() working
Adds using of emscripten_sleep() under SDL_Delay() on Emscripten platform to get the full support for asyncify work

Description Vitaly Novichkov 2020-02-18 14:55:33 UTC
Some time ago I was found that I am able to port my game project to Emscripten easily with using of Asyncify thing:

https://emscripten.org/docs/porting/asyncify.html

The magic will happen when using `emscripten_sleep()` call. Using regular SDL_Delay() will cause a stuck of the browser window and no update happens and the application got stuck.

A solution is taking the `emscripten_sleep()` call for use as a timer call.
Comment 1 Sam Lantinga 2020-03-02 23:23:07 UTC
Can you provide a tested patch?

Thanks!
Comment 2 Vitaly Novichkov 2020-03-02 23:29:44 UTC
Do you want I'll implement SDL_Delay() for Emscripten myself? Okay, I'll try make that tomorrow after sleep. (My time zone is utc+3). Will reply once I'll have a thing to post here. To test? Easy, I have a project where I can verify that.
Comment 3 Sam Lantinga 2020-03-03 01:25:51 UTC
Great, thanks!
Comment 4 Vitaly Novichkov 2020-03-03 10:13:06 UTC
One minor question: would it be good as a minor patch into timer/unix/SDL_systimer.c to use `emscripten_sleep()` instead of `nanosleep()` especially on Emscripten, rather building a brand-new module for Esmcripten specific purposes?
Comment 5 Vitaly Novichkov 2020-03-03 10:44:14 UTC
Okay, I did a very minor test, and it works with one condition: you must specify the "-s ASYNCIFY=1" compile flag to make `emscripten_sleep()` work, otherwise, the browser will throw an exception.

Having a next sample:
```
emcc kak.c -o a.html -O2 -L. -lSDL2 -I./include -I../include
```
browser will throw an exception:
```
Please compile your program with async support in order to use asynchronous operations like emscripten_sleep
```

And if compile this sample with using of this command:
```
emcc kak.c -s ASYNCIFY=1 -o a.html -O2 -L. -lSDL2 -I./include -I../include
```
it will work fine. I'll attach the patch and the test program I did use to verify the work of the thing.
Comment 6 Vitaly Novichkov 2020-03-03 10:45:14 UTC
Created attachment 4233 [details]
Very simple test of SDL_Delay() working
Comment 7 Vitaly Novichkov 2020-03-03 10:51:25 UTC
Created attachment 4234 [details]
Adds using of emscripten_sleep() under SDL_Delay() on Emscripten platform to get the full support for asyncify work
Comment 8 Vitaly Novichkov 2020-03-03 10:55:28 UTC
The note in the documentation which explains a necessary of -s ASYNCIFY to use these calls:

https://emscripten.org/docs/api_reference/emscripten.h.html#pseudo-synchronous-functions

Note that this flag is not needed by the library, and avoiding of SDL_Delay() usage will don't require the `-s ASYNCIFY` flag on a client side.
Comment 9 Sam Lantinga 2020-03-03 16:55:56 UTC
Maybe we should have an SDL hint for this?

Ryan, after the 2.0.12 release, can you take a look at this?
Comment 10 Ryan C. Gordon 2020-03-24 21:19:34 UTC
(In reply to Sam Lantinga from comment #9)
> Maybe we should have an SDL hint for this?
> 
> Ryan, after the 2.0.12 release, can you take a look at this?

I have to read up on this further, but my initial reading of Asyncify is that it's not something you want to rely on.

If Emscripten supplies a preprocessor define we can wrap this in (#ifdef ASYNCIFY or whatever), then it's a no-brainer to use it when building SDL in this mode.

--ryan.
Comment 11 Ryan C. Gordon 2020-06-27 20:30:49 UTC
Okay, the Emscripten team recently did something similar in their fork, so I'm pulling it upstream:

https://hg.libsdl.org/SDL/rev/e06e70f12286
https://hg.libsdl.org/SDL/rev/dd5a816a4d61

Basically this should Just Work as expected, with and without Asyncify, and there's an SDL hint if for some reason you need to use Asyncify elsewhere and turn it off inside SDL (but by default, if you're using Asyncify, SDL will use emscripten_sleep() as this bug requested).

--ryan.