Skip to content

Commit

Permalink
Rollup merge of #133472 - rust-wasi-web:master, r=joboet
Browse files Browse the repository at this point in the history
Run TLS destructors for wasm32-wasip1-threads

The target wasm32-wasip1-threads has support for pthreads and allows registration of TLS destructors.

For spawned threads, this registers Rust TLS destructors by creating a pthreads key with an attached destructor function.
For the main thread, this registers an `atexit` handler to run the TLS destructors.

try-job: test-various
  • Loading branch information
fmease authored Dec 10, 2024
2 parents b1122b5 + 4f16640 commit ce8d241
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
20 changes: 20 additions & 0 deletions library/std/src/sys/thread_local/key/unix.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
use crate::mem;

// For WASI add a few symbols not in upstream `libc` just yet.
#[cfg(all(target_os = "wasi", target_env = "p1", target_feature = "atomics"))]
mod libc {
use crate::ffi;

#[allow(non_camel_case_types)]
pub type pthread_key_t = ffi::c_uint;

extern "C" {
pub fn pthread_key_create(
key: *mut pthread_key_t,
destructor: unsafe extern "C" fn(*mut ffi::c_void),
) -> ffi::c_int;
#[allow(dead_code)]
pub fn pthread_getspecific(key: pthread_key_t) -> *mut ffi::c_void;
pub fn pthread_setspecific(key: pthread_key_t, value: *const ffi::c_void) -> ffi::c_int;
pub fn pthread_key_delete(key: pthread_key_t) -> ffi::c_int;
}
}

pub type Key = libc::pthread_key_t;

#[inline]
Expand Down
5 changes: 4 additions & 1 deletion library/std/src/sys/thread_local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ pub(crate) mod guard {
mod windows;
pub(crate) use windows::enable;
} else if #[cfg(any(
target_family = "wasm",
all(target_family = "wasm", not(
all(target_os = "wasi", target_env = "p1", target_feature = "atomics")
)),
target_os = "uefi",
target_os = "zkvm",
))] {
Expand Down Expand Up @@ -135,6 +137,7 @@ pub(crate) mod key {
target_family = "unix",
),
target_os = "teeos",
all(target_os = "wasi", target_env = "p1", target_feature = "atomics"),
))] {
mod racy;
mod unix;
Expand Down

0 comments on commit ce8d241

Please sign in to comment.