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

Commit 3f40610e authored by Matt Buckley's avatar Matt Buckley
Browse files

Refactor HintManagerService locking to fix race

Bug: 335341012
Test: atest HintManagerServiceTest
Change-Id: I36670f144bc638143ebbe785885ace8aa957f40c
Merged-In: I36670f144bc638143ebbe785885ace8aa957f40c
(cherry picked from commit c0365183)
parent 7e5c107f
Loading
Loading
Loading
Loading
+8 −13
Original line number Diff line number Diff line
@@ -70,7 +70,7 @@ public final class HintManagerService extends SystemService {
    @GuardedBy("mLock")
    private final ArrayMap<Integer, ArrayMap<IBinder, ArraySet<AppHintSession>>> mActiveSessions;

    /** Lock to protect HAL handles and listen list. */
    /** Lock to protect HAL handles, listen list, and the UidObserver. */
    private final Object mLock = new Object();

    @VisibleForTesting final MyUidObserver mUidObserver;
@@ -266,11 +266,10 @@ public final class HintManagerService extends SystemService {

    @VisibleForTesting
    final class MyUidObserver extends UidObserver {
        private final Object mCacheLock = new Object();
        @GuardedBy("mCacheLock")
        @GuardedBy("mLock")
        private final SparseIntArray mProcStatesCache = new SparseIntArray();
        public boolean isUidForeground(int uid) {
            synchronized (mCacheLock) {
            synchronized (mLock) {
                return mProcStatesCache.get(uid, ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND)
                        <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;
            }
@@ -279,10 +278,8 @@ public final class HintManagerService extends SystemService {
        @Override
        public void onUidGone(int uid, boolean disabled) {
            FgThread.getHandler().post(() -> {
                synchronized (mCacheLock) {
                    mProcStatesCache.delete(uid);
                }
                synchronized (mLock) {
                    mProcStatesCache.delete(uid);
                    ArrayMap<IBinder, ArraySet<AppHintSession>> tokenMap = mActiveSessions.get(uid);
                    if (tokenMap == null) {
                        return;
@@ -305,11 +302,9 @@ public final class HintManagerService extends SystemService {
        @Override
        public void onUidStateChanged(int uid, int procState, long procStateSeq, int capability) {
            FgThread.getHandler().post(() -> {
                synchronized (mCacheLock) {
                synchronized (mLock) {
                    mProcStatesCache.put(uid, procState);
                }
                    boolean shouldAllowUpdate = isUidForeground(uid);
                synchronized (mLock) {
                    ArrayMap<IBinder, ArraySet<AppHintSession>> tokenMap = mActiveSessions.get(uid);
                    if (tokenMap == null) {
                        return;
@@ -400,10 +395,10 @@ public final class HintManagerService extends SystemService {
                    return null;
                }

                AppHintSession hs = new AppHintSession(callingUid, callingTgid, tids, token,
                        halSessionPtr, durationNanos);
                logPerformanceHintSessionAtom(callingUid, halSessionPtr, durationNanos, tids);
                synchronized (mLock) {
                    AppHintSession hs = new AppHintSession(callingUid, callingTgid, tids, token,
                            halSessionPtr, durationNanos);
                    ArrayMap<IBinder, ArraySet<AppHintSession>> tokenMap =
                            mActiveSessions.get(callingUid);
                    if (tokenMap == null) {