If a finalizer wedges, raise prio and retry.

The VM uses a watchdog mechanism to detect stuck finalizers.  It appears
that, in some cases, the watchdog may be firing because the HeapWorker
thread in a background process doesn't get any CPU time after a
remotely-induced GC finishes.  With this change, if the HeapWorker is
running at a reduced priority, we raise the priority and allow it to
try some more.

No attempt is made to put the thread priority back.  (The HeapWorker
thread doesn't do anything in an idle process, and what it does do is
geared toward freeing one kind of resource or another.)

For bug 2492196.

Change-Id: Ic734c2e2819b9d60d20b2961f2a75085d5879495
diff --git a/vm/Thread.h b/vm/Thread.h
index 964c968..2d6ce5f 100644
--- a/vm/Thread.h
+++ b/vm/Thread.h
@@ -22,6 +22,9 @@
 
 #include "jni.h"
 
+#include <cutils/sched_policy.h>
+
+
 #if defined(CHECK_MUTEX) && !defined(__USE_UNIX98)
 /* glibc lacks this unless you #define __USE_UNIX98 */
 int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
@@ -434,6 +437,14 @@
 Thread* dvmGetThreadFromThreadObject(Object* vmThreadObj);
 
 /*
+ * Given a pthread handle, return the associated Thread*.
+ * Caller must NOT hold the thread list lock.
+ *
+ * Returns NULL if the thread was not found.
+ */
+Thread* dvmGetThreadByHandle(pthread_t handle);
+
+/*
  * Sleep in a thread.  Returns when the sleep timer returns or the thread
  * is interrupted.
  */
@@ -466,6 +477,25 @@
  */
 void dvmChangeThreadPriority(Thread* thread, int newPriority);
 
+/* "change flags" values for raise/reset thread priority calls */
+#define kChangedPriority    0x01
+#define kChangedPolicy      0x02
+
+/*
+ * If necessary, raise the thread's priority to nice=0 cgroup=fg.
+ *
+ * Returns bit flags indicating changes made (zero if nothing was done).
+ */
+int dvmRaiseThreadPriorityIfNeeded(Thread* thread, int* pSavedThreadPrio,
+    SchedPolicy* pSavedThreadPolicy);
+
+/*
+ * Drop the thread priority to what it was before an earlier call to
+ * dvmRaiseThreadPriorityIfNeeded().
+ */
+void dvmResetThreadPriority(Thread* thread, int changeFlags,
+    int savedThreadPrio, SchedPolicy savedThreadPolicy);
+
 /*
  * Debug: dump information about a single thread.
  */