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

export_native_api.md: add a note about thread termination #2572

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Changes from all commits
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
30 changes: 29 additions & 1 deletion doc/export_native_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,32 @@ wasm_response_send(wasm_exec_env_t exec_env, char *buffer, int size)
```



## Native API implementation notes

### Async thread termination

When threading support is enabled, a thread can be requested to terminate
itself either explicitly by the `wasm_runtime_terminate` API or implicitly
by cluster-wide activities like WASI `proc_exit`.
If a call to your native function can take long or even forever,
you should make it to respond to termination requests in a timely manner.
There are a few implementation approaches for that:

* Design your API so that it always returns in a timely manner.
This is the most simple approach when possible.

* Make your native function check the cluster state by calling
`wasm_cluster_is_thread_terminated` from time to time.
If `wasm_cluster_is_thread_terminated` returns true, make your
native function return.
Note: as of writing this, `wasm_cluster_is_thread_terminated` is not
exported as a runtime API.

* If your native function is a simple wrapper of blocking system call,
you can probably use the `wasm_runtime_begin_blocking_op` and
`wasm_runtime_end_blocking_op` runtime API.
See the comment in `wamr_export.h` for details.

* If your native function is very complex, it might be simpler to put the
guts of it into a separate worker process so that it can be cleaned up
by simply killing it.