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

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

Merge "Clean up the Closeable functionality in ExpiredTimer" into main

parents e26e285e 23be877c
Loading
Loading
Loading
Loading
+4 −20
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ public class TimeoutRecord {
    public final AnrLatencyTracker mLatencyTracker;

    /** A handle to the timer that expired.  A value of null means "no timer". */
    private AutoCloseable mExpiredTimer;
    private Object mExpiredTimer;

    private static final String TAG = "TimeoutRecord";

@@ -209,13 +209,10 @@ public class TimeoutRecord {
    }

    /**
     * Record the timer that expired. The argument is an opaque handle. If an expired timer had
     * already been set, close it now.
     * Record the timer that expired. The argument is an opaque handle.
     */
    @NonNull
    public TimeoutRecord setExpiredTimer(@Nullable AutoCloseable handle) {
        // Close the current value of mExpiredTimer, if it not null.
        closeExpiredTimer();
    public TimeoutRecord setExpiredTimer(@Nullable Object handle) {
        mExpiredTimer = handle;
        return this;
    }
@@ -224,23 +221,10 @@ public class TimeoutRecord {
     * Return the expired timer.
     */
    @Nullable
    public AutoCloseable getExpiredTimer() {
    public Object getExpiredTimer() {
        return mExpiredTimer;
    }

    /** Close the ExpiredTimer, if one is present. getExpiredTimer will return null after this. */
    public void closeExpiredTimer() {
        try {
            if (mExpiredTimer != null) {
                mExpiredTimer.close();
                mExpiredTimer = null;
            }
        } catch (Exception e) {
            // mExpiredTimer.close() should never, ever throw.  If it does, just rethrow as a
            // RuntimeException.
            throw new RuntimeException(e);
        }
    }

    /**
     * Maps a {@link TimeoutRecord.TimeoutKind} to its corresponding
+0 −3
Original line number Diff line number Diff line
@@ -303,9 +303,6 @@ class ProcessErrorStateRecord {
        SparseBooleanArray lastPids = new SparseBooleanArray(20);
        ActivityManagerService.VolatileDropboxEntryStates volatileDropboxEntriyStates = null;

        // Release the expired timer preparatory to starting the dump or returning without dumping.
        timeoutRecord.closeExpiredTimer();

        if (mApp.isDebugging()) {
            Slog.i(TAG, "Skipping debugged app ANR: " + this + " " + annotation);
            return;
+5 −42
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ import android.os.SystemClock;
import android.os.Trace;
import android.text.format.TimeMigrationUtils;
import android.util.ArrayMap;
import android.util.CloseGuard;
import android.util.IndentingPrintWriter;
import android.util.Log;
import android.util.LongSparseArray;
@@ -299,48 +298,14 @@ public class AnrTimer<V> implements AutoCloseable {
    }

    /**
     * A target process may be modified when its timer expires.  The modification (if any) will be
     * undone if the expiration is discarded, but is persisted if the expiration is accepted.  If
     * the expiration is accepted, then a ExpiredTimer is returned to the client.  The client must
     * close the ExpiredTimer to complete the state machine.
     * Information about a timer that has expired.
     */
    public static class ExpiredTimer implements AutoCloseable {
        // Detect failures to close.
        private final CloseGuard mGuard = new CloseGuard();

        // A lock to ensure closing is thread-safe.
        private final Object mLock = new Object();

        // Allow multiple calls to close().
        private boolean mClosed = false;

    public static class ExpiredTimer {
        // The timer ID.
        final int mTimerId;

        ExpiredTimer(int id) {
            mTimerId = id;
            mGuard.open("AnrTimer.release");
        }

        @Override
        public void close() {
            synchronized (mLock) {
                if (!mClosed) {
                    mGuard.close();
                    mClosed = true;
                }
            }
        }

        @Override
        protected void finalize() throws Throwable {
            try {
                // Note that guard could be null if the constructor threw.
                if (mGuard != null) mGuard.warnIfOpen();
                close();
            } finally {
                super.finalize();
            }
        }
    }

@@ -713,7 +678,6 @@ public class AnrTimer<V> implements AutoCloseable {
                // timer.  Wrap the timer ID in a ExpiredTimer and return it to the caller.  If
                // "accepted" is false then the native later does not have any pending operations.
                if (!accepted) {
                    timer.close();
                    timer = null;
                }
                return timer;
@@ -798,15 +762,14 @@ public class AnrTimer<V> implements AutoCloseable {

        /**
         * Delete the entries associated with arg from the maps and return the ID of the timer, if
         * any.  If there is a ExpiredTimer present, it is closed.
         * any.
         */
        @GuardedBy("mLock")
        private Integer removeLocked(V arg) {
            final Integer r = mTimerIdMap.remove(arg);
            if (r != null) {
                mTimerArgMap.remove(r);
                ExpiredTimer l = mExpiredTimers.removeReturnOld(r);
                if (l != null) l.close();
                mExpiredTimers.removeReturnOld(r);
            }
            return r;
        }
@@ -999,7 +962,7 @@ public class AnrTimer<V> implements AutoCloseable {
     */
    @Nullable
    public static ExpiredTimer expiredTimer(TimeoutRecord tr) {
        AutoCloseable expiredTimer = tr.getExpiredTimer();
        Object expiredTimer = tr.getExpiredTimer();
        if (expiredTimer instanceof ExpiredTimer lock) {
            return lock;
        } else {
+0 −3
Original line number Diff line number Diff line
@@ -564,9 +564,6 @@ public class AnrTimerTest {
            TimeoutRecord tr2a = TimeoutRecord.forApp("testing purposes");
            timer.accept(t2, tr2a);
            assertThat(tr2a.getExpiredTimer()).isNotNull();
            assertThat(tr2a.getExpiredTimer()).isNotNull();
            tr2a.closeExpiredTimer();
            assertThat(tr2a.getExpiredTimer()).isNull();
            assertThat(timer.discard(t2)).isFalse();
            TimeoutRecord tr2b = TimeoutRecord.forApp("testing purposes");
            timer.accept(t2, tr2b);