summaryrefslogtreecommitdiffstats
path: root/runtime/thread-inl.h
diff options
context:
space:
mode:
authorHiroshi Yamauchi <yamauchi@google.com>2016-10-03 15:32:01 -0700
committerHiroshi Yamauchi <yamauchi@google.com>2016-10-05 17:52:29 -0700
commit02e7f1a46d8dbb277d045182cd1fa4b058d55162 (patch)
treebd14cec6d5c4a545c2b9081d72b6e1b49ec7ef1e /runtime/thread-inl.h
parentd1224dce59eb0019507e41da5e10f12dda66bee4 (diff)
downloadart-02e7f1a46d8dbb277d045182cd1fa4b058d55162.tar.gz
art-02e7f1a46d8dbb277d045182cd1fa4b058d55162.tar.bz2
art-02e7f1a46d8dbb277d045182cd1fa4b058d55162.zip
Fix a deadlock between thread flip and suspend request.
See 31683379#9 for the deadlock scenario. Make ModifySuspendCount(+1) retry if the thread flip function is set. Bug: 31683379 Bug: 12687968 Test: test-art, N9 libartd boot, Ritz EAAC with CC. Test: 129-GetThreadId with gcstress and CC. Change-Id: Id5cdfcd90a08a2ff497f9f0e2842fa4c613549bc
Diffstat (limited to 'runtime/thread-inl.h')
-rw-r--r--runtime/thread-inl.h31
1 files changed, 31 insertions, 0 deletions
diff --git a/runtime/thread-inl.h b/runtime/thread-inl.h
index bb6eb790a2..21b84dd55a 100644
--- a/runtime/thread-inl.h
+++ b/runtime/thread-inl.h
@@ -314,6 +314,37 @@ inline void Thread::PoisonObjectPointersIfDebug() {
}
}
+inline bool Thread::ModifySuspendCount(Thread* self,
+ int delta,
+ AtomicInteger* suspend_barrier,
+ bool for_debugger) {
+ if (delta > 0 && ((kUseReadBarrier && this != self) || suspend_barrier != nullptr)) {
+ // When delta > 0 (requesting a suspend), ModifySuspendCountInternal() may fail either if
+ // active_suspend_barriers is full or we are in the middle of a thread flip. Retry in a loop.
+ while (true) {
+ if (LIKELY(ModifySuspendCountInternal(self, delta, suspend_barrier, for_debugger))) {
+ return true;
+ } else {
+ // Failure means the list of active_suspend_barriers is full or we are in the middle of a
+ // thread flip, we should release the thread_suspend_count_lock_ (to avoid deadlock) and
+ // wait till the target thread has executed or Thread::PassActiveSuspendBarriers() or the
+ // flip function. Note that we could not simply wait for the thread to change to a suspended
+ // state, because it might need to run checkpoint function before the state change or
+ // resumes from the resume_cond_, which also needs thread_suspend_count_lock_.
+ //
+ // The list of active_suspend_barriers is very unlikely to be full since more than
+ // kMaxSuspendBarriers threads need to execute SuspendAllInternal() simultaneously, and
+ // target thread stays in kRunnable in the mean time.
+ Locks::thread_suspend_count_lock_->ExclusiveUnlock(self);
+ NanoSleep(100000);
+ Locks::thread_suspend_count_lock_->ExclusiveLock(self);
+ }
+ }
+ } else {
+ return ModifySuspendCountInternal(self, delta, suspend_barrier, for_debugger);
+ }
+}
+
} // namespace art
#endif // ART_RUNTIME_THREAD_INL_H_