Fix races in reading activeTrack->mState without lock
Change-Id: I8cfbd9884fa5959a846ddb52cdc797266b896769
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index 44763a6..1564a44 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -4215,6 +4215,7 @@
// start recording
for (;;) {
sp<RecordTrack> activeTrack;
+ TrackBase::track_state activeTrackState;
Vector< sp<EffectChain> > effectChains;
// sleep with mutex unlocked
@@ -4251,7 +4252,8 @@
continue;
}
- switch (activeTrack->mState) {
+ activeTrackState = activeTrack->mState;
+ switch (activeTrackState) {
case TrackBase::PAUSING:
standby();
mActiveTrack.clear();
@@ -4284,16 +4286,15 @@
break;
default:
- LOG_FATAL("Unexpected activeTrack->mState %d", activeTrack->mState);
+ LOG_FATAL("Unexpected activeTrackState %d", activeTrackState);
}
lockEffectChains_l(effectChains);
}
// thread mutex is now unlocked, mActiveTrack unknown, activeTrack != 0, kept, immutable
- // FIXME RecordThread::stop assigns to mState under lock, but we read without lock
- if (activeTrack->mState != TrackBase::ACTIVE &&
- activeTrack->mState != TrackBase::RESUMING) {
+ // activeTrack->mState unknown, activeTrackState immutable
+ if (activeTrackState != TrackBase::ACTIVE && activeTrackState != TrackBase::RESUMING) {
unlockEffectChains(effectChains);
doSleep = true;
continue;
@@ -4345,8 +4346,8 @@
mBytesRead = mInput->stream->read(mInput->stream, readInto,
mBufferSize);
if (mBytesRead <= 0) {
- // FIXME read mState without lock
- if ((mBytesRead < 0) && (activeTrack->mState == TrackBase::ACTIVE))
+ // TODO: verify that it's benign to use a stale track state
+ if ((mBytesRead < 0) && (activeTrackState == TrackBase::ACTIVE))
{
ALOGE("Error reading audio input");
// Force input into standby so that it tries to