From a2d41393365df0c0c9f728de7f79b8f0d4e14ef2 Mon Sep 17 00:00:00 2001 From: Orson Peters Date: Sun, 13 Jul 2025 12:16:40 +0200 Subject: [PATCH 1/2] Use zero for initialized Once state --- library/std/src/sys/sync/once/futex.rs | 8 ++++---- library/std/src/sys/sync/once/queue.rs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/library/std/src/sys/sync/once/futex.rs b/library/std/src/sys/sync/once/futex.rs index 539f0fe89eaaa..827bbf014cd61 100644 --- a/library/std/src/sys/sync/once/futex.rs +++ b/library/std/src/sys/sync/once/futex.rs @@ -8,16 +8,16 @@ use crate::sys::futex::{Futex, Primitive, futex_wait, futex_wake_all}; // This means we only need one atomic value with 4 states: /// No initialization has run yet, and no thread is currently using the Once. -const INCOMPLETE: Primitive = 0; +const INCOMPLETE: Primitive = 3; /// Some thread has previously attempted to initialize the Once, but it panicked, /// so the Once is now poisoned. There are no other threads currently accessing /// this Once. -const POISONED: Primitive = 1; +const POISONED: Primitive = 2; /// Some thread is currently attempting to run initialization. It may succeed, /// so all future threads need to wait for it to finish. -const RUNNING: Primitive = 2; +const RUNNING: Primitive = 1; /// Initialization has completed and all future calls should finish immediately. -const COMPLETE: Primitive = 3; +const COMPLETE: Primitive = 0; // An additional bit indicates whether there are waiting threads: diff --git a/library/std/src/sys/sync/once/queue.rs b/library/std/src/sys/sync/once/queue.rs index 6a2ab0dcf1b33..7bebb6696a877 100644 --- a/library/std/src/sys/sync/once/queue.rs +++ b/library/std/src/sys/sync/once/queue.rs @@ -75,10 +75,10 @@ pub struct OnceState { // Four states that a Once can be in, encoded into the lower bits of // `state_and_queue` in the Once structure. -const INCOMPLETE: usize = 0x0; -const POISONED: usize = 0x1; -const RUNNING: usize = 0x2; -const COMPLETE: usize = 0x3; +const INCOMPLETE: usize = 0x3; +const POISONED: usize = 0x2; +const RUNNING: usize = 0x1; +const COMPLETE: usize = 0x0; // Mask to learn about the state. All other bits are the queue of waiters if // this is in the RUNNING state. From f041962694875c0f0a33f8a5e27fd00597042169 Mon Sep 17 00:00:00 2001 From: Orson Peters Date: Sun, 13 Jul 2025 12:37:34 +0200 Subject: [PATCH 2/2] Add comment why we use zero for COMPLETE --- library/std/src/sys/sync/once/futex.rs | 2 ++ library/std/src/sys/sync/once/queue.rs | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/library/std/src/sys/sync/once/futex.rs b/library/std/src/sys/sync/once/futex.rs index 827bbf014cd61..407fdcebcf5cc 100644 --- a/library/std/src/sys/sync/once/futex.rs +++ b/library/std/src/sys/sync/once/futex.rs @@ -17,6 +17,8 @@ const POISONED: Primitive = 2; /// so all future threads need to wait for it to finish. const RUNNING: Primitive = 1; /// Initialization has completed and all future calls should finish immediately. +/// By choosing this state as the all-zero state the `is_completed` check can be +/// a bit faster on some platforms. const COMPLETE: Primitive = 0; // An additional bit indicates whether there are waiting threads: diff --git a/library/std/src/sys/sync/once/queue.rs b/library/std/src/sys/sync/once/queue.rs index 7bebb6696a877..49e15d65f25a2 100644 --- a/library/std/src/sys/sync/once/queue.rs +++ b/library/std/src/sys/sync/once/queue.rs @@ -74,7 +74,8 @@ pub struct OnceState { } // Four states that a Once can be in, encoded into the lower bits of -// `state_and_queue` in the Once structure. +// `state_and_queue` in the Once structure. By choosing COMPLETE as the all-zero +// state the `is_completed` check can be a bit faster on some platforms. const INCOMPLETE: usize = 0x3; const POISONED: usize = 0x2; const RUNNING: usize = 0x1;