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

WIP: Add wasmtime_linker_define_wasi_threads to the c-api #10030

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/c-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ wat = { workspace = true, optional = true }
cap-std = { workspace = true, optional = true }
tokio = { workspace = true, optional = true, features = ["fs"] }
wasmtime-wasi = { workspace = true, optional = true, features = ["preview1"] }
wasmtime-wasi-threads = { workspace = true, optional = true }

# Optional dependencies for the `async` feature
futures = { workspace = true, optional = true }
Expand All @@ -57,5 +58,6 @@ gc-null = ["wasmtime/gc-null"]
cranelift = ['wasmtime/cranelift']
winch = ['wasmtime/winch']
debug-builtins = ['wasmtime/debug-builtins']
wasi-threads = ['wasmtime-wasi-threads']
# ... if you add a line above this be sure to change the other locations
# marked WASMTIME_FEATURE_LIST
2 changes: 2 additions & 0 deletions crates/c-api/artifact/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ default = [
'profiling',
'wat',
'wasi',
'wasi-threads',
'cache',
'parallel-compilation',
'async',
Expand All @@ -47,6 +48,7 @@ profiling = ["wasmtime-c-api/profiling"]
cache = ["wasmtime-c-api/cache"]
parallel-compilation = ['wasmtime-c-api/parallel-compilation']
wasi = ['wasmtime-c-api/wasi']
wasi-threads = ['wasmtime-c-api/wasi-threads']
logging = ['wasmtime-c-api/logging']
disable-logging = ["wasmtime-c-api/disable-logging"]
coredump = ["wasmtime-c-api/coredump"]
Expand Down
1 change: 1 addition & 0 deletions crates/c-api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const FEATURES: &[&str] = &[
"CACHE",
"PARALLEL_COMPILATION",
"WASI",
"WASI_THREADS",
"LOGGING",
"DISABLE_LOGGING",
"COREDUMP",
Expand Down
1 change: 1 addition & 0 deletions crates/c-api/cmake/features.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ feature(wat ON)
feature(cache ON)
feature(parallel-compilation ON)
feature(wasi ON)
feature(wasi-threads ON)
feature(logging ON)
feature(disable-logging OFF)
feature(coredump ON)
Expand Down
1 change: 1 addition & 0 deletions crates/c-api/include/wasmtime/conf.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#cmakedefine WASMTIME_FEATURE_CACHE
#cmakedefine WASMTIME_FEATURE_PARALLEL_COMPILATION
#cmakedefine WASMTIME_FEATURE_WASI
#cmakedefine WASMTIME_FEATURE_WASI_THREADS
#cmakedefine WASMTIME_FEATURE_LOGGING
#cmakedefine WASMTIME_FEATURE_DISABLE_LOGGING
#cmakedefine WASMTIME_FEATURE_COREDUMP
Expand Down
24 changes: 24 additions & 0 deletions crates/c-api/src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ use crate::{
use std::ffi::c_void;
use std::mem::MaybeUninit;
use std::str;
use std::sync::Arc;
use wasmtime::{Func, Instance, Linker};

#[cfg(feature = "wasi-threads")]
use wasmtime_wasi_threads::WasiThreadsCtx;

#[repr(C)]
pub struct wasmtime_linker_t {
pub(crate) linker: Linker<crate::WasmtimeStoreData>,
Expand Down Expand Up @@ -119,6 +123,26 @@ pub extern "C" fn wasmtime_linker_define_wasi(
)
}

#[cfg(feature = "wasi-threads")]
#[unsafe(no_mangle)]
pub extern "C" fn wasmtime_linker_define_wasi_threads(
linker: &mut wasmtime_linker_t,
store: WasmtimeStoreContextMut<'_>,
module: &wasmtime_module_t,
) -> Option<Box<wasmtime_error_t>> {
handle_result(|| {
let linker = &mut linker.linker;
store.data_mut().wasi_threads = Some(Arc::new(WasiThreadsCtx::new(
&module.module.clone(),
Arc::new(linker.clone()),
)?));
wasmtime_wasi_threads::add_to_linker(linker, store, &module.module, |host| {
host.wasi_threads.as_ref().unwrap()
})?;
Ok(())
})
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn wasmtime_linker_define_instance(
linker: &mut wasmtime_linker_t,
Expand Down
Loading