FunctionHandler::Invoke

Contents

FunctionHandler::Invoke#

template<typename ...Args>
inline ObjectRef<ReturnType<F>> YR::FunctionHandler::Invoke(Args&&... args)#

Invokes the registered function with the provided arguments.

The Invoke method sends a request to the backend to execute the registered function with the specified arguments. The function must have been registered using the YR_INVOKE macro. The arguments passed to the Invoke method must match the function’s parameter types and number, otherwise a compile-time error will occur. It is crucial to ensure that the argument types match exactly to avoid potential issues caused by implicit type conversions.

#include "yr/yr.h"

int PlusOne(int x)
{
    return x + 1;
}

YR_INVOKE(PlusOne)

int main()
{
    YR::Config conf;
    YR::Init(conf);

    std::vector<YR::ObjectRef<int>> n2;
    int k = 1;
    for (int i = 0; i < k; i++) {
        auto r2 = YR::Function(PlusOne).Invoke(2);
        n2.emplace_back(r2);
    }
    for (int i = 0; i < k; i++) {
        auto integer = *YR::Get(n2[i]);
        printf("%d :%d\n", i, integer);
    }

    YR::Finalize();
    return 0;
}

Chained Invocation Usage:

In single-machine mode, the Invoke method can be chained to perform multiple function calls. However, if the total number of function execution instances exceeds the thread pool size, the program may become blocked. It is recommended to configure the thread pool size according to the number of expected function calls. For example, if a series of function calls involves three levels (e.g., AddThree, AddTwo, AddOne), each call will create a new thread, so the thread pool size should be configured to at least 3.

int AddOne(int x)
{
    return x + 1;
}

int AddTwo(int x)
{
    auto obj = YR::Function(AddOne).Invoke(x);
    auto res = *YR::Get(obj);
    return res + 1;
}

int AddThree(int x)
{
    auto obj = YR::Function(AddTwo).Invoke(x);
    auto res = *YR::Get(obj);
    return res + 1;
}

YR_INVOKE(AddOne, AddTwo, AddThree);

int main(void) {
    YR::Config conf;
    conf.mode = YR::Config::Mode::LOCAL_MODE;
    conf.threadPoolSize = 3;
    YR::Init(conf);
    auto r = YR::Function(AddThree).Invoke(5);
    int ret = *(YR::Get(r)); // ret = 8
    return 0;
}
YR_INVOKE(HelloWorld)
For more details on configuring the thread pool, refer to the struct-Config.

Template Parameters:

Args – The types of the arguments to be passed to the function.

Parameters:

args – The arguments to be passed to the function. The types and number of arguments must match the function’s signature.

Throws:

Exception – If the backend interface call fails, for example due to errors in creating function instances or insufficient resources, an exception will be thrown.

Returns:

ObjectRef<ReturnType<F>>: An ObjectRef object representing the return value of the function. This object acts as a key and requires the YR::Get method to be called to obtain the actual value.