drm_hwcomposer: avoid potential race condition between worker init and exit am: ba494c8efc am: d7a6c091e4
am: 884ae6f0d7

Change-Id: I4f9e5911dd296c299042eb74e71d84147600e183
diff --git a/worker.cpp b/worker.cpp
index 47aeb86..da6c580 100644
--- a/worker.cpp
+++ b/worker.cpp
@@ -30,21 +30,23 @@
 }
 
 int Worker::InitWorker() {
+  std::lock_guard<std::mutex> lk(mutex_);
   if (initialized())
     return -EALREADY;
 
   thread_ = std::unique_ptr<std::thread>(
       new std::thread(&Worker::InternalRoutine, this));
   initialized_ = true;
+  exit_ = false;
 
   return 0;
 }
 
 void Worker::Exit() {
+  std::unique_lock<std::mutex> lk(mutex_);
+  exit_ = true;
   if (initialized()) {
-    Lock();
-    exit_ = true;
-    Unlock();
+    lk.unlock();
     cond_.notify_all();
     thread_->join();
     initialized_ = false;
@@ -68,7 +70,7 @@
   if (should_exit())
     ret = -EINTR;
 
-  // release leaves lock unlocked when returning
+  // release leaves mutex locked when going out of scope
   lk.release();
 
   return ret;