Skip to content

Commit 5a3cc94

Browse files
kornelskigitbot
authored and
gitbot
committed
Use faster thread_local in current_thread_id()
1 parent 7720fdb commit 5a3cc94

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

std/src/sync/mpmc/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1382,3 +1382,6 @@ impl<T> fmt::Debug for Receiver<T> {
13821382
f.pad("Receiver { .. }")
13831383
}
13841384
}
1385+
1386+
#[cfg(test)]
1387+
mod tests;

std/src/sync/mpmc/tests.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Ensure that thread_local init with `const { 0 }` still has unique address at run-time
2+
#[test]
3+
fn waker_current_thread_id() {
4+
let first = super::waker::current_thread_id();
5+
let t = crate::thread::spawn(move || {
6+
let second = super::waker::current_thread_id();
7+
assert_ne!(first, second);
8+
assert_eq!(second, super::waker::current_thread_id());
9+
});
10+
11+
assert_eq!(first, super::waker::current_thread_id());
12+
t.join().unwrap();
13+
assert_eq!(first, super::waker::current_thread_id());
14+
}

std/src/sync/mpmc/waker.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,6 @@ impl Drop for SyncWaker {
204204
pub fn current_thread_id() -> usize {
205205
// `u8` is not drop so this variable will be available during thread destruction,
206206
// whereas `thread::current()` would not be
207-
thread_local! { static DUMMY: u8 = 0 }
207+
thread_local! { static DUMMY: u8 = const { 0 } }
208208
DUMMY.with(|x| (x as *const u8).addr())
209209
}

0 commit comments

Comments
 (0)