Skip to content

Commit cf4669e

Browse files
committed
Also apply opt to OS-specific TLS impls
1 parent db7b096 commit cf4669e

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

library/std/src/sys/thread_local/key/racy.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ impl LazyKey {
3434
LazyKey { key: AtomicUsize::new(KEY_SENTVAL), dtor }
3535
}
3636

37-
#[inline]
37+
#[inline(always)]
3838
pub fn force(&self) -> super::Key {
3939
match self.key.load(Ordering::Acquire) {
4040
KEY_SENTVAL => self.lazy_init() as super::Key,
4141
n => n as super::Key,
4242
}
4343
}
4444

45+
#[cold]
46+
#[inline(never)]
4547
fn lazy_init(&self) -> usize {
4648
// POSIX allows the key created here to be KEY_SENTVAL, but the compare_exchange
4749
// below relies on using KEY_SENTVAL as a sentinel value to check who won the

library/std/src/sys/thread_local/key/windows.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl LazyKey {
5858
}
5959
}
6060

61-
#[inline]
61+
#[inline(always)]
6262
pub fn force(&'static self) -> Key {
6363
match self.key.load(Acquire) {
6464
0 => unsafe { self.init() },
@@ -67,6 +67,7 @@ impl LazyKey {
6767
}
6868

6969
#[cold]
70+
#[inline(never)]
7071
unsafe fn init(&'static self) -> Key {
7172
if self.dtor.is_some() {
7273
let mut pending = c::FALSE;

library/std/src/sys/thread_local/key/xous.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ unsafe extern "Rust" {
6767
static DTORS: Atomic<*mut Node>;
6868
}
6969

70+
#[inline]
7071
fn tls_ptr_addr() -> *mut *mut u8 {
7172
let mut tp: usize;
7273
unsafe {
@@ -80,14 +81,20 @@ fn tls_ptr_addr() -> *mut *mut u8 {
8081

8182
/// Creates an area of memory that's unique per thread. This area will
8283
/// contain all thread local pointers.
84+
#[inline]
8385
fn tls_table() -> &'static mut [*mut u8] {
8486
let tp = tls_ptr_addr();
8587

8688
if !tp.is_null() {
87-
return unsafe {
88-
core::slice::from_raw_parts_mut(tp, TLS_MEMORY_SIZE / size_of::<*mut u8>())
89-
};
89+
unsafe { core::slice::from_raw_parts_mut(tp, TLS_MEMORY_SIZE / size_of::<*mut u8>()) }
90+
} else {
91+
tls_table_slow()
9092
}
93+
}
94+
95+
#[cold]
96+
#[inline(never)]
97+
fn tls_table_slow() -> &'static mut [*mut u8] {
9198
// If the TP register is `0`, then this thread hasn't initialized
9299
// its TLS yet. Allocate a new page to store this memory.
93100
let tp = unsafe {

library/std/src/sys/thread_local/os.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ impl<T: 'static> Storage<T> {
6868
///
6969
/// The resulting pointer may not be used after reentrant inialialization
7070
/// or thread destruction has occurred.
71+
#[inline]
7172
pub fn get(&'static self, i: Option<&mut Option<T>>, f: impl FnOnce() -> T) -> *const T {
7273
let key = self.key.force();
7374
let ptr = unsafe { get(key) as *mut Value<T> };
@@ -84,6 +85,8 @@ impl<T: 'static> Storage<T> {
8485
/// # Safety
8586
/// * `key` must be the result of calling `self.key.force()`
8687
/// * `ptr` must be the current value associated with `key`.
88+
#[cold]
89+
#[inline(never)]
8790
unsafe fn try_initialize(
8891
key: Key,
8992
ptr: *mut Value<T>,

0 commit comments

Comments
 (0)