From 0d192a9e279133f80a288d34e50c64a5c91c1a5b Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Tue, 15 Jul 2014 16:39:47 -0700 Subject: [PATCH] Maybe fix issue #16167054: Wakelock is acquired and released in... ...battery history even though CPU is in suspend There is a race in the power manager between noting a wake lock acquire and ensuring the device is staying awake. Change-Id: I3d76f99d73dca119b09f253f0e31448408c88a3c --- .../android/server/power/PowerManagerService.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/services/core/java/com/android/server/power/PowerManagerService.java b/services/core/java/com/android/server/power/PowerManagerService.java index ebad4224199af..cb4d3399de9ad 100644 --- a/services/core/java/com/android/server/power/PowerManagerService.java +++ b/services/core/java/com/android/server/power/PowerManagerService.java @@ -698,6 +698,7 @@ public final class PowerManagerService extends com.android.server.SystemService WakeLock wakeLock; int index = findWakeLockIndexLocked(lock); + boolean notifyAcquire; if (index >= 0) { wakeLock = mWakeLocks.get(index); if (!wakeLock.hasSameProperties(flags, tag, ws, uid, pid)) { @@ -706,6 +707,7 @@ public final class PowerManagerService extends com.android.server.SystemService uid, pid, ws, historyTag); wakeLock.updateProperties(flags, tag, packageName, ws, historyTag, uid, pid); } + notifyAcquire = false; } else { wakeLock = new WakeLock(lock, flags, tag, packageName, ws, historyTag, uid, pid); try { @@ -713,13 +715,21 @@ public final class PowerManagerService extends com.android.server.SystemService } catch (RemoteException ex) { throw new IllegalArgumentException("Wake lock is already dead."); } - notifyWakeLockAcquiredLocked(wakeLock); mWakeLocks.add(wakeLock); + notifyAcquire = true; } applyWakeLockFlagsOnAcquireLocked(wakeLock); mDirty |= DIRTY_WAKE_LOCKS; updatePowerStateLocked(); + if (notifyAcquire) { + // This needs to be done last so we are sure we have acquired the + // kernel wake lock. Otherwise we have a race where the system may + // go to sleep between the time we start the accounting in battery + // stats and when we actually get around to telling the kernel to + // stay awake. + notifyWakeLockAcquiredLocked(wakeLock); + } } }