Search in sources :

Example 1 with WorkerSet

use of org.ballerinalang.util.codegen.CallableUnitInfo.WorkerSet in project ballerina by ballerina-lang.

the class BLangFunctions method invokeNonNativeCallable.

public static WorkerExecutionContext invokeNonNativeCallable(CallableUnitInfo callableUnitInfo, WorkerExecutionContext parentCtx, int[] argRegs, int[] retRegs, boolean waitForResponse, int flags) {
    WorkerSet workerSet = callableUnitInfo.getWorkerSet();
    int generalWorkersCount = workerSet.generalWorkers.length;
    CallableWorkerResponseContext respCtx = createWorkerResponseContext(callableUnitInfo.getRetParamTypes(), generalWorkersCount);
    WaitForResponseCallback respCallback = null;
    if (waitForResponse) {
        respCallback = new WaitForResponseCallback();
        respCtx.registerResponseCallback(respCallback);
    }
    if (TraceManagerWrapper.getInstance().isTraceEnabled() && FunctionFlags.isObserved(flags)) {
        respCtx.registerResponseCallback(new TraceableCallback(parentCtx));
    }
    respCtx.joinTargetContextInfo(parentCtx, retRegs);
    WorkerDataIndex wdi = callableUnitInfo.retWorkerIndex;
    /* execute the init worker and extract the local variables created by it */
    WorkerData initWorkerLocalData = null;
    CodeAttributeInfo initWorkerCAI = null;
    if (workerSet.initWorker != null) {
        initWorkerLocalData = executeInitWorker(parentCtx, argRegs, callableUnitInfo, workerSet.initWorker, wdi);
        if (initWorkerLocalData == null) {
            handleError(parentCtx);
            return null;
        }
        initWorkerCAI = workerSet.initWorker.getCodeAttributeInfo();
    }
    for (int i = 1; i < generalWorkersCount; i++) {
        executeWorker(respCtx, parentCtx, argRegs, callableUnitInfo, workerSet.generalWorkers[i], wdi, initWorkerLocalData, initWorkerCAI, false);
    }
    WorkerExecutionContext runInCallerCtx = executeWorker(respCtx, parentCtx, argRegs, callableUnitInfo, workerSet.generalWorkers[0], wdi, initWorkerLocalData, initWorkerCAI, true);
    if (waitForResponse) {
        BLangScheduler.executeNow(runInCallerCtx);
        respCallback.waitForResponse();
        // An error in the context at this point means an unhandled runtime error has propagated
        // all the way up to the entry point. Hence throw a {@link BLangRuntimeException} and
        // terminate the execution.
        BStruct error = parentCtx.getError();
        if (error != null) {
            handleError(parentCtx);
        }
        return null;
    } else {
        return runInCallerCtx;
    }
}
Also used : WorkerExecutionContext(org.ballerinalang.bre.bvm.WorkerExecutionContext) WorkerSet(org.ballerinalang.util.codegen.CallableUnitInfo.WorkerSet) BStruct(org.ballerinalang.model.values.BStruct) CodeAttributeInfo(org.ballerinalang.util.codegen.attributes.CodeAttributeInfo) WorkerData(org.ballerinalang.bre.bvm.WorkerData) TraceableCallback(org.ballerinalang.util.tracer.TraceableCallback) SyncCallableWorkerResponseContext(org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext) CallableWorkerResponseContext(org.ballerinalang.bre.bvm.CallableWorkerResponseContext)

Example 2 with WorkerSet

use of org.ballerinalang.util.codegen.CallableUnitInfo.WorkerSet in project ballerina by ballerina-lang.

the class BLangFunctions method invokeCallable.

/**
 * This method does not short circuit the execution of the first worker to execute in the
 * same calling thread, but rather executes all the workers in their own separate threads.
 * This is specifically useful in executing service resources, where the calling transport
 * threads shouldn't be blocked, but rather the worker threads should be used.
 */
