From 3b4e30d50f9c2fefa63d8e596d884dcf586fcfd8 Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Sun, 22 Jun 2014 17:26:15 +0200 Subject: [PATCH] bg audio: Sprinkle on some thread safety --- UI/BackgroundAudio.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/UI/BackgroundAudio.cpp b/UI/BackgroundAudio.cpp index da253314a6..71f9ed8c74 100644 --- a/UI/BackgroundAudio.cpp +++ b/UI/BackgroundAudio.cpp @@ -1,6 +1,7 @@ #include #include "base/logging.h" #include "base/timeutil.h" +#include "base/mutex.h" #include "native/file/chunk_file.h" #include "Common/CommonTypes.h" @@ -142,17 +143,21 @@ private: SimpleAudio *decoder_; }; +static recursive_mutex bgMutex; static std::string bgGamePath; static int playbackOffset; static AT3PlusReader *at3Reader; static double gameLastChanged; void SetBackgroundAudioGame(const std::string &path) { + time_update(); + + lock_guard lock(bgMutex); if (path == bgGamePath) { // Do nothing return; } - time_update(); + gameLastChanged = time_now_d(); if (at3Reader) { at3Reader->Shutdown(); @@ -165,6 +170,8 @@ void SetBackgroundAudioGame(const std::string &path) { int MixBackgroundAudio(short *buffer, int size) { time_update(); + + lock_guard lock(bgMutex); // If there's a game, and some time has passed since the selected game // last changed... (to prevent crazy amount of reads when skipping through a list) if (!at3Reader && bgGamePath.size() && (time_now_d() - gameLastChanged > 0.5)) {