From 9568aa6a84af76e9f33db7dde600eee9b7fbb386 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 27 Nov 2015 04:30:09 -0800 Subject: [PATCH] Use a condition variable to wake up the audio stream thread This should make starting streams a bit more responsive, and allows us to do more in it that really shouldn't wait for its next wake up. --- apps/openmw/mwsound/openal_output.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/apps/openmw/mwsound/openal_output.cpp b/apps/openmw/mwsound/openal_output.cpp index a5246bde9..685979bcf 100644 --- a/apps/openmw/mwsound/openal_output.cpp +++ b/apps/openmw/mwsound/openal_output.cpp @@ -217,6 +217,7 @@ struct OpenAL_Output::StreamThread { typedef std::vector StreamVec; StreamVec mStreams; boost::recursive_mutex mMutex; + boost::condition_variable_any mCondVar; boost::thread mThread; StreamThread() @@ -231,9 +232,9 @@ struct OpenAL_Output::StreamThread { // boost::thread entry point void operator()() { + boost::unique_lock lock(mMutex); while(1) { - boost::unique_lock lock(mMutex); StreamVec::iterator iter = mStreams.begin(); while(iter != mStreams.end()) { @@ -242,16 +243,19 @@ struct OpenAL_Output::StreamThread { else ++iter; } - lock.unlock(); - boost::this_thread::sleep(boost::posix_time::milliseconds(50)); + mCondVar.timed_wait(lock, boost::posix_time::milliseconds(50)); } } void add(OpenAL_SoundStream *stream) { - boost::lock_guard lock(mMutex); + boost::unique_lock lock(mMutex); if(std::find(mStreams.begin(), mStreams.end(), stream) == mStreams.end()) + { mStreams.push_back(stream); + lock.unlock(); + mCondVar.notify_all(); + } } void remove(OpenAL_SoundStream *stream)