Skip to content

Commit

Permalink
Rollup merge of rust-lang#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.
  • Loading branch information
fmease authored Dec 4, 2024
2 parents ae59014 + 4fe15b0 commit 79abe24
Show file tree
Hide file tree
Showing 2 changed files with 22 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(target_os = "wasi")]
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
3 changes: 2 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,7 @@ pub(crate) mod guard {
mod windows;
pub(crate) use windows::enable;
} else if #[cfg(any(
target_family = "wasm",
all(target_family = "wasm", not(target_os="wasi")),
target_os = "uefi",
target_os = "zkvm",
))] {
Expand Down Expand Up @@ -135,6 +135,7 @@ pub(crate) mod key {
target_family = "unix",
),
target_os = "teeos",
target_os = "wasi",
))] {
mod racy;
mod unix;
Expand Down

0 comments on commit 79abe24

Please sign in to comment.