From 5f70d4b276ae29be82e2e5942750ee1a6ad69219 Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Thu, 25 Apr 2024 15:14:09 +0530 Subject: [PATCH] Add dummy strand for non strand cases --- .../runtime/internal/ValueUtils.java | 75 ++----------------- .../runtime/internal/scheduling/Strand.java | 8 ++ .../internal/values/TypedescValueImpl.java | 2 - 3 files changed, 14 insertions(+), 71 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/ValueUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/ValueUtils.java index e51259bca0ca..0b35b4a9d979 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/ValueUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/ValueUtils.java @@ -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; @@ -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. @@ -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. * @@ -147,19 +135,12 @@ private static BMap populateRecordDefaultValues( BMap recordValue, Map> 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> 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> field : defaultValues.entrySet()) { + recordValue.populateInitialValue(StringUtils.fromString(field.getKey()), + field.getValue().call(new Object[]{strand})); } return recordValue; } @@ -184,50 +165,6 @@ private static BMap populateRecordDefaultValues( return result; } - private static void populateInitialValuesWithNoStrand(BMap recordValue, CountDownLatch latch, - Map> 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 recordValue, - Map> defaultValues, String[] fields, - String strandName, int noOfIterations, AtomicInteger callCount, - Consumer 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. diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/scheduling/Strand.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/scheduling/Strand.java index e421bbf9d0b2..df347a897fef 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/scheduling/Strand.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/scheduling/Strand.java @@ -94,6 +94,14 @@ public class Strand { public BMap 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 properties) { this.id = nextStrandId.incrementAndGet(); diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java index 6d014bcf16ed..398f3fe0deae 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java @@ -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;