diff --git a/crates/runtime/src/lib.rs b/crates/runtime/src/lib.rs index 6e5d4f6fb3..2c7aa1f584 100644 --- a/crates/runtime/src/lib.rs +++ b/crates/runtime/src/lib.rs @@ -49,8 +49,8 @@ pub use crate::memory::{Memory, RuntimeLinearMemory, RuntimeMemoryCreator}; pub use crate::mmap::Mmap; pub use crate::table::{Table, TableElement}; pub use crate::traphandlers::{ - catch_traps, init_traps, raise_lib_trap, raise_user_trap, resume_panic, SignalHandler, - TlsRestore, Trap, + catch_traps, init_traps, raise_lib_trap, raise_user_trap, resume_panic, tls_eager_initialize, + SignalHandler, TlsRestore, Trap, }; pub use crate::vmcontext::{ VMCallerCheckedAnyfunc, VMContext, VMFunctionBody, VMFunctionImport, VMGlobalDefinition, diff --git a/crates/runtime/src/traphandlers.rs b/crates/runtime/src/traphandlers.rs index 7332ae0a7e..142d36133b 100644 --- a/crates/runtime/src/traphandlers.rs +++ b/crates/runtime/src/traphandlers.rs @@ -12,7 +12,7 @@ use std::sync::atomic::Ordering::SeqCst; use std::sync::Once; use wasmtime_environ::ir; -pub use self::tls::TlsRestore; +pub use self::tls::{tls_eager_initialize, TlsRestore}; extern "C" { #[allow(improper_ctypes)] @@ -386,12 +386,29 @@ mod tls { }) } + #[inline(never)] + /// Eagerly initialize thread-local runtime functionality. This will be performed + /// lazily by the runtime if users do not perform it eagerly. + pub fn initialize() -> Result<(), Trap> { + PTR.with(|p| { + let (state, mut initialized) = p.get(); + if !initialized { + super::super::sys::lazy_per_thread_init()?; + initialized = true; + } + p.set((state, initialized)); + Ok(()) + }) + } + #[inline(never)] // see module docs for why this is here pub fn get() -> Ptr { PTR.with(|p| p.get().0) } } + pub use raw::initialize as tls_eager_initialize; + /// Opaque state used to help control TLS state across stack switches for /// async support. pub struct TlsRestore(raw::Ptr); diff --git a/crates/wasmtime/src/engine.rs b/crates/wasmtime/src/engine.rs index bce82a8a9f..c367e9425a 100644 --- a/crates/wasmtime/src/engine.rs +++ b/crates/wasmtime/src/engine.rs @@ -1,5 +1,5 @@ use crate::signatures::SignatureRegistry; -use crate::Config; +use crate::{Config, Trap}; use anyhow::Result; use std::sync::Arc; #[cfg(feature = "cache")] @@ -63,6 +63,12 @@ impl Engine { }) } + /// Eagerly initialize thread-local functionality shared by all [`Engine`]s. This + /// will be performed lazily by the runtime if users do not perform it eagerly. + pub fn tls_eager_initialize() -> Result<(), Trap> { + wasmtime_runtime::tls_eager_initialize().map_err(Trap::from_runtime) + } + /// Returns the configuration settings that this engine is using. #[inline] pub fn config(&self) -> &Config {