HWC: Protect hwc_set from async events

Wait for hwc_set completion if already in progress before processing
the async events.

Bug: 7288769
Change-Id: Ibdd0ee82f079df566b899c94df19f6bb1031d686
Signed-off-by: Iliyan Malchev <malchev@google.com>
diff --git a/libhwcomposer/hwc.cpp b/libhwcomposer/hwc.cpp
index 1e66235..c510b66 100644
--- a/libhwcomposer/hwc.cpp
+++ b/libhwcomposer/hwc.cpp
@@ -278,7 +278,7 @@
 
 static int hwc_set_external(hwc_context_t *ctx,
         hwc_display_contents_1_t* list) {
-
+    Locker::Autolock _l(ctx->mExtSetLock);
     if(!ctx->overlayInUse[HWC_DISPLAY_EXTERNAL])
         ctx->mOverlay[HWC_DISPLAY_EXTERNAL]->setState(ovutils::OV_CLOSED);
 
diff --git a/libhwcomposer/hwc_uevents.cpp b/libhwcomposer/hwc_uevents.cpp
index a7f58ea..987957a 100644
--- a/libhwcomposer/hwc_uevents.cpp
+++ b/libhwcomposer/hwc_uevents.cpp
@@ -59,6 +59,7 @@
             ctx->mExtDisplay->setExternalDisplay(connected);;
         } else if(!(strncmp(str,"offline@",strlen("offline@")))) {
             connected = 0;
+            Locker::Autolock _l(ctx->mExtSetLock);
             ctx->mExtDisplay->setExternalDisplay(connected);;
         }
     }
diff --git a/libhwcomposer/hwc_utils.h b/libhwcomposer/hwc_utils.h
index 9193540..ef2e3c8 100644
--- a/libhwcomposer/hwc_utils.h
+++ b/libhwcomposer/hwc_utils.h
@@ -212,6 +212,8 @@
 
     //Lock to prevent set from being called while blanking
     mutable Locker mBlankLock;
+    //Lock to protect set when detaching external disp
+    mutable Locker mExtSetLock;
     //Vsync
     struct vsync_state vstate;
 };