Search in sources :

Example 16 with WorkerExecutionContext

use of org.ballerinalang.bre.bvm.WorkerExecutionContext in project ballerina by ballerina-lang.

the class BLangFunctions method invokeNativeCallable.

private static WorkerExecutionContext invokeNativeCallable(CallableUnitInfo callableUnitInfo, WorkerExecutionContext parentCtx, int[] argRegs, int[] retRegs, int flags) {
    WorkerData parentLocalData = parentCtx.workerLocal;
    BType[] retTypes = callableUnitInfo.getRetParamTypes();
    WorkerData caleeSF = BLangVMUtils.createWorkerDataForLocal(callableUnitInfo.getDefaultWorkerInfo(), parentCtx, argRegs, callableUnitInfo.getParamTypes());
    Context ctx = new NativeCallContext(parentCtx, callableUnitInfo, caleeSF);
    NativeCallableUnit nativeCallable = callableUnitInfo.getNativeCallableUnit();
    if (nativeCallable == null) {
        return parentCtx;
    }
    try {
        if (nativeCallable.isBlocking()) {
            nativeCallable.execute(ctx, null);
            BLangVMUtils.populateWorkerDataWithValues(parentLocalData, retRegs, ctx.getReturnValues(), retTypes);
            if (TraceManagerWrapper.getInstance().isTraceEnabled() && FunctionFlags.isObserved(flags)) {
                TraceUtil.finishTraceSpan(TraceUtil.getTracer(parentCtx));
            }
            /* we want the parent to continue, since we got the response of the native call already */
            return parentCtx;
        } else {
            CallableUnitCallback callback;
            if (TraceManagerWrapper.getInstance().isTraceEnabled() && FunctionFlags.isObserved(flags)) {
                callback = new TraceableCallbackWrapper(parentCtx, new BLangCallableUnitCallback(ctx, parentCtx, retRegs, retTypes));
            } else {
                callback = new BLangCallableUnitCallback(ctx, parentCtx, retRegs, retTypes);
            }
            nativeCallable.execute(ctx, callback);
            /* we want the parent to suspend (i.e. go to wait for response state) and stay until notified */
            return null;
        }
    } catch (BLangNullReferenceException e) {
        return BLangVMUtils.handleNativeInvocationError(parentCtx, BLangVMErrors.createNullRefException(callableUnitInfo));
    } catch (Throwable e) {
        return BLangVMUtils.handleNativeInvocationError(parentCtx, BLangVMErrors.createError(callableUnitInfo, e.getMessage()));
    }
}
Also used : InitWorkerResponseContext(org.ballerinalang.bre.bvm.InitWorkerResponseContext) SyncCallableWorkerResponseContext(org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext) NativeCallContext(org.ballerinalang.bre.NativeCallContext) ForkJoinWorkerResponseContext(org.ballerinalang.bre.bvm.ForkJoinWorkerResponseContext) WorkerResponseContext(org.ballerinalang.bre.bvm.WorkerResponseContext) Context(org.ballerinalang.bre.Context) CallableWorkerResponseContext(org.ballerinalang.bre.bvm.CallableWorkerResponseContext) WorkerExecutionContext(org.ballerinalang.bre.bvm.WorkerExecutionContext) AsyncInvocableWorkerResponseContext(org.ballerinalang.bre.bvm.AsyncInvocableWorkerResponseContext) BLangCallableUnitCallback(org.ballerinalang.bre.BLangCallableUnitCallback) BType(org.ballerinalang.model.types.BType) TraceableCallbackWrapper(org.ballerinalang.util.tracer.TraceableCallbackWrapper) BLangNullReferenceException(org.ballerinalang.util.exceptions.BLangNullReferenceException) NativeCallContext(org.ballerinalang.bre.NativeCallContext) NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit) WorkerData(org.ballerinalang.bre.bvm.WorkerData) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback) BLangCallableUnitCallback(org.ballerinalang.bre.BLangCallableUnitCallback)

Example 17 with WorkerExecutionContext

use of org.ballerinalang.bre.bvm.WorkerExecutionContext in project ballerina by ballerina-lang.

the class BLangFunctions method invokeServiceInitFunction.

public static void invokeServiceInitFunction(FunctionInfo initFuncInfo) {
    WorkerExecutionContext context = new WorkerExecutionContext(initFuncInfo.getPackageInfo().getProgramFile());
    invokeCallable(initFuncInfo, context, new int[0], new int[0], true);
    if (context.getError() != null) {
        String stackTraceStr = BLangVMErrors.getPrintableStackTrace(context.getError());
        throw new BLangRuntimeException("error: " + stackTraceStr);
    }
}
Also used : WorkerExecutionContext(org.ballerinalang.bre.bvm.WorkerExecutionContext) BLangRuntimeException(org.ballerinalang.util.exceptions.BLangRuntimeException)

Example 18 with WorkerExecutionContext

use of org.ballerinalang.bre.bvm.WorkerExecutionContext in project ballerina by ballerina-lang.

the class BLangFunctions method invokeEntrypointCallable.

public static BValue[] invokeEntrypointCallable(ProgramFile programFile, PackageInfo packageInfo, FunctionInfo functionInfo, BValue[] args) {
    WorkerExecutionContext parentCtx = new WorkerExecutionContext(programFile);
    if (functionInfo.getParamTypes().length != args.length) {
        throw new RuntimeException("Size of input argument arrays is not equal to size of function parameters");
    }
    invokePackageInitFunction(packageInfo.getInitFunctionInfo(), parentCtx);
    invokeVMUtilFunction(packageInfo.getStartFunctionInfo(), parentCtx);
    BValue[] result = invokeCallable(functionInfo, parentCtx, args);
    BLangScheduler.waitForWorkerCompletion();
    return result;
}
Also used : WorkerExecutionContext(org.ballerinalang.bre.bvm.WorkerExecutionContext) BLangRuntimeException(org.ballerinalang.util.exceptions.BLangRuntimeException) BValue(org.ballerinalang.model.values.BValue)

Example 19 with WorkerExecutionContext

use of org.ballerinalang.bre.bvm.WorkerExecutionContext 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

WorkerExecutionContext (org.ballerinalang.bre.bvm.WorkerExecutionContext)19 WorkerData (org.ballerinalang.bre.bvm.WorkerData)7 AsyncInvocableWorkerResponseContext (org.ballerinalang.bre.bvm.AsyncInvocableWorkerResponseContext)3 CallableWorkerResponseContext (org.ballerinalang.bre.bvm.CallableWorkerResponseContext)3 InitWorkerResponseContext (org.ballerinalang.bre.bvm.InitWorkerResponseContext)3 SyncCallableWorkerResponseContext (org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext)3 Context (org.ballerinalang.bre.Context)2 NativeCallContext (org.ballerinalang.bre.NativeCallContext)2 ForkJoinWorkerResponseContext (org.ballerinalang.bre.bvm.ForkJoinWorkerResponseContext)2 WorkerResponseContext (org.ballerinalang.bre.bvm.WorkerResponseContext)2 NativeCallableUnit (org.ballerinalang.model.NativeCallableUnit)2 BCallableFuture (org.ballerinalang.model.values.BCallableFuture)2 WorkerSet (org.ballerinalang.util.codegen.CallableUnitInfo.WorkerSet)2 CodeAttributeInfo (org.ballerinalang.util.codegen.attributes.CodeAttributeInfo)2 Debugger (org.ballerinalang.util.debugger.Debugger)2 BLangRuntimeException (org.ballerinalang.util.exceptions.BLangRuntimeException)2 ArrayList (java.util.ArrayList)1 BLangCallableUnitCallback (org.ballerinalang.bre.BLangCallableUnitCallback)1 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)1 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)1