From 402aee85a3a98fb1cc9b468798cc6d6d1aee9093 Mon Sep 17 00:00:00 2001 From: Alan Leung Date: Wed, 13 Jul 2016 10:59:54 -0700 Subject: [PATCH] Fix thread priority after boosting. The previous CL wrongly restores all priority to nice 0. This CL changes two things: 1. It remembers and restores the previous priority. 2. If the thread has priority of < -2. Do not set it to -2. BUG: 29930920 Change-Id: I927e060d789c9bbf92f6a166f44317d9db0f5205 (cherry picked from commit d6e67e6a165e0f1161090730996f5e82d3b6421b) --- .../android/server/am/ActivityManagerService.java | 39 ++++++++++++++-------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index c124e5d9a9bc..a6ac9d4d4dcd 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -640,30 +640,41 @@ public final class ActivityManagerService extends ActivityManagerNative return mShowDialogs && !mSleeping && !mShuttingDown; } - // it's a semaphore; boost when 0->1, reset when 1->0 - static ThreadLocal sIsBoosted = new ThreadLocal() { - @Override protected Integer initialValue() { - return 0; + private static final class PriorityState { + // Acts as counter for number of synchronized region that needs to acquire 'this' as a lock + // the current thread is currently in. When it drops down to zero, we will no longer boost + // the thread's priority. + private int regionCounter = 0; + + // The thread's previous priority before boosting. + private int prevPriority = Integer.MIN_VALUE; + } + + static ThreadLocal sThreadPriorityState = new ThreadLocal() { + @Override protected PriorityState initialValue() { + return new PriorityState(); } }; static void boostPriorityForLockedSection() { - if (sIsBoosted.get() == 0) { - // boost to prio 118 while holding a global lock - Process.setThreadPriority(Process.myTid(), -2); - //Log.e(TAG, "PRIORITY BOOST: set priority on TID " + Process.myTid()); + int tid = Process.myTid(); + int prevPriority = Process.getThreadPriority(tid); + PriorityState state = sThreadPriorityState.get(); + if (state.regionCounter == 0 && prevPriority > -2) { + state.prevPriority = prevPriority; + Process.setThreadPriority(tid, -2); } - int cur = sIsBoosted.get(); - sIsBoosted.set(cur + 1); + state.regionCounter++; } static void resetPriorityAfterLockedSection() { - sIsBoosted.set(sIsBoosted.get() - 1); - if (sIsBoosted.get() == 0) { - //Log.e(TAG, "PRIORITY BOOST: reset priority on TID " + Process.myTid()); - Process.setThreadPriority(Process.myTid(), 0); + PriorityState state = sThreadPriorityState.get(); + state.regionCounter--; + if (state.regionCounter == 0 && state.prevPriority > -2) { + Process.setThreadPriority(Process.myTid(), state.prevPriority); } } + public class PendingAssistExtras extends Binder implements Runnable { public final ActivityRecord activity; public final Bundle extras; -- 2.11.0