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

Commit 1c110302 authored by Andrii Kulian's avatar Andrii Kulian
Browse files

Fix object pool for lifecycler

The original implementation of object pool for lifecycle
transactions tried to always recycle objects after a
transaction was scheduled. In case when a client was running
in the same process this lead to objects being emptied before
it could actually perform the transaction.
Also when checking if object was already in the pool we should
use "==" instead of equality check.

Bug: 70568084
Bug: 70526039
Bug: 70616950
Test: android.app.servertransaction.ObjectPoolTests
Change-Id: I45eeecc189b9a458d8efdfed256b81cf0baf8b95
parent 93592a9b
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -1650,7 +1650,9 @@ public final class ActivityThread extends ClientTransactionHandler {
                            (String[]) ((SomeArgs) msg.obj).arg2);
                    break;
                case EXECUTE_TRANSACTION:
                    mTransactionExecutor.execute(((ClientTransaction) msg.obj));
                    final ClientTransaction transaction = (ClientTransaction) msg.obj;
                    mTransactionExecutor.execute(transaction);
                    transaction.recycle();
                    break;
            }
            Object obj = msg.obj;
+5 −0
Original line number Diff line number Diff line
@@ -54,6 +54,11 @@ public class ClientTransaction implements Parcelable, ObjectPoolItem {
    /** Target client activity. Might be null if the entire transaction is targeting an app. */
    private IBinder mActivityToken;

    /** Get the target client of the transaction. */
    public IApplicationThread getClient() {
        return mClient;
    }

    /**
     * Add a message to the end of the sequence of callbacks.
     * @param activityCallback A single message that can contain a lifecycle request/callback.
+13 −9
Original line number Diff line number Diff line
@@ -16,8 +16,8 @@

package android.app.servertransaction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
@@ -27,7 +27,7 @@ import java.util.Map;
class ObjectPool {

    private static final Object sPoolSync = new Object();
    private static final Map<Class, LinkedList<? extends ObjectPoolItem>> sPoolMap =
    private static final Map<Class, ArrayList<? extends ObjectPoolItem>> sPoolMap =
            new HashMap<>();

    private static final int MAX_POOL_SIZE = 50;
@@ -40,9 +40,9 @@ class ObjectPool {
    public static <T extends ObjectPoolItem> T obtain(Class<T> itemClass) {
        synchronized (sPoolSync) {
            @SuppressWarnings("unchecked")
            LinkedList<T> itemPool = (LinkedList<T>) sPoolMap.get(itemClass);
            final ArrayList<T> itemPool = (ArrayList<T>) sPoolMap.get(itemClass);
            if (itemPool != null && !itemPool.isEmpty()) {
                return itemPool.poll();
                return itemPool.remove(itemPool.size() - 1);
            }
            return null;
        }
@@ -56,16 +56,20 @@ class ObjectPool {
    public static <T extends ObjectPoolItem> void recycle(T item) {
        synchronized (sPoolSync) {
            @SuppressWarnings("unchecked")
            LinkedList<T> itemPool = (LinkedList<T>) sPoolMap.get(item.getClass());
            ArrayList<T> itemPool = (ArrayList<T>) sPoolMap.get(item.getClass());
            if (itemPool == null) {
                itemPool = new LinkedList<>();
                itemPool = new ArrayList<>();
                sPoolMap.put(item.getClass(), itemPool);
            }
            if (itemPool.contains(item)) {
            // Check if the item is already in the pool
            final int size = itemPool.size();
            for (int i = 0; i < size; i++) {
                if (itemPool.get(i) == item) {
                    throw new IllegalStateException("Trying to recycle already recycled item");
                }
            }

            if (itemPool.size() < MAX_POOL_SIZE) {
            if (size < MAX_POOL_SIZE) {
                itemPool.add(item);
            }
        }
+1 −2
Original line number Diff line number Diff line
@@ -42,8 +42,7 @@ import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
@SmallTest
// TODO: b/70616950
//@Presubmit
@Presubmit
public class ObjectPoolTests {

    // 1. Check if two obtained objects from pool are not the same.
+7 −2
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.app.IApplicationThread;
import android.app.servertransaction.ClientTransaction;
import android.app.servertransaction.ClientTransactionItem;
import android.app.servertransaction.ActivityLifecycleItem;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;

@@ -43,8 +44,12 @@ class ClientLifecycleManager {
     */
    void scheduleTransaction(ClientTransaction transaction) throws RemoteException {
        transaction.schedule();
        // TODO: b/70616950
        //transaction.recycle();
        if (!(transaction.getClient() instanceof Binder)) {
            // If client is not an instance of Binder - it's a remote call and at this point it is
            // safe to recycle the object. All objects used for local calls will be recycled after
            // the transaction is executed on client in ActivityThread.
            transaction.recycle();
        }
    }

    /**