audiopolicy: Synchronize calls to Effects state

Synchronize calls that might access Effects state (alternate approach)

Bug: 26516500

Change-Id: Ie03de9c67232e0ac7f5db5cbfe87ec058acbfc8e
diff --git a/services/audiopolicy/service/AudioPolicyInterfaceImpl.cpp b/services/audiopolicy/service/AudioPolicyInterfaceImpl.cpp
index ae5cf3d..c9eb31f 100644
--- a/services/audiopolicy/service/AudioPolicyInterfaceImpl.cpp
+++ b/services/audiopolicy/service/AudioPolicyInterfaceImpl.cpp
@@ -497,6 +497,7 @@
     if (mAudioPolicyManager == NULL) {
         return NO_INIT;
     }
+    Mutex::Autolock _l(mEffectsLock);
     return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
 }
 
@@ -505,6 +506,7 @@
     if (mAudioPolicyManager == NULL) {
         return NO_INIT;
     }
+    Mutex::Autolock _l(mEffectsLock);
     return mAudioPolicyManager->unregisterEffect(id);
 }
 
@@ -513,6 +515,7 @@
     if (mAudioPolicyManager == NULL) {
         return NO_INIT;
     }
+    Mutex::Autolock _l(mEffectsLock);
     return mAudioPolicyManager->setEffectEnabled(id, enabled);
 }
 
@@ -577,6 +580,8 @@
         return false;
     }
     Mutex::Autolock _l(mLock);
+    Mutex::Autolock _le(mEffectsLock); // isOffloadSupported queries for
+                                      // non-offloadable effects
     return mAudioPolicyManager->isOffloadSupported(info);
 }
 
diff --git a/services/audiopolicy/service/AudioPolicyService.h b/services/audiopolicy/service/AudioPolicyService.h
index 160f4f0..a91c560 100644
--- a/services/audiopolicy/service/AudioPolicyService.h
+++ b/services/audiopolicy/service/AudioPolicyService.h
@@ -553,6 +553,10 @@
 
     mutable Mutex mLock;    // prevents concurrent access to AudioPolicy manager functions changing
                             // device connection state  or routing
+    mutable Mutex mEffectsLock; // serialize access to Effect state within APM.
+    // Note: lock acquisition order is always mLock > mEffectsLock:
+    // mLock protects AudioPolicyManager methods that can call into audio flinger
+    // and possibly back in to audio policy service and acquire mEffectsLock.
     sp<AudioCommandThread> mAudioCommandThread;     // audio commands thread
     sp<AudioCommandThread> mTonePlaybackThread;     // tone playback thread
     sp<AudioCommandThread> mOutputCommandThread;    // process stop and release output