Use a non-recursive mutex and properly end the streaming thrread

openmw-38
Chris Robinson 9 years ago
parent b5ed2e65f8
commit 0f05ccf72a

@ -217,24 +217,28 @@ public:
struct OpenAL_Output::StreamThread { struct OpenAL_Output::StreamThread {
typedef std::vector<OpenAL_SoundStream*> StreamVec; typedef std::vector<OpenAL_SoundStream*> StreamVec;
StreamVec mStreams; StreamVec mStreams;
boost::recursive_mutex mMutex; volatile bool mQuitNow;
boost::condition_variable_any mCondVar; boost::mutex mMutex;
boost::condition_variable mCondVar;
boost::thread mThread; boost::thread mThread;
StreamThread() StreamThread()
: mThread(boost::ref(*this)) : mQuitNow(false), mThread(boost::ref(*this))
{ {
} }
~StreamThread() ~StreamThread()
{ {
mThread.interrupt(); mQuitNow = true;
mMutex.lock(); mMutex.unlock();
mCondVar.notify_all();
mThread.join();
} }
// boost::thread entry point // boost::thread entry point
void operator()() void operator()()
{ {
boost::unique_lock<boost::recursive_mutex> lock(mMutex); boost::unique_lock<boost::mutex> lock(mMutex);
while(1) while(!mQuitNow)
{ {
StreamVec::iterator iter = mStreams.begin(); StreamVec::iterator iter = mStreams.begin();
while(iter != mStreams.end()) while(iter != mStreams.end())
@ -250,7 +254,7 @@ struct OpenAL_Output::StreamThread {
void add(OpenAL_SoundStream *stream) void add(OpenAL_SoundStream *stream)
{ {
boost::unique_lock<boost::recursive_mutex> lock(mMutex); boost::unique_lock<boost::mutex> lock(mMutex);
if(std::find(mStreams.begin(), mStreams.end(), stream) == mStreams.end()) if(std::find(mStreams.begin(), mStreams.end(), stream) == mStreams.end())
{ {
mStreams.push_back(stream); mStreams.push_back(stream);
@ -261,14 +265,14 @@ struct OpenAL_Output::StreamThread {
void remove(OpenAL_SoundStream *stream) void remove(OpenAL_SoundStream *stream)
{ {
boost::lock_guard<boost::recursive_mutex> lock(mMutex); boost::lock_guard<boost::mutex> lock(mMutex);
StreamVec::iterator iter = std::find(mStreams.begin(), mStreams.end(), stream); StreamVec::iterator iter = std::find(mStreams.begin(), mStreams.end(), stream);
if(iter != mStreams.end()) mStreams.erase(iter); if(iter != mStreams.end()) mStreams.erase(iter);
} }
void removeAll() void removeAll()
{ {
boost::lock_guard<boost::recursive_mutex> lock(mMutex); boost::lock_guard<boost::mutex> lock(mMutex);
mStreams.clear(); mStreams.clear();
} }
@ -360,7 +364,7 @@ bool OpenAL_SoundStream::isPlaying()
{ {
ALint state; ALint state;
boost::lock_guard<boost::recursive_mutex> lock(mOutput.mStreamThread->mMutex); boost::lock_guard<boost::mutex> lock(mOutput.mStreamThread->mMutex);
alGetSourcei(mSource, AL_SOURCE_STATE, &state); alGetSourcei(mSource, AL_SOURCE_STATE, &state);
throwALerror(); throwALerror();
@ -375,7 +379,7 @@ double OpenAL_SoundStream::getTimeOffset()
ALint offset; ALint offset;
double t; double t;
boost::lock_guard<boost::recursive_mutex> lock(mOutput.mStreamThread->mMutex); boost::lock_guard<boost::mutex> lock(mOutput.mStreamThread->mMutex);
alGetSourcei(mSource, AL_SAMPLE_OFFSET, &offset); alGetSourcei(mSource, AL_SAMPLE_OFFSET, &offset);
alGetSourcei(mSource, AL_SOURCE_STATE, &state); alGetSourcei(mSource, AL_SOURCE_STATE, &state);
if(state == AL_PLAYING || state == AL_PAUSED) if(state == AL_PLAYING || state == AL_PAUSED)

Loading…
Cancel
Save