hal: Fix the audio loss issue on codec back end

- Start music playback, plug in and plug out headset and press
  'Next' button in the Music app immediately. Repeating these steps
  result complete loss of audio on HW codec.
- When headset is pluged out and Next is pressed immediately, the
  audio HAL triggers audio routing change from Headset to Speaker,
  and closure of compress playback driver. The later is not lock
  protected which result un protected access of back end information
  in the ALSA framework. This leads to incorrect routing and hence
  loss of audio. It is also observed that sometimes it could lead
  to crash in kernel and phone reboots.
- Fix by ensuring that the kernel driver close is also lock protected
  along with other routing events.

Bug: 11088400
Change-Id: I785effb09e5cef7ba20ee43e0ef91dc296d4e58a
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 0b603d7..48c426d 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -1184,6 +1184,7 @@
     if (!out->standby) {
+        pthread_mutex_lock(&adev->lock);
         out->standby = true;
         if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
             if (out->pcm) {
@@ -1199,7 +1200,6 @@
                 out->compr = NULL;
-        pthread_mutex_lock(&adev->lock);
@@ -1673,12 +1673,12 @@
     ALOGV("%s: enter", __func__);
     if (!in->standby) {
+        pthread_mutex_lock(&adev->lock);
         in->standby = true;
         if (in->pcm) {
             in->pcm = NULL;
-        pthread_mutex_lock(&adev->lock);
         status = stop_input_stream(in);