diff options
-rw-r--r-- | dalvik/src/main/java/dalvik/system/RuntimeHooks.java | 32 | ||||
-rw-r--r-- | dalvik/src/main/java/dalvik/system/ThreadPrioritySetter.java | 28 | ||||
-rw-r--r-- | mmodules/core_platform_api/api/platform/current-api.txt | 6 | ||||
-rw-r--r-- | non_openjdk_java_files.bp | 1 | ||||
-rw-r--r-- | ojluni/src/main/java/java/lang/Thread.java | 14 |
5 files changed, 80 insertions, 1 deletions
diff --git a/dalvik/src/main/java/dalvik/system/RuntimeHooks.java b/dalvik/src/main/java/dalvik/system/RuntimeHooks.java index 320ea28253..f1af86790c 100644 --- a/dalvik/src/main/java/dalvik/system/RuntimeHooks.java +++ b/dalvik/src/main/java/dalvik/system/RuntimeHooks.java @@ -16,10 +16,15 @@ package dalvik.system; +import dalvik.system.ThreadPrioritySetter; + import java.util.Objects; import java.util.TimeZone; import java.util.function.Supplier; +import libcore.util.NonNull; +import libcore.util.Nullable; + /** * Provides lifecycle methods and other hooks for an Android runtime "container" to call into the * runtime and core libraries during initialization. For example, from @@ -35,6 +40,10 @@ public final class RuntimeHooks { private static Supplier<String> zoneIdSupplier; + // BEGIN Android-added: Customize behavior of Thread.setPriority(). http://b/139521784 + private static volatile ThreadPrioritySetter threadPrioritySetter; + // END Android-added: Customize behavior of Thread.setPriority(). http://b/139521784 + private RuntimeHooks() { // No need to construct an instance. All methods are static. } @@ -77,4 +86,27 @@ public final class RuntimeHooks { Thread.UncaughtExceptionHandler uncaughtExceptionHandler) { Thread.setUncaughtExceptionPreHandler(uncaughtExceptionHandler); } + + // BEGIN Android-added: Customize behavior of Thread.setPriority(). http://b/139521784 + /** + * Sets a {@link ThreadPrioritySetter} that will be invoked instead of + * the default implementation during {@link Thread.setPriority(int)}. + * @hide + */ + @libcore.api.CorePlatformApi + public static void setThreadPrioritySetter(@NonNull ThreadPrioritySetter threadPrioritySetter) { + RuntimeHooks.threadPrioritySetter = Objects.requireNonNull(threadPrioritySetter); + } + + /** + * Returns the last {@code ThreadPrioritySetter} that has been + * {@code #setThreadPrioritySetter(ThreadPrioritySetter) set}, or + * null if the setter has not yet been called. + * @hide + */ + @libcore.api.CorePlatformApi + public static @Nullable ThreadPrioritySetter getThreadPrioritySetter() { + return threadPrioritySetter; + } + // END Android-added: Customize behavior of Thread.setPriority(). http://b/139521784 } diff --git a/dalvik/src/main/java/dalvik/system/ThreadPrioritySetter.java b/dalvik/src/main/java/dalvik/system/ThreadPrioritySetter.java new file mode 100644 index 0000000000..f1d5e7211a --- /dev/null +++ b/dalvik/src/main/java/dalvik/system/ThreadPrioritySetter.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dalvik.system; + +/** + * Interface for hooking to thread priority runtime setting + * @hide + */ +@libcore.api.CorePlatformApi +@FunctionalInterface +public interface ThreadPrioritySetter { + @libcore.api.CorePlatformApi + void setPriority(int priority); +} diff --git a/mmodules/core_platform_api/api/platform/current-api.txt b/mmodules/core_platform_api/api/platform/current-api.txt index 8abb9be236..cbb729a3d7 100644 --- a/mmodules/core_platform_api/api/platform/current-api.txt +++ b/mmodules/core_platform_api/api/platform/current-api.txt @@ -627,6 +627,8 @@ package dalvik.system { } public final class RuntimeHooks { + method @Nullable public static dalvik.system.ThreadPrioritySetter getThreadPrioritySetter(); + method public static void setThreadPrioritySetter(@NonNull dalvik.system.ThreadPrioritySetter); method public static void setTimeZoneIdSupplier(java.util.function.Supplier<java.lang.String>); method public static void setUncaughtExceptionPreHandler(java.lang.Thread.UncaughtExceptionHandler); } @@ -643,6 +645,10 @@ package dalvik.system { method public final void untag(java.net.DatagramSocket) throws java.net.SocketException; } + @java.lang.FunctionalInterface public interface ThreadPrioritySetter { + method public void setPriority(int); + } + public final class VMDebug { method public static void attachAgent(String, ClassLoader) throws java.io.IOException; method public static boolean cacheRegisterMap(String); diff --git a/non_openjdk_java_files.bp b/non_openjdk_java_files.bp index afc8de86f0..7b39f09226 100644 --- a/non_openjdk_java_files.bp +++ b/non_openjdk_java_files.bp @@ -49,6 +49,7 @@ filegroup { "dalvik/src/main/java/dalvik/system/RuntimeHooks.java", "dalvik/src/main/java/dalvik/system/SocketTagger.java", "dalvik/src/main/java/dalvik/system/TemporaryDirectory.java", + "dalvik/src/main/java/dalvik/system/ThreadPrioritySetter.java", "dalvik/src/main/java/dalvik/system/VMDebug.java", "dalvik/src/main/java/dalvik/system/ZygoteHooks.java", "dalvik/src/main/java/org/apache/harmony/dalvik/NativeTestTarget.java", diff --git a/ojluni/src/main/java/java/lang/Thread.java b/ojluni/src/main/java/java/lang/Thread.java index 2e2af6ae15..7b5cc4975a 100644 --- a/ojluni/src/main/java/java/lang/Thread.java +++ b/ojluni/src/main/java/java/lang/Thread.java @@ -40,6 +40,8 @@ import java.util.concurrent.ConcurrentMap; import java.util.concurrent.locks.LockSupport; import sun.nio.ch.Interruptible; import sun.reflect.CallerSensitive; +import dalvik.system.RuntimeHooks; +import dalvik.system.ThreadPrioritySetter; import dalvik.system.VMStack; import libcore.util.EmptyArray; @@ -1244,7 +1246,17 @@ class Thread implements Runnable { synchronized(this) { this.priority = newPriority; if (isAlive()) { - setPriority0(newPriority); + // BEGIN Android-added: Customize behavior of Thread.setPriority(). + // http://b/139521784 + // setPriority0(newPriority); + ThreadPrioritySetter threadPrioritySetter = + RuntimeHooks.getThreadPrioritySetter(); + if (threadPrioritySetter != null) { + threadPrioritySetter.setPriority(newPriority); + } else { + setPriority0(newPriority); + } + // END Android-added: Customize behavior of Thread.setPriority(). } } } |