Skip to content

Commit

Permalink
Adapt JDK-8338383: Implementation of Synchronize Virtual Threads with…
Browse files Browse the repository at this point in the history
…out Pinning
  • Loading branch information
mur47x111 committed Nov 4, 2024
1 parent 2e51fa1 commit 36240f8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ public final int arrayOopDescLengthOffset() {
public final int threadCarrierThreadObjectOffset = getFieldOffset("JavaThread::_threadObj", Integer.class, "OopHandle");
public final int threadScopedValueCacheOffset = getFieldOffset("JavaThread::_scopedValueCache", Integer.class, "OopHandle");

public final int javaThreadLockIDOffset = getFieldOffset("JavaThread::_lock_id", Integer.class, "int64_t", -1, JDK >= 24);

public final int threadIsInVTMSTransitionOffset = getFieldOffset("JavaThread::_is_in_VTMS_transition", Integer.class, "bool");
public final int threadIsInTmpVTMSTransitionOffset = getFieldOffset("JavaThread::_is_in_tmp_VTMS_transition", Integer.class, "bool", -1, JDK == 21);
public final int threadIsDisableSuspendOffset = getFieldOffset("JavaThread::_is_disable_suspend", Integer.class, "bool", -1, JDK >= 22);
Expand Down Expand Up @@ -381,13 +383,16 @@ public int threadLastJavaFpOffset() {
public final int lockMaskInPlace = getConstant("markWord::lock_mask_in_place", Integer.class);
public final int unlockedValue = getConstant("markWord::unlocked_value", Integer.class);
public final int monitorValue = getConstant("markWord::monitor_value", Integer.class);
public final int ageMaskInPlace = getConstant("markWord::age_mask_in_place", Integer.class);
public final int unusedMark = getConstant("markWord::marked_value", Integer.class);

// This field has no type in vmStructs.cpp
public final int objectMonitorOwner = getFieldOffset("ObjectMonitor::_owner", Integer.class, null);
public final int objectMonitorOwner = getFieldOffset("ObjectMonitor::_owner", Integer.class, JDK >= 24 ? "int64_t" : null);
public final int objectMonitorRecursions = getFieldOffset("ObjectMonitor::_recursions", Integer.class, "intptr_t");
public final int objectMonitorCxq = getFieldOffset("ObjectMonitor::_cxq", Integer.class, "ObjectWaiter*");
public final int objectMonitorEntryList = getFieldOffset("ObjectMonitor::_EntryList", Integer.class, "ObjectWaiter*");
public final int objectMonitorSucc = getFieldOffset("ObjectMonitor::_succ", Integer.class, "JavaThread*");
public final int objectMonitorSucc = getFieldOffset("ObjectMonitor::_succ", Integer.class, JDK >= 24 ? "int64_t" : "JavaThread*");
public final int objectMonitorStackLocker = getFieldOffset("ObjectMonitor::_stack_locker", Integer.class, "BasicLock*");

public final int markWordNoHashInPlace = getConstant("markWord::no_hash_in_place", Integer.class);
public final int markWordNoLockInPlace = getConstant("markWord::no_lock_in_place", Integer.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import static jdk.graal.compiler.hotspot.replacements.HotSpotReplacementsUtil.HOTSPOT_CURRENT_THREAD_OOP_HANDLE_LOCATION;
import static jdk.graal.compiler.hotspot.replacements.HotSpotReplacementsUtil.HOTSPOT_JAVA_THREAD_CONT_ENTRY;
import static jdk.graal.compiler.hotspot.replacements.HotSpotReplacementsUtil.HOTSPOT_JAVA_THREAD_SCOPED_VALUE_CACHE_HANDLE_LOCATION;
import static jdk.graal.compiler.hotspot.replacements.HotSpotReplacementsUtil.JAVA_THREAD_LOCK_ID_LOCATION;
import static jdk.graal.compiler.java.BytecodeParserOptions.InlineDuringParsing;
import static jdk.graal.compiler.nodes.ConstantNode.forBoolean;
import static jdk.graal.compiler.nodes.ProfileData.BranchProbabilityData.injected;
Expand Down Expand Up @@ -728,6 +729,13 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
b.add(new WriteNode(handleAddress, HOTSPOT_CURRENT_THREAD_OOP_HANDLE_LOCATION, thread,
barrierSet.writeBarrierType(HOTSPOT_CURRENT_THREAD_OOP_HANDLE_LOCATION), MemoryOrderMode.PLAIN));

if (JavaVersionUtil.JAVA_SPEC >= 24) {
GraalError.guarantee(config.javaThreadLockIDOffset != -1, "JavaThread::_lock_id should have been exported");
// Change the lock_id of the JavaThread
ValueNode tid = helper.loadField(thread, helper.getField(b.getMetaAccess().lookupJavaType(Thread.class), "tid"));
OffsetAddressNode address = b.add(new OffsetAddressNode(javaThread, helper.asWord(config.javaThreadLockIDOffset)));
b.add(new JavaWriteNode(JavaKind.Long, address, JAVA_THREAD_LOCK_ID_LOCATION, tid, BarrierType.NONE, false));
}
if (HotSpotReplacementsUtil.supportsVirtualThreadUpdateJFR(config)) {
b.add(new VirtualThreadUpdateJFRNode(thread));
}
Expand Down Expand Up @@ -955,6 +963,21 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
}
});
}

