From 5e1727c7387618b4a8b722ad9f7cfb8d3b6d2d9f Mon Sep 17 00:00:00 2001 From: joboet Date: Tue, 17 Oct 2023 17:45:22 +0200 Subject: [PATCH] std: complete unifying the TLS destructor list implementations --- library/std/src/sys/pal/hermit/mod.rs | 6 +-- library/std/src/sys/pal/hermit/thread.rs | 3 +- .../src/sys/pal/hermit/thread_local_dtor.rs | 29 ------------- .../src/sys/pal/hermit/thread_local_guard.rs | 6 +++ library/std/src/sys/pal/itron/thread.rs | 6 +-- library/std/src/sys/pal/solid/mod.rs | 2 +- .../src/sys/pal/solid/thread_local_dtor.rs | 43 ------------------- .../src/sys/pal/solid/thread_local_guard.rs | 21 +++++++++ 8 files changed, 36 insertions(+), 80 deletions(-) delete mode 100644 library/std/src/sys/pal/hermit/thread_local_dtor.rs create mode 100644 library/std/src/sys/pal/hermit/thread_local_guard.rs delete mode 100644 library/std/src/sys/pal/solid/thread_local_dtor.rs create mode 100644 library/std/src/sys/pal/solid/thread_local_guard.rs diff --git a/library/std/src/sys/pal/hermit/mod.rs b/library/std/src/sys/pal/hermit/mod.rs index 57cc656e266a1..c0956499bc85f 100644 --- a/library/std/src/sys/pal/hermit/mod.rs +++ b/library/std/src/sys/pal/hermit/mod.rs @@ -34,7 +34,7 @@ pub mod pipe; pub mod process; pub mod stdio; pub mod thread; -pub mod thread_local_dtor; +pub mod thread_local_guard; #[path = "../unsupported/thread_local_key.rs"] pub mod thread_local_key; pub mod time; @@ -109,7 +109,7 @@ pub unsafe extern "C" fn runtime_entry( argv: *const *const c_char, env: *const *const c_char, ) -> ! { - use thread_local_dtor::run_dtors; + use crate::sys::common::thread_local::run_dtors; extern "C" { fn main(argc: isize, argv: *const *const c_char) -> i32; } @@ -119,7 +119,7 @@ pub unsafe extern "C" fn runtime_entry( let result = main(argc as isize, argv); - run_dtors(); + run_dtors(crate::ptr::null_mut()); abi::exit(result); } diff --git a/library/std/src/sys/pal/hermit/thread.rs b/library/std/src/sys/pal/hermit/thread.rs index fee80c02d4a6f..5928d0941fa3f 100644 --- a/library/std/src/sys/pal/hermit/thread.rs +++ b/library/std/src/sys/pal/hermit/thread.rs @@ -7,6 +7,7 @@ use crate::io; use crate::mem; use crate::num::NonZero; use crate::ptr; +use crate::sys::common::thread_local::run_dtors; use crate::time::Duration; pub type Tid = abi::Tid; @@ -50,7 +51,7 @@ impl Thread { Box::from_raw(ptr::from_exposed_addr::>(main).cast_mut())(); // run all destructors - run_dtors(); + run_dtors(ptr::null_mut()); } } } diff --git a/library/std/src/sys/pal/hermit/thread_local_dtor.rs b/library/std/src/sys/pal/hermit/thread_local_dtor.rs deleted file mode 100644 index 98adaf4bff1aa..0000000000000 --- a/library/std/src/sys/pal/hermit/thread_local_dtor.rs +++ /dev/null @@ -1,29 +0,0 @@ -#![cfg(target_thread_local)] -#![unstable(feature = "thread_local_internals", issue = "none")] - -// Simplify dtor registration by using a list of destructors. -// The this solution works like the implementation of macOS and -// doesn't additional OS support - -use crate::cell::RefCell; - -#[thread_local] -static DTORS: RefCell> = RefCell::new(Vec::new()); - -pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) { - match DTORS.try_borrow_mut() { - Ok(mut dtors) => dtors.push((t, dtor)), - Err(_) => rtabort!("global allocator may not use TLS"), - } -} - -// every thread call this function to run through all possible destructors -pub unsafe fn run_dtors() { - let mut list = DTORS.take(); - while !list.is_empty() { - for (ptr, dtor) in list { - dtor(ptr); - } - list = DTORS.take(); - } -} diff --git a/library/std/src/sys/pal/hermit/thread_local_guard.rs b/library/std/src/sys/pal/hermit/thread_local_guard.rs new file mode 100644 index 0000000000000..16d4cb1b91056 --- /dev/null +++ b/library/std/src/sys/pal/hermit/thread_local_guard.rs @@ -0,0 +1,6 @@ +#![cfg(target_thread_local)] +#![unstable(feature = "thread_local_internals", issue = "none")] + +pub fn activate() { + // run_dtors is always executed by the threading support. +} diff --git a/library/std/src/sys/pal/itron/thread.rs b/library/std/src/sys/pal/itron/thread.rs index 9c1387bf4083a..4aec8c9130510 100644 --- a/library/std/src/sys/pal/itron/thread.rs +++ b/library/std/src/sys/pal/itron/thread.rs @@ -12,9 +12,9 @@ use crate::{ hint, io, mem::ManuallyDrop, num::NonZero, - ptr::NonNull, + ptr::{self, NonNull}, sync::atomic::{AtomicUsize, Ordering}, - sys::thread_local_dtor::run_dtors, + sys::common::thread_local::run_dtors, time::Duration, }; @@ -116,7 +116,7 @@ impl Thread { // Run TLS destructors now because they are not // called automatically for terminated tasks. - unsafe { run_dtors() }; + unsafe { run_dtors(ptr::null_mut()) }; let old_lifecycle = inner .lifecycle diff --git a/library/std/src/sys/pal/solid/mod.rs b/library/std/src/sys/pal/solid/mod.rs index be8e00339021f..fc5d55c758c23 100644 --- a/library/std/src/sys/pal/solid/mod.rs +++ b/library/std/src/sys/pal/solid/mod.rs @@ -36,7 +36,7 @@ pub mod process; pub mod stdio; pub use self::itron::thread; pub mod memchr; -pub mod thread_local_dtor; +pub mod thread_local_guard; pub mod thread_local_key; pub use self::itron::thread_parking; pub mod time; diff --git a/library/std/src/sys/pal/solid/thread_local_dtor.rs b/library/std/src/sys/pal/solid/thread_local_dtor.rs deleted file mode 100644 index 26918a4fcb012..0000000000000 --- a/library/std/src/sys/pal/solid/thread_local_dtor.rs +++ /dev/null @@ -1,43 +0,0 @@ -#![cfg(target_thread_local)] -#![unstable(feature = "thread_local_internals", issue = "none")] - -// Simplify dtor registration by using a list of destructors. - -use super::{abi, itron::task}; -use crate::cell::{Cell, RefCell}; - -#[thread_local] -static REGISTERED: Cell = Cell::new(false); - -#[thread_local] -static DTORS: RefCell> = RefCell::new(Vec::new()); - -pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) { - if !REGISTERED.get() { - let tid = task::current_task_id_aborting(); - // Register `tls_dtor` to make sure the TLS destructors are called - // for tasks created by other means than `std::thread` - unsafe { abi::SOLID_TLS_AddDestructor(tid as i32, tls_dtor) }; - REGISTERED.set(true); - } - - match DTORS.try_borrow_mut() { - Ok(mut dtors) => dtors.push((t, dtor)), - Err(_) => rtabort!("global allocator may not use TLS"), - } -} - -pub unsafe fn run_dtors() { - let mut list = DTORS.take(); - while !list.is_empty() { - for (ptr, dtor) in list { - unsafe { dtor(ptr) }; - } - - list = DTORS.take(); - } -} - -unsafe extern "C" fn tls_dtor(_unused: *mut u8) { - unsafe { run_dtors() }; -} diff --git a/library/std/src/sys/pal/solid/thread_local_guard.rs b/library/std/src/sys/pal/solid/thread_local_guard.rs new file mode 100644 index 0000000000000..986d40aa5fc75 --- /dev/null +++ b/library/std/src/sys/pal/solid/thread_local_guard.rs @@ -0,0 +1,21 @@ +//! Ensures that thread-local destructors are run on thread exit. + +#![cfg(target_thread_local)] +#![unstable(feature = "thread_local_internals", issue = "none")] + +use super::{abi, itron::task}; +use crate::cell::Cell; +use crate::sys::common::thread_local::run_dtors; + +#[thread_local] +static REGISTERED: Cell = Cell::new(false); + +pub fn activate() { + if !REGISTERED.get() { + let tid = task::current_task_id_aborting(); + // Register `tls_dtor` to make sure the TLS destructors are called + // for tasks created by other means than `std::thread` + unsafe { abi::SOLID_TLS_AddDestructor(tid as i32, run_dtors) }; + REGISTERED.set(true); + } +}