public static void invokeCallable(CallableUnitInfo callableUnitInfo, WorkerExecutionContext parentCtx, int[] argRegs, int[] retRegs, CallableUnitCallback responseCallback) {
    WorkerSet workerSet = callableUnitInfo.getWorkerSet();
    SyncCallableWorkerResponseContext respCtx = new SyncCallableWorkerResponseContext(callableUnitInfo.getRetParamTypes(), workerSet.generalWorkers.length);
    respCtx.registerResponseCallback(responseCallback);
    if (TraceManagerWrapper.getInstance().isTraceEnabled()) {
        respCtx.registerResponseCallback(new TraceableCallback(parentCtx));
    }
    respCtx.joinTargetContextInfo(parentCtx, retRegs);
    WorkerDataIndex wdi = callableUnitInfo.retWorkerIndex;
    /* execute the init worker and extract the local variables created by it */
    WorkerData initWorkerLocalData = null;
    CodeAttributeInfo initWorkerCAI = null;
    if (workerSet.initWorker != null) {
        initWorkerLocalData = executeInitWorker(parentCtx, argRegs, callableUnitInfo, workerSet.initWorker, wdi);
        if (initWorkerLocalData == null) {
            handleError(parentCtx);
            return;
        }
        initWorkerCAI = workerSet.initWorker.getCodeAttributeInfo();
    }
    for (int i = 0; i < workerSet.generalWorkers.length; i++) {
        executeWorker(respCtx, parentCtx, argRegs, callableUnitInfo, workerSet.generalWorkers[i], wdi, initWorkerLocalData, initWorkerCAI, false);
    }
}
Also used : WorkerSet(org.ballerinalang.util.codegen.CallableUnitInfo.WorkerSet) CodeAttributeInfo(org.ballerinalang.util.codegen.attributes.CodeAttributeInfo) WorkerData(org.ballerinalang.bre.bvm.WorkerData) TraceableCallback(org.ballerinalang.util.tracer.TraceableCallback) SyncCallableWorkerResponseContext(org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext)

Example 3 with WorkerSet

use of org.ballerinalang.util.codegen.CallableUnitInfo.WorkerSet in project ballerina by ballerina-lang.

the class BLangFunctions method invokeNonNativeCallableAsync.

public static void invokeNonNativeCallableAsync(CallableUnitInfo callableUnitInfo, WorkerExecutionContext parentCtx, int[] argRegs, int[] retRegs) {
    WorkerSet workerSet = callableUnitInfo.getWorkerSet();
    int generalWorkersCount = workerSet.generalWorkers.length;
    AsyncInvocableWorkerResponseContext respCtx = new AsyncInvocableWorkerResponseContext(callableUnitInfo, generalWorkersCount);
    WorkerDataIndex wdi = callableUnitInfo.retWorkerIndex;
    /* execute the init worker and extract the local variables created by it */
    WorkerData initWorkerLocalData = null;
    CodeAttributeInfo initWorkerCAI = null;
    if (workerSet.initWorker != null) {
        initWorkerLocalData = executeInitWorker(parentCtx, argRegs, callableUnitInfo, workerSet.initWorker, wdi);
        if (initWorkerLocalData == null) {
            handleError(parentCtx);
            return;
        }
        initWorkerCAI = workerSet.initWorker.getCodeAttributeInfo();
    }
    List<WorkerExecutionContext> workerExecutionContexts = new ArrayList<>();
    /* execute all the workers in their own threads */
    for (int i = 0; i < generalWorkersCount; i++) {
        workerExecutionContexts.add(executeWorker(respCtx, parentCtx, argRegs, callableUnitInfo, workerSet.generalWorkers[i], wdi, initWorkerLocalData, initWorkerCAI, false));
    }
    /* set the worker execution contexts in the response context, so it can use them to do later
         * operations such as cancel */
    respCtx.setWorkerExecutionContexts(workerExecutionContexts);
    /* create the future encapsulating the worker response context, and set it as the return value
         * to the parent */
    BLangVMUtils.populateWorkerDataWithValues(parentCtx.workerLocal, retRegs, new BValue[] { new BCallableFuture(callableUnitInfo.getName(), respCtx) }, new BType[] { BTypes.typeFuture });
    return;
}
Also used : WorkerExecutionContext(org.ballerinalang.bre.bvm.WorkerExecutionContext) WorkerSet(org.ballerinalang.util.codegen.CallableUnitInfo.WorkerSet) AsyncInvocableWorkerResponseContext(org.ballerinalang.bre.bvm.AsyncInvocableWorkerResponseContext) CodeAttributeInfo(org.ballerinalang.util.codegen.attributes.CodeAttributeInfo) ArrayList(java.util.ArrayList) BCallableFuture(org.ballerinalang.model.values.BCallableFuture) WorkerData(org.ballerinalang.bre.bvm.WorkerData)

Aggregations

WorkerData (org.ballerinalang.bre.bvm.WorkerData)3 WorkerSet (org.ballerinalang.util.codegen.CallableUnitInfo.WorkerSet)3 CodeAttributeInfo (org.ballerinalang.util.codegen.attributes.CodeAttributeInfo)3 SyncCallableWorkerResponseContext (org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext)2 WorkerExecutionContext (org.ballerinalang.bre.bvm.WorkerExecutionContext)2 TraceableCallback (org.ballerinalang.util.tracer.TraceableCallback)2 ArrayList (java.util.ArrayList)1 AsyncInvocableWorkerResponseContext (org.ballerinalang.bre.bvm.AsyncInvocableWorkerResponseContext)1 CallableWorkerResponseContext (org.ballerinalang.bre.bvm.CallableWorkerResponseContext)1 BCallableFuture (org.ballerinalang.model.values.BCallableFuture)1 BStruct (org.ballerinalang.model.values.BStruct)1