blast: protect destruction of callback thread

Wrap the creation and destruction of TransactionCompletedThread's
mThread in a mutex. This will prevent the race condition between
the creation of mThread and mThread.join().

Test: libsurfaceflinger_unittest
      Transaction_test

Change-Id: Ic448872cd5418ce7885b8c2e52a7f1bf02afd772
diff --git a/services/surfaceflinger/TransactionCompletedThread.cpp b/services/surfaceflinger/TransactionCompletedThread.cpp
index 9b9dc57..389118a 100644
--- a/services/surfaceflinger/TransactionCompletedThread.cpp
+++ b/services/surfaceflinger/TransactionCompletedThread.cpp
@@ -30,6 +30,8 @@
 namespace android {
 
 TransactionCompletedThread::~TransactionCompletedThread() {
+    std::lock_guard lockThread(mThreadMutex);
+
     {
         std::lock_guard lock(mMutex);
         mKeepRunning = false;
@@ -50,11 +52,13 @@
 
 void TransactionCompletedThread::run() {
     std::lock_guard lock(mMutex);
-    if (mRunning) {
+    if (mRunning || !mKeepRunning) {
         return;
     }
     mDeathRecipient = new ThreadDeathRecipient();
     mRunning = true;
+
+    std::lock_guard lockThread(mThreadMutex);
     mThread = std::thread(&TransactionCompletedThread::threadMain, this);
 }
 
diff --git a/services/surfaceflinger/TransactionCompletedThread.h b/services/surfaceflinger/TransactionCompletedThread.h
index 5af4097..1612f69 100644
--- a/services/surfaceflinger/TransactionCompletedThread.h
+++ b/services/surfaceflinger/TransactionCompletedThread.h
@@ -90,7 +90,10 @@
         }
     };
 
-    std::thread mThread;
+    // Protects the creation and destruction of mThread
+    std::mutex mThreadMutex;
+
+    std::thread mThread GUARDED_BY(mThreadMutex);
 
     std::mutex mMutex;
     std::condition_variable_any mConditionVariable;