1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package android.os; 17 18 import android.util.Pair; 19 20 public class Process_ravenwood { 21 22 private static volatile ThreadLocal<Pair<Integer, Boolean>> sThreadPriority; 23 24 static { reset()25 reset(); 26 } 27 reset()28 public static void reset() { 29 // Reset the thread local variable 30 sThreadPriority = ThreadLocal.withInitial( 31 () -> Pair.create(Process.THREAD_PRIORITY_DEFAULT, true)); 32 } 33 34 /** 35 * Called by {@link Process#setThreadPriority(int, int)} 36 */ setThreadPriority(int tid, int priority)37 public static void setThreadPriority(int tid, int priority) { 38 if (Process.myTid() == tid) { 39 boolean backgroundOk = sThreadPriority.get().second; 40 if (priority >= Process.THREAD_PRIORITY_BACKGROUND && !backgroundOk) { 41 throw new IllegalArgumentException( 42 "Priority " + priority + " blocked by setCanSelfBackground()"); 43 } 44 sThreadPriority.set(Pair.create(priority, backgroundOk)); 45 } else { 46 throw new UnsupportedOperationException( 47 "Cross-thread priority management not yet available in Ravenwood"); 48 } 49 } 50 51 /** 52 * Called by {@link Process#setCanSelfBackground(boolean)} 53 */ setCanSelfBackground(boolean backgroundOk)54 public static void setCanSelfBackground(boolean backgroundOk) { 55 int priority = sThreadPriority.get().first; 56 sThreadPriority.set(Pair.create(priority, backgroundOk)); 57 } 58 59 /** 60 * Called by {@link Process#getThreadPriority(int)} 61 */ getThreadPriority(int tid)62 public static int getThreadPriority(int tid) { 63 if (Process.myTid() == tid) { 64 return sThreadPriority.get().first; 65 } else { 66 throw new UnsupportedOperationException( 67 "Cross-thread priority management not yet available in Ravenwood"); 68 } 69 } 70 } 71