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

Commit 8341526d authored by Makoto Onuki's avatar Makoto Onuki
Browse files

Add BackgroundThread as Executor directly.

Bug: 123774494
Test: atest AppBindingHostTest

Change-Id: I12715f4b2dd444f1d9b1a5a53ae385af497fb52d
parent dbc97088
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -17,10 +17,13 @@
package com.android.internal.os;

import android.os.Handler;
import android.os.HandlerExecutor;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Trace;

import java.util.concurrent.Executor;

/**
 * Shared singleton background thread for each process.
 */
@@ -29,6 +32,7 @@ public final class BackgroundThread extends HandlerThread {
    private static final long SLOW_DELIVERY_THRESHOLD_MS = 30_000;
    private static BackgroundThread sInstance;
    private static Handler sHandler;
    private static HandlerExecutor sHandlerExecutor;

    private BackgroundThread() {
        super("android.bg", android.os.Process.THREAD_PRIORITY_BACKGROUND);
@@ -43,6 +47,7 @@ public final class BackgroundThread extends HandlerThread {
            looper.setSlowLogThresholdMs(
                    SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS);
            sHandler = new Handler(sInstance.getLooper());
            sHandlerExecutor = new HandlerExecutor(sHandler);
        }
    }

@@ -59,4 +64,11 @@ public final class BackgroundThread extends HandlerThread {
            return sHandler;
        }
    }

    public static Executor getExecutor() {
        synchronized (BackgroundThread.class) {
            ensureThreadLocked();
            return sHandlerExecutor;
        }
    }
}
+12 −0
Original line number Diff line number Diff line
@@ -17,9 +17,12 @@
package com.android.server;

import android.os.Handler;
import android.os.HandlerExecutor;
import android.os.Looper;
import android.os.Trace;

import java.util.concurrent.Executor;

/**
 * Shared singleton foreground thread for the system.  This is a thread for regular
 * foreground service operations, which shouldn't be blocked by anything running in
@@ -34,6 +37,7 @@ public final class FgThread extends ServiceThread {

    private static FgThread sInstance;
    private static Handler sHandler;
    private static HandlerExecutor sHandlerExecutor;

    private FgThread() {
        super("android.fg", android.os.Process.THREAD_PRIORITY_DEFAULT, true /*allowIo*/);
@@ -48,6 +52,7 @@ public final class FgThread extends ServiceThread {
            looper.setSlowLogThresholdMs(
                    SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS);
            sHandler = new Handler(sInstance.getLooper());
            sHandlerExecutor = new HandlerExecutor(sHandler);
        }
    }

@@ -64,4 +69,11 @@ public final class FgThread extends ServiceThread {
            return sHandler;
        }
    }

    public static Executor getExecutor() {
        synchronized (FgThread.class) {
            ensureThreadLocked();
            return sHandlerExecutor;
        }
    }
}
+12 −0
Original line number Diff line number Diff line
@@ -17,8 +17,11 @@
package com.android.server;

import android.os.Handler;
import android.os.HandlerExecutor;
import android.os.Trace;

import java.util.concurrent.Executor;

/**
 * Shared singleton I/O thread for the system.  This is a thread for non-background
 * service operations that can potential block briefly on network IO operations
@@ -27,6 +30,7 @@ import android.os.Trace;
public final class IoThread extends ServiceThread {
    private static IoThread sInstance;
    private static Handler sHandler;
    private static HandlerExecutor sHandlerExecutor;

    private IoThread() {
        super("android.io", android.os.Process.THREAD_PRIORITY_DEFAULT, true /*allowIo*/);
@@ -38,6 +42,7 @@ public final class IoThread extends ServiceThread {
            sInstance.start();
            sInstance.getLooper().setTraceTag(Trace.TRACE_TAG_SYSTEM_SERVER);
            sHandler = new Handler(sInstance.getLooper());
            sHandlerExecutor = new HandlerExecutor(sHandler);
        }
    }

@@ -54,4 +59,11 @@ public final class IoThread extends ServiceThread {
            return sHandler;
        }
    }

    public static Executor getExecutor() {
        synchronized (IoThread.class) {
            ensureThreadLocked();
            return sHandlerExecutor;
        }
    }
}
+2 −4
Original line number Diff line number Diff line
@@ -99,7 +99,7 @@ public class CarrierMessagingClientServiceFinder
    @Override
    public void startMonitoring() {
        mRoleManager.addOnRoleHoldersChangedListenerAsUser(
                mContext.getMainExecutor(), mRoleHolderChangedListener, UserHandle.ALL);
                BackgroundThread.getExecutor(), mRoleHolderChangedListener, UserHandle.ALL);
    }

    @Override
@@ -120,9 +120,7 @@ public class CarrierMessagingClientServiceFinder

    private final OnRoleHoldersChangedListener mRoleHolderChangedListener = (role, user) -> {
        if (RoleManager.ROLE_SMS.equals(role)) {
            BackgroundThread.getHandler().post(() -> {
            mListener.accept(CarrierMessagingClientServiceFinder.this, user.getIdentifier());
            });
        }
    };
}