Skip to content

Commit

Permalink
svm: adopt "JDK-8331189: Implementation of Scoped Values (Third Previ…
Browse files Browse the repository at this point in the history
…ew)"
  • Loading branch information
zapster committed May 30, 2024
1 parent 59a170e commit 2b2754d
Showing 1 changed file with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import java.util.concurrent.Callable;
import java.util.function.BooleanSupplier;

import com.oracle.svm.core.annotate.TargetElement;
import com.oracle.svm.core.jdk.JDK21OrEarlier;
import com.oracle.svm.core.jdk.JDK23OrLater;
import jdk.graal.compiler.serviceprovider.JavaVersionUtil;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
Expand All @@ -37,15 +40,7 @@
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.jdk.ModuleNative;

@Platforms(Platform.HOSTED_ONLY.class)
final class ScopedValuesEnabled implements BooleanSupplier {
@Override
public boolean getAsBoolean() {
return JavaVersionUtil.JAVA_SPEC >= 21 || (JavaVersionUtil.JAVA_SPEC >= 20 && ModuleNative.bootLayerContainsModule("jdk.incubator.concurrent"));
}
}

@TargetClass(className = "java.lang.ScopedValue", onlyWith = ScopedValuesEnabled.class)
@TargetClass(className = "java.lang.ScopedValue")
final class Target_java_lang_ScopedValue {
@Substitute
static Target_java_lang_ScopedValue_Snapshot scopedValueBindings() {
Expand All @@ -58,15 +53,20 @@ static Target_java_lang_ScopedValue_Snapshot scopedValueBindings() {
}
}

@TargetClass(className = "java.lang.ScopedValue", innerClass = "CallableOp", onlyWith = JDK23OrLater.class)
final class Target_java_lang_ScopedValue_CallableOp {
}

/**
* Substituted to directly call {@link Target_java_lang_Thread#setScopedValueBindings} for forced
* inlining.
*/
@TargetClass(className = "java.lang.ScopedValue", innerClass = "Carrier", onlyWith = ScopedValuesEnabled.class)
@TargetClass(className = "java.lang.ScopedValue", innerClass = "Carrier")
final class Target_java_lang_ScopedValue_Carrier {
@Alias int bitmask;

@Substitute
@TargetElement(onlyWith = JDK21OrEarlier.class)
@Uninterruptible(reason = "Ensure no safepoint actions can disrupt reverting scoped value bindings.", calleeMustBe = false)
private <R> R runWith(Target_java_lang_ScopedValue_Snapshot newSnapshot, Callable<R> op) throws Exception {
Target_java_lang_Thread.setScopedValueBindings(newSnapshot);
Expand All @@ -78,6 +78,19 @@ private <R> R runWith(Target_java_lang_ScopedValue_Snapshot newSnapshot, Callabl
}
}

@Substitute
@TargetElement(onlyWith = JDK23OrLater.class)
@Uninterruptible(reason = "Ensure no safepoint actions can disrupt reverting scoped value bindings.", calleeMustBe = false)
private <R> R runWith(Target_java_lang_ScopedValue_Snapshot newSnapshot, Target_java_lang_ScopedValue_CallableOp op) throws Exception {
Target_java_lang_Thread.setScopedValueBindings(newSnapshot);
try {
return Target_jdk_internal_vm_ScopedValueContainer.call(op);
} finally {
Target_java_lang_Thread.setScopedValueBindings(newSnapshot.prev);
Target_java_lang_ScopedValue_Cache.invalidate(bitmask);
}
}

@Substitute
@Uninterruptible(reason = "Ensure no safepoint actions can disrupt reverting scoped value bindings.", calleeMustBe = false)
private void runWith(Target_java_lang_ScopedValue_Snapshot newSnapshot, Runnable op) {
Expand All @@ -91,16 +104,21 @@ private void runWith(Target_java_lang_ScopedValue_Snapshot newSnapshot, Runnable
}
}

@TargetClass(className = "jdk.internal.vm.ScopedValueContainer", onlyWith = ScopedValuesEnabled.class)
@TargetClass(className = "jdk.internal.vm.ScopedValueContainer")
final class Target_jdk_internal_vm_ScopedValueContainer {
@Alias
@TargetElement(onlyWith = JDK21OrEarlier.class)
static native <V> V call(Callable<V> op) throws Exception;

@Alias
@TargetElement(onlyWith = JDK23OrLater.class)
static native <V> V call(Target_java_lang_ScopedValue_CallableOp op) throws Exception;

@Alias
static native void run(Runnable op);
}

@TargetClass(className = "java.lang.ScopedValue", innerClass = "Snapshot", onlyWith = ScopedValuesEnabled.class)
@TargetClass(className = "java.lang.ScopedValue", innerClass = "Snapshot")
final class Target_java_lang_ScopedValue_Snapshot {
// Checkstyle: stop
@Alias //
Expand All @@ -111,7 +129,7 @@ final class Target_java_lang_ScopedValue_Snapshot {
Target_java_lang_ScopedValue_Snapshot prev;
}

@TargetClass(className = "java.lang.ScopedValue", innerClass = "Cache", onlyWith = ScopedValuesEnabled.class)
@TargetClass(className = "java.lang.ScopedValue", innerClass = "Cache")
final class Target_java_lang_ScopedValue_Cache {
@Alias
static native void invalidate(int toClearBits);
Expand Down

0 comments on commit 2b2754d

Please sign in to comment.