Use sched_yield in Monitor::MonitorEnter.
Previously we used NanoSleep(1000), but this was unreliable. It could
result in waiting for >= 40ms instead of 1us. Since this was in a loop
it was especially bad if the GC was trying to suspend all the
threads when we were sleeping. This resulted in thread suspension
occasionally taking longer than a second.
Results on the provided picasso-sample app on Nexus 5:
Longest GC pause before: ~1.5s.
Longest GC pause after: <5ms.
Also added a warning if thread suspension takes longer than a
threshold (currently 5ms).
Bug: 16307460
External bug: https://code.google.com/p/android-developer-preview/issues/detail?id=367
Change-Id: I3c2a9636357e255f38634615101eff8ca84e632f
diff --git a/runtime/monitor.cc b/runtime/monitor.cc
index 5633a77..da481e4 100644
--- a/runtime/monitor.cc
+++ b/runtime/monitor.cc
@@ -746,7 +746,11 @@
contention_count++;
Runtime* runtime = Runtime::Current();
if (contention_count <= runtime->GetMaxSpinsBeforeThinkLockInflation()) {
- NanoSleep(1000); // Sleep for 1us and re-attempt.
+ // TODO: Consider switch thread state to kBlocked when we are yielding.
+ // Use sched_yield instead of NanoSleep since NanoSleep can wait much longer than the
+ // parameter you pass in. This can cause thread suspension to take excessively long
+ // make long pauses. See b/16307460.
+ sched_yield();
} else {
contention_count = 0;
InflateThinLocked(self, h_obj, lock_word, 0);