From 44ca73a0e84dd4f463f2487c12722906041ac7e9 Mon Sep 17 00:00:00 2001 From: Matt McCormick Date: Wed, 15 Jan 2025 23:21:02 -0500 Subject: [PATCH] WIP: Add wasmtime_linker_define_wasi_threads to the c-api To define `wasi_threads` on the linker to support rrunning wasm threads. --- Cargo.lock | 1 + crates/c-api/Cargo.toml | 2 ++ crates/c-api/artifact/Cargo.toml | 2 ++ crates/c-api/build.rs | 1 + crates/c-api/cmake/features.cmake | 1 + crates/c-api/include/wasmtime/conf.h.in | 1 + crates/c-api/src/linker.rs | 24 ++++++++++++++++++++++++ 7 files changed, 32 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index bfb0a2df0331..f3059902d05c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4056,6 +4056,7 @@ dependencies = [ "wasmtime", "wasmtime-c-api-macros", "wasmtime-wasi", + "wasmtime-wasi-threads", "wat", ] diff --git a/crates/c-api/Cargo.toml b/crates/c-api/Cargo.toml index d77312f42788..93f1f29526f5 100644 --- a/crates/c-api/Cargo.toml +++ b/crates/c-api/Cargo.toml @@ -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 } @@ -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 diff --git a/crates/c-api/artifact/Cargo.toml b/crates/c-api/artifact/Cargo.toml index 8aa846d23344..b5a0ff3bed03 100644 --- a/crates/c-api/artifact/Cargo.toml +++ b/crates/c-api/artifact/Cargo.toml @@ -26,6 +26,7 @@ default = [ 'profiling', 'wat', 'wasi', + 'wasi-threads', 'cache', 'parallel-compilation', 'async', @@ -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"] diff --git a/crates/c-api/build.rs b/crates/c-api/build.rs index 12bc0b5016e8..b447840251d1 100644 --- a/crates/c-api/build.rs +++ b/crates/c-api/build.rs @@ -8,6 +8,7 @@ const FEATURES: &[&str] = &[ "CACHE", "PARALLEL_COMPILATION", "WASI", + "WASI_THREADS", "LOGGING", "DISABLE_LOGGING", "COREDUMP", diff --git a/crates/c-api/cmake/features.cmake b/crates/c-api/cmake/features.cmake index 727412a0433c..a80f8ef4fe75 100644 --- a/crates/c-api/cmake/features.cmake +++ b/crates/c-api/cmake/features.cmake @@ -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) diff --git a/crates/c-api/include/wasmtime/conf.h.in b/crates/c-api/include/wasmtime/conf.h.in index 03f4debf92d1..f5b5aae814e5 100644 --- a/crates/c-api/include/wasmtime/conf.h.in +++ b/crates/c-api/include/wasmtime/conf.h.in @@ -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 diff --git a/crates/c-api/src/linker.rs b/crates/c-api/src/linker.rs index 28fa087153ae..224a58bcd068 100644 --- a/crates/c-api/src/linker.rs +++ b/crates/c-api/src/linker.rs @@ -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, @@ -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> { + 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,