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.
This commit is contained in:
Valentyn Kit
2026-07-03 11:59:56 +03:00
parent 7232830d10
commit 5b40f3d400
17 changed files with 55 additions and 50 deletions

View File

@@ -33,3 +33,6 @@ c682aa162b0d41e21cc6748f4fecfe01efb69d1f
1fcae03369abb4c2cc180cd5a49e1f4440a81300 1fcae03369abb4c2cc180cd5a49e1f4440a81300
# Breaking up of compiletest runtest.rs # Breaking up of compiletest runtest.rs
60600a6fa403216bfd66e04f948b1822f6450af7 60600a6fa403216bfd66e04f948b1822f6450af7
# std: move futex implementations into sys::sync::futex
7232830d10b6af772e0e4670a2ff61dd23830ed8

View File

@@ -21,7 +21,6 @@ use crate::os::hermit::hermit_abi;
use crate::os::raw::c_char; use crate::os::raw::c_char;
use crate::sys::env; use crate::sys::env;
pub mod futex;
#[path = "../unix/time.rs"] #[path = "../unix/time.rs"]
pub mod time; pub mod time;

View File

@@ -1,7 +1,5 @@
#![allow(unsafe_op_in_unsafe_fn)] #![allow(unsafe_op_in_unsafe_fn)]
pub use moto_rt::futex;
use crate::io; use crate::io;
pub(crate) fn map_motor_error(err: moto_rt::Error) -> io::Error { pub(crate) fn map_motor_error(err: moto_rt::Error) -> io::Error {

View File

@@ -5,7 +5,6 @@ use crate::io;
pub mod conf; pub mod conf;
#[cfg(target_os = "fuchsia")] #[cfg(target_os = "fuchsia")]
pub mod fuchsia; pub mod fuchsia;
pub mod futex;
pub mod stack_overflow; pub mod stack_overflow;
pub mod sync; pub mod sync;
pub mod thread_parking; pub mod thread_parking;

View File

@@ -11,24 +11,6 @@ pub mod stack_overflow;
#[path = "../unix/time.rs"] #[path = "../unix/time.rs"]
pub mod time; 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"))] #[cfg(not(target_env = "p1"))]
mod cabi_realloc; mod cabi_realloc;

View File

@@ -16,10 +16,6 @@
#![deny(unsafe_op_in_unsafe_fn)] #![deny(unsafe_op_in_unsafe_fn)]
#[cfg(target_feature = "atomics")]
#[path = "atomics/futex.rs"]
pub mod futex;
#[path = "../unsupported/common.rs"] #[path = "../unsupported/common.rs"]
#[deny(unsafe_op_in_unsafe_fn)] #[deny(unsafe_op_in_unsafe_fn)]
mod common; mod common;

View File

@@ -15,8 +15,6 @@ pub mod compat;
pub mod api; pub mod api;
pub mod c; pub mod c;
#[cfg(not(target_vendor = "win7"))]
pub mod futex;
pub mod handle; pub mod handle;
pub mod time; pub mod time;
cfg_select! { cfg_select! {

View File

@@ -1,6 +1,6 @@
use crate::sync::atomic::Ordering::Relaxed; 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::Mutex;
use crate::sys::sync::futex::{Futex, futex_wait, futex_wake, futex_wake_all};
use crate::time::Duration; use crate::time::Duration;
pub struct Condvar { pub struct Condvar {

View File

@@ -1,4 +1,4 @@
use super::hermit_abi; use crate::os::hermit::hermit_abi;
use crate::ptr::null; use crate::ptr::null;
use crate::sync::atomic::Atomic; use crate::sync::atomic::Atomic;
use crate::time::Duration; use crate::time::Duration;

View File

@@ -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::*;
}
_ => {}
}

View File

@@ -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::sync::atomic::Atomic;
use crate::time::Duration; use crate::time::Duration;
@@ -28,9 +18,9 @@ pub type SmallPrimitive = u32;
/// Returns false on timeout, and true in all other cases. /// Returns false on timeout, and true in all other cases.
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
pub fn futex_wait(futex: &Atomic<u32>, expected: u32, timeout: Option<Duration>) -> bool { pub fn futex_wait(futex: &Atomic<u32>, expected: u32, timeout: Option<Duration>) -> bool {
use super::time::Timespec;
use crate::ptr::null; use crate::ptr::null;
use crate::sync::atomic::Ordering::Relaxed; use crate::sync::atomic::Ordering::Relaxed;
use crate::sys::pal::time::Timespec;
// Calculate the timeout as an absolute timespec. // Calculate the timeout as an absolute timespec.
// //
@@ -149,8 +139,8 @@ pub fn futex_wake_all(futex: &Atomic<u32>) {
#[cfg(target_os = "openbsd")] #[cfg(target_os = "openbsd")]
pub fn futex_wait(futex: &Atomic<u32>, expected: u32, timeout: Option<Duration>) -> bool { pub fn futex_wait(futex: &Atomic<u32>, expected: u32, timeout: Option<Duration>) -> bool {
use super::time::Timespec;
use crate::ptr::{null, null_mut}; use crate::ptr::{null, null_mut};
use crate::sys::pal::time::Timespec;
// Overflows are rounded up to an infinite timeout (None). // Overflows are rounded up to an infinite timeout (None).
let timespec = timeout let timespec = timeout
@@ -258,7 +248,7 @@ pub fn futex_wake_all(futex: &Atomic<u32>) {
#[cfg(target_os = "fuchsia")] #[cfg(target_os = "fuchsia")]
pub fn futex_wait(futex: &Atomic<u32>, expected: u32, timeout: Option<Duration>) -> bool { pub fn futex_wait(futex: &Atomic<u32>, expected: u32, timeout: Option<Duration>) -> bool {
use super::fuchsia::*; use crate::sys::pal::fuchsia::*;
// Sleep forever if the timeout is longer than fits in a i64. // Sleep forever if the timeout is longer than fits in a i64.
let deadline = timeout let deadline = timeout
@@ -274,11 +264,11 @@ pub fn futex_wait(futex: &Atomic<u32>, expected: u32, timeout: Option<Duration>)
// Fuchsia doesn't tell us how many threads are woken up, so this always returns false. // Fuchsia doesn't tell us how many threads are woken up, so this always returns false.
#[cfg(target_os = "fuchsia")] #[cfg(target_os = "fuchsia")]
pub fn futex_wake(futex: &Atomic<u32>) -> bool { pub fn futex_wake(futex: &Atomic<u32>) -> bool {
unsafe { super::fuchsia::zx_futex_wake(futex, 1) }; unsafe { crate::sys::pal::fuchsia::zx_futex_wake(futex, 1) };
false false
} }
#[cfg(target_os = "fuchsia")] #[cfg(target_os = "fuchsia")]
pub fn futex_wake_all(futex: &Atomic<u32>) { pub fn futex_wake_all(futex: &Atomic<u32>) {
unsafe { super::fuchsia::zx_futex_wake(futex, u32::MAX) }; unsafe { crate::sys::pal::fuchsia::zx_futex_wake(futex, u32::MAX) };
} }

View File

@@ -6,7 +6,7 @@ use core::sync::atomic::{
}; };
use core::time::Duration; use core::time::Duration;
use super::api::{self, WinError}; use crate::sys::pal::api::{self, WinError};
use crate::sys::{c, dur2timeout}; use crate::sys::{c, dur2timeout};
/// An atomic for use as a futex that is at least 32-bits but may be larger /// An atomic for use as a futex that is at least 32-bits but may be larger

View File

@@ -1,4 +1,5 @@
mod condvar; mod condvar;
mod futex;
mod mutex; mod mutex;
mod once; mod once;
mod once_box; mod once_box;

View File

@@ -1,5 +1,5 @@
use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release}; 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 Futex = futex::SmallFutex;
type State = futex::SmallPrimitive; type State = futex::SmallPrimitive;

View File

@@ -2,7 +2,7 @@ use crate::cell::Cell;
use crate::sync as public; use crate::sync as public;
use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release}; use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release};
use crate::sync::once::OnceExclusiveState; 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. // 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: // This means we only need one atomic value with 4 states:

View File

@@ -1,5 +1,5 @@
use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release}; 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 { pub struct RwLock {
// The state consists of a 30-bit reader counter, a 'readers waiting' flag, and a 'writers waiting' flag. // The state consists of a 30-bit reader counter, a 'readers waiting' flag, and a 'writers waiting' flag.

View File

@@ -1,7 +1,7 @@
#![forbid(unsafe_op_in_unsafe_fn)] #![forbid(unsafe_op_in_unsafe_fn)]
use crate::pin::Pin; use crate::pin::Pin;
use crate::sync::atomic::Ordering::{Acquire, Release}; 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; use crate::time::Duration;
type Futex = futex::SmallFutex; type Futex = futex::SmallFutex;