added waiting for active API threads on program exit
[rocksndiamonds.git] / src / init.c
index 7802e8e9a7da848e6abd41291bbbe3c0b61d66b2..8c6c7e12e1d6d7010cb9fdfea0e03a15dba24c9c 100644 (file)
@@ -6332,9 +6332,6 @@ void OpenAll(void)
 
   DrawMainMenu();
 
-  // needed once to offer uploading score tapes after starting the program
-  CheckUploadTapes();
-
 #if 0
   Debug("internal:path", "SDL_GetBasePath() == '%s'",
        SDL_GetBasePath());
@@ -6354,8 +6351,44 @@ void OpenAll(void)
 #endif
 }
 
+static boolean WaitForApiThreads(void)
+{
+  unsigned int thread_delay = 0;
+  unsigned int thread_delay_value = 10000;
+
+  if (program.api_thread_count == 0)
+    return TRUE;
+
+  // deactivate global animations (not accessible in game state "loading")
+  setup.toons = FALSE;
+
+  // set game state to "loading" to be able to show busy animation
+  SetGameStatus(GAME_MODE_LOADING);
+
+  ResetDelayCounter(&thread_delay);
+
+  // wait for threads to finish (and fail on timeout)
+  while (program.api_thread_count > 0)
+  {
+    if (DelayReached(&thread_delay, thread_delay_value))
+    {
+      Error("failed waiting for threads - TIMEOUT");
+
+      return FALSE;
+    }
+
+    UPDATE_BUSY_STATE();
+
+    Delay(20);
+  }
+
+  return TRUE;
+}
+
 void CloseAllAndExit(int exit_value)
 {
+  WaitForApiThreads();
+
   StopSounds();
   FreeAllSounds();
   FreeAllMusic();