ART can't create more than 8k threads during the worktime

ART uses LDT to point to the Java Thread structure. This structure
can holds up to 8k pointers, so we can have 8k threads simultaneously.
But the current implementation does not free slots for the finished threads.
This means there cannot be more than 8k threads created during the whole
life cycle.
This patch implements the LDT slots freeing mechanism.

Change-Id: Ifcf8fe1f4434a13f940146fff39b9c7bf91ee17b
Signed-off-by: Alexei Zavjalov <alexei.zavjalov@intel.com>
diff --git a/runtime/thread.cc b/runtime/thread.cc
index 68f3c04..c3ef228 100644
--- a/runtime/thread.cc
+++ b/runtime/thread.cc
@@ -1046,9 +1046,11 @@
   }
   opeer_ = nullptr;
 
-  delete jni_env_;
-  jni_env_ = nullptr;
-
+  bool initialized = (jni_env_ != nullptr);  // Did Thread::Init run?
+  if (initialized) {
+    delete jni_env_;
+    jni_env_ = nullptr;
+  }
   CHECK_NE(GetState(), kRunnable);
   CHECK_NE(ReadFlag(kCheckpointRequest), true);
   CHECK(checkpoint_functions_[0] == nullptr);
@@ -1065,6 +1067,10 @@
     delete long_jump_context_;
   }
 
+  if (initialized) {
+    CleanupCpu();
+  }
+
   delete debug_invoke_req_;
   delete single_step_control_;
   delete instrumentation_stack_;