Fix race condition in thread pool shutdown

We were not holding the task queue lock when we did broadcast, causing
a race where a thread waits on the CV after the broadcast.

Fixes dex2oat deadlocking.

Change-Id: I84f30020511c2bd43f71d9b7b392720bd8d03eab
diff --git a/src/thread_pool.cc b/src/thread_pool.cc
index fa0cf79..f3319e2 100644
--- a/src/thread_pool.cc
+++ b/src/thread_pool.cc
@@ -69,11 +69,15 @@
 }
 
 ThreadPool::~ThreadPool() {
-  // Tell any remaining workers to shut down.
-  shutting_down_ = true;
-  android_memory_barrier();
-  // Broadcast to everyone waiting.
-  task_queue_condition_.Broadcast(Thread::Current());
+  {
+    Thread* self = Thread::Current();
+    MutexLock mu(self, task_queue_lock_);
+    // Tell any remaining workers to shut down.
+    shutting_down_ = true;
+    android_memory_barrier();
+    // Broadcast to everyone waiting.
+    task_queue_condition_.Broadcast(self);
+  }
   // Wait for the threads to finish.
   STLDeleteElements(&threads_);
 }