Skip to content

Commit

Permalink
std: complete unifying the TLS destructor list implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
joboet committed Feb 16, 2024
1 parent 2e425cf commit 5e1727c
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 80 deletions.
6 changes: 3 additions & 3 deletions library/std/src/sys/pal/hermit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion library/std/src/sys/pal/hermit/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,7 +51,7 @@ impl Thread {
Box::from_raw(ptr::from_exposed_addr::<Box<dyn FnOnce()>>(main).cast_mut())();

// run all destructors
run_dtors();
run_dtors(ptr::null_mut());
}
}
}
Expand Down
29 changes: 0 additions & 29 deletions library/std/src/sys/pal/hermit/thread_local_dtor.rs

This file was deleted.

6 changes: 6 additions & 0 deletions library/std/src/sys/pal/hermit/thread_local_guard.rs
Original file line number Diff line number Diff line change
@@ -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.
}
6 changes: 3 additions & 3 deletions library/std/src/sys/pal/itron/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/solid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 0 additions & 43 deletions library/std/src/sys/pal/solid/thread_local_dtor.rs

This file was deleted.

21 changes: 21 additions & 0 deletions library/std/src/sys/pal/solid/thread_local_guard.rs
Original file line number Diff line number Diff line change
@@ -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<bool> = 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);
}
}

0 comments on commit 5e1727c

Please sign in to comment.