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

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

Merge "Fully deprecate Process.setCanSelfBackground" into main

parents e9b670a6 3a1b4e49
Loading
Loading
Loading
Loading
+7 −11
Original line number Diff line number Diff line
@@ -1134,21 +1134,21 @@ public class Process {
            throws IllegalArgumentException, SecurityException;

    /**
     * No-op stub, kept only for app compat purposes.
     *
     * Historical description:
     * Call with 'false' to cause future calls to {@link #setThreadPriority(int)} to
     * throw an exception if passed a background-level thread priority.  This is only
     * effective if the JNI layer is built with GUARD_THREAD_PRIORITY defined to 1.
     * This does not prevent a thread from backgrounding itself via other means, such
     * as a call to Thread.setPriority() or a native setpriority() call.
     *
     * @deprecated This method does nothing.  Do not use.
     *
     * @hide
     */
    @RavenwoodRedirect
    @FastNative
    public static final native void setCanSelfBackground(boolean backgroundOk);

    @RavenwoodRedirect
    @CriticalNative
    private static native boolean getCanSelfBackground();
    @Deprecated
    public static final void setCanSelfBackground(boolean backgroundOk) {}

    /**
     * Sets the scheduling group for a thread.
@@ -1312,10 +1312,6 @@ public class Process {
            setThreadPriority(myTid(), priority);
            return;
        }
        if (priority >= THREAD_PRIORITY_BACKGROUND && !getCanSelfBackground()) {
            throw new IllegalArgumentException(
                "Priority " + priority + " blocked by setCanSelfBackground()");
        }
        boolean succ = VMRuntime.getRuntime().setThreadNiceness(Thread.currentThread(), priority);
        // VMRuntime.setThreadNiceness() just returns false for out-of-range priority.
        if (!succ) {
+4 −26
Original line number Diff line number Diff line
@@ -15,11 +15,10 @@
 */
package android.os;

import android.util.Pair;

public class Process_ravenwood {

    private static volatile ThreadLocal<Pair<Integer, Boolean>> sThreadPriority;
    private static volatile ThreadLocal<Integer> sThreadPriority;

    static {
        reset();
@@ -27,8 +26,7 @@ public class Process_ravenwood {

    public static void reset() {
        // Reset the thread local variable
        sThreadPriority = ThreadLocal.withInitial(
                () -> Pair.create(Process.THREAD_PRIORITY_DEFAULT, true));
        sThreadPriority = ThreadLocal.withInitial(() -> Process.THREAD_PRIORITY_DEFAULT);
    }

    /**
@@ -48,30 +46,10 @@ public class Process_ravenwood {
     * The real implementation uses an Android-specific API.
     */
    public static void setThreadPriority(int priority) {
        boolean backgroundOk = getCanSelfBackground();
        if (priority >= Process.THREAD_PRIORITY_BACKGROUND && !backgroundOk) {
            throw new IllegalArgumentException(
                    "Priority " + priority + " blocked by setCanSelfBackground()");
        }
        if (priority < -20 || priority > 19) {
            throw new IllegalArgumentException("Priority/niceness " + priority + " is invalid");
        }
        sThreadPriority.set(Pair.create(priority, backgroundOk));
    }

    /**
     * Called by {@link Process#setCanSelfBackground(boolean)}
     */
    public static void setCanSelfBackground(boolean backgroundOk) {
        int priority = sThreadPriority.get().first;
        sThreadPriority.set(Pair.create(priority, backgroundOk));
    }

    /**
     * Called by {@link Process#getCanSelfBackground(int)}
     */
    public static boolean getCanSelfBackground() {
        return sThreadPriority.get().second;
        sThreadPriority.set(priority);
    }

    /**
@@ -79,7 +57,7 @@ public class Process_ravenwood {
     */
    public static int getThreadPriority(int tid) {
        if (Process.myTid() == tid) {
            return sThreadPriority.get().first;
            return sThreadPriority.get();
        } else {
            throw new UnsupportedOperationException(
                    "Cross-thread priority management not yet available in Ravenwood");
+0 −3
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package com.android.server;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Process;
import android.os.StrictMode;

/**
@@ -40,8 +39,6 @@ public class ServiceThread extends HandlerThread {

    @Override
    public void run() {
        Process.setCanSelfBackground(false);

        if (!mAllowIo) {
            StrictMode.initThreadDefaults(null);
        }
+0 −53
Original line number Diff line number Diff line
@@ -64,8 +64,6 @@
#include <time.h>
#include <unistd.h>

#define GUARD_THREAD_PRIORITY 0

using namespace android;

static constexpr bool kDebugPolicy = false;
@@ -83,11 +81,6 @@ static constexpr size_t kProcReadStackBufferSize = 1024;
// this size and retry until the whole file fits.
static constexpr size_t kProcReadMinHeapBufferSize = 4096;

#if GUARD_THREAD_PRIORITY
Mutex gKeyCreateMutex;
static pthread_key_t gBgKey = -1;
#endif

// For both of these, err should be in the errno range (positive), not a status_t (negative)
static void signalExceptionForError(JNIEnv* env, int err, int tid) {
    switch (err) {
@@ -534,33 +527,6 @@ jlongArray android_os_Process_getSchedAffinity(JNIEnv* env, jobject clazz, jint
    return masks;
}

static void android_os_Process_setCanSelfBackground(JNIEnv* env, jobject clazz, jboolean bgOk) {
    // Establishes the calling thread as illegal to put into the background.
    // Typically used only for the system process's main looper.
#if GUARD_THREAD_PRIORITY
    ALOGV("Process.setCanSelfBackground(%d) : tid=%d", bgOk, gettid());
    {
        Mutex::Autolock _l(gKeyCreateMutex); // Acquired nowhere else.
        if (gBgKey == -1) {
            pthread_key_create(&gBgKey, NULL);
        }
    }

    // inverted:  not-okay, we set a sentinel value
    pthread_setspecific(gBgKey, (void*)(bgOk ? 0 : 0xbaad));
#endif
}

static jboolean android_os_Process_getCanSelfBackground(CRITICAL_JNI_PARAMS) {
#if GUARD_THREAD_PRIORITY
    void* bgOk = pthread_getspecific(gBgKey);
    if (bgOk == ((void*)0xbaad)) {
        return false;
    }
#endif
    return true;
}

jint android_os_Process_getThreadScheduler(JNIEnv* env, jclass clazz,
                                              jint tid)
{
@@ -595,21 +561,6 @@ void android_os_Process_setThreadScheduler(JNIEnv* env, jclass clazz,
}

void android_os_Process_setThreadPriorityNative(JNIEnv* env, jobject clazz, jint pid, jint pri) {
#if GUARD_THREAD_PRIORITY
    // if we're putting the current thread into the background, check the TLS
    // to make sure this thread isn't guarded.  If it is, raise an exception.
    if (pri >= ANDROID_PRIORITY_BACKGROUND) {
        if (pid == gettid()) {
            void* bgOk = pthread_getspecific(gBgKey);
            if (bgOk == ((void*)0xbaad)) {
                ALOGE("Thread marked fg-only put self in background!");
                jniThrowException(env, "java/lang/SecurityException", "May not put this thread into background");
                return;
            }
        }
    }
#endif

    int rc = androidSetThreadPriority(pid, pri);
    if (rc != 0) {
        if (rc == INVALID_OPERATION) {
@@ -1413,10 +1364,6 @@ static const JNINativeMethod methods[] = {
        {"setThreadPriorityNative", "(II)V", (void*)android_os_Process_setThreadPriorityNative},
        // @FastNative
        {"getThreadPriority", "(I)I", (void*)android_os_Process_getThreadPriority},
        // @FastNative
        {"setCanSelfBackground", "(Z)V", (void*)android_os_Process_setCanSelfBackground},
        // @CriticalNative
        {"getCanSelfBackground", "()Z", (void*)android_os_Process_getCanSelfBackground},
        {"getThreadScheduler", "(I)I", (void*)android_os_Process_getThreadScheduler},
        {"setThreadGroup", "(II)V", (void*)android_os_Process_setThreadGroup},
        {"setThreadGroupAndCpuset", "(II)V", (void*)android_os_Process_setThreadGroupAndCpuset},
+2 −3
Original line number Diff line number Diff line
@@ -45,10 +45,9 @@ public class ProcessTest {
        assertEquals(THREAD_PRIORITY_DEFAULT, Process.getThreadPriority(Process.myTid()));
        Process.setThreadPriority(THREAD_PRIORITY_FOREGROUND);
        assertEquals(THREAD_PRIORITY_FOREGROUND, Process.getThreadPriority(Process.myTid()));
        Process.setCanSelfBackground(false);
        Process.setThreadPriority(THREAD_PRIORITY_DEFAULT);
        assertEquals(THREAD_PRIORITY_DEFAULT, Process.getThreadPriority(Process.myTid()));
        assertThrows(IllegalArgumentException.class,
                () -> Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND));
        Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND);
        assertEquals(THREAD_PRIORITY_BACKGROUND, Process.getThreadPriority(Process.myTid()));
    }
}
Loading