Skip to content

Commit 8082fb9

Browse files
committed
rename fast_thread_local -> thread_local_dtor; thread_local -> thread_local_key
1 parent daecab3 commit 8082fb9

10 files changed

+65
-45
lines changed

src/libstd/sys/unix/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ pub mod cmath;
4747
pub mod condvar;
4848
pub mod env;
4949
pub mod ext;
50-
pub mod fast_thread_local;
5150
pub mod fd;
5251
pub mod fs;
5352
pub mod io;
@@ -68,7 +67,8 @@ pub mod rwlock;
6867
pub mod stack_overflow;
6968
pub mod stdio;
7069
pub mod thread;
71-
pub mod thread_local;
70+
pub mod thread_local_key;
71+
pub mod thread_local_dtor;
7272
pub mod time;
7373

7474
pub use crate::sys_common::os_str_bytes as os_str;

src/libstd/sys/unix/fast_thread_local.rs src/libstd/sys/unix/thread_local_dtor.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#![cfg(target_thread_local)]
22
#![unstable(feature = "thread_local_internals", issue = "none")]
33

4+
//! Provides thread-local destructors without an associated "key", which
5+
//! can be more efficient.
6+
47
// Since what appears to be glibc 2.18 this symbol has been shipped which
58
// GCC and clang both use to invoke destructors in thread_local globals, so
69
// let's do the same!
@@ -16,7 +19,7 @@
1619
))]
1720
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
1821
use crate::mem;
19-
use crate::sys_common::thread_local::register_dtor_fallback;
22+
use crate::sys_common::thread_local_dtor::register_dtor_fallback;
2023

