-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #116123 - joboet:rewrite_native_tls, r=m-ou-se
Rewrite native thread-local storage (part of #110897) The current native thread-local storage implementation has become quite messy, uses indescriptive names and unnecessarily adds code to the macro expansion. This PR tries to fix that by using a new implementation that also allows more layout optimizations and potentially increases performance by eliminating unnecessary TLS accesses. This does not change the recursive initialization behaviour I described in [this comment](#110897 (comment)), so it should be a library-only change. Changing that behaviour should be quite easy now, however. r? `@m-ou-se` `@rustbot` label +T-libs
- Loading branch information
Showing
10 changed files
with
331 additions
and
332 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use crate::cell::{Cell, UnsafeCell}; | ||
use crate::ptr::{self, drop_in_place}; | ||
use crate::sys::thread_local::abort_on_dtor_unwind; | ||
use crate::sys::thread_local_dtor::register_dtor; | ||
|
||
#[derive(Clone, Copy)] | ||
enum State { | ||
Initial, | ||
Alive, | ||
Destroyed, | ||
} | ||
|
||
#[allow(missing_debug_implementations)] | ||
pub struct Storage<T> { | ||
state: Cell<State>, | ||
val: UnsafeCell<T>, | ||
} | ||
|
||
impl<T> Storage<T> { | ||
pub const fn new(val: T) -> Storage<T> { | ||
Storage { state: Cell::new(State::Initial), val: UnsafeCell::new(val) } | ||
} | ||
|
||
/// Get a reference to the TLS value. If the TLS variable has been destroyed, | ||
/// `None` is returned. | ||
/// | ||
/// # Safety | ||
/// * The `self` reference must remain valid until the TLS destructor has been | ||
/// run. | ||
/// * The returned reference may only be used until thread destruction occurs | ||
/// and may not be used after reentrant initialization has occurred. | ||
/// | ||
// FIXME(#110897): return NonNull instead of lying about the lifetime. | ||
#[inline] | ||
pub unsafe fn get(&self) -> Option<&'static T> { | ||
match self.state.get() { | ||
// SAFETY: as the state is not `Destroyed`, the value cannot have | ||
// been destroyed yet. The reference fulfills the terms outlined | ||
// above. | ||
State::Alive => unsafe { Some(&*self.val.get()) }, | ||
State::Destroyed => None, | ||
State::Initial => unsafe { self.initialize() }, | ||
} | ||
} | ||
|
||
#[cold] | ||
unsafe fn initialize(&self) -> Option<&'static T> { | ||
// Register the destructor | ||
|
||
// SAFETY: | ||
// * the destructor will be called at thread destruction. | ||
// * the caller guarantees that `self` will be valid until that time. | ||
unsafe { | ||
register_dtor(ptr::from_ref(self).cast_mut().cast(), destroy::<T>); | ||
} | ||
self.state.set(State::Alive); | ||
// SAFETY: as the state is not `Destroyed`, the value cannot have | ||
// been destroyed yet. The reference fulfills the terms outlined | ||
// above. | ||
unsafe { Some(&*self.val.get()) } | ||
} | ||
} | ||
|
||
/// Transition an `Alive` TLS variable into the `Destroyed` state, dropping its | ||
/// value. | ||
/// | ||
/// # Safety | ||
/// * Must only be called at thread destruction. | ||
/// * `ptr` must point to an instance of `Storage` with `Alive` state and be | ||
/// valid for accessing that instance. | ||
unsafe extern "C" fn destroy<T>(ptr: *mut u8) { | ||
// Print a nice abort message if a panic occurs. | ||
abort_on_dtor_unwind(|| { | ||
let storage = unsafe { &*(ptr as *const Storage<T>) }; | ||
// Update the state before running the destructor as it may attempt to | ||
// access the variable. | ||
storage.state.set(State::Destroyed); | ||
unsafe { | ||
drop_in_place(storage.val.get()); | ||
} | ||
}) | ||
} |
Oops, something went wrong.