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

Commit f0a3517f authored by Yifei Zhang's avatar Yifei Zhang Committed by Android (Google) Code Review
Browse files

Merge "snapshotcache: replce global lock with local one" into main

parents c8d07d64 c08978ce
Loading
Loading
Loading
Loading
+9 −7
Original line number Diff line number Diff line
@@ -23,13 +23,14 @@ import android.window.TaskSnapshot;
 */
class ActivitySnapshotCache extends SnapshotCache<ActivityRecord> {

    ActivitySnapshotCache(WindowManagerService service) {
        super(service, "Activity");
    ActivitySnapshotCache() {
        super("Activity");
    }

    @Override
    void putSnapshot(ActivityRecord ar, TaskSnapshot snapshot) {
        final int hasCode = System.identityHashCode(ar);
        synchronized (mLock) {
            final CacheEntry entry = mRunningCache.get(hasCode);
            if (entry != null) {
                mAppIdMap.remove(entry.topApp);
@@ -38,3 +39,4 @@ class ActivitySnapshotCache extends SnapshotCache<ActivityRecord> {
            mRunningCache.put(hasCode, new CacheEntry(snapshot, ar));
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -102,7 +102,7 @@ class ActivitySnapshotController extends AbsAppSnapshotController<ActivityRecord
                Environment::getDataSystemCeDirectory);
        mPersister = new TaskSnapshotPersister(persistQueue, mPersistInfoProvider);
        mSnapshotLoader = new AppSnapshotLoader(mPersistInfoProvider);
        initialize(new ActivitySnapshotCache(service));
        initialize(new ActivitySnapshotCache());

        final boolean snapshotEnabled =
                !service.mContext
+37 −20
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ import android.annotation.Nullable;
import android.util.ArrayMap;
import android.window.TaskSnapshot;

import com.android.internal.annotations.GuardedBy;

import java.io.PrintWriter;

/**
@@ -26,25 +28,31 @@ import java.io.PrintWriter;
 * @param <TYPE> The basic type, either Task or ActivityRecord
 */
abstract class SnapshotCache<TYPE extends WindowContainer> {
    protected final WindowManagerService mService;
    protected final Object mLock = new Object();

    protected final String mName;

    @GuardedBy("mLock")
    protected final ArrayMap<ActivityRecord, Integer> mAppIdMap = new ArrayMap<>();

    @GuardedBy("mLock")
    protected final ArrayMap<Integer, CacheEntry> mRunningCache = new ArrayMap<>();

    SnapshotCache(WindowManagerService service, String name) {
        mService = service;
    SnapshotCache(String name) {
        mName = name;
    }

    abstract void putSnapshot(TYPE window, TaskSnapshot snapshot);

    void clearRunningCache() {
        synchronized (mLock) {
            mRunningCache.clear();
        }
    }

    @Nullable
    final TaskSnapshot getSnapshot(Integer id) {
        synchronized (mService.mGlobalLock) {
        synchronized (mLock) {
            // Try the running cache.
            final CacheEntry entry = mRunningCache.get(id);
            if (entry != null) {
@@ -56,36 +64,44 @@ abstract class SnapshotCache<TYPE extends WindowContainer> {

    /** Called when an app token has been removed. */
    void onAppRemoved(ActivityRecord activity) {
        synchronized (mLock) {
            final Integer id = mAppIdMap.get(activity);
            if (id != null) {
                removeRunningEntry(id);
            }
        }
    }

    /** Called when an app window token's process died. */
    void onAppDied(ActivityRecord activity) {
        synchronized (mLock) {
            final Integer id = mAppIdMap.get(activity);
            if (id != null) {
                removeRunningEntry(id);
            }
        }
    }

    void onIdRemoved(Integer index) {
        removeRunningEntry(index);
    }

    void removeRunningEntry(Integer id) {
        synchronized (mLock) {
            final CacheEntry entry = mRunningCache.get(id);
            if (entry != null) {
                mAppIdMap.remove(entry.topApp);
                mRunningCache.remove(id);
            }
        }
    }

    void dump(PrintWriter pw, String prefix) {
        final String doublePrefix = prefix + "  ";
        final String triplePrefix = doublePrefix + "  ";
        pw.println(prefix + "SnapshotCache " + mName);

        synchronized (mLock) {
            for (int i = mRunningCache.size() - 1; i >= 0; i--) {
                final CacheEntry entry = mRunningCache.valueAt(i);
                pw.println(doublePrefix + "Entry token=" + mRunningCache.keyAt(i));
@@ -93,6 +109,7 @@ abstract class SnapshotCache<TYPE extends WindowContainer> {
                pw.println(triplePrefix + "snapshot=" + entry.snapshot);
            }
        }
    }

    static final class CacheEntry {
        /** The snapshot. */
+10 −8
Original line number Diff line number Diff line
@@ -28,12 +28,13 @@ class TaskSnapshotCache extends SnapshotCache<Task> {

    private final AppSnapshotLoader mLoader;

    TaskSnapshotCache(WindowManagerService service, AppSnapshotLoader loader) {
        super(service, "Task");
    TaskSnapshotCache(AppSnapshotLoader loader) {
        super("Task");
        mLoader = loader;
    }

    void putSnapshot(Task task, TaskSnapshot snapshot) {
        synchronized (mLock) {
            final CacheEntry entry = mRunningCache.get(task.mTaskId);
            if (entry != null) {
                mAppIdMap.remove(entry.topApp);
@@ -42,6 +43,7 @@ class TaskSnapshotCache extends SnapshotCache<Task> {
            mAppIdMap.put(top, task.mTaskId);
            mRunningCache.put(task.mTaskId, new CacheEntry(snapshot, top));
        }
    }

    /**
     * If {@param restoreFromDisk} equals {@code true}, DO NOT HOLD THE WINDOW MANAGER LOCK!
+1 −1
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ class TaskSnapshotController extends AbsAppSnapshotController<Task, TaskSnapshot
                Environment::getDataSystemCeDirectory);
        mPersister = new TaskSnapshotPersister(persistQueue, mPersistInfoProvider);

        initialize(new TaskSnapshotCache(service, new AppSnapshotLoader(mPersistInfoProvider)));
        initialize(new TaskSnapshotCache(new AppSnapshotLoader(mPersistInfoProvider)));
        final boolean snapshotEnabled =
                !service.mContext
                        .getResources()
Loading