From 598de6c701e989385eeffa7c5dfd61f0459a2631 Mon Sep 17 00:00:00 2001 From: Glenn Kasten Date: Wed, 16 Oct 2013 17:02:13 -0700 Subject: Fix race condition in AudioTrack::pause followed by start Bug: 11148722 Change-Id: Iec88f00c8510363d4418e4b8d5b34feb06ecf04d --- include/media/AudioTrack.h | 1 + media/libmedia/AudioTrack.cpp | 15 ++++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/include/media/AudioTrack.h b/include/media/AudioTrack.h index 22ad57e..f2f9c22 100644 --- a/include/media/AudioTrack.h +++ b/include/media/AudioTrack.h @@ -612,6 +612,7 @@ protected: bool mPaused; // whether thread is requested to pause at next loop entry bool mPausedInt; // whether thread internally requests pause nsecs_t mPausedNs; // if mPausedInt then associated timeout, otherwise ignored + bool mIgnoreNextPausedInt; // whether to ignore next mPausedInt request }; // body of AudioTrackThread::threadLoop() diff --git a/media/libmedia/AudioTrack.cpp b/media/libmedia/AudioTrack.cpp index 37d50cf..80f5155 100644 --- a/media/libmedia/AudioTrack.cpp +++ b/media/libmedia/AudioTrack.cpp @@ -1785,7 +1785,8 @@ void AudioTrack::DeathNotifier::binderDied(const wp& who) // ========================================================================= AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava) - : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL) + : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL), + mIgnoreNextPausedInt(false) { } @@ -1802,6 +1803,10 @@ bool AudioTrack::AudioTrackThread::threadLoop() // caller will check for exitPending() return true; } + if (mIgnoreNextPausedInt) { + mIgnoreNextPausedInt = false; + mPausedInt = false; + } if (mPausedInt) { if (mPausedNs > 0) { (void) mMyCond.waitRelative(mMyLock, mPausedNs); @@ -1836,12 +1841,7 @@ void AudioTrack::AudioTrackThread::requestExit() { // must be in this order to avoid a race condition Thread::requestExit(); - AutoMutex _l(mMyLock); - if (mPaused || mPausedInt) { - mPaused = false; - mPausedInt = false; - mMyCond.signal(); - } + resume(); } void AudioTrack::AudioTrackThread::pause() @@ -1853,6 +1853,7 @@ void AudioTrack::AudioTrackThread::pause() void AudioTrack::AudioTrackThread::resume() { AutoMutex _l(mMyLock); + mIgnoreNextPausedInt = true; if (mPaused || mPausedInt) { mPaused = false; mPausedInt = false; -- cgit v1.1