From 210256177786dab0da7cdff8aecbcd883d05a7c1 Mon Sep 17 00:00:00 2001 From: Northfear <11291116+Northfear@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:55:56 +0300 Subject: [PATCH] Replaced std mutexes and lock guards with SDL_mutex, since they weren't working properly on Vita --- CMakeLists.txt | 3 +- src/audio_engine.cc | 73 +++++++++++++++++++++++++++++++++++---------- vita/vita.cmake | 2 +- 3 files changed, 59 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f3416c0d..54deb792 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -310,8 +310,7 @@ if(WIN32) endif() if(VITA) - #set(VITA_FLAGS "-Ofast -mcpu=cortex-a9 -mfpu=neon -fgraphite-identity -floop-parallelize-all -floop-nest-optimize -ftree-parallelize-loops=3 -flto -fno-peephole2 -DNDEBUG") - set(VITA_FLAGS "-O2 -g -mcpu=cortex-a9 -mfpu=neon -fno-peephole2 -DNDEBUG") + set(VITA_FLAGS "-O2 -mcpu=cortex-a9 -mfpu=neon -DNDEBUG") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${VITA_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${VITA_FLAGS}") diff --git a/src/audio_engine.cc b/src/audio_engine.cc index 448ecce7..b7b5c494 100644 --- a/src/audio_engine.cc +++ b/src/audio_engine.cc @@ -2,8 +2,6 @@ #include -#include - #include namespace fallout { @@ -22,7 +20,7 @@ struct AudioEngineSoundBuffer { bool looping; unsigned int pos; SDL_AudioStream* stream; - std::recursive_mutex mutex; + SDL_mutex* mutex; }; extern bool gProgramIsActive; @@ -54,7 +52,10 @@ static void audioEngineMixin(void* userData, Uint8* stream, int length) for (int index = 0; index < AUDIO_ENGINE_SOUND_BUFFERS; index++) { AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[index]); - std::lock_guard lock(soundBuffer->mutex); + // for some strange reason std::lock_guard isn't locking the mutex properly on Vita + // which results in audioEngineSoundBufferRelease call during this loop and subsequent crash + // replacing the std::recursive_mutex with SDL_mutex is fixing this issue + SDL_LockMutex(soundBuffer->mutex); if (soundBuffer->active && soundBuffer->playing) { int srcFrameSize = soundBuffer->bitsPerSample / 8 * soundBuffer->channels; @@ -90,11 +91,18 @@ static void audioEngineMixin(void* userData, Uint8* stream, int length) pos += bytesRead; } } + + SDL_UnlockMutex(soundBuffer->mutex); } } bool audioEngineInit() { + for (int index = 0; index < AUDIO_ENGINE_SOUND_BUFFERS; index++) { + AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[index]); + soundBuffer->mutex = SDL_CreateMutex(); + } + if (SDL_InitSubSystem(SDL_INIT_AUDIO) == -1) { return false; } @@ -126,6 +134,11 @@ void audioEngineExit() if (SDL_WasInit(SDL_INIT_AUDIO)) { SDL_QuitSubSystem(SDL_INIT_AUDIO); } + + for (int index = 0; index < AUDIO_ENGINE_SOUND_BUFFERS; index++) { + AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[index]); + SDL_DestroyMutex(soundBuffer->mutex); + } } void audioEnginePause() @@ -150,7 +163,7 @@ int audioEngineCreateSoundBuffer(unsigned int size, int bitsPerSample, int chann for (int index = 0; index < AUDIO_ENGINE_SOUND_BUFFERS; index++) { AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[index]); - std::lock_guard lock(soundBuffer->mutex); + SDL_LockMutex(soundBuffer->mutex); if (!soundBuffer->active) { soundBuffer->active = true; @@ -164,8 +177,11 @@ int audioEngineCreateSoundBuffer(unsigned int size, int bitsPerSample, int chann soundBuffer->pos = 0; soundBuffer->data = malloc(size); soundBuffer->stream = SDL_NewAudioStream(bitsPerSample == 16 ? AUDIO_S16 : AUDIO_S8, channels, rate, gAudioEngineSpec.format, gAudioEngineSpec.channels, gAudioEngineSpec.freq); + SDL_UnlockMutex(soundBuffer->mutex); return index; } + + SDL_UnlockMutex(soundBuffer->mutex); } return -1; @@ -182,9 +198,10 @@ bool audioEngineSoundBufferRelease(int soundBufferIndex) } AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]); - std::lock_guard lock(soundBuffer->mutex); + SDL_LockMutex(soundBuffer->mutex); if (!soundBuffer->active) { + SDL_UnlockMutex(soundBuffer->mutex); return false; } @@ -196,6 +213,7 @@ bool audioEngineSoundBufferRelease(int soundBufferIndex) SDL_FreeAudioStream(soundBuffer->stream); soundBuffer->stream = nullptr; + SDL_UnlockMutex(soundBuffer->mutex); return true; } @@ -210,14 +228,16 @@ bool audioEngineSoundBufferSetVolume(int soundBufferIndex, int volume) } AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]); - std::lock_guard lock(soundBuffer->mutex); + SDL_LockMutex(soundBuffer->mutex); if (!soundBuffer->active) { + SDL_UnlockMutex(soundBuffer->mutex); return false; } soundBuffer->volume = volume; + SDL_UnlockMutex(soundBuffer->mutex); return true; } @@ -232,14 +252,16 @@ bool audioEngineSoundBufferGetVolume(int soundBufferIndex, int* volumePtr) } AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]); - std::lock_guard lock(soundBuffer->mutex); + SDL_LockMutex(soundBuffer->mutex); if (!soundBuffer->active) { + SDL_UnlockMutex(soundBuffer->mutex); return false; } *volumePtr = soundBuffer->volume; + SDL_UnlockMutex(soundBuffer->mutex); return true; } @@ -254,15 +276,17 @@ bool audioEngineSoundBufferSetPan(int soundBufferIndex, int pan) } AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]); - std::lock_guard lock(soundBuffer->mutex); + SDL_LockMutex(soundBuffer->mutex); if (!soundBuffer->active) { + SDL_UnlockMutex(soundBuffer->mutex); return false; } // NOTE: Audio engine does not support sound panning. I'm not sure it's // even needed. For now this value is silently ignored. + SDL_UnlockMutex(soundBuffer->mutex); return true; } @@ -277,9 +301,10 @@ bool audioEngineSoundBufferPlay(int soundBufferIndex, unsigned int flags) } AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]); - std::lock_guard lock(soundBuffer->mutex); + SDL_LockMutex(soundBuffer->mutex); if (!soundBuffer->active) { + SDL_UnlockMutex(soundBuffer->mutex); return false; } @@ -289,6 +314,7 @@ bool audioEngineSoundBufferPlay(int soundBufferIndex, unsigned int flags) soundBuffer->looping = true; } + SDL_UnlockMutex(soundBuffer->mutex); return true; } @@ -303,14 +329,16 @@ bool audioEngineSoundBufferStop(int soundBufferIndex) } AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]); - std::lock_guard lock(soundBuffer->mutex); + SDL_LockMutex(soundBuffer->mutex); if (!soundBuffer->active) { + SDL_UnlockMutex(soundBuffer->mutex); return false; } soundBuffer->playing = false; + SDL_UnlockMutex(soundBuffer->mutex); return true; } @@ -325,9 +353,10 @@ bool audioEngineSoundBufferGetCurrentPosition(int soundBufferIndex, unsigned int } AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]); - std::lock_guard lock(soundBuffer->mutex); + SDL_LockMutex(soundBuffer->mutex); if (!soundBuffer->active) { + SDL_UnlockMutex(soundBuffer->mutex); return false; } @@ -346,6 +375,7 @@ bool audioEngineSoundBufferGetCurrentPosition(int soundBufferIndex, unsigned int } } + SDL_UnlockMutex(soundBuffer->mutex); return true; } @@ -360,14 +390,16 @@ bool audioEngineSoundBufferSetCurrentPosition(int soundBufferIndex, unsigned int } AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]); - std::lock_guard lock(soundBuffer->mutex); + SDL_LockMutex(soundBuffer->mutex); if (!soundBuffer->active) { + SDL_UnlockMutex(soundBuffer->mutex); return false; } soundBuffer->pos = pos % soundBuffer->size; + SDL_UnlockMutex(soundBuffer->mutex); return true; } @@ -382,18 +414,21 @@ bool audioEngineSoundBufferLock(int soundBufferIndex, unsigned int writePos, uns } AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]); - std::lock_guard lock(soundBuffer->mutex); + SDL_LockMutex(soundBuffer->mutex); if (!soundBuffer->active) { + SDL_UnlockMutex(soundBuffer->mutex); return false; } if (audioBytes1 == nullptr) { + SDL_UnlockMutex(soundBuffer->mutex); return false; } if ((flags & AUDIO_ENGINE_SOUND_BUFFER_LOCK_FROM_WRITE_POS) != 0) { if (!audioEngineSoundBufferGetCurrentPosition(soundBufferIndex, nullptr, &writePos)) { + SDL_UnlockMutex(soundBuffer->mutex); return false; } } @@ -429,6 +464,7 @@ bool audioEngineSoundBufferLock(int soundBufferIndex, unsigned int writePos, uns // TODO: Mark range as locked. + SDL_UnlockMutex(soundBuffer->mutex); return true; } @@ -443,14 +479,16 @@ bool audioEngineSoundBufferUnlock(int soundBufferIndex, void* audioPtr1, unsigne } AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]); - std::lock_guard lock(soundBuffer->mutex); + SDL_LockMutex(soundBuffer->mutex); if (!soundBuffer->active) { + SDL_UnlockMutex(soundBuffer->mutex); return false; } // TODO: Mark range as unlocked. + SDL_UnlockMutex(soundBuffer->mutex); return true; } @@ -465,13 +503,15 @@ bool audioEngineSoundBufferGetStatus(int soundBufferIndex, unsigned int* statusP } AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]); - std::lock_guard lock(soundBuffer->mutex); + SDL_LockMutex(soundBuffer->mutex); if (!soundBuffer->active) { + SDL_UnlockMutex(soundBuffer->mutex); return false; } if (statusPtr == nullptr) { + SDL_UnlockMutex(soundBuffer->mutex); return false; } @@ -485,6 +525,7 @@ bool audioEngineSoundBufferGetStatus(int soundBufferIndex, unsigned int* statusP } } + SDL_UnlockMutex(soundBuffer->mutex); return true; } diff --git a/vita/vita.cmake b/vita/vita.cmake index 5b58fbfe..e609c290 100644 --- a/vita/vita.cmake +++ b/vita/vita.cmake @@ -4,7 +4,7 @@ set(VITA_APP_NAME "Fallout 2 CE") set(VITA_TITLEID "FOUT00002") set(EXECUTABLE_NAME fallout2-ce) -set(VITA_VERSION "01.00") +set(VITA_VERSION "01.30") set(VITA_MKSFOEX_FLAGS "-d ATTRIBUTE2=12") vita_create_self(${EXECUTABLE_NAME}.self ${EXECUTABLE_NAME})