Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] run func timeout use std::time::Duration #76

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions crates/wasmedge-sys/src/async/fiber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub(crate) struct TimeoutFiberFuture<'a> {
fiber: Fiber<'a, Result<(), ()>, (), Result<(), ()>>,
current_suspend: *mut *const Suspend<Result<(), ()>, (), Result<(), ()>>,
current_poll_cx: *mut *mut Context<'static>,
timeout_sec: u64,
deadline: std::time::SystemTime,
}

impl<'a> TimeoutFiberFuture<'a> {
Expand All @@ -115,15 +115,15 @@ impl<'a> TimeoutFiberFuture<'a> {
///
/// * `func` - The function to execute.
///
/// * `timeout_sec` - The maximum execution time in seconds for the function instance.
/// * `deadline` - The deadline the function instance.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refine the description for the deadline argument. Thanks a lot!

///
/// # Error
///
/// If fail to create the fiber stack or the fiber fail to resume, then an error is returned.
pub(crate) async fn on_fiber<R>(
async_state: &AsyncState,
apepkuss marked this conversation as resolved.
Show resolved Hide resolved
func: impl FnOnce() -> R + Send,
timeout_sec: u64,
deadline: std::time::SystemTime,
) -> Result<R, ()> {
let mut slot = None;

Expand All @@ -149,7 +149,7 @@ impl<'a> TimeoutFiberFuture<'a> {
fiber,
current_suspend,
current_poll_cx,
timeout_sec,
deadline,
}
};

Expand Down Expand Up @@ -184,8 +184,16 @@ impl<'a> Future for TimeoutFiberFuture<'a> {
if libc::timer_create(libc::CLOCK_REALTIME, &mut sev, &mut timerid) < 0 {
return Poll::Ready(Err(()));
}

let timeout = self.deadline.duration_since(std::time::SystemTime::now());
if timeout.is_err() {
return Poll::Ready(Err(()));
}
let timeout = timeout.unwrap().max(std::time::Duration::from_millis(100));
apepkuss marked this conversation as resolved.
Show resolved Hide resolved

let mut value: libc::itimerspec = std::mem::zeroed();
value.it_value.tv_sec = self.timeout_sec as i64;
value.it_value.tv_sec = timeout.as_secs() as _;
value.it_value.tv_nsec = timeout.subsec_nanos() as _;
if libc::timer_settime(timerid, 0, &value, std::ptr::null_mut()) < 0 {
libc::timer_delete(timerid);
return Poll::Ready(Err(()));
Expand Down
14 changes: 8 additions & 6 deletions crates/wasmedge-sys/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ impl Executor {
///
/// * `params` - The arguments to pass to the function.
///
/// * `timeout` - The maximum execution time (in seconds) of the function to be run.
/// * `timeout` - The maximum execution time of the function to be run.
///
/// # Errors
///
Expand All @@ -374,7 +374,7 @@ impl Executor {
&self,
func: &Function,
params: impl IntoIterator<Item = WasmValue>,
timeout: u64,
timeout: std::time::Duration,
) -> WasmEdgeResult<Vec<WasmValue>> {
use wasmedge_types::error;

Expand Down Expand Up @@ -402,7 +402,8 @@ impl Executor {
)));
}
let mut value: libc::itimerspec = std::mem::zeroed();
value.it_value.tv_sec = timeout as i64;
value.it_value.tv_sec = timeout.as_secs() as _;
value.it_value.tv_nsec = timeout.subsec_nanos() as _;
apepkuss marked this conversation as resolved.
Show resolved Hide resolved
if libc::timer_settime(timerid, 0, &value, std::ptr::null_mut()) < 0 {
libc::timer_delete(timerid);
return Err(Box::new(error::WasmEdgeError::Operation(
Expand Down Expand Up @@ -466,7 +467,7 @@ impl Executor {
///
/// * `params` - The arguments to pass to the function.
///
/// * `timeout` - The maximum execution time (in seconds) of the function to be run.
/// * `timeout` - The maximum execution time of the function to be run.
///
/// # Errors
///
Expand All @@ -482,10 +483,11 @@ impl Executor {
async_state: &AsyncState,
func: &Function,
params: impl IntoIterator<Item = WasmValue> + Send,
timeout: u64,
timeout: std::time::Duration,
) -> WasmEdgeResult<Vec<WasmValue>> {
use wasmedge_types::error;
TimeoutFiberFuture::on_fiber(async_state, || self.call_func(func, params), timeout)
let ldd = std::time::SystemTime::now() + timeout;
TimeoutFiberFuture::on_fiber(async_state, || self.call_func(func, params), ldd)
.await
.map_err(|_| Box::new(error::WasmEdgeError::ExecuteTimeout))?
}
Expand Down
16 changes: 6 additions & 10 deletions src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Executor {
///
/// * `params` - The arguments to pass to the function.
///
/// * `timeout` - The maximum execution time (in seconds) of the function to be run.
/// * `timeout` - The maximum execution time of the function to be run.
///
/// # Errors
///
Expand All @@ -77,14 +77,10 @@ impl Executor {
&self,
func: &Func,
params: impl IntoIterator<Item = WasmValue>,
timeout: u64,
timeout: std::time::Duration,
) -> WasmEdgeResult<Vec<WasmValue>> {
if timeout > 0 {
self.inner
.call_func_with_timeout(&func.inner, params, timeout)
} else {
self.inner.call_func(&func.inner, params)
}
self.inner
.call_func_with_timeout(&func.inner, params, timeout)
}

/// Asynchronously runs a host function instance and returns the results.
Expand Down Expand Up @@ -119,7 +115,7 @@ impl Executor {
///
/// * `params` - The arguments to pass to the function.
///
/// * `timeout` - The maximum execution time (in seconds) of the function to be run.
/// * `timeout` - The maximum execution time of the function to be run.
///
/// # Errors
///
Expand All @@ -134,7 +130,7 @@ impl Executor {
async_state: &AsyncState,
func: &Func,
params: impl IntoIterator<Item = WasmValue> + Send,
timeout: u64,
timeout: std::time::Duration,
) -> WasmEdgeResult<Vec<WasmValue>> {
self.inner
.call_func_async_with_timeout(async_state, &func.inner, params, timeout)
Expand Down
8 changes: 4 additions & 4 deletions src/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl Func {
///
/// * `args` - The arguments passed to the host function.
///
/// * `timeout` - The maximum execution time (in seconds) of the function to be run.
/// * `timeout` - The maximum execution time of the function to be run.
///
/// # Error
///
Expand All @@ -262,7 +262,7 @@ impl Func {
&self,
executor: &Executor,
args: impl IntoIterator<Item = WasmValue>,
timeout: u64,
timeout: std::time::Duration,
) -> WasmEdgeResult<Vec<WasmValue>> {
executor.run_func_with_timeout(self, args, timeout)
}
Expand Down Expand Up @@ -297,7 +297,7 @@ impl Func {
///
/// * `args` - The arguments passed to the host function.
///
/// * `timeout` - The maximum execution time (in seconds) of the function to be run.
/// * `timeout` - The maximum execution time of the function to be run.
///
/// # Error
///
Expand All @@ -312,7 +312,7 @@ impl Func {
async_state: &AsyncState,
executor: &Executor,
args: impl IntoIterator<Item = WasmValue> + Send,
timeout: u64,
timeout: std::time::Duration,
) -> WasmEdgeResult<Vec<WasmValue>> {
executor
.run_func_async_with_timeout(async_state, self, args, timeout)
Expand Down
8 changes: 4 additions & 4 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ impl Vm {
///
/// * `args` - The arguments to be passed to the target wasm function.
///
/// * `timeout` - The maximum execution time (in seconds) of the function to be run.
/// * `timeout` - The maximum execution time of the function to be run.
///
/// # Error
///
Expand All @@ -507,7 +507,7 @@ impl Vm {
mod_name: Option<&str>,
func_name: impl AsRef<str>,
args: impl IntoIterator<Item = WasmValue>,
timeout: u64,
timeout: std::time::Duration,
) -> WasmEdgeResult<Vec<WasmValue>> {
match mod_name {
Some(mod_name) => {
Expand Down Expand Up @@ -589,7 +589,7 @@ impl Vm {
///
/// * `args` - The arguments to be passed to the target wasm function.
///
/// * `timeout` - The maximum execution time (in seconds) of the function to be run.
/// * `timeout` - The maximum execution time of the function to be run.
///
/// # Error
///
Expand All @@ -605,7 +605,7 @@ impl Vm {
mod_name: Option<&str>,
func_name: impl AsRef<str> + Send,
args: impl IntoIterator<Item = WasmValue> + Send,
timeout: u64,
timeout: std::time::Duration,
) -> WasmEdgeResult<Vec<WasmValue>> {
match mod_name {
Some(mod_name) => match self.named_instances.get(mod_name) {
Expand Down