Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 83f8dbda authored by Nicolo' Mazzucato's avatar Nicolo' Mazzucato Committed by Nicolò Mazzucato
Browse files

Use separate locks for BatteryController estimation and callbacks

Initially mFetchCallbacks was used as a lock for everything going on in the class.
From droidfooders traces we noticed that getEstimatedTimeRemainingString was blocking the main thread because an estimate update was going on the in background, even if it was only waiting to add a callback to a list.

 This was causing priority inversion (ui thread depending on the background thread with less priority), ending up blocking the ui thread for 150ms.

 With this cl there are 2 locks: one just for the callbacks list, and one for the estimate computations

Bug: 272677942
Test: BatteryControllerTest
Change-Id: I31cc862161377678f60b70f5870a5295bb2625fa
parent 4626fa48
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -57,6 +57,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import javax.annotation.concurrent.GuardedBy;

/**
 * Default implementation of a {@link BatteryController}. This controller monitors for battery
 * level change events that are broadcasted by the system.
@@ -94,7 +96,10 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC
    private boolean mTestMode = false;
    @VisibleForTesting
    boolean mHasReceivedBattery = false;
    @GuardedBy("mEstimateLock")
    private Estimate mEstimate;
    private final Object mEstimateLock = new Object();

    private boolean mFetchingEstimate = false;

    // Use AtomicReference because we may request it from a different thread
@@ -321,7 +326,7 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC

    @Nullable
    private String generateTimeRemainingString() {
        synchronized (mFetchCallbacks) {
        synchronized (mEstimateLock) {
            if (mEstimate == null) {
                return null;
            }
@@ -340,7 +345,7 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC
        mFetchingEstimate = true;
        mBgHandler.post(() -> {
            // Only fetch the estimate if they are enabled
            synchronized (mFetchCallbacks) {
            synchronized (mEstimateLock) {
                mEstimate = null;
                if (mEstimates.isHybridNotificationEnabled()) {
                    updateEstimate();
@@ -363,6 +368,7 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC
    }

    @WorkerThread
    @GuardedBy("mEstimateLock")
    private void updateEstimate() {
        Assert.isNotMainThread();
        // if the estimate has been cached we can just use that, otherwise get a new one and