2124
extern "C" {
2225
#[linkage = "extern_weak"]
File renamed without changes.

src/libstd/sys/windows/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub mod cmath;
2020
pub mod condvar;
2121
pub mod env;
2222
pub mod ext;
23-
pub mod fast_thread_local;
2423
pub mod fs;
2524
pub mod handle;
2625
pub mod io;
@@ -35,7 +34,8 @@ pub mod process;
3534
pub mod rand;
3635
pub mod rwlock;
3736
pub mod thread;
38-
pub mod thread_local;
37+
pub mod thread_local_key;
38+
pub mod thread_local_dtor;
3939
pub mod time;
4040
cfg_if::cfg_if! {
4141
if #[cfg(not(target_vendor = "uwp"))] {
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#![unstable(feature = "thread_local_internals", issue = "none")]
22
#![cfg(target_thread_local)]
33

4-
pub use crate::sys_common::thread_local::register_dtor_fallback as register_dtor;
4+
pub use crate::sys_common::thread_local_dtor::register_dtor_fallback as register_dtor;

src/libstd/sys_common/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ pub mod remutex;
6565
pub mod rwlock;
6666
pub mod thread;
6767
pub mod thread_info;
68-
pub mod thread_local;
68+
pub mod thread_local_key;
69+
pub mod thread_local_dtor;
6970
pub mod util;
7071
pub mod wtf8;
7172

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//! Thread-local destructor
2+
//!
3+
//! Besides thread-local "keys" (pointer-sized non-adressable thread-local store
4+
//! with an associated destructor), many platforms also provide thread-local
5+
//! destructors that are not associated with any particular data. These are
6+
//! often more efficient.
7+
//!
8+
//! This module provides a fallback implementation for that interface, based
9+
//! on the less efficient thread-local "keys". Each platform provides
10+
//! a `thread_local_dtor` module which will either re-export the fallback,
11+
//! or implement something more efficient.
12+
13+
#![unstable(feature = "thread_local_internals", issue = "none")]
14+
#![allow(dead_code)] // sys isn't exported yet
15+
16+
use crate::ptr;
17+
use crate::sys_common::thread_local_key::StaticKey;
18+
19+
pub unsafe fn register_dtor_fallback(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
20+
// The fallback implementation uses a vanilla OS-based TLS key to track
21+
// the list of destructors that need to be run for this thread. The key
22+
// then has its own destructor which runs all the other destructors.
23+
//
24+
// The destructor for DTORS is a little special in that it has a `while`
25+
// loop to continuously drain the list of registered destructors. It
26+
// *should* be the case that this loop always terminates because we
27+
// provide the guarantee that a TLS key cannot be set after it is
28+
// flagged for destruction.
29+
30+
static DTORS: StaticKey = StaticKey::new(Some(run_dtors));
31+
type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
32+
if DTORS.get().is_null() {
33+
let v: Box<List> = box Vec::new();
34+
DTORS.set(Box::into_raw(v) as *mut u8);
35+
}
36+
let list: &mut List = &mut *(DTORS.get() as *mut List);
37+
list.push((t, dtor));
38+
39+
unsafe extern "C" fn run_dtors(mut ptr: *mut u8) {
40+
while !ptr.is_null() {
41+
let list: Box<List> = Box::from_raw(ptr as *mut List);
42+
for (ptr, dtor) in list.into_iter() {
43+
dtor(ptr);
44+
}
45+
ptr = DTORS.get();
46+
DTORS.set(ptr::null_mut());
47+
}
48+
}
49+
}

src/libstd/sys_common/thread_local.rs src/libstd/sys_common/thread_local_key.rs

+3-36
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! using the native OS-provided facilities (think `TlsAlloc` or
55
//! `pthread_setspecific`). The interface of this differs from the other types
66
//! of thread-local-storage provided in this crate in that OS-based TLS can only
7-
//! get/set pointers,
7+
//! get/set pointer-sized data, possibly with an associated destructor.
88
//!
99
//! This module also provides two flavors of TLS. One is intended for static
1010
//! initialization, and does not contain a `Drop` implementation to deallocate
@@ -14,7 +14,7 @@
1414
//! # Usage
1515
//!
1616
//! This module should likely not be used directly unless other primitives are
17-
//! being built on. types such as `thread_local::spawn::Key` are likely much
17+
//! being built on. Types such as `thread_local::spawn::Key` are likely much
1818
//! more useful in practice than this OS-based version which likely requires
1919
//! unsafe code to interoperate with.
2020
//!
@@ -48,9 +48,8 @@
4848
#![unstable(feature = "thread_local_internals", issue = "none")]
4949
#![allow(dead_code)] // sys isn't exported yet
5050

51-
use crate::ptr;
5251
use crate::sync::atomic::{self, AtomicUsize, Ordering};
53-
use crate::sys::thread_local as imp;
52+
use crate::sys::thread_local_key as imp;
5453
use crate::sys_common::mutex::Mutex;
5554

5655
/// A type for TLS keys that are statically allocated.
@@ -233,38 +232,6 @@ impl Drop for Key {
233232
}
234233
}
235234

236-
pub unsafe fn register_dtor_fallback(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
237-
// The fallback implementation uses a vanilla OS-based TLS key to track
238-
// the list of destructors that need to be run for this thread. The key
239-
// then has its own destructor which runs all the other destructors.
240-
//
241-
// The destructor for DTORS is a little special in that it has a `while`
242-
// loop to continuously drain the list of registered destructors. It
243-
// *should* be the case that this loop always terminates because we
244-
// provide the guarantee that a TLS key cannot be set after it is
245-
// flagged for destruction.
246-
247-
static DTORS: StaticKey = StaticKey::new(Some(run_dtors));
248-
type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
249-
if DTORS.get().is_null() {
250-
let v: Box<List> = box Vec::new();
251-
DTORS.set(Box::into_raw(v) as *mut u8);
252-
}
253-
let list: &mut List = &mut *(DTORS.get() as *mut List);
254-
list.push((t, dtor));
255-
256-
unsafe extern "C" fn run_dtors(mut ptr: *mut u8) {
257-
while !ptr.is_null() {
258-
let list: Box<List> = Box::from_raw(ptr as *mut List);
259-
for (ptr, dtor) in list.into_iter() {
260-
dtor(ptr);
261-
}
262-
ptr = DTORS.get();
263-
DTORS.set(ptr::null_mut());
264-
}
265-
}
266-
}
267-
268235
#[cfg(test)]
269236
mod tests {
270237
use super::{Key, StaticKey};

src/libstd/thread/local.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ pub mod fast {
363363
use crate::cell::Cell;
364364
use crate::fmt;
365365
use crate::mem;
366-
use crate::sys::fast_thread_local::register_dtor;
366+
use crate::sys::thread_local_dtor::register_dtor;
367367

368368
#[derive(Copy, Clone)]
369369
enum DtorState {
@@ -468,7 +468,7 @@ pub mod os {
468468
use crate::fmt;
469469
use crate::marker;
470470
use crate::ptr;
471-
use crate::sys_common::thread_local::StaticKey as OsStaticKey;
471+
use crate::sys_common::thread_local_key::StaticKey as OsStaticKey;
472472

473473
pub struct Key<T> {
474474
// OS-TLS key that we'll use to key off.

0 commit comments

Comments
 (0)