hwc: fix some race conditions

Fix some race conditions between hwc_eventControl and the hwcVsyncThread.

Bug: 7274951
Change-Id: Ic71d65918303b4cef6a379ff5397702b2b578373
Signed-off-by: Iliyan Malchev <malchev@google.com>
diff --git a/libhwcomposer/hwc.cpp b/libhwcomposer/hwc.cpp
index c4431b6..920902e 100644
--- a/libhwcomposer/hwc.cpp
+++ b/libhwcomposer/hwc.cpp
@@ -165,11 +165,11 @@
                              int event, int enabled)
 {
     int ret = 0;
-    static int prev_value;
 
     hwc_context_t* ctx = (hwc_context_t*)(dev);
     private_module_t* m = reinterpret_cast<private_module_t*>(
                 ctx->mFbDev->common.module);
+    pthread_mutex_lock(&ctx->vstate.lock);
     switch(event) {
         case HWC_EVENT_VSYNC:
             if (ctx->vstate.enable == enabled)
@@ -178,30 +178,18 @@
                      &enabled) < 0) {
                 ALOGE("%s: vsync control failed. Dpy=%d, enabled=%d : %s",
                       __FUNCTION__, dpy, enabled, strerror(errno));
-
                 ret = -errno;
             }
-            /* vsync state change logic */
-            if (enabled == 1) {
-                //unblock vsync thread
-                pthread_mutex_lock(&ctx->vstate.lock);
-                ctx->vstate.enable = true;
+            ctx->vstate.enable = !!enabled;
+            if (enabled)
                 pthread_cond_signal(&ctx->vstate.cond);
-                pthread_mutex_unlock(&ctx->vstate.lock);
-            } else if (enabled == 0 && ctx->vstate.enable) {
-                //vsync thread will block
-                pthread_mutex_lock(&ctx->vstate.lock);
-                ctx->vstate.enable = false;
-                pthread_mutex_unlock(&ctx->vstate.lock);
-            }
-            ALOGD_IF (VSYNC_DEBUG, "VSYNC state changed from %s to %s",
-              (prev_value)?"ENABLED":"DISABLED", (enabled)?"ENABLED":"DISABLED");
-            prev_value = enabled;
-            /* vsync state change logic - end*/
-           break;
+            ALOGD_IF (VSYNC_DEBUG, "VSYNC state changed to %s",
+                      (enabled)?"ENABLED":"DISABLED");
+            break;
         default:
             ret = -EINVAL;
     }
+    pthread_mutex_unlock(&ctx->vstate.lock);
     return ret;
 }