Skip to content

Commit

Permalink
[API] Update the async API for supporting the function arity getting.
Browse files Browse the repository at this point in the history
Signed-off-by: YiYing He <[email protected]>
  • Loading branch information
q82419 authored and hydai committed Jan 20, 2022
1 parent ab56eeb commit f5f57fb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
16 changes: 16 additions & 0 deletions include/api/wasmedge/wasmedge.h
Original file line number Diff line number Diff line change
Expand Up @@ -2545,8 +2545,24 @@ WASMEDGE_CAPI_EXPORT bool WasmEdge_AsyncWaitFor(WasmEdge_Async *Cxt,
/// \param Cxt the WasmEdge_ASync.
WASMEDGE_CAPI_EXPORT void WasmEdge_AsyncCancel(WasmEdge_Async *Cxt);

/// Wait and get the return list length of the WasmEdge_Async execution.
///
/// This function will wait until the execution finished and return the return
/// value list length of the executed function. This function will return 0 if
/// the `Cxt` is NULL, the execution was failed, or the execution was canceled.
/// Developers can call the `WasmEdge_AsyncGet` to get the execution status and
/// the return values.
///
/// \param Cxt the WasmEdge_ASync.
///
/// \returns the return list length of the executed function.
WASMEDGE_CAPI_EXPORT uint32_t
WasmEdge_AsyncGetReturnsLength(WasmEdge_Async *Cxt);

/// Wait and get the result of WasmEdge_Async execution.
///
/// This function will wait until the execution finished and return the
/// execution status and the return values.
/// If the `Returns` buffer length is smaller than the arity of the function,
/// the overflowed return values will be discarded.
///
Expand Down
5 changes: 3 additions & 2 deletions include/vm/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
//===----------------------------------------------------------------------===//
///
/// \file
/// This file is the definition class of VM class.
/// This file is the definition class of Async class.
///
//===----------------------------------------------------------------------===//
#pragma once

#include "vm.h"

#include <future>
#include <thread>

Expand Down Expand Up @@ -76,7 +77,7 @@ template <typename T> class Async {
}

private:
std::future<T> Future;
std::shared_future<T> Future;
std::thread Thread;
VM *VMPtr;
};
Expand Down
66 changes: 49 additions & 17 deletions lib/api/wasmedge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2311,16 +2311,33 @@ WasmEdge_ImportObjectDelete(WasmEdge_ImportObjectContext *Cxt) {
// >>>>>>>> WasmEdge Async functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

WASMEDGE_CAPI_EXPORT void WasmEdge_AsyncWait(WasmEdge_Async *Cxt) {
Cxt->Async.wait();
if (Cxt) {
Cxt->Async.wait();
}
}

WASMEDGE_CAPI_EXPORT bool WasmEdge_AsyncWaitFor(WasmEdge_Async *Cxt,
uint64_t Milliseconds) {
return Cxt->Async.waitFor(std::chrono::milliseconds(Milliseconds));
if (Cxt) {
return Cxt->Async.waitFor(std::chrono::milliseconds(Milliseconds));
}
return false;
}

WASMEDGE_CAPI_EXPORT void WasmEdge_AsyncCancel(WasmEdge_Async *Cxt) {
Cxt->Async.cancel();
if (Cxt) {
Cxt->Async.cancel();
}
}

WASMEDGE_CAPI_EXPORT uint32_t
WasmEdge_AsyncGetReturnsLength(WasmEdge_Async *Cxt) {
if (Cxt) {
if (auto Res = Cxt->Async.get()) {
return static_cast<uint32_t>((*Res).size());
}
}
return 0;
}

WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_AsyncGet(
Expand Down Expand Up @@ -2445,29 +2462,38 @@ WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncRunWasmFromFile(
WasmEdge_VMContext *Cxt, const char *Path, const WasmEdge_String FuncName,
const WasmEdge_Value *Params, const uint32_t ParamLen) {
auto ParamPair = genParamPair(Params, ParamLen);
return new WasmEdge_Async(Cxt->VM.asyncRunWasmFile(
std::filesystem::absolute(Path), genStrView(FuncName), ParamPair.first,
ParamPair.second));
if (Cxt) {
return new WasmEdge_Async(Cxt->VM.asyncRunWasmFile(
std::filesystem::absolute(Path), genStrView(FuncName), ParamPair.first,
ParamPair.second));
}
return nullptr;
}

WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncRunWasmFromBuffer(
WasmEdge_VMContext *Cxt, const uint8_t *Buf, const uint32_t BufLen,
const WasmEdge_String FuncName, const WasmEdge_Value *Params,
const uint32_t ParamLen) {
auto ParamPair = genParamPair(Params, ParamLen);
return new WasmEdge_Async(
Cxt->VM.asyncRunWasmFile(genSpan(Buf, BufLen), genStrView(FuncName),
ParamPair.first, ParamPair.second));
if (Cxt) {
return new WasmEdge_Async(
Cxt->VM.asyncRunWasmFile(genSpan(Buf, BufLen), genStrView(FuncName),
ParamPair.first, ParamPair.second));
}
return nullptr;
}

WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncRunWasmFromASTModule(
WasmEdge_VMContext *Cxt, const WasmEdge_ASTModuleContext *ASTCxt,
const WasmEdge_String FuncName, const WasmEdge_Value *Params,
const uint32_t ParamLen) {
auto ParamPair = genParamPair(Params, ParamLen);
return new WasmEdge_Async(
Cxt->VM.asyncRunWasmFile(*ASTCxt->Module.get(), genStrView(FuncName),
ParamPair.first, ParamPair.second));
if (Cxt && ASTCxt) {
return new WasmEdge_Async(
Cxt->VM.asyncRunWasmFile(*ASTCxt->Module.get(), genStrView(FuncName),
ParamPair.first, ParamPair.second));
}
return nullptr;
}

WASMEDGE_CAPI_EXPORT WasmEdge_Result
Expand Down Expand Up @@ -2532,18 +2558,24 @@ WASMEDGE_CAPI_EXPORT WasmEdge_Async *
WasmEdge_VMAsyncExecute(WasmEdge_VMContext *Cxt, const WasmEdge_String FuncName,
const WasmEdge_Value *Params, const uint32_t ParamLen) {
auto ParamPair = genParamPair(Params, ParamLen);
return new WasmEdge_Async(Cxt->VM.asyncExecute(
genStrView(FuncName), ParamPair.first, ParamPair.second));
if (Cxt) {
return new WasmEdge_Async(Cxt->VM.asyncExecute(
genStrView(FuncName), ParamPair.first, ParamPair.second));
}
return nullptr;
}

WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncExecuteRegistered(
WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName,
const WasmEdge_String FuncName, const WasmEdge_Value *Params,
const uint32_t ParamLen) {
auto ParamPair = genParamPair(Params, ParamLen);
return new WasmEdge_Async(
Cxt->VM.asyncExecute(genStrView(ModuleName), genStrView(FuncName),
ParamPair.first, ParamPair.second));
if (Cxt) {
return new WasmEdge_Async(
Cxt->VM.asyncExecute(genStrView(ModuleName), genStrView(FuncName),
ParamPair.first, ParamPair.second));
}
return nullptr;
}

WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext *
Expand Down

0 comments on commit f5f57fb

Please sign in to comment.