Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2201.9.0-stage] Add dummy strand for non strand cases #42635

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
package io.ballerina.runtime.internal;

import io.ballerina.runtime.api.Module;
import io.ballerina.runtime.api.creators.ErrorCreator;
import io.ballerina.runtime.api.creators.TypeCreator;
import io.ballerina.runtime.api.flags.SymbolFlags;
import io.ballerina.runtime.api.types.Field;
import io.ballerina.runtime.api.types.FunctionType;
import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.utils.TypeUtils;
Expand All @@ -34,28 +32,20 @@
import io.ballerina.runtime.api.values.BTypedesc;
import io.ballerina.runtime.api.values.BValue;
import io.ballerina.runtime.api.values.BXml;
import io.ballerina.runtime.internal.scheduling.AsyncFunctionCallback;
import io.ballerina.runtime.internal.scheduling.Scheduler;
import io.ballerina.runtime.internal.scheduling.State;
import io.ballerina.runtime.internal.scheduling.Strand;
import io.ballerina.runtime.internal.types.BRecordType;
import io.ballerina.runtime.internal.values.FutureValue;
import io.ballerina.runtime.internal.values.MapValue;
import io.ballerina.runtime.internal.values.MapValueImpl;
import io.ballerina.runtime.internal.values.TypedescValueImpl;
import io.ballerina.runtime.internal.values.ValueCreator;

import java.io.PrintStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

import static io.ballerina.runtime.api.values.BError.ERROR_PRINT_PREFIX;

/**
* Class @{@link ValueUtils} provides utils to create Ballerina Values.
Expand All @@ -64,8 +54,6 @@
*/
public class ValueUtils {

private static final PrintStream errStream = System.err;

/**
* Create a record value using the given package ID and record type name.
*
Expand Down Expand Up @@ -147,19 +135,12 @@ private static BMap<BString, Object> populateRecordDefaultValues(
BMap<BString, Object> recordValue, Map<String, BFunctionPointer<Object, ?>> defaultValues) {
Strand strand = Scheduler.getStrandNoException();
if (strand == null) {
try {
final CountDownLatch latch = new CountDownLatch(defaultValues.size());
populateInitialValuesWithNoStrand(recordValue, latch, defaultValues);
latch.await();
} catch (InterruptedException e) {
throw ErrorCreator.createError(
StringUtils.fromString("error occurred when populating default values"), e);
}
} else {
for (Map.Entry<String, BFunctionPointer<Object, ?>> field : defaultValues.entrySet()) {
recordValue.populateInitialValue(StringUtils.fromString(field.getKey()),
field.getValue().call(new Object[]{strand}));
}
// Create a dummy strand only for keep frames.
strand = new Strand();
}
for (Map.Entry<String, BFunctionPointer<Object, ?>> field : defaultValues.entrySet()) {
recordValue.populateInitialValue(StringUtils.fromString(field.getKey()),
field.getValue().call(new Object[]{strand}));
}
return recordValue;
}
Expand All @@ -184,50 +165,6 @@ private static BMap<BString, Object> populateRecordDefaultValues(
return result;
}

private static void populateInitialValuesWithNoStrand(BMap<BString, Object> recordValue, CountDownLatch latch,
Map<String, BFunctionPointer<Object, ?>> defaultValues) {
String[] fields = defaultValues.keySet().toArray(new String[0]);
int noOfIterations = defaultValues.size();
if (noOfIterations <= 0) {
return;
}
AtomicInteger callCount = new AtomicInteger(0);
scheduleNextFunction(recordValue, defaultValues, fields, "default", noOfIterations, callCount,
o -> { }, latch, Scheduler.getDaemonStrand());
}

private static void scheduleNextFunction(BMap<BString, Object> recordValue,
Map<String, BFunctionPointer<Object, ?>> defaultValues, String[] fields,
String strandName, int noOfIterations, AtomicInteger callCount,
Consumer<Object> futureResultConsumer, CountDownLatch latch,
Strand parent) {
BFunctionPointer<?, ?> func = defaultValues.get(fields[callCount.get()]);
Type retType = ((FunctionType) TypeUtils.getImpliedType(func.getType())).getReturnType();
FutureValue future = parent.scheduler.createFuture(Scheduler.getDaemonStrand(), null, null, retType,
strandName, parent.getMetadata());
AsyncFunctionCallback callback = new AsyncFunctionCallback(null) {
@Override
public void notifySuccess(Object result) {
futureResultConsumer.accept(getFutureResult());
recordValue.populateInitialValue(StringUtils.fromString(fields[callCount.get()]), result);
int i = callCount.incrementAndGet();
latch.countDown();
if (i != noOfIterations) {
scheduleNextFunction(recordValue, defaultValues, fields, strandName, noOfIterations,
callCount, futureResultConsumer, latch, parent);
}
}

@Override
public void notifyFailure(BError error) {
errStream.println(ERROR_PRINT_PREFIX + error.getPrintableStackTrace());
}
};
future.callback = callback;
callback.setFuture(future);
parent.scheduler.schedule(new Object[1], func.getFunction(), future);
}

/**
* Create a record value that populates record fields using the given package ID, record type name and a map of
* field names and associated values for the fields.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ public class Strand {
public BMap<BString, Object> workerReceiveMap = null;
public int channelCount = 0;

public Strand() {
this.id = -1;
this.strandLock = null;
this.name = null;
this.metadata = null;
this.state = RUNNABLE;
}

public Strand(String name, StrandMetadata metadata, Scheduler scheduler, Strand parent,
Map<String, Object> properties) {
this.id = nextStrandId.incrementAndGet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,11 @@ public class TypedescValueImpl implements TypedescValue {
public MapValue annotations;
private BTypedesc typedesc;

@Deprecated
public TypedescValueImpl(Type describingType) {
this.type = new BTypedescType(describingType);
this.describingType = describingType;
}

@Deprecated
public TypedescValueImpl(Type describingType, MapValue[] closures) {
this.type = new BTypedescType(describingType);
this.describingType = describingType;
Expand Down
Loading