bg audio: Sprinkle on some thread safety

This commit is contained in:
Henrik Rydgard 2014-06-22 17:26:15 +02:00
parent f8e32f4a71
commit 3b4e30d50f

View File

@ -1,6 +1,7 @@
#include <string> #include <string>
#include "base/logging.h" #include "base/logging.h"
#include "base/timeutil.h" #include "base/timeutil.h"
#include "base/mutex.h"
#include "native/file/chunk_file.h" #include "native/file/chunk_file.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
@ -142,17 +143,21 @@ private:
SimpleAudio *decoder_; SimpleAudio *decoder_;
}; };
static recursive_mutex bgMutex;
static std::string bgGamePath; static std::string bgGamePath;
static int playbackOffset; static int playbackOffset;
static AT3PlusReader *at3Reader; static AT3PlusReader *at3Reader;
static double gameLastChanged; static double gameLastChanged;
void SetBackgroundAudioGame(const std::string &path) { void SetBackgroundAudioGame(const std::string &path) {
time_update();
lock_guard lock(bgMutex);
if (path == bgGamePath) { if (path == bgGamePath) {
// Do nothing // Do nothing
return; return;
} }
time_update();
gameLastChanged = time_now_d(); gameLastChanged = time_now_d();
if (at3Reader) { if (at3Reader) {
at3Reader->Shutdown(); at3Reader->Shutdown();
@ -165,6 +170,8 @@ void SetBackgroundAudioGame(const std::string &path) {
int MixBackgroundAudio(short *buffer, int size) { int MixBackgroundAudio(short *buffer, int size) {
time_update(); time_update();
lock_guard lock(bgMutex);
// If there's a game, and some time has passed since the selected game // 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) // last changed... (to prevent crazy amount of reads when skipping through a list)
if (!at3Reader && bgGamePath.size() && (time_now_d() - gameLastChanged > 0.5)) { if (!at3Reader && bgGamePath.size() && (time_now_d() - gameLastChanged > 0.5)) {