From 6b984a8563fada8f884b76d0640c08eac555f361 Mon Sep 17 00:00:00 2001 From: Christian Speckner Date: Sun, 28 Jan 2018 00:27:25 +0100 Subject: [PATCH] Avoid race condition in pause, switch to shared_ptr for simpler semantics. --- src/common/SoundSDL2.cxx | 5 +++-- src/common/SoundSDL2.hxx | 4 ++-- src/emucore/Console.cxx | 6 +++--- src/emucore/Console.hxx | 2 +- src/emucore/Sound.hxx | 2 +- src/emucore/tia/Audio.cxx | 2 +- src/emucore/tia/Audio.hxx | 4 ++-- src/emucore/tia/TIA.cxx | 2 +- src/emucore/tia/TIA.hxx | 2 +- 9 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/common/SoundSDL2.cxx b/src/common/SoundSDL2.cxx index f111a86a4..818ac96a5 100644 --- a/src/common/SoundSDL2.cxx +++ b/src/common/SoundSDL2.cxx @@ -100,7 +100,7 @@ void SoundSDL2::setEnabled(bool state) } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void SoundSDL2::open(AudioQueue* audioQueue) +void SoundSDL2::open(shared_ptr audioQueue) { myOSystem.logMessage("SoundSDL2::open started ...", 2); mute(true); @@ -141,10 +141,11 @@ void SoundSDL2::close() { if(!myIsInitializedFlag) return; + mute(true); + myAudioQueue = 0; myCurrentFragment = 0; - mute(true); myOSystem.logMessage("SoundSDL2::close", 2); } diff --git a/src/common/SoundSDL2.hxx b/src/common/SoundSDL2.hxx index b216b5a41..a058eaca8 100644 --- a/src/common/SoundSDL2.hxx +++ b/src/common/SoundSDL2.hxx @@ -59,7 +59,7 @@ class SoundSDL2 : public Sound Initializes the sound device. This must be called before any calls are made to derived methods. */ - void open(AudioQueue* audioQueue) override; + void open(shared_ptr audioQueue) override; /** Should be called to close the sound device. Once called the sound @@ -117,7 +117,7 @@ class SoundSDL2 : public Sound // Audio specification structure SDL_AudioSpec myHardwareSpec; - AudioQueue* myAudioQueue; + shared_ptr myAudioQueue; Int16* myCurrentFragment; uInt32 myTimeIndex; diff --git a/src/emucore/Console.cxx b/src/emucore/Console.cxx index a420cb15f..ffc714452 100644 --- a/src/emucore/Console.cxx +++ b/src/emucore/Console.cxx @@ -183,7 +183,7 @@ Console::Console(OSystem& osystem, unique_ptr& cart, } createAudioQueue(); - myTIA->setAudioQueue(myAudioQueue.get()); + myTIA->setAudioQueue(myAudioQueue); bool joyallow4 = myOSystem.settings().getBool("joyallow4"); myOSystem.eventHandler().allowAllDirections(joyallow4); @@ -580,7 +580,7 @@ void Console::initializeAudio() // const string& sound = myProperties.get(Cartridge_Sound); // myOSystem.sound().setChannels(sound == "STEREO" ? 2 : 1); - myOSystem.sound().open(myAudioQueue.get()); + myOSystem.sound().open(myAudioQueue); // Make sure auto-frame calculation is only enabled when necessary myTIA->enableAutoFrame(framerate <= 0); @@ -754,7 +754,7 @@ void Console::createAudioQueue() throw runtime_error("invalid console timing"); } - myAudioQueue = make_unique( + myAudioQueue = make_shared( fragmentSize, AUDIO_QUEUE_CAPACITY_FRAGMENTS, myProperties.get(Cartridge_Sound) == "STEREO", diff --git a/src/emucore/Console.hxx b/src/emucore/Console.hxx index 7e67e70dd..880911e0a 100644 --- a/src/emucore/Console.hxx +++ b/src/emucore/Console.hxx @@ -388,7 +388,7 @@ class Console : public Serializable unique_ptr myFrameManager; // The audio fragment queue that connects TIA and audio driver - unique_ptr myAudioQueue; + shared_ptr myAudioQueue; // Pointer to the Cartridge (the debugger needs it) unique_ptr myCart; diff --git a/src/emucore/Sound.hxx b/src/emucore/Sound.hxx index 3aa4726e4..67b3de1af 100644 --- a/src/emucore/Sound.hxx +++ b/src/emucore/Sound.hxx @@ -51,7 +51,7 @@ class Sound Start the sound system, initializing it if necessary. This must be called before any calls are made to derived methods. */ - virtual void open(AudioQueue* audioQueue) = 0; + virtual void open(shared_ptr audioQueue) = 0; /** Should be called to stop the sound system. Once called the sound diff --git a/src/emucore/tia/Audio.cxx b/src/emucore/tia/Audio.cxx index b776447bc..3397af214 100644 --- a/src/emucore/tia/Audio.cxx +++ b/src/emucore/tia/Audio.cxx @@ -53,7 +53,7 @@ void Audio::reset() } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void Audio::setAudioQueue(AudioQueue* queue) +void Audio::setAudioQueue(shared_ptr queue) { myAudioQueue = queue; diff --git a/src/emucore/tia/Audio.hxx b/src/emucore/tia/Audio.hxx index 595b53765..b4a59dfc4 100644 --- a/src/emucore/tia/Audio.hxx +++ b/src/emucore/tia/Audio.hxx @@ -30,7 +30,7 @@ class Audio void reset(); - void setAudioQueue(AudioQueue *queue); + void setAudioQueue(shared_ptr queue); void tick(); @@ -42,7 +42,7 @@ class Audio void phase1(); private: - AudioQueue* myAudioQueue; + shared_ptr myAudioQueue; uInt8 myCounter; diff --git a/src/emucore/tia/TIA.cxx b/src/emucore/tia/TIA.cxx index 641d1d4c9..f7cbe949a 100644 --- a/src/emucore/tia/TIA.cxx +++ b/src/emucore/tia/TIA.cxx @@ -117,7 +117,7 @@ void TIA::setFrameManager(AbstractFrameManager *frameManager) } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void TIA::setAudioQueue(AudioQueue* queue) +void TIA::setAudioQueue(shared_ptr queue) { myAudio.setAudioQueue(queue); } diff --git a/src/emucore/tia/TIA.hxx b/src/emucore/tia/TIA.hxx index da7f29c10..3c0f0b2cb 100644 --- a/src/emucore/tia/TIA.hxx +++ b/src/emucore/tia/TIA.hxx @@ -114,7 +114,7 @@ class TIA : public Device Set the audio queue. This needs to be dynamic as the queue is created after the timing has been determined. */ - void setAudioQueue(AudioQueue *audioQueue); + void setAudioQueue(shared_ptr audioQueue); /** Clear the configured frame manager and deteach the lifecycle callbacks.