forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#74263 - RalfJung:thread-local, r=Mark-Simul…
…acrum Slight reorganization of sys/(fast_)thread_local I was long confused by the `thread_local` and `fast_thread_local` modules in the `sys(_common)` part of libstd. The names make it *sound* like `fast_thread_local` is just a faster version of `thread_local`, but really these are totally different APIs: one provides thread-local "keys", which are non-addressable pointer-sized pieces of local storage with an associated destructor; the other (the "fast" one) provides just a destructor. So I propose we rename `fast_thread_local` to `thread_local_dtor`, and `thread_local` to `thread_local_key`. That's what this PR does.
- Loading branch information
Showing
23 changed files
with
83 additions
and
67 deletions.
There are no files selected for viewing
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
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
File renamed without changes.
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
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
File renamed without changes.
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
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
File renamed without changes.
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
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
File renamed without changes.
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
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
File renamed without changes.
File renamed without changes.
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
2 changes: 1 addition & 1 deletion
2
src/libstd/sys/windows/fast_thread_local.rs → src/libstd/sys/windows/thread_local_dtor.rs
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#![unstable(feature = "thread_local_internals", issue = "none")] | ||
#![cfg(target_thread_local)] | ||
|
||
pub use crate::sys_common::thread_local::register_dtor_fallback as register_dtor; | ||
pub use crate::sys_common::thread_local_dtor::register_dtor_fallback as register_dtor; |
File renamed without changes.
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
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,49 @@ | ||
//! Thread-local destructor | ||
//! | ||
//! Besides thread-local "keys" (pointer-sized non-adressable thread-local store | ||
//! with an associated destructor), many platforms also provide thread-local | ||
//! destructors that are not associated with any particular data. These are | ||
//! often more efficient. | ||
//! | ||
//! This module provides a fallback implementation for that interface, based | ||
//! on the less efficient thread-local "keys". Each platform provides | ||
//! a `thread_local_dtor` module which will either re-export the fallback, | ||
//! or implement something more efficient. | ||
|
||
#![unstable(feature = "thread_local_internals", issue = "none")] | ||
#![allow(dead_code)] // sys isn't exported yet | ||
|
||
use crate::ptr; | ||
use crate::sys_common::thread_local_key::StaticKey; | ||
|
||
pub unsafe fn register_dtor_fallback(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) { | ||
// The fallback implementation uses a vanilla OS-based TLS key to track | ||
// the list of destructors that need to be run for this thread. The key | ||
// then has its own destructor which runs all the other destructors. | ||
// | ||
// The destructor for DTORS is a little special in that it has a `while` | ||
// loop to continuously drain the list of registered destructors. It | ||
// *should* be the case that this loop always terminates because we | ||
// provide the guarantee that a TLS key cannot be set after it is | ||
// flagged for destruction. | ||
|
||
static DTORS: StaticKey = StaticKey::new(Some(run_dtors)); | ||
type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>; | ||
if DTORS.get().is_null() { | ||
let v: Box<List> = box Vec::new(); | ||
DTORS.set(Box::into_raw(v) as *mut u8); | ||
} | ||
let list: &mut List = &mut *(DTORS.get() as *mut List); | ||
list.push((t, dtor)); | ||
|
||
unsafe extern "C" fn run_dtors(mut ptr: *mut u8) { | ||
while !ptr.is_null() { | ||
let list: Box<List> = Box::from_raw(ptr as *mut List); | ||
for (ptr, dtor) in list.into_iter() { | ||
dtor(ptr); | ||
} | ||
ptr = DTORS.get(); | ||
DTORS.set(ptr::null_mut()); | ||
} | ||
} | ||
} |
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
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