From 670deaef2a8d24742d11eed1ec8d51c9a8249108 Mon Sep 17 00:00:00 2001 From: stwf Date: Mon, 7 Jan 2013 21:02:22 +0000 Subject: [PATCH] added variable sleep time depending on load an seperate mutex for deleting old items This was SVN commit r13055. --- source/soundmanager/SoundManager.cpp | 23 +++++++++++++---------- source/soundmanager/items/CSoundBase.cpp | 6 ++++++ source/soundmanager/items/CSoundBase.h | 1 + source/soundmanager/items/ISoundItem.h | 3 ++- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/source/soundmanager/SoundManager.cpp b/source/soundmanager/SoundManager.cpp index 332bbaa103..cbe72dfc89 100644 --- a/source/soundmanager/SoundManager.cpp +++ b/source/soundmanager/SoundManager.cpp @@ -27,6 +27,7 @@ #include "soundmanager/js/AmbientSound.h" #include "soundmanager/js/MusicSound.h" #include "soundmanager/js/Sound.h" +#include "lib/external_libraries/libsdl.h" #include "ps/CLogger.h" #include "ps/CStr.h" #include "ps/Profiler2.h" @@ -43,7 +44,6 @@ public: m_DeadItems = new ItemsList; m_Shutdown = false; m_Enabled = false; - m_PauseUntilTime = 0; int ret = pthread_create(&m_WorkerThread, NULL, &RunThread, this); ENSURE(ret == 0); @@ -102,7 +102,7 @@ public: void CleanupItems() { - CScopeLock lock(m_WorkerMutex); + CScopeLock lock(m_DeadItemsMutex); AL_CHECK ItemsList::iterator deadItems = m_DeadItems->begin(); while (deadItems != m_DeadItems->end()) @@ -114,6 +114,7 @@ public: } m_DeadItems->clear(); } + void DeleteItem(long itemNum) { CScopeLock lock(m_WorkerMutex); @@ -145,10 +146,6 @@ private: // Wait until the main thread wakes us up while ( true ) { - - if (timer_Time() < m_PauseUntilTime) - continue; - // Handle shutdown requests as soon as possible if (GetShutdown()) return; @@ -157,6 +154,7 @@ private: if (!GetEnabled()) continue; + int pauseTime = 1000; { CScopeLock lock(m_WorkerMutex); @@ -167,9 +165,16 @@ private: while (lstr != m_Items->end()) { if ((*lstr)->IdleTask()) + { + if ( (*lstr)->IsFading() ) + pauseTime = 100; nextItemList->push_back(*lstr); + } else + { + CScopeLock lock(m_DeadItemsMutex); m_DeadItems->push_back(*lstr); + } lstr++; AL_CHECK @@ -180,7 +185,7 @@ private: AL_CHECK } - m_PauseUntilTime = timer_Time() + 0.1; + SDL_Delay( pauseTime ); } } @@ -202,6 +207,7 @@ private: // Thread-related members: pthread_t m_WorkerThread; CMutex m_WorkerMutex; + CMutex m_DeadItemsMutex; // Shared by main thread and worker thread: // These variables are all protected by m_WorkerMutex @@ -210,9 +216,6 @@ private: bool m_Enabled; bool m_Shutdown; - - // Initialised in constructor by main thread; otherwise used only by worker thread: - double m_PauseUntilTime; }; void CSoundManager::ScriptingInit() diff --git a/source/soundmanager/items/CSoundBase.cpp b/source/soundmanager/items/CSoundBase.cpp index bcc1e55aa4..63159060b6 100644 --- a/source/soundmanager/items/CSoundBase.cpp +++ b/source/soundmanager/items/CSoundBase.cpp @@ -213,6 +213,12 @@ bool CSoundBase::HandleFade() return true; } +bool CSoundBase::IsFading() +{ + return ((m_ALSource != 0) && (m_StartFadeTime != 0)); +} + + bool CSoundBase::GetLooping() { return m_Looping; diff --git a/source/soundmanager/items/CSoundBase.h b/source/soundmanager/items/CSoundBase.h index 3bbb281932..10ffc1b229 100644 --- a/source/soundmanager/items/CSoundBase.h +++ b/source/soundmanager/items/CSoundBase.h @@ -57,6 +57,7 @@ public: virtual void SetDirection(const CVector3D& direction); virtual void SetCone(ALfloat innerCone, ALfloat outerCone, ALfloat coneGain); virtual void SetLastPlay(bool last); + virtual bool IsFading(); void Play(); void PlayAndDelete(); diff --git a/source/soundmanager/items/ISoundItem.h b/source/soundmanager/items/ISoundItem.h index 0bec057d6d..33d975e314 100644 --- a/source/soundmanager/items/ISoundItem.h +++ b/source/soundmanager/items/ISoundItem.h @@ -38,7 +38,8 @@ public: virtual CStrW* GetName() = 0; virtual bool IdleTask() = 0; - + virtual bool IsFading() = 0; + virtual void Play() = 0; virtual void Stop() = 0;