From 7232830d10b6af772e0e4670a2ff61dd23830ed8 Mon Sep 17 00:00:00 2001 From: Valentyn Kit Date: Fri, 3 Jul 2026 11:45:09 +0300 Subject: [PATCH 1/2] std: move futex implementations into sys::sync::futex Pure file moves; the module path repointing and platform gating follow in the next commit. Recorded in .git-blame-ignore-revs so blame skips the rename. --- library/std/src/sys/{pal/hermit/futex.rs => sync/futex/hermit.rs} | 0 library/std/src/sys/{pal/unix/futex.rs => sync/futex/unix.rs} | 0 .../sys/{pal/wasi/wasilibc_futex.rs => sync/futex/wasilibc.rs} | 0 .../std/src/sys/{pal/wasm/atomics/futex.rs => sync/futex/wasm.rs} | 0 .../std/src/sys/{pal/windows/futex.rs => sync/futex/windows.rs} | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename library/std/src/sys/{pal/hermit/futex.rs => sync/futex/hermit.rs} (100%) rename library/std/src/sys/{pal/unix/futex.rs => sync/futex/unix.rs} (100%) rename library/std/src/sys/{pal/wasi/wasilibc_futex.rs => sync/futex/wasilibc.rs} (100%) rename library/std/src/sys/{pal/wasm/atomics/futex.rs => sync/futex/wasm.rs} (100%) rename library/std/src/sys/{pal/windows/futex.rs => sync/futex/windows.rs} (100%) diff --git a/library/std/src/sys/pal/hermit/futex.rs b/library/std/src/sys/sync/futex/hermit.rs similarity index 100% rename from library/std/src/sys/pal/hermit/futex.rs rename to library/std/src/sys/sync/futex/hermit.rs diff --git a/library/std/src/sys/pal/unix/futex.rs b/library/std/src/sys/sync/futex/unix.rs similarity index 100% rename from library/std/src/sys/pal/unix/futex.rs rename to library/std/src/sys/sync/futex/unix.rs diff --git a/library/std/src/sys/pal/wasi/wasilibc_futex.rs b/library/std/src/sys/sync/futex/wasilibc.rs similarity index 100% rename from library/std/src/sys/pal/wasi/wasilibc_futex.rs rename to library/std/src/sys/sync/futex/wasilibc.rs diff --git a/library/std/src/sys/pal/wasm/atomics/futex.rs b/library/std/src/sys/sync/futex/wasm.rs similarity index 100% rename from library/std/src/sys/pal/wasm/atomics/futex.rs rename to library/std/src/sys/sync/futex/wasm.rs diff --git a/library/std/src/sys/pal/windows/futex.rs b/library/std/src/sys/sync/futex/windows.rs similarity index 100% rename from library/std/src/sys/pal/windows/futex.rs rename to library/std/src/sys/sync/futex/windows.rs From 5b40f3d400ed3ebb3794b1c9911d3640267635fc Mon Sep 17 00:00:00 2001 From: Valentyn Kit Date: Fri, 3 Jul 2026 11:59:56 +0300 Subject: [PATCH 2/2] std: connect sys::sync::futex and drop the pal declarations Select the platform implementation with a cfg_select! in sys::sync::futex, repoint each one at the pal primitives it uses (time, fuchsia, the windows api module, hermit_abi), and remove the now-unused futex declarations from the pal modules. The sync primitives import crate::sys::sync::futex rather than the crate::sys::futex glob re-export. --- .git-blame-ignore-revs | 3 ++ library/std/src/sys/pal/hermit/mod.rs | 1 - library/std/src/sys/pal/motor/mod.rs | 2 - library/std/src/sys/pal/unix/mod.rs | 1 - library/std/src/sys/pal/wasi/mod.rs | 18 --------- library/std/src/sys/pal/wasm/mod.rs | 4 -- library/std/src/sys/pal/windows/mod.rs | 2 - library/std/src/sys/sync/condvar/futex.rs | 2 +- library/std/src/sys/sync/futex/hermit.rs | 2 +- library/std/src/sys/sync/futex/mod.rs | 39 +++++++++++++++++++ library/std/src/sys/sync/futex/unix.rs | 20 +++------- library/std/src/sys/sync/futex/windows.rs | 2 +- library/std/src/sys/sync/mod.rs | 1 + library/std/src/sys/sync/mutex/futex.rs | 2 +- library/std/src/sys/sync/once/futex.rs | 2 +- library/std/src/sys/sync/rwlock/futex.rs | 2 +- .../std/src/sys/sync/thread_parking/futex.rs | 2 +- 17 files changed, 55 insertions(+), 50 deletions(-) create mode 100644 library/std/src/sys/sync/futex/mod.rs diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index af071c706856e..4e2bef94982cc 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -33,3 +33,6 @@ c682aa162b0d41e21cc6748f4fecfe01efb69d1f 1fcae03369abb4c2cc180cd5a49e1f4440a81300 # Breaking up of compiletest runtest.rs 60600a6fa403216bfd66e04f948b1822f6450af7 + +# std: move futex implementations into sys::sync::futex +7232830d10b6af772e0e4670a2ff61dd23830ed8 diff --git a/library/std/src/sys/pal/hermit/mod.rs b/library/std/src/sys/pal/hermit/mod.rs index 53f6ddd7065d7..e8c9bf70b99df 100644 --- a/library/std/src/sys/pal/hermit/mod.rs +++ b/library/std/src/sys/pal/hermit/mod.rs @@ -21,7 +21,6 @@ use crate::os::hermit::hermit_abi; use crate::os::raw::c_char; use crate::sys::env; -pub mod futex; #[path = "../unix/time.rs"] pub mod time; diff --git a/library/std/src/sys/pal/motor/mod.rs b/library/std/src/sys/pal/motor/mod.rs index ac10d81ecfb89..5bf217db9013a 100644 --- a/library/std/src/sys/pal/motor/mod.rs +++ b/library/std/src/sys/pal/motor/mod.rs @@ -1,7 +1,5 @@ #![allow(unsafe_op_in_unsafe_fn)] -pub use moto_rt::futex; - use crate::io; pub(crate) fn map_motor_error(err: moto_rt::Error) -> io::Error { diff --git a/library/std/src/sys/pal/unix/mod.rs b/library/std/src/sys/pal/unix/mod.rs index 2bd28ba498370..8fca169d93119 100644 --- a/library/std/src/sys/pal/unix/mod.rs +++ b/library/std/src/sys/pal/unix/mod.rs @@ -5,7 +5,6 @@ use crate::io; pub mod conf; #[cfg(target_os = "fuchsia")] pub mod fuchsia; -pub mod futex; pub mod stack_overflow; pub mod sync; pub mod thread_parking; diff --git a/library/std/src/sys/pal/wasi/mod.rs b/library/std/src/sys/pal/wasi/mod.rs index 9069d0f0064a7..056f632ae0be2 100644 --- a/library/std/src/sys/pal/wasi/mod.rs +++ b/library/std/src/sys/pal/wasi/mod.rs @@ -11,24 +11,6 @@ pub mod stack_overflow; #[path = "../unix/time.rs"] pub mod time; -// The wasi-libc based futex is new enough that it's not present in older -// wasi-libc builds. For now that means it's only required on wasip3 (which -// requires a newer wasi-libc anyway). In the future this'll probably switch to -// unconditionally using `wasilibc_futex` as the implementation for all WASI -// targets (and switching all synchronization primitives to the futex version). -cfg_select! { - target_env = "p3" => { - pub mod wasilibc_futex; - pub use wasilibc_futex as futex; - } - target_feature = "atomics" => { - #[allow(unused)] - #[path = "../wasm/atomics/futex.rs"] - pub mod futex; - } - _ => {} -} - #[cfg(not(target_env = "p1"))] mod cabi_realloc; diff --git a/library/std/src/sys/pal/wasm/mod.rs b/library/std/src/sys/pal/wasm/mod.rs index 24a2ab8eca30f..72e5982fc0732 100644 --- a/library/std/src/sys/pal/wasm/mod.rs +++ b/library/std/src/sys/pal/wasm/mod.rs @@ -16,10 +16,6 @@ #![deny(unsafe_op_in_unsafe_fn)] -#[cfg(target_feature = "atomics")] -#[path = "atomics/futex.rs"] -pub mod futex; - #[path = "../unsupported/common.rs"] #[deny(unsafe_op_in_unsafe_fn)] mod common; diff --git a/library/std/src/sys/pal/windows/mod.rs b/library/std/src/sys/pal/windows/mod.rs index b67ba37749789..4fa8c1b9a1323 100644 --- a/library/std/src/sys/pal/windows/mod.rs +++ b/library/std/src/sys/pal/windows/mod.rs @@ -15,8 +15,6 @@ pub mod compat; pub mod api; pub mod c; -#[cfg(not(target_vendor = "win7"))] -pub mod futex; pub mod handle; pub mod time; cfg_select! { diff --git a/library/std/src/sys/sync/condvar/futex.rs b/library/std/src/sys/sync/condvar/futex.rs index 0d0c5f0dbe701..b5b82e4c38257 100644 --- a/library/std/src/sys/sync/condvar/futex.rs +++ b/library/std/src/sys/sync/condvar/futex.rs @@ -1,6 +1,6 @@ use crate::sync::atomic::Ordering::Relaxed; -use crate::sys::futex::{Futex, futex_wait, futex_wake, futex_wake_all}; use crate::sys::sync::Mutex; +use crate::sys::sync::futex::{Futex, futex_wait, futex_wake, futex_wake_all}; use crate::time::Duration; pub struct Condvar { diff --git a/library/std/src/sys/sync/futex/hermit.rs b/library/std/src/sys/sync/futex/hermit.rs index 78c86071fdd53..783052526c525 100644 --- a/library/std/src/sys/sync/futex/hermit.rs +++ b/library/std/src/sys/sync/futex/hermit.rs @@ -1,4 +1,4 @@ -use super::hermit_abi; +use crate::os::hermit::hermit_abi; use crate::ptr::null; use crate::sync::atomic::Atomic; use crate::time::Duration; diff --git a/library/std/src/sys/sync/futex/mod.rs b/library/std/src/sys/sync/futex/mod.rs new file mode 100644 index 0000000000000..0edb46cc10f86 --- /dev/null +++ b/library/std/src/sys/sync/futex/mod.rs @@ -0,0 +1,39 @@ +cfg_select! { + any( + target_os = "linux", + target_os = "android", + all(target_os = "emscripten", target_feature = "atomics"), + target_os = "freebsd", + target_os = "openbsd", + target_os = "dragonfly", + target_os = "fuchsia", + ) => { + mod unix; + pub use unix::*; + } + all(target_os = "windows", not(target_vendor = "win7")) => { + mod windows; + pub use windows::*; + } + target_os = "hermit" => { + mod hermit; + pub use hermit::*; + } + // The wasi-libc based futex is new enough that it's not present in older + // wasi-libc builds. For now that means it's only required on wasip3 (which + // requires a newer wasi-libc anyway). In the future this'll probably switch to + // unconditionally using `wasilibc` as the implementation for all WASI + // targets (and switching all synchronization primitives to the futex version). + all(target_os = "wasi", target_env = "p3") => { + mod wasilibc; + pub use wasilibc::*; + } + all(target_family = "wasm", target_feature = "atomics") => { + mod wasm; + pub use wasm::*; + } + target_os = "motor" => { + pub use moto_rt::futex::*; + } + _ => {} +} diff --git a/library/std/src/sys/sync/futex/unix.rs b/library/std/src/sys/sync/futex/unix.rs index 2948d3d594eaa..16fda3ecbc7c3 100644 --- a/library/std/src/sys/sync/futex/unix.rs +++ b/library/std/src/sys/sync/futex/unix.rs @@ -1,13 +1,3 @@ -#![cfg(any( - target_os = "linux", - target_os = "android", - all(target_os = "emscripten", target_feature = "atomics"), - target_os = "freebsd", - target_os = "openbsd", - target_os = "dragonfly", - target_os = "fuchsia", -))] - use crate::sync::atomic::Atomic; use crate::time::Duration; @@ -28,9 +18,9 @@ pub type SmallPrimitive = u32; /// Returns false on timeout, and true in all other cases. #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] pub fn futex_wait(futex: &Atomic, expected: u32, timeout: Option) -> bool { - use super::time::Timespec; use crate::ptr::null; use crate::sync::atomic::Ordering::Relaxed; + use crate::sys::pal::time::Timespec; // Calculate the timeout as an absolute timespec. // @@ -149,8 +139,8 @@ pub fn futex_wake_all(futex: &Atomic) { #[cfg(target_os = "openbsd")] pub fn futex_wait(futex: &Atomic, expected: u32, timeout: Option) -> bool { - use super::time::Timespec; use crate::ptr::{null, null_mut}; + use crate::sys::pal::time::Timespec; // Overflows are rounded up to an infinite timeout (None). let timespec = timeout @@ -258,7 +248,7 @@ pub fn futex_wake_all(futex: &Atomic) { #[cfg(target_os = "fuchsia")] pub fn futex_wait(futex: &Atomic, expected: u32, timeout: Option) -> bool { - use super::fuchsia::*; + use crate::sys::pal::fuchsia::*; // Sleep forever if the timeout is longer than fits in a i64. let deadline = timeout @@ -274,11 +264,11 @@ pub fn futex_wait(futex: &Atomic, expected: u32, timeout: Option) // Fuchsia doesn't tell us how many threads are woken up, so this always returns false. #[cfg(target_os = "fuchsia")] pub fn futex_wake(futex: &Atomic) -> bool { - unsafe { super::fuchsia::zx_futex_wake(futex, 1) }; + unsafe { crate::sys::pal::fuchsia::zx_futex_wake(futex, 1) }; false } #[cfg(target_os = "fuchsia")] pub fn futex_wake_all(futex: &Atomic) { - unsafe { super::fuchsia::zx_futex_wake(futex, u32::MAX) }; + unsafe { crate::sys::pal::fuchsia::zx_futex_wake(futex, u32::MAX) }; } diff --git a/library/std/src/sys/sync/futex/windows.rs b/library/std/src/sys/sync/futex/windows.rs index cfa0a6b3815bd..eed0bb2548c1d 100644 --- a/library/std/src/sys/sync/futex/windows.rs +++ b/library/std/src/sys/sync/futex/windows.rs @@ -6,7 +6,7 @@ use core::sync::atomic::{ }; use core::time::Duration; -use super::api::{self, WinError}; +use crate::sys::pal::api::{self, WinError}; use crate::sys::{c, dur2timeout}; /// An atomic for use as a futex that is at least 32-bits but may be larger diff --git a/library/std/src/sys/sync/mod.rs b/library/std/src/sys/sync/mod.rs index 0691e96785198..8ee0b2649ed3d 100644 --- a/library/std/src/sys/sync/mod.rs +++ b/library/std/src/sys/sync/mod.rs @@ -1,4 +1,5 @@ mod condvar; +mod futex; mod mutex; mod once; mod once_box; diff --git a/library/std/src/sys/sync/mutex/futex.rs b/library/std/src/sys/sync/mutex/futex.rs index 70e2ea9f60586..015b5aacbc53b 100644 --- a/library/std/src/sys/sync/mutex/futex.rs +++ b/library/std/src/sys/sync/mutex/futex.rs @@ -1,5 +1,5 @@ use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release}; -use crate::sys::futex::{self, futex_wait, futex_wake}; +use crate::sys::sync::futex::{self, futex_wait, futex_wake}; type Futex = futex::SmallFutex; type State = futex::SmallPrimitive; diff --git a/library/std/src/sys/sync/once/futex.rs b/library/std/src/sys/sync/once/futex.rs index 236bc9ca4b7c7..8f17f065669a8 100644 --- a/library/std/src/sys/sync/once/futex.rs +++ b/library/std/src/sys/sync/once/futex.rs @@ -2,7 +2,7 @@ use crate::cell::Cell; use crate::sync as public; use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release}; use crate::sync::once::OnceExclusiveState; -use crate::sys::futex::{Futex, Primitive, futex_wait, futex_wake_all}; +use crate::sys::sync::futex::{Futex, Primitive, futex_wait, futex_wake_all}; // On some platforms, the OS is very nice and handles the waiter queue for us. // This means we only need one atomic value with 4 states: diff --git a/library/std/src/sys/sync/rwlock/futex.rs b/library/std/src/sys/sync/rwlock/futex.rs index 0e8e954de0758..c9389fe144b4d 100644 --- a/library/std/src/sys/sync/rwlock/futex.rs +++ b/library/std/src/sys/sync/rwlock/futex.rs @@ -1,5 +1,5 @@ use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release}; -use crate::sys::futex::{Futex, Primitive, futex_wait, futex_wake, futex_wake_all}; +use crate::sys::sync::futex::{Futex, Primitive, futex_wait, futex_wake, futex_wake_all}; pub struct RwLock { // The state consists of a 30-bit reader counter, a 'readers waiting' flag, and a 'writers waiting' flag. diff --git a/library/std/src/sys/sync/thread_parking/futex.rs b/library/std/src/sys/sync/thread_parking/futex.rs index c8f7f26386a01..691d839c41e6d 100644 --- a/library/std/src/sys/sync/thread_parking/futex.rs +++ b/library/std/src/sys/sync/thread_parking/futex.rs @@ -1,7 +1,7 @@ #![forbid(unsafe_op_in_unsafe_fn)] use crate::pin::Pin; use crate::sync::atomic::Ordering::{Acquire, Release}; -use crate::sys::futex::{self, futex_wait, futex_wake}; +use crate::sys::sync::futex::{self, futex_wait, futex_wake}; use crate::time::Duration; type Futex = futex::SmallFutex;