foxygit / doom Log in
commit 149e888f25b774725dfba65d02aa0eeaa179baf7
Author:     James Baicoianu <james_github@baicoianu.com>
AuthorDate: Mon Jan 13 06:27:33 2025 -0800
Commit:     GitHub <noreply@github.com>
CommitDate: Mon Jan 13 15:27:33 2025 +0100

    Fixed startup delay caused by incorrect emscripten_sleep() argument (#1719)

    * Fixed startup delay caused by incorrect emscripten_sleep() argument

    * Moved emscripten async sleep into mutex check to prevent browser main thread from locking during OPL initialization

    * Added comments clarifying OPL_Delay() functionality
---
 opl/opl.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/opl/opl.c b/opl/opl.c
index 1561304f..e99821cb 100644
--- a/opl/opl.c
+++ b/opl/opl.c
@@ -467,6 +467,8 @@ static void DelayCallback(void *_delay_data)
     SDL_UnlockMutex(delay_data->mutex);
 }

+// Delay for specified number of microseconds after OPL subsystem is initialized
+
 void OPL_Delay(uint64_t us)
 {
     delay_data_t delay_data;
@@ -476,14 +478,13 @@ void OPL_Delay(uint64_t us)
         return;
     }

-#ifdef EMSCRIPTEN
-    // Use async sleep when compiled with emscripten
-    emscripten_sleep(us);
-#else
-
     // Create a callback that will signal this thread after the
     // specified time.

+    // Note that this is not just a simple time-based delay, it will ensure
+    // that the OPL system is initialized and the queue has begun processing
+    // before releasing the mutex lock
+
     delay_data.finished = 0;
     delay_data.mutex = SDL_CreateMutex();
     delay_data.cond = SDL_CreateCond();
@@ -497,6 +498,10 @@ void OPL_Delay(uint64_t us)
     while (!delay_data.finished)
     {
         SDL_CondWait(delay_data.cond, delay_data.mutex);
+#ifdef EMSCRIPTEN
+        // Use async sleep to avoid locking browser main thread
+        emscripten_sleep(us / 1000);
+#endif
     }

     SDL_UnlockMutex(delay_data.mutex);
@@ -505,7 +510,6 @@ void OPL_Delay(uint64_t us)

     SDL_DestroyMutex(delay_data.mutex);
     SDL_DestroyCond(delay_data.cond);
-#endif
 }

 void OPL_SetPaused(int paused)