Skip to content

Commit 2102561

Browse files
committed
Replaced std mutexes and lock guards with SDL_mutex, since they weren't working properly on Vita
1 parent 6cfb013 commit 2102561

File tree

3 files changed

+59
-19
lines changed

3 files changed

+59
-19
lines changed

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ if(WIN32)
310310
endif()
311311

312312
if(VITA)
313-
#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")
314-
set(VITA_FLAGS "-O2 -g -mcpu=cortex-a9 -mfpu=neon -fno-peephole2 -DNDEBUG")
313+
set(VITA_FLAGS "-O2 -mcpu=cortex-a9 -mfpu=neon -DNDEBUG")
315314
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${VITA_FLAGS}")
316315
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${VITA_FLAGS}")
317316

src/audio_engine.cc

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
#include <string.h>
44

5-
#include <mutex>
6-
75
#include <SDL.h>
86

97
namespace fallout {
@@ -22,7 +20,7 @@ struct AudioEngineSoundBuffer {
2220
bool looping;
2321
unsigned int pos;
2422
SDL_AudioStream* stream;
25-
std::recursive_mutex mutex;
23+
SDL_mutex* mutex;
2624
};
2725

2826
extern bool gProgramIsActive;
@@ -54,7 +52,10 @@ static void audioEngineMixin(void* userData, Uint8* stream, int length)
5452

5553
for (int index = 0; index < AUDIO_ENGINE_SOUND_BUFFERS; index++) {
5654
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[index]);
57-
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
55+
// for some strange reason std::lock_guard isn't locking the mutex properly on Vita
56+
// which results in audioEngineSoundBufferRelease call during this loop and subsequent crash
57+
// replacing the std::recursive_mutex with SDL_mutex is fixing this issue
58+
SDL_LockMutex(soundBuffer->mutex);
5859

5960
if (soundBuffer->active && soundBuffer->playing) {
6061
int srcFrameSize = soundBuffer->bitsPerSample / 8 * soundBuffer->channels;
@@ -90,11 +91,18 @@ static void audioEngineMixin(void* userData, Uint8* stream, int length)
9091
pos += bytesRead;
9192
}
9293
}
94+
95+
SDL_UnlockMutex(soundBuffer->mutex);
9396
}
9497
}
9598

9699
bool audioEngineInit()
97100
{
101+
for (int index = 0; index < AUDIO_ENGINE_SOUND_BUFFERS; index++) {
102+
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[index]);
103+
soundBuffer->mutex = SDL_CreateMutex();
104+
}
105+
98106
if (SDL_InitSubSystem(SDL_INIT_AUDIO) == -1) {
99107
return false;
100108
}
@@ -126,6 +134,11 @@ void audioEngineExit()
126134
if (SDL_WasInit(SDL_INIT_AUDIO)) {
127135
SDL_QuitSubSystem(SDL_INIT_AUDIO);
128136
}
137+
138+
for (int index = 0; index < AUDIO_ENGINE_SOUND_BUFFERS; index++) {
139+
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[index]);
140+
SDL_DestroyMutex(soundBuffer->mutex);
141+
}
129142
}
130143

131144
void audioEnginePause()
@@ -150,7 +163,7 @@ int audioEngineCreateSoundBuffer(unsigned int size, int bitsPerSample, int chann
150163

151164
for (int index = 0; index < AUDIO_ENGINE_SOUND_BUFFERS; index++) {
152165
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[index]);
153-
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
166+
SDL_LockMutex(soundBuffer->mutex);
154167

155168
if (!soundBuffer->active) {
156169
soundBuffer->active = true;
@@ -164,8 +177,11 @@ int audioEngineCreateSoundBuffer(unsigned int size, int bitsPerSample, int chann
164177
soundBuffer->pos = 0;
165178
soundBuffer->data = malloc(size);
166179
soundBuffer->stream = SDL_NewAudioStream(bitsPerSample == 16 ? AUDIO_S16 : AUDIO_S8, channels, rate, gAudioEngineSpec.format, gAudioEngineSpec.channels, gAudioEngineSpec.freq);
180+
SDL_UnlockMutex(soundBuffer->mutex);
167181
return index;
168182
}
183+
184+
SDL_UnlockMutex(soundBuffer->mutex);
169185
}
170186

171187
return -1;
@@ -182,9 +198,10 @@ bool audioEngineSoundBufferRelease(int soundBufferIndex)
182198
}
183199

