Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move futex locks to sys_common #104329

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/std/src/sync/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests;

use crate::fmt;
use crate::sync::{mutex, poison, LockResult, MutexGuard, PoisonError};
use crate::sys::locks as sys;
use crate::sys_common::locks as sys;
use crate::time::{Duration, Instant};

/// A type indicating whether a timed wait on a condition variable returned
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::cell::UnsafeCell;
use crate::fmt;
use crate::ops::{Deref, DerefMut};
use crate::sync::{poison, LockResult, TryLockError, TryLockResult};
use crate::sys::locks as sys;
use crate::sys_common::locks as sys;

/// A mutual exclusion primitive useful for protecting shared data
///
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sync/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::fmt;
use crate::ops::{Deref, DerefMut};
use crate::ptr::NonNull;
use crate::sync::{poison, LockResult, TryLockError, TryLockResult};
use crate::sys::locks as sys;
use crate::sys_common::locks as sys;

/// A reader-writer lock
///
Expand Down
10 changes: 0 additions & 10 deletions library/std/src/sys/hermit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,6 @@ pub mod thread_local_dtor;
pub mod thread_local_key;
pub mod time;

#[path = "../unix/locks"]
pub mod locks {
mod futex_condvar;
mod futex_mutex;
mod futex_rwlock;
pub(crate) use futex_condvar::Condvar;
pub(crate) use futex_mutex::Mutex;
pub(crate) use futex_rwlock::RwLock;
}

use crate::io::ErrorKind;

#[allow(unused_extern_crates)]
Expand Down
29 changes: 10 additions & 19 deletions library/std/src/sys/unix/locks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
#![cfg(not(any(
target_os = "linux",
target_os = "android",
all(target_os = "emscripten", target_feature = "atomics"),
target_os = "freebsd",
target_os = "openbsd",
target_os = "dragonfly",
)))]

cfg_if::cfg_if! {
if #[cfg(any(
target_os = "linux",
target_os = "android",
all(target_os = "emscripten", target_feature = "atomics"),
target_os = "freebsd",
target_os = "openbsd",
target_os = "dragonfly",
))] {
mod futex_mutex;
mod futex_rwlock;
mod futex_condvar;
pub(crate) use futex_mutex::Mutex;
pub(crate) use futex_rwlock::RwLock;
pub(crate) use futex_condvar::Condvar;
} else if #[cfg(target_os = "fuchsia")] {
if #[cfg(target_os = "fuchsia")] {
mod fuchsia_mutex;
mod futex_rwlock;
mod futex_condvar;
pub(crate) use fuchsia_mutex::Mutex;
pub(crate) use futex_rwlock::RwLock;
pub(crate) use futex_condvar::Condvar;
} else {
mod pthread_mutex;
mod pthread_rwlock;
Expand Down
10 changes: 0 additions & 10 deletions library/std/src/sys/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ pub mod time;

cfg_if::cfg_if! {
if #[cfg(target_feature = "atomics")] {
#[path = "../unix/locks"]
pub mod locks {
#![allow(unsafe_op_in_unsafe_fn)]
mod futex_condvar;
mod futex_mutex;
mod futex_rwlock;
pub(crate) use futex_condvar::Condvar;
pub(crate) use futex_mutex::Mutex;
pub(crate) use futex_rwlock::RwLock;
}
#[path = "atomics/futex.rs"]
pub mod futex;
#[path = "atomics/thread.rs"]
Expand Down
27 changes: 27 additions & 0 deletions library/std/src/sys_common/locks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
cfg_if::cfg_if! {
if #[cfg(any(
target_os = "android",
target_os = "dragonfly",
all(target_os = "emscripten", target_feature = "atomics"),
target_os = "freebsd",
target_os = "hermit",
target_os = "linux",
target_os = "openbsd",
all(any(target_arch = "wasm32", target_arch = "wasm64"), target_feature = "atomics"),
))] {
mod futex_mutex;
mod futex_rwlock;
mod futex_condvar;
pub(crate) use futex_mutex::Mutex;
pub(crate) use futex_rwlock::RwLock;
pub(crate) use futex_condvar::Condvar;
} else if #[cfg(target_os = "fuchsia")] {
mod futex_rwlock;
mod futex_condvar;
pub(crate) use crate::sys::locks::Mutex;
pub(crate) use futex_rwlock::RwLock;
pub(crate) use futex_condvar::Condvar;
} else {
pub(crate) use crate::sys::locks::{Mutex, RwLock, Condvar};
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably isn't the right place either because sys_common shouldn't contain platform cfg() stuff even though tidy currently doesn't complain about it either.

I was going to suggest moving it to sys::common instead but its mod.rs does say

// This module contains code that is shared between all platforms, mostly utility or fallback code.
// This explicitly does not include code that is shared between only a few platforms,
// such as when reusing an implementation from `unix` or `unsupported`.
// In those cases the desired code should be included directly using the #[path] attribute,
// not moved to this module.

So the #[path] seems to be the approved way already.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this concerns other code like the thread parker and one-time initialization as well, I suggest we discuss this in #84187 (the tracking issue for that guideline).

Copy link
Member

@workingjubilee workingjubilee Mar 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to devalue the contributions of anyone who has worked on libstd, but this mostly seems like it was one person's idea 2 years ago about what would be a good organization of stuff, tbh. Since then we've sprouted a lot more small niche mostly-tier-3 targets that are mostly just repeats of Linux or other POSIX platforms, yet with enough annoying variations that they induce quite a lot of configuration around that. I think we should revisit those guidelines heavily.

1 change: 1 addition & 0 deletions library/std/src/sys_common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod backtrace;
pub mod fs;
pub mod io;
pub mod lazy_box;
pub mod locks;
pub mod memchr;
pub mod once;
pub mod process;
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys_common/remutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::cell::UnsafeCell;
use crate::ops::Deref;
use crate::panic::{RefUnwindSafe, UnwindSafe};
use crate::sync::atomic::{AtomicUsize, Ordering::Relaxed};
use crate::sys::locks as sys;
use crate::sys_common::locks as sys;

/// A re-entrant mutual exclusion
///
Expand Down