From a2d63e2767bf9bb538c69c62d06353254344011b Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Thu, 18 Jan 2007 08:16:10 +0000 Subject: [PATCH] RageSound::DeleteSelfWhenFinishedPlaying(). Replaces RageSoundManager::DeleteSoundWhenFinished, without needing to poll every sound in the list every frame and without any extra locking. --- stepmania/src/RageSound.cpp | 32 ++++++++++++++++++++++++++++++++ stepmania/src/RageSound.h | 2 ++ 2 files changed, 34 insertions(+) diff --git a/stepmania/src/RageSound.cpp b/stepmania/src/RageSound.cpp index 638fcca3cb..3c723cbecf 100644 --- a/stepmania/src/RageSound.cpp +++ b/stepmania/src/RageSound.cpp @@ -68,6 +68,7 @@ RageSound::RageSound(): m_iStreamFrame = 0; m_iStoppedSourceFrame = 0; m_bPlaying = false; + m_bDeleteWhenFinished = false; } RageSound::~RageSound() @@ -90,10 +91,15 @@ RageSound &RageSound::operator=( const RageSound &cpy ) { LockMut(cpy.m_Mutex); + /* If m_bDeleteWhenFinished, then nobody that has a reference to the sound should + * be making copies. */ + ASSERT( !cpy.m_bDeleteWhenFinished ); + m_Param = cpy.m_Param; m_iStreamFrame = cpy.m_iStreamFrame; m_iStoppedSourceFrame = cpy.m_iStoppedSourceFrame; m_bPlaying = false; + m_bDeleteWhenFinished = false; delete m_pSource; if( cpy.m_pSource ) @@ -119,6 +125,24 @@ void RageSound::Unload() m_sFilePath = ""; } +/* The sound will self-delete itself when it stops playing. If the sound is not + * playing, the sound will be deleted immediately. The caller loses ownership + * of the sound. */ +void RageSound::DeleteSelfWhenFinishedPlaying() +{ + m_Mutex.Lock(); + + if( !m_bPlaying ) + { + m_Mutex.Unlock(); + delete this; + return; + } + + m_bDeleteWhenFinished = true; + m_Mutex.Unlock(); +} + bool RageSound::IsLoaded() const { return m_pSource != NULL; @@ -359,6 +383,14 @@ void RageSound::SoundIsFinishedPlaying() m_Mutex.Lock(); + if( m_bDeleteWhenFinished ) + { + m_bDeleteWhenFinished = false; + m_Mutex.Unlock(); + delete this; + return; + } + /* Lock the mutex after calling SOUNDMAN->GetPosition(). We must not make driver * calls with our mutex locked (driver mutex < sound mutex). */ if( !m_HardwareToStreamMap.IsEmpty() && !m_StreamToSourceMap.IsEmpty() ) diff --git a/stepmania/src/RageSound.h b/stepmania/src/RageSound.h index aaecc1f37b..c2e27c7de0 100644 --- a/stepmania/src/RageSound.h +++ b/stepmania/src/RageSound.h @@ -116,6 +116,7 @@ public: void Unload(); bool IsLoaded() const; + void DeleteSelfWhenFinishedPlaying(); void StartPlaying(); void StopPlaying(); @@ -173,6 +174,7 @@ private: * position when stopped, and when playing but pos_map hasn't yet been filled. */ int m_iStoppedSourceFrame; bool m_bPlaying; + bool m_bDeleteWhenFinished; RString m_sError;