Search in sources :

Example 6 with WorkerData

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

the class BLangVMUtils method populateArgAndReturnData.

@SuppressWarnings("rawtypes")
public static int[][] populateArgAndReturnData(WorkerExecutionContext ctx, CallableUnitInfo callableUnitInfo, BValue[] args) {
    WorkerDataIndex wdi1 = callableUnitInfo.paramWorkerIndex;
    WorkerDataIndex wdi2 = callableUnitInfo.retWorkerIndex;
    WorkerData local = createWorkerData(wdi1, wdi2);
    BType[] types = callableUnitInfo.getParamTypes();
    int longParamCount = 0, doubleParamCount = 0, stringParamCount = 0, intParamCount = 0, byteParamCount = 0, refParamCount = 0;
    for (int i = 0; i < types.length; i++) {
        switch(types[i].getTag()) {
            case TypeTags.INT_TAG:
                if (args[i] instanceof BString) {
                    local.longRegs[longParamCount] = ((BString) args[i]).intValue();
                } else {
                    local.longRegs[longParamCount] = ((BInteger) args[i]).intValue();
                }
                longParamCount++;
                break;
            case TypeTags.FLOAT_TAG:
                if (args[i] instanceof BString) {
                    local.doubleRegs[doubleParamCount] = ((BString) args[i]).floatValue();
                } else {
                    local.doubleRegs[doubleParamCount] = ((BFloat) args[i]).floatValue();
                }
                doubleParamCount++;
                break;
            case TypeTags.STRING_TAG:
                local.stringRegs[stringParamCount] = args[i].stringValue();
                stringParamCount++;
                break;
            case TypeTags.BOOLEAN_TAG:
                if (args[i] instanceof BString) {
                    local.intRegs[intParamCount] = ((BString) args[i]).value().toLowerCase().equals("true") ? 1 : 0;
                } else {
                    local.intRegs[intParamCount] = ((BBoolean) args[i]).booleanValue() ? 1 : 0;
                }
                intParamCount++;
                break;
            case TypeTags.BLOB_TAG:
                local.byteRegs[byteParamCount] = ((BBlob) args[i]).blobValue();
                byteParamCount++;
                break;
            default:
                local.refRegs[refParamCount] = (BRefType) args[i];
                refParamCount++;
                break;
        }
    }
    ctx.workerLocal = local;
    return new int[][] { wdi1.retRegs, BLangVMUtils.createReturnRegValues(wdi1, wdi2, callableUnitInfo.getRetParamTypes()) };
}
Also used : BType(org.ballerinalang.model.types.BType) BString(org.ballerinalang.model.values.BString) BBoolean(org.ballerinalang.model.values.BBoolean) WorkerData(org.ballerinalang.bre.bvm.WorkerData)

Example 7 with WorkerData

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

the class BLangVMUtils method createWorkerDataForLocal.

static WorkerData createWorkerDataForLocal(WorkerInfo workerInfo, WorkerExecutionContext parentCtx, int[] argRegs) {
    WorkerData wd = createWorkerData(workerInfo);
    BLangVMUtils.copyValuesForForkJoin(parentCtx.workerLocal, wd, argRegs);
    return wd;
}
Also used : WorkerData(org.ballerinalang.bre.bvm.WorkerData)

Example 8 with WorkerData

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

the class BLangVMUtils method populateReturnData.

public static BValue[] populateReturnData(WorkerExecutionContext ctx, CallableUnitInfo callableUnitInfo, int[] retRegs) {
    WorkerData data = ctx.workerLocal;
    BType[] retTypes = callableUnitInfo.getRetParamTypes();
    BValue[] returnValues = new BValue[retTypes.length];
    for (int i = 0; i < returnValues.length; i++) {
        BType retType = retTypes[i];
        switch(retType.getTag()) {
            case TypeTags.INT_TAG:
                returnValues[i] = new BInteger(data.longRegs[retRegs[i]]);
                break;
            case TypeTags.FLOAT_TAG:
                returnValues[i] = new BFloat(data.doubleRegs[retRegs[i]]);
                break;
            case TypeTags.STRING_TAG:
                returnValues[i] = new BString(data.stringRegs[retRegs[i]]);
                break;
            case TypeTags.BOOLEAN_TAG:
                boolean boolValue = data.intRegs[retRegs[i]] == 1;
                returnValues[i] = new BBoolean(boolValue);
                break;
            case TypeTags.BLOB_TAG:
                returnValues[i] = new BBlob(data.byteRegs[retRegs[i]]);
                break;
            default:
                returnValues[i] = data.refRegs[retRegs[i]];
                break;
        }
    }
    return returnValues;
}
Also used : BType(org.ballerinalang.model.types.BType) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BFloat(org.ballerinalang.model.values.BFloat) BBoolean(org.ballerinalang.model.values.BBoolean) WorkerData(org.ballerinalang.bre.bvm.WorkerData) BBlob(org.ballerinalang.model.values.BBlob)

Example 9 with WorkerData

use of org.ballerinalang.bre.bvm.WorkerData 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 10 with WorkerData

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

Aggregations

WorkerData (org.ballerinalang.bre.bvm.WorkerData)12 WorkerExecutionContext (org.ballerinalang.bre.bvm.WorkerExecutionContext)7 SyncCallableWorkerResponseContext (org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext)4 AsyncInvocableWorkerResponseContext (org.ballerinalang.bre.bvm.AsyncInvocableWorkerResponseContext)3 CallableWorkerResponseContext (org.ballerinalang.bre.bvm.CallableWorkerResponseContext)3 InitWorkerResponseContext (org.ballerinalang.bre.bvm.InitWorkerResponseContext)3 BType (org.ballerinalang.model.types.BType)3 WorkerSet (org.ballerinalang.util.codegen.CallableUnitInfo.WorkerSet)3 CodeAttributeInfo (org.ballerinalang.util.codegen.attributes.CodeAttributeInfo)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 BBoolean (org.ballerinalang.model.values.BBoolean)2 BCallableFuture (org.ballerinalang.model.values.BCallableFuture)2 BString (org.ballerinalang.model.values.BString)2 TraceableCallback (org.ballerinalang.util.tracer.TraceableCallback)2 ArrayList (java.util.ArrayList)1 BLangCallableUnitCallback (org.ballerinalang.bre.BLangCallableUnitCallback)1