Skip to content

Commit

Permalink
Merge pull request #43424 from warunalakshitha/java21_jbal_tests
Browse files Browse the repository at this point in the history
Remove yield codegen before terminators
  • Loading branch information
warunalakshitha authored Sep 26, 2024
2 parents a78c679 + 617a5e7 commit 8a5a434
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public Parameter[] getFunctionPathParameters() {

/**
* Mark the current executing strand as async. Execution of Ballerina code after the current
* interop will stop until given BalFuture is completed. However the java thread will not be blocked
* and will be reused for running other Ballerina code in the meantime. Therefore callee of this method
* interop will stop until given BalFuture is completed. However, the java thread will not be blocked
* and will be reused for running other Ballerina code in the meantime. Therefore, callee of this method
* must return as soon as possible to avoid starvation of ballerina code execution.WD
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ public BLockStore() {
*/
@SuppressWarnings("unused")
public void lock(Strand strand, String lockName) {
strand.yield();
getLockFromMap(lockName).lock();
strand.acquiredLockCount++;
strand.resume();
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
import org.wso2.ballerinalang.compiler.bir.codegen.internal.NameHashComparator;
import org.wso2.ballerinalang.compiler.bir.codegen.internal.ScheduleFunctionInfo;
import org.wso2.ballerinalang.compiler.bir.codegen.interop.InteropMethodGen;
import org.wso2.ballerinalang.compiler.bir.codegen.model.JTermKind;
import org.wso2.ballerinalang.compiler.bir.codegen.model.JTerminator;
import org.wso2.ballerinalang.compiler.bir.codegen.model.JType;
import org.wso2.ballerinalang.compiler.bir.codegen.model.JTypeTags;
import org.wso2.ballerinalang.compiler.bir.codegen.split.JvmConstantsGen;
Expand Down Expand Up @@ -503,32 +501,6 @@ public static BirScope getLastScopeFromTerminator(MethodVisitor mv, BIRNode.BIRB
return lastScope;
}

public static void genStrandAction(MethodVisitor mv, BIRTerminator terminator, int localVarOffset,
String strandAction) {
switch (terminator.kind) {
case WK_RECEIVE, WK_ALT_RECEIVE, WK_MULTIPLE_RECEIVE, FLUSH, WAIT, WAIT_ALL:
genStrandAction(mv, localVarOffset, strandAction);
break;
case WK_SEND:
if (((BIRTerminator.WorkerSend) terminator).isSync) {
genStrandAction(mv, localVarOffset, strandAction);
}
break;
case PLATFORM:
if (terminator instanceof JTerminator jTerminator &&
jTerminator.jTermKind == JTermKind.JI_METHOD_CALL) {
genStrandAction(mv, localVarOffset, strandAction);
}
break;
default:
}
}

private static void genStrandAction(MethodVisitor mv, int localVarOffset, String strandAction) {
mv.visitVarInsn(ALOAD, localVarOffset);
mv.visitMethodInsn(INVOKEVIRTUAL, STRAND_CLASS, strandAction, VOID_METHOD_DESC, false);
}

public static void genGotoThenBB(MethodVisitor mv, BIRNode.BIRBasicBlock thenBB, LabelGenerator labelGen,
BIRTerminator terminator, String funcName) {
if (thenBB != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ private void genJICallTerm(JIMethodCall callIns, int localVarOffset, BIRNode.BIR
if (callIns.lhsOp != null && callIns.lhsOp.variableDcl != null) {
this.storeToVar(callIns.lhsOp.variableDcl);
}
mv.visitVarInsn(ALOAD, localVarOffset);
mv.visitMethodInsn(INVOKEVIRTUAL, STRAND_CLASS, "resume", VOID_METHOD_DESC, false);
}

private void genJIConstructorTerm(JIConstructorCall callIns) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,15 +535,13 @@ void generateBasicBlocks(MethodVisitor mv, LabelGenerator labelGen, JvmErrorGen
mv.visitLabel(bbEndLabel);
BIRTerminator terminator = bb.terminator;
processTerminator(mv, func, module, funcName, terminator);
JvmCodeGenUtil.genStrandAction(mv, terminator, localVarOffset, "yield");
termGen.genTerminator(terminator, func, funcName, localVarOffset, returnVarRefIndex, attachedType,
channelMapVarIndex, sendWorkerChannelNamesVar, receiveWorkerChannelNamesVar);
lastScope = JvmCodeGenUtil.getLastScopeFromTerminator(mv, bb, funcName, labelGen, lastScope,
visitedScopesSet);
errorGen.generateTryCatch(func, funcName, bb, termGen, labelGen, channelMapVarIndex,
sendWorkerChannelNamesVar, receiveWorkerChannelNamesVar, localVarOffset);
BIRBasicBlock thenBB = terminator.thenBB;
JvmCodeGenUtil.genStrandAction(mv, terminator, localVarOffset, "resume");
JvmCodeGenUtil.genGotoThenBB(mv, thenBB, labelGen, terminator, funcName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.ballerinalang.langlib.runtime;

import io.ballerina.runtime.api.Environment;
import io.ballerina.runtime.api.creators.ErrorCreator;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.values.BDecimal;
Expand All @@ -33,7 +34,7 @@ public class Sleep {

private static final BigDecimal LONG_MAX = new BigDecimal(Long.MAX_VALUE);

public static void sleep(BDecimal delaySeconds) {
public static void sleep(Environment env, BDecimal delaySeconds) {
BigDecimal delayDecimal = delaySeconds.decimalValue();
if (delayDecimal.compareTo(BigDecimal.ZERO) <= 0) {
return;
Expand All @@ -45,6 +46,7 @@ public static void sleep(BDecimal delaySeconds) {
} else {
delay = delayDecimal.longValue();
}
env.markAsync();
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class Utils {

public static void sleep(Environment env, long delayMillis) {
try {
env.markAsync();
Thread.sleep(delayMillis);
} catch (InterruptedException e) {
throw ErrorCreator.createError(e);
Expand Down

0 comments on commit 8a5a434

Please sign in to comment.