|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package com.oracle.svm.core.thread; |
| 26 | + |
| 27 | +import java.util.concurrent.Callable; |
| 28 | +import java.util.function.BooleanSupplier; |
| 29 | + |
| 30 | +import org.graalvm.compiler.serviceprovider.JavaVersionUtil; |
| 31 | +import org.graalvm.nativeimage.Platform; |
| 32 | +import org.graalvm.nativeimage.Platforms; |
| 33 | + |
| 34 | +import com.oracle.svm.core.annotate.Alias; |
| 35 | +import com.oracle.svm.core.annotate.Substitute; |
| 36 | +import com.oracle.svm.core.annotate.TargetClass; |
| 37 | +import com.oracle.svm.core.jdk.ModuleUtil; |
| 38 | + |
| 39 | +@Platforms(Platform.HOSTED_ONLY.class) |
| 40 | +final class IncubatorConcurrentModule implements BooleanSupplier { |
| 41 | + @Override |
| 42 | + public boolean getAsBoolean() { |
| 43 | + return JavaVersionUtil.JAVA_SPEC >= 20 && ModuleUtil.bootLayerContainsModule("jdk.incubator.concurrent"); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Substituted to directly call {@link Target_java_lang_Thread#setScopedValueBindings} for forced |
| 49 | + * inlining. |
| 50 | + */ |
| 51 | +@TargetClass(className = "jdk.incubator.concurrent.ScopedValue", innerClass = "Carrier", onlyWith = IncubatorConcurrentModule.class) |
| 52 | +final class Target_jdk_incubator_concurrent_ScopedValue_Carrier { |
| 53 | + @Alias int bitmask; |
| 54 | + |
| 55 | + @Substitute |
| 56 | + private <R> R runWith(Target_jdk_incubator_concurrent_ScopedValue_Snapshot newSnapshot, Callable<R> op) throws Exception { |
| 57 | + Target_java_lang_Thread.setScopedValueBindings(newSnapshot); |
| 58 | + try { |
| 59 | + return Target_jdk_internal_vm_ScopedValueContainer.call(op); |
| 60 | + } finally { |
| 61 | + Target_java_lang_Thread.setScopedValueBindings(newSnapshot.prev); |
| 62 | + Target_jdk_incubator_concurrent_ScopedValue_Cache.invalidate(bitmask); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Substitute |
| 67 | + private void runWith(Target_jdk_incubator_concurrent_ScopedValue_Snapshot newSnapshot, Runnable op) { |
| 68 | + Target_java_lang_Thread.setScopedValueBindings(newSnapshot); |
| 69 | + try { |
| 70 | + Target_jdk_internal_vm_ScopedValueContainer.run(op); |
| 71 | + } finally { |
| 72 | + Target_java_lang_Thread.setScopedValueBindings(newSnapshot.prev); |
| 73 | + Target_jdk_incubator_concurrent_ScopedValue_Cache.invalidate(bitmask); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +@TargetClass(className = "jdk.internal.vm.ScopedValueContainer", onlyWith = IncubatorConcurrentModule.class) |
| 79 | +final class Target_jdk_internal_vm_ScopedValueContainer { |
| 80 | + @Alias |
| 81 | + static native <V> V call(Callable<V> op) throws Exception; |
| 82 | + |
| 83 | + @Alias |
| 84 | + static native void run(Runnable op); |
| 85 | +} |
| 86 | + |
| 87 | +@TargetClass(className = "jdk.incubator.concurrent.ScopedValue", innerClass = "Snapshot", onlyWith = IncubatorConcurrentModule.class) |
| 88 | +final class Target_jdk_incubator_concurrent_ScopedValue_Snapshot { |
| 89 | + @Alias // |
| 90 | + Target_jdk_incubator_concurrent_ScopedValue_Snapshot prev; |
| 91 | +} |
| 92 | + |
| 93 | +@TargetClass(className = "jdk.incubator.concurrent.ScopedValue", innerClass = "Cache", onlyWith = IncubatorConcurrentModule.class) |
| 94 | +final class Target_jdk_incubator_concurrent_ScopedValue_Cache { |
| 95 | + @Alias |
| 96 | + static native void invalidate(int toClearBits); |
| 97 | +} |
0 commit comments