if (JavaVersionUtil.JAVA_SPEC >= 24) {
r.registerConditional(config.javaThreadLockIDOffset != -1, new InvocationPlugin("setLockId", Receiver.class, long.class) {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode tid) {
try (HotSpotInvocationPluginHelper helper = new HotSpotInvocationPluginHelper(b, targetMethod, config)) {
receiver.get(true);
CurrentJavaThreadNode javaThread = b.add(new CurrentJavaThreadNode(helper.getWordKind()));
OffsetAddressNode address = b.add(new OffsetAddressNode(javaThread, helper.asWord(config.javaThreadLockIDOffset)));
b.add(new JavaWriteNode(JavaKind.Long, address, JAVA_THREAD_LOCK_ID_LOCATION, tid, BarrierType.NONE, false));
}
return true;
}
});
}
}

private static ResolvedJavaType resolveTypeAESCrypt(ResolvedJavaType context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ public static Object getPendingException(Word thread) {

public static final LocationIdentity JAVA_THREAD_CARRIER_THREAD_OBJECT_LOCATION = NamedLocationIdentity.mutable("JavaThread::_threadObj");

public static final LocationIdentity JAVA_THREAD_LOCK_ID_LOCATION = NamedLocationIdentity.mutable("JavaThread::_lock_id");

public static final LocationIdentity JAVA_THREAD_OSTHREAD_LOCATION = NamedLocationIdentity.mutable("JavaThread::_osthread");

public static final LocationIdentity JAVA_THREAD_HOLD_MONITOR_COUNT_LOCATION = NamedLocationIdentity.mutable("JavaThread::_held_monitor_count");
Expand Down Expand Up @@ -504,6 +506,11 @@ public static int monitorValue(@InjectedParameter GraalHotSpotVMConfig config) {
return config.monitorValue;
}

@Fold
public static int unusedMark(@InjectedParameter GraalHotSpotVMConfig config) {
return config.unusedMark;
}

@Fold
public static int objectMonitorOwnerOffset(@InjectedParameter GraalHotSpotVMConfig config) {
return config.objectMonitorOwner;
Expand All @@ -529,6 +536,11 @@ public static int objectMonitorSuccOffset(@InjectedParameter GraalHotSpotVMConfi
return config.objectMonitorSucc;
}

@Fold
public static int objectMonitorStackLockerOffset(@InjectedParameter GraalHotSpotVMConfig config) {
return config.objectMonitorStackLocker;
}

/**
* Mask for a biasable, locked or unlocked mark word. It is the least significant 3 bits prior
* to Java 18 (1 bit for biased locking and 2 bits for stack locking or heavy locking), and 2
Expand Down Expand Up @@ -679,6 +691,8 @@ public static int secondarySupersOffset(@InjectedParameter GraalHotSpotVMConfig

public static final LocationIdentity OBJECT_MONITOR_SUCC_LOCATION = NamedLocationIdentity.mutable("ObjectMonitor::_succ");

public static final LocationIdentity OBJECT_MONITOR_STACK_LOCKER_LOCATION = NamedLocationIdentity.mutable("ObjectMonitor::_stack_locker");

@Fold
public static int lockMetadataOffset(@InjectedParameter GraalHotSpotVMConfig config) {
return config.basicLockMetadataOffset;
Expand All @@ -689,6 +703,11 @@ static int heldMonitorCountOffset(@InjectedParameter GraalHotSpotVMConfig config
return config.threadHeldMonitorCountOffset;
}

@Fold
static int javaThreadLockIDOffset(@InjectedParameter GraalHotSpotVMConfig config) {
return config.javaThreadLockIDOffset;
}

@Fold
static int javaThreadLockStackTopOffset(@InjectedParameter GraalHotSpotVMConfig config) {
return config.threadLockStackOffset + config.lockStackTopOffset;
Expand Down
Loading

0 comments on commit 36240f8

Please sign in to comment.