184200
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
185-
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
201+
SDL_LockMutex(soundBuffer->mutex);
186202

187203
if (!soundBuffer->active) {
204+
SDL_UnlockMutex(soundBuffer->mutex);
188205
return false;
189206
}
190207

@@ -196,6 +213,7 @@ bool audioEngineSoundBufferRelease(int soundBufferIndex)
196213
SDL_FreeAudioStream(soundBuffer->stream);
197214
soundBuffer->stream = nullptr;
198215

216+
SDL_UnlockMutex(soundBuffer->mutex);
199217
return true;
200218
}
201219

@@ -210,14 +228,16 @@ bool audioEngineSoundBufferSetVolume(int soundBufferIndex, int volume)
210228
}
211229

212230
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
213-
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
231+
SDL_LockMutex(soundBuffer->mutex);
214232

215233
if (!soundBuffer->active) {
234+
SDL_UnlockMutex(soundBuffer->mutex);
216235
return false;
217236
}
218237

219238
soundBuffer->volume = volume;
220239

240+
SDL_UnlockMutex(soundBuffer->mutex);
221241
return true;
222242
}
223243

@@ -232,14 +252,16 @@ bool audioEngineSoundBufferGetVolume(int soundBufferIndex, int* volumePtr)
232252
}
233253

234254
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
235-
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
255+
SDL_LockMutex(soundBuffer->mutex);
236256

237257
if (!soundBuffer->active) {
258+
SDL_UnlockMutex(soundBuffer->mutex);
238259
return false;
239260
}
240261

241262
*volumePtr = soundBuffer->volume;
242263

264+
SDL_UnlockMutex(soundBuffer->mutex);
243265
return true;
244266
}
245267

@@ -254,15 +276,17 @@ bool audioEngineSoundBufferSetPan(int soundBufferIndex, int pan)
254276
}
255277

256278
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
257-
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
279+
SDL_LockMutex(soundBuffer->mutex);
258280

259281
if (!soundBuffer->active) {
282+
SDL_UnlockMutex(soundBuffer->mutex);
260283
return false;
261284
}
262285

263286
// NOTE: Audio engine does not support sound panning. I'm not sure it's
264287
// even needed. For now this value is silently ignored.
265288

289+
SDL_UnlockMutex(soundBuffer->mutex);
266290
return true;
267291
}
268292

@@ -277,9 +301,10 @@ bool audioEngineSoundBufferPlay(int soundBufferIndex, unsigned int flags)
277301
}
278302

279303
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
280-
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
304+
SDL_LockMutex(soundBuffer->mutex);
281305

282306
if (!soundBuffer->active) {
307+
SDL_UnlockMutex(soundBuffer->mutex);
283308
return false;
284309
}
285310

@@ -289,6 +314,7 @@ bool audioEngineSoundBufferPlay(int soundBufferIndex, unsigned int flags)
289314
soundBuffer->looping = true;
290315
}
291316

317+
SDL_UnlockMutex(soundBuffer->mutex);
292318
return true;
293319
}
294320

@@ -303,14 +329,16 @@ bool audioEngineSoundBufferStop(int soundBufferIndex)
303329
}
304330

305331
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
306-
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
332+
SDL_LockMutex(soundBuffer->mutex);
307333

308334
if (!soundBuffer->active) {
335+
SDL_UnlockMutex(soundBuffer->mutex);
309336
return false;
310337
}
311338

312339
soundBuffer->playing = false;
313340

341+
SDL_UnlockMutex(soundBuffer->mutex);
314342
return true;
315343
}
316344

@@ -325,9 +353,10 @@ bool audioEngineSoundBufferGetCurrentPosition(int soundBufferIndex, unsigned int
325353
}
326354

327355
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
328-
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
356+
SDL_LockMutex(soundBuffer->mutex);
329357

330358
if (!soundBuffer->active) {
359+
SDL_UnlockMutex(soundBuffer->mutex);
331360
return false;
332361
}
333362

@@ -346,6 +375,7 @@ bool audioEngineSoundBufferGetCurrentPosition(int soundBufferIndex, unsigned int
346375
}
347376
}
348377

378+
SDL_UnlockMutex(soundBuffer->mutex);
349379
return true;
350380
}
351381

@@ -360,14 +390,16 @@ bool audioEngineSoundBufferSetCurrentPosition(int soundBufferIndex, unsigned int
360390
}
361391

362392
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
363-
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
393+
SDL_LockMutex(soundBuffer->mutex);
364394

365395
if (!soundBuffer->active) {
396+
SDL_UnlockMutex(soundBuffer->mutex);
366397
return false;
367398
}
368399

369400
soundBuffer->pos = pos % soundBuffer->size;
370401

402+
SDL_UnlockMutex(soundBuffer->mutex);
371403
return true;
372404
}
373405

@@ -382,18 +414,21 @@ bool audioEngineSoundBufferLock(int soundBufferIndex, unsigned int writePos, uns
382414
}
383415

384416
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
385-
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
417+
SDL_LockMutex(soundBuffer->mutex);
386418

387419
if (!soundBuffer->active) {
420+
SDL_UnlockMutex(soundBuffer->mutex);
388421
return false;
389422
}
390423

391424
if (audioBytes1 == nullptr) {
425+
SDL_UnlockMutex(soundBuffer->mutex);
392426
return false;
393427
}
394428

395429
if ((flags & AUDIO_ENGINE_SOUND_BUFFER_LOCK_FROM_WRITE_POS) != 0) {
396430
if (!audioEngineSoundBufferGetCurrentPosition(soundBufferIndex, nullptr, &writePos)) {
431+
SDL_UnlockMutex(soundBuffer->mutex);
397432
return false;
398433
}
399434
}
@@ -429,6 +464,7 @@ bool audioEngineSoundBufferLock(int soundBufferIndex, unsigned int writePos, uns
429464

430465
// TODO: Mark range as locked.
431466

467+
SDL_UnlockMutex(soundBuffer->mutex);
432468
return true;
433469
}
434470

@@ -443,14 +479,16 @@ bool audioEngineSoundBufferUnlock(int soundBufferIndex, void* audioPtr1, unsigne
443479
}
444480

445481
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
446-
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
482+
SDL_LockMutex(soundBuffer->mutex);
447483

448484
if (!soundBuffer->active) {
485+
SDL_UnlockMutex(soundBuffer->mutex);
449486
return false;
450487
}
451488

452489
// TODO: Mark range as unlocked.
453490

491+
SDL_UnlockMutex(soundBuffer->mutex);
454492
return true;
455493
}
456494

@@ -465,13 +503,15 @@ bool audioEngineSoundBufferGetStatus(int soundBufferIndex, unsigned int* statusP
465503
}
466504

467505
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
468-
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
506+
SDL_LockMutex(soundBuffer->mutex);
469507

470508
if (!soundBuffer->active) {
509+
SDL_UnlockMutex(soundBuffer->mutex);
471510
return false;
472511
}
473512

474513
if (statusPtr == nullptr) {
514+
SDL_UnlockMutex(soundBuffer->mutex);
475515
return false;
476516
}
477517

@@ -485,6 +525,7 @@ bool audioEngineSoundBufferGetStatus(int soundBufferIndex, unsigned int* statusP
485525
}
486526
}
487527

528+
SDL_UnlockMutex(soundBuffer->mutex);
488529
return true;
489530
}
490531

vita/vita.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set(VITA_APP_NAME "Fallout 2 CE")
44
set(VITA_TITLEID "FOUT00002")
55

66
set(EXECUTABLE_NAME fallout2-ce)
7-
set(VITA_VERSION "01.00")
7+
set(VITA_VERSION "01.30")
88
set(VITA_MKSFOEX_FLAGS "-d ATTRIBUTE2=12")
99

1010
vita_create_self(${EXECUTABLE_NAME}.self ${EXECUTABLE_NAME})

0 commit comments

Comments
 (0)