Add back join of windows eventhandler threads

These threads are polluting the exit code despite using the better
ExitProcess call. Instead of adding back Thread::Join, I've instead
inlined the call into the eventhandler to make it easier to be sure
the open handles are closed.

R=johnmccutchan@google.com

Review URL: https://codereview.chromium.org/1989463003 .
diff --git a/runtime/bin/eventhandler_win.cc b/runtime/bin/eventhandler_win.cc
index 9c95204..f125bb8 100644
--- a/runtime/bin/eventhandler_win.cc
+++ b/runtime/bin/eventhandler_win.cc
@@ -126,6 +126,7 @@
       last_error_(NOERROR),
       flags_(0),
       read_thread_id_(Thread::kInvalidThreadId),
+      read_thread_handle_(NULL),
       read_thread_starting_(false),
       read_thread_finished_(false),
       monitor_(new Monitor()) {
@@ -188,13 +189,24 @@
 
 
 void Handle::WaitForReadThreadFinished() {
-  MonitorLocker ml(monitor_);
-  if (read_thread_id_ != Thread::kInvalidThreadId) {
-    while (!read_thread_finished_) {
-      ml.Wait();
+  HANDLE to_join = NULL;
+  {
+    MonitorLocker ml(monitor_);
+    if (read_thread_id_ != Thread::kInvalidThreadId) {
+      while (!read_thread_finished_) {
+        ml.Wait();
+      }
+      read_thread_finished_ = false;
+      read_thread_id_ = Thread::kInvalidThreadId;
+      to_join = read_thread_handle_;
+      read_thread_handle_ = NULL;
     }
-    read_thread_finished_ = false;
-    read_thread_id_ = Thread::kInvalidThreadId;
+  }
+  if (to_join != NULL) {
+    // Join the read thread.
+    DWORD res = WaitForSingleObject(to_join, INFINITE);
+    CloseHandle(to_join);
+    ASSERT(res == WAIT_OBJECT_0);
   }
 }
 
@@ -242,10 +254,12 @@
   ASSERT(read_thread_starting_);
   ASSERT(read_thread_id_ == Thread::kInvalidThreadId);
   read_thread_id_ = Thread::GetCurrentThreadId();
+  read_thread_handle_ = OpenThread(SYNCHRONIZE, false, read_thread_id_);
   read_thread_starting_ = false;
   ml.Notify();
 }
 
+
 void Handle::NotifyReadThreadFinished() {
   MonitorLocker ml(monitor_);
   ASSERT(!read_thread_finished_);
@@ -742,6 +756,7 @@
   MonitorLocker ml(monitor_);
   write_thread_running_ = true;
   thread_id_ = Thread::GetCurrentThreadId();
+  thread_handle_ = OpenThread(SYNCHRONIZE, false, thread_id_);
   // Notify we have started.
   ml.Notify();
 
@@ -831,6 +846,10 @@
     while (write_thread_exists_) {
       ml.Wait(Monitor::kNoTimeout);
     }
+    // Join the thread.
+    DWORD res = WaitForSingleObject(thread_handle_, INFINITE);
+    CloseHandle(thread_handle_);
+    ASSERT(res == WAIT_OBJECT_0);
   }
   Handle::DoClose();
 }
@@ -1357,6 +1376,7 @@
 EventHandlerImplementation::EventHandlerImplementation() {
   startup_monitor_ = new Monitor();
   handler_thread_id_ = Thread::kInvalidThreadId;
+  handler_thread_handle_ = NULL;
   completion_port_ =
       CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1);
   if (completion_port_ == NULL) {
@@ -1367,6 +1387,10 @@
 
 
 EventHandlerImplementation::~EventHandlerImplementation() {
+  // Join the handler thread.
+  DWORD res = WaitForSingleObject(handler_thread_handle_, INFINITE);
+  CloseHandle(handler_thread_handle_);
+  ASSERT(res == WAIT_OBJECT_0);
   delete startup_monitor_;
   CloseHandle(completion_port_);
 }
@@ -1405,6 +1429,8 @@
   {
     MonitorLocker ml(handler_impl->startup_monitor_);
     handler_impl->handler_thread_id_ = Thread::GetCurrentThreadId();
+    handler_impl->handler_thread_handle_ =
+        OpenThread(SYNCHRONIZE, false, handler_impl->handler_thread_id_);
     ml.Notify();
   }
 
diff --git a/runtime/bin/eventhandler_win.h b/runtime/bin/eventhandler_win.h
index edb5c48..9daff93 100644
--- a/runtime/bin/eventhandler_win.h
+++ b/runtime/bin/eventhandler_win.h
@@ -263,6 +263,7 @@
   DWORD last_error_;
 
   ThreadId read_thread_id_;
+  HANDLE read_thread_handle_;
   bool read_thread_starting_;
   bool read_thread_finished_;
 
@@ -298,6 +299,7 @@
   explicit StdHandle(HANDLE handle)
       : FileHandle(handle),
         thread_id_(Thread::kInvalidThreadId),
+        thread_handle_(NULL),
         thread_wrote_(0),
         write_thread_exists_(false),
         write_thread_running_(false) {
@@ -312,6 +314,7 @@
 
  private:
   ThreadId thread_id_;
+  HANDLE thread_handle_;
   intptr_t thread_wrote_;
   bool write_thread_exists_;
   bool write_thread_running_;
@@ -538,6 +541,7 @@
 
   Monitor* startup_monitor_;
   ThreadId handler_thread_id_;
+  HANDLE handler_thread_handle_;
 
   TimeoutQueue timeout_queue_;  // Time for next timeout.
   bool shutdown_;