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

Commit c3a6902e authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Refactor HintManagerService locking to fix race" into 24D1-dev

parents f3552b6d 3f40610e
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) {