Skip to content

Commit bcf0b88

Browse files
committed
Cache tid in futex tls
1 parent a360d54 commit bcf0b88

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

library/std/src/sys/pal/unix/pi_futex.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
use crate::cell::Cell;
2+
use crate::thread_local;
3+
4+
thread_local! {
5+
static TID: Cell<u32> = Cell::new(0);
6+
}
7+
18
#[cfg(any(target_os = "linux", target_os = "android"))]
29
mod linux {
10+
use super::TID;
311
use crate::ops::Deref;
412
use crate::sync::atomic::AtomicU32;
513
use crate::sys::cvt;
@@ -27,7 +35,14 @@ mod linux {
2735
}
2836

2937
pub fn locked() -> State {
30-
(unsafe { libc::gettid() }) as _
38+
let tid = TID.get();
39+
if tid == 0 {
40+
let tid = (unsafe { libc::gettid() }) as u32;
41+
TID.set(tid);
42+
tid
43+
} else {
44+
tid
45+
}
3146
}
3247

3348
pub fn is_contended(futex_val: State) -> bool {
@@ -75,6 +90,7 @@ pub use linux::*;
7590

7691
#[cfg(target_os = "freebsd")]
7792
mod freebsd {
93+
use super::TID;
7894
use crate::mem::transmute;
7995
use crate::ops::Deref;
8096
use crate::sync::atomic::AtomicU32;
@@ -125,9 +141,16 @@ mod freebsd {
125141
}
126142

127143
pub fn locked() -> State {
128-
let mut tid: libc::c_long = 0;
129-
let _ = unsafe { libc::thr_self(ptr::from_mut(&mut tid)) };
130-
tid as _
144+
let tid = TID.get();
145+
if tid == 0 {
146+
let mut tid: libc::c_long = 0;
147+
let _ = unsafe { libc::thr_self(ptr::from_mut(&mut tid)) };
148+
let tid = tid as u32;
149+
TID.set(tid);
150+
tid
151+
} else {
152+
tid
153+
}
131154
}
132155

133156
pub fn is_contended(futex_val: State) -> bool {

0 commit comments

Comments
 (0)