foxygit / fake-08-xbox360 Log in
commit b97b89e0a03ae6a4e5dd27c5b1e57db99a77b701
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Tue Jan 4 21:52:01 2022 -0700
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Tue Jan 4 21:52:01 2022 -0700

    Try to avoid freeze on exit on bittboy

    Use PauseAudio, a mutex, and a delay. Seems to help from what I can tell
---
 platform/bittboy/source/BittBoyHost.cpp | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/platform/bittboy/source/BittBoyHost.cpp b/platform/bittboy/source/BittBoyHost.cpp
index f9165a7..f95e8cd 100644
--- a/platform/bittboy/source/BittBoyHost.cpp
+++ b/platform/bittboy/source/BittBoyHost.cpp
@@ -61,6 +61,7 @@ SDL_Surface *window;
 SDL_Surface *texture;
 SDL_bool done = SDL_FALSE;
 SDL_AudioSpec want, have;
+SDL_mutex *sound_mutex;
 void *pixels;
 uint16_t *base;
 int pitch;
@@ -87,15 +88,27 @@ void postFlipFunction(){
 }

 void audioCleanup(){
+    //seems to be necessary to lock the audio before closing to avoid a freeze on
+    //bittboy devices... maybe due to slow speed? not sure.
+    //SDL_LockAudio();
+    SDL_PauseAudio(1);
+    SDL_DestroyMutex(sound_mutex);
+
     audioInitialized = false;

     SDL_CloseAudio();
+    //hack to maybe prevent lockup?
+    SDL_Delay(100);
 }


 void FillAudioDeviceBuffer(void* UserData, Uint8* DeviceBuffer, int Length)
 {
+    SDL_LockMutex(sound_mutex);
+
     _audio->FillMonoAudioBuffer(DeviceBuffer, 0, Length / 2);
+
+    SDL_UnlockMutex(sound_mutex);
 }

 void audioSetup(){
@@ -116,6 +129,7 @@ void audioSetup(){
         if (have.format != want.format) {
             Logger_Write("We didn't get requested audio format.");
         }
+        sound_mutex = SDL_CreateMutex();
         SDL_PauseAudio(0);
         audioInitialized = true;
     }
@@ -222,11 +236,11 @@ void Host::oneTimeSetup(Color* paletteColors, Audio* audio){
 }

 void Host::oneTimeCleanup(){
-    //saving seems to increase the number of hard locks
-    //saveSettingsIni();
-
     audioCleanup();

+    //saving seems to increase the number of hard locks
+    saveSettingsIni();
+
     //SDL_DestroyRenderer(renderer);
     //SDL_DestroyWindow(window);