Search in sources :

Example 1 with BCallableFuture

use of org.ballerinalang.model.values.BCallableFuture in project ballerina by ballerina-lang.

the class BLangFunctions method invokeNativeCallableAsync.

private static void invokeNativeCallableAsync(CallableUnitInfo callableUnitInfo, WorkerExecutionContext parentCtx, int[] argRegs, int[] retRegs) {
    WorkerData caleeSF = BLangVMUtils.createWorkerDataForLocal(callableUnitInfo.getDefaultWorkerInfo(), parentCtx, argRegs, callableUnitInfo.getParamTypes());
    Context nativeCtx = new NativeCallContext(parentCtx, callableUnitInfo, caleeSF);
    NativeCallableUnit nativeCallable = callableUnitInfo.getNativeCallableUnit();
    if (nativeCallable == null) {
        return;
    }
    AsyncInvocableWorkerResponseContext respCtx;
    if (nativeCallable.isBlocking()) {
        respCtx = BLangScheduler.executeBlockingNativeAsync(nativeCallable, nativeCtx);
    } else {
        respCtx = BLangScheduler.executeNonBlockingNativeAsync(nativeCallable, nativeCtx);
    }
    BLangVMUtils.populateWorkerDataWithValues(parentCtx.workerLocal, retRegs, new BValue[] { new BCallableFuture(callableUnitInfo.getName(), respCtx) }, new BType[] { BTypes.typeFuture });
}
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) AsyncInvocableWorkerResponseContext(org.ballerinalang.bre.bvm.AsyncInvocableWorkerResponseContext) NativeCallContext(org.ballerinalang.bre.NativeCallContext) NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit) BCallableFuture(org.ballerinalang.model.values.BCallableFuture) WorkerData(org.ballerinalang.bre.bvm.WorkerData)

Example 2 with BCallableFuture

use of org.ballerinalang.model.values.BCallableFuture 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

AsyncInvocableWorkerResponseContext (org.ballerinalang.bre.bvm.AsyncInvocableWorkerResponseContext)2 WorkerData (org.ballerinalang.bre.bvm.WorkerData)2 WorkerExecutionContext (org.ballerinalang.bre.bvm.WorkerExecutionContext)2 BCallableFuture (org.ballerinalang.model.values.BCallableFuture)2 ArrayList (java.util.ArrayList)1 Context (org.ballerinalang.bre.Context)1 NativeCallContext (org.ballerinalang.bre.NativeCallContext)1 CallableWorkerResponseContext (org.ballerinalang.bre.bvm.CallableWorkerResponseContext)1 ForkJoinWorkerResponseContext (org.ballerinalang.bre.bvm.ForkJoinWorkerResponseContext)1 InitWorkerResponseContext (org.ballerinalang.bre.bvm.InitWorkerResponseContext)1 SyncCallableWorkerResponseContext (org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext)1 WorkerResponseContext (org.ballerinalang.bre.bvm.WorkerResponseContext)1 NativeCallableUnit (org.ballerinalang.model.NativeCallableUnit)1 WorkerSet (org.ballerinalang.util.codegen.CallableUnitInfo.WorkerSet)1 CodeAttributeInfo (org.ballerinalang.util.codegen.attributes.CodeAttributeInfo)1