From 69b9bae57ded527f5fd7f036b6cc1cca0b462ac4 Mon Sep 17 00:00:00 2001 From: Orson Peters Date: Sun, 13 Jul 2025 00:58:12 +0200 Subject: [PATCH 1/2] Guarantee 8 bytes of alignment in Thread::into_raw --- library/std/src/thread/mod.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/library/std/src/thread/mod.rs b/library/std/src/thread/mod.rs index 26b2fb4472436..2552d6316fabf 100644 --- a/library/std/src/thread/mod.rs +++ b/library/std/src/thread/mod.rs @@ -1380,6 +1380,11 @@ where } /// The internal representation of a `Thread` handle +/// +/// We explicitly set the alignment for our guarantee in Thread::into_raw. This +/// allows applications to stuff extra metadata bits into the alignment, which +/// can be rather useful when working with atomics. +#[repr(align(8))] struct Inner { name: Option, id: ThreadId, @@ -1563,7 +1568,8 @@ impl Thread { /// Consumes the `Thread`, returning a raw pointer. /// /// To avoid a memory leak the pointer must be converted - /// back into a `Thread` using [`Thread::from_raw`]. + /// back into a `Thread` using [`Thread::from_raw`]. The pointer is + /// guaranteed to be aligned to at least 8 bytes. /// /// # Examples /// From 6f4d0bdde85f9e2a0e61bb3e16d407540ddc09ba Mon Sep 17 00:00:00 2001 From: Orson Peters Date: Sun, 13 Jul 2025 01:20:29 +0200 Subject: [PATCH 2/2] Tidy --- library/std/src/thread/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/thread/mod.rs b/library/std/src/thread/mod.rs index 2552d6316fabf..73c0925709207 100644 --- a/library/std/src/thread/mod.rs +++ b/library/std/src/thread/mod.rs @@ -1380,7 +1380,7 @@ where } /// The internal representation of a `Thread` handle -/// +/// /// We explicitly set the alignment for our guarantee in Thread::into_raw. This /// allows applications to stuff extra metadata bits into the alignment, which /// can be rather useful when working with atomics.