Wait for threads to finish unregistering
There was a race where Thread::join would return before the thread was
unregistered. This caused a problem with Daemons.stop since the thread
list could get deleted before the daemon thread was removed from list_.
This caused occasional "Request to unregister unattached thread"
errors and warnings.
The fix is to wait until threads finish registering before destroying
the thread list. The only threads which can be unregistering at this
point are the daemons we stopped earlier during the runtime shutdown
process. The issue is that thread join finishes before we remove the
thread from the thread list.
Also some cleanup.
Bug: 18713034
Change-Id: I8921122fe8462643a6b814b5f00632481e3831fb
diff --git a/runtime/base/mutex.h b/runtime/base/mutex.h
index 45d2347..3b052c0 100644
--- a/runtime/base/mutex.h
+++ b/runtime/base/mutex.h
@@ -487,7 +487,7 @@
class Locks {
public:
static void Init();
-
+ static void InitConditions() NO_THREAD_SAFETY_ANALYSIS; // Condition variables.
// Guards allocation entrypoint instrumenting.
static Mutex* instrument_entrypoints_lock_;
@@ -575,6 +575,9 @@
// attaching and detaching.
static Mutex* thread_list_lock_ ACQUIRED_AFTER(deoptimization_lock_);
+ // Signaled when threads terminate. Used to determine when all non-daemons have terminated.
+ static ConditionVariable* thread_exit_cond_ GUARDED_BY(Locks::thread_list_lock_);
+
// Guards maintaining loading library data structures.
static Mutex* jni_libraries_lock_ ACQUIRED_AFTER(thread_list_lock_);