AudioFlinger: Protect initialization code in threadLoop with mutex

Flag: EXEMPT bugfix
Test: atest CtsMediaAudioTestCases
Bug: 364243966
Change-Id: Ia87be0b9240f3da3c06f38cae9b6039cf0a7246e
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index 0d961bb..82f68df 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -4065,7 +4065,13 @@
     // FIXME could this be made local to while loop?
     writeFrames = 0;
 
-    cacheParameters_l();
+    {
+        audio_utils::lock_guard l(mutex());
+
+        cacheParameters_l();
+        checkSilentMode_l();
+    }
+
     mSleepTimeUs = mIdleSleepTimeUs;
 
     if (mType == MIXER || mType == SPATIALIZER) {
@@ -4090,8 +4096,6 @@
     // suspended mode (for now) to help schedule the wait time until next iteration.
     nsecs_t timeLoopNextNs = 0;
 
-    checkSilentMode_l();
-
     audio_patch_handle_t lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE;
 
     sendCheckOutputStageEffectsEvent();