Skip to content

Commit

Permalink
Use Asyncify by default, as replacement for Emterpreter Sync
Browse files Browse the repository at this point in the history
Emterpreter was removed in Emscripten 1.39.17. The Emterpreter code
can run using Asyncify with trivial changes. Since this works well
and creates a more fully functioning DOSBox, it is now the default.
Use ./configure --disable-asyncify if you don't want it.
  • Loading branch information
dreamlayers committed Dec 6, 2022
1 parent af97479 commit af3aef2
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 18 deletions.
9 changes: 9 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,14 @@ AC_ARG_ENABLE(
[], [enable_sync=no])
AM_CONDITIONAL([EMTERPRETER], [test "x$enable_sync" != "xno"])

AH_TEMPLATE([EM_ASYNCIFY],[Compile using Emscripten asyncify.])
AC_ARG_ENABLE(
[asyncify],
[AC_HELP_STRING([--disable-asyncify],
[Disable Emscripten asyncify])],
[], [enable_asyncify=yes])
AM_CONDITIONAL([ASYNCIFY], [test "x$enable_asyncify" != "xno"])

AC_ARG_ENABLE(
[wasm],
[AC_HELP_STRING([--disable-wasm],
Expand All @@ -641,6 +649,7 @@ AM_CONDITIONAL([WEBASSEMBLY], [test "x$enable_wasm" == "xyes"])
# host compiler, so those flags are set at the end.
AS_IF([test "x$enable_emscripten" = "xyes"],[
AS_IF([test "x$enable_sync" != "xno"], [AC_DEFINE([EMTERPRETER_SYNC],[1])])
AS_IF([test "x$enable_asyncify" != "xno"], [AC_DEFINE([EM_ASYNCIFY],[1])])
AS_IF([test "x$with_sdl2" != "xno"],[
AS_IF([test "x$with_sdl2" = "xyes"],
[CXXFLAGS="$CXXFLAGS -s USE_SDL=2"],
Expand Down
2 changes: 1 addition & 1 deletion include/dosbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Section;
typedef Bitu (LoopHandler)(void);

void DOSBOX_RunMachine();
#if defined(EMSCRIPTEN) && defined(EMTERPRETER_SYNC)
#if defined(EMSCRIPTEN) && (defined(EMTERPRETER_SYNC) || defined(EM_ASYNCIFY))
/* This is for cases where RunMachine is called from code not using
* emterpreter. There, emscripten_sleep() is prohibited and emulation
* will be aborted with a timeout error if this takes too long.
Expand Down
3 changes: 3 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ else
# Memory init file is huge and it doesn't save bandwith even if using gzip.
# It is only required when using emterpreter.
dosbox_LDFLAGS+=--memory-init-file 0
if ASYNCIFY
dosbox_LDFLAGS+=-s ASYNCIFY=1 -s FETCH=1
endif
endif
if WEBASSEMBLY
dosbox_LDFLAGS+=-s WASM=1
Expand Down
4 changes: 3 additions & 1 deletion src/debug/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <iomanip>
#include <string>
#include <sstream>
#if defined(EMSCRIPTEN) && defined(EMTERPRETER_SYNC)
#if defined(EMSCRIPTEN) && (defined(EMTERPRETER_SYNC) || defined(EM_ASYNCIFY))
#include <emscripten.h>
#endif
using namespace std;
Expand Down Expand Up @@ -1826,6 +1826,8 @@ Bitu DEBUG_Loop(void) {
#if defined(EMSCRIPTEN)
#if defined(EMTERPRETER_SYNC)
emscripten_sleep_with_yield(1);
#elif defined(EM_ASYNCIFY)
emscripten_sleep(1);
#endif
#else
SDL_Delay(1);
Expand Down
17 changes: 11 additions & 6 deletions src/dos/dos_programs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <ctype.h>
#include <string>
#include <vector>
#if defined(EMSCRIPTEN) && defined(EMTERPRETER_SYNC)
#if defined(EMSCRIPTEN) && (defined(EMTERPRETER_SYNC) || defined(EM_ASYNCIFY))
#include <emscripten.h>
#include <emscripten/fetch.h>
#include "dos_inc.h"
Expand Down Expand Up @@ -1535,7 +1535,7 @@ static void KEYB_ProgramStart(Program * * make) {
*make=new KEYB;
}

#if defined(EMSCRIPTEN) && defined(EMTERPRETER_SYNC)
#if defined(EMSCRIPTEN) && (defined(EMTERPRETER_SYNC) || defined(EM_ASYNCIFY))
class WGET : public Program {
public:
void Run(void);
Expand Down Expand Up @@ -1598,7 +1598,12 @@ void WGET::Run(void) {
attr.onerror = downloadFailed;
fetchdone = false;
emscripten_fetch_t *fetch = emscripten_fetch(&attr, url.c_str());
while (!fetchdone) emscripten_sleep_with_yield(10);
while (!fetchdone)
#ifdef EMTERPRETER_SYNC
emscripten_sleep_with_yield(10);
#elif defined(EM_ASYNCIFY)
emscripten_sleep(10);
#endif
if (fetchsuccess) {
Bit16u fhandle;
if (!DOS_CreateFile(outname.c_str(),OPEN_WRITE,&fhandle)) {
Expand Down Expand Up @@ -1640,7 +1645,7 @@ void WGET::Run(void) {
static void WGET_ProgramStart(Program * * make) {
*make=new WGET;
}
#endif // defined(EMSCRIPTEN) && defined(EMTERPRETER_SYNC)
#endif // defined(EMSCRIPTEN) && (defined(EMTERPRETER_SYNC) || defined(EM_ASYNCIFY))

void DOS_SetupPrograms(void) {
/*Add Messages */
Expand Down Expand Up @@ -1863,7 +1868,7 @@ void DOS_SetupPrograms(void) {
MSG_Add("PROGRAM_KEYB_INVALIDFILE","Keyboard file %s invalid\n");
MSG_Add("PROGRAM_KEYB_LAYOUTNOTFOUND","No layout in %s for codepage %i\n");
MSG_Add("PROGRAM_KEYB_INVCPFILE","None or invalid codepage file for layout %s\n\n");
#if defined(EMSCRIPTEN) && defined(EMTERPRETER_SYNC)
#if defined(EMSCRIPTEN) && (defined(EMTERPRETER_SYNC) || defined(EM_ASYNCIFY))
MSG_Add("PROGRAM_WGET_SHOWHELP",
"\033[32;1mWGET\033[0m [-o FILENAME] URL\n\n"
"Downloads file from URL and saves it to the file system.\n"
Expand All @@ -1887,7 +1892,7 @@ void DOS_SetupPrograms(void) {
PROGRAMS_MakeFile("LOADROM.COM", LOADROM_ProgramStart);
PROGRAMS_MakeFile("IMGMOUNT.COM", IMGMOUNT_ProgramStart);
PROGRAMS_MakeFile("KEYB.COM", KEYB_ProgramStart);
#if defined(EMSCRIPTEN) && defined(EMTERPRETER_SYNC)
#if defined(EMSCRIPTEN) && (defined(EMTERPRETER_SYNC) || defined(EM_ASYNCIFY))
PROGRAMS_MakeFile("WGET.COM", WGET_ProgramStart);
#endif
}
18 changes: 13 additions & 5 deletions src/dosbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Bit32u ticksScheduled;
bool ticksLocked;

#ifdef EMSCRIPTEN
#ifdef EMTERPRETER_SYNC
#if defined(EMTERPRETER_SYNC) || defined(EM_ASYNCIFY)
int nosleep_lock = 0;
#else
static int runcount = 0;
Expand All @@ -148,7 +148,7 @@ static Bitu Normal_Loop(void) {
Bits ret;
#ifdef EMSCRIPTEN
int ticksEntry = GetTicks();
#ifdef EMTERPRETER_SYNC
#if defined(EMTERPRETER_SYNC) || defined(EM_ASYNCIFY)
/* Normal DOSBox is free to use up all available host CPU time, but
* in a browser, sleep has to happen regularly so the screen is updated,
* sound isn't interrupted, and the script does not appear to hang.
Expand All @@ -158,7 +158,11 @@ static Bitu Normal_Loop(void) {
if (SDL_TICKS_PASSED(ticksEntry, last_sleep + 10)) {
if (nosleep_lock == 0) {
last_sleep = ticksEntry;
#ifdef EMTERPRETER_SYNC
emscripten_sleep_with_yield(1);
#elif defined(EM_ASYNCIFY)
emscripten_sleep(1);
#endif
ticksEntry = GetTicks();
} else if (SDL_TICKS_PASSED(ticksEntry, last_sleep + 2000) &&
!SDL_TICKS_PASSED(ticksEntry, last_loop + 200)) {
Expand Down Expand Up @@ -360,10 +364,14 @@ static Bitu Normal_Loop(void) {
ticksAdded = 0;
#ifndef EMSCRIPTEN
SDL_Delay(1);
#elif defined(EMTERPRETER_SYNC)
#elif defined(EMTERPRETER_SYNC) || defined(EM_ASYNCIFY)
if (nosleep_lock == 0) {
last_sleep = ticksNew;
#ifdef EMTERPRETER_SYNC
emscripten_sleep_with_yield(1);
#elif defined(EM_ASYNCIFY)
emscripten_sleep(1);
#endif
}
#endif
ticksDone -= GetTicks() - ticksNew;
Expand Down Expand Up @@ -417,7 +425,7 @@ static void em_main_loop(void) {
#endif

void DOSBOX_RunMachine(void){
#if defined(EMSCRIPTEN) && !defined(EMTERPRETER_SYNC)
#if defined(EMSCRIPTEN) && !defined(EMTERPRETER_SYNC) && !defined(EM_ASYNCIFY)
if (runcount == 0) {
runcount = 1;
} else if (runcount == 1) {
Expand All @@ -434,7 +442,7 @@ void DOSBOX_RunMachine(void){
Bitu ret;
do {
ret=(*loop)();
#if defined(EMSCRIPTEN) && !defined(EMTERPRETER_SYNC)
#if defined(EMSCRIPTEN) && !defined(EMTERPRETER_SYNC) && !defined(EM_ASYNCIFY)
/* These should be very short operations, like interrupts.
* Anything taking a long time will probably run indefinitely,
* making DOSBox appear to hang.
Expand Down
12 changes: 8 additions & 4 deletions src/gui/sdlmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ static Bitu Pause_Loop(void) {
}
}
}
#ifdef EMTERPRETER_SYNC
#if defined(EMTERPRETER_SYNC) || defined(EM_ASYNCIFY)
emscripten_sleep(10);
#endif
return 0;
Expand Down Expand Up @@ -2027,7 +2027,7 @@ static void GUI_StartUp(Section * sec) {
* The splash screen requires emterpreter sync.
* Creating a 2D context prevents subsequent creation of a 3D context.
*/
#if !defined(EMSCRIPTEN) || defined(EMTERPRETER_SYNC)
#if !defined(EMSCRIPTEN) || defined(EMTERPRETER_SYNC) || defined(EM_ASYNCIFY)
/* Please leave the Splash screen stuff in working order in DOSBox. We spend a lot of time making DOSBox. */
SDL_Surface* splash_surf = NULL;
#ifdef EMSCRIPTEN
Expand Down Expand Up @@ -2080,8 +2080,12 @@ static void GUI_StartUp(Section * sec) {
}
}
if (exit_splash) break;
#if defined(EMSCRIPTEN) && defined(EMTERPRETER_SYNC)
#ifdef EMSCRIPTEN
#ifdef EMTERPRETER_SYNC
emscripten_sleep_with_yield(1);
#elif defined(EM_ASYNCIFY)
emscripten_sleep(1);
#endif
#endif

if (ct<1) {
Expand Down Expand Up @@ -2136,7 +2140,7 @@ static void GUI_StartUp(Section * sec) {
delete [] tmpbufp;

}
#endif // !defined(EMSCRIPTEN) || defined(EMTERPRETER_SYNC)
#endif // !defined(EMSCRIPTEN) || defined(EMTERPRETER_SYNC) || defined(EM_ASYNCIFY)
/* Get some Event handlers */
MAPPER_AddHandler(KillSwitch,MK_f9,MMOD1,"shutdown","ShutDown");
MAPPER_AddHandler(CaptureMouse,MK_f10,MMOD1,"capmouse","Cap Mouse");
Expand Down
2 changes: 1 addition & 1 deletion src/shell/shell_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static void outc(Bit8u c) {
}

void DOS_Shell::InputCommand(char * line) {
#if defined(EMSCRIPTEN) && !defined(EMTERPRETER_SYNC)
#if defined(EMSCRIPTEN) && !defined(EMTERPRETER_SYNC) && !defined(EM_ASYNCIFY)
if (!strcmp(Files[input_handle]->name, "CON")) {
// This can be called during startup, before main loop being used.
LOG_MSG("Emulation ended because interactive shell is not supported.");
Expand Down

0 comments on commit af3aef2

Please sign in to comment.