From 0cd21cc549229c1cd6bb544eb53e7eb98190168f Mon Sep 17 00:00:00 2001 From: joboet Date: Thu, 15 Feb 2024 17:03:59 +0100 Subject: [PATCH 1/9] =?UTF-8?q?std:=20move=20locks=20to=20`sys`=20on=20?= =?UTF-8?q?=C2=B5ITRON?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../condvar.rs => locks/condvar/itron.rs} | 4 +++- library/std/src/sys/locks/condvar/mod.rs | 6 +++++ library/std/src/sys/locks/mod.rs | 7 ++++++ .../itron/mutex.rs => locks/mutex/itron.rs} | 19 +-------------- library/std/src/sys/locks/mutex/mod.rs | 6 +++++ library/std/src/sys/locks/rwlock/mod.rs | 6 +++++ .../solid/rwlock.rs => locks/rwlock/solid.rs} | 2 +- library/std/src/sys/mod.rs | 1 + library/std/src/sys/pal/solid/abi/fs.rs | 5 ++-- library/std/src/sys/pal/solid/abi/sockets.rs | 2 +- library/std/src/sys/pal/solid/mod.rs | 24 ++++++------------- 11 files changed, 41 insertions(+), 41 deletions(-) rename library/std/src/sys/{pal/itron/condvar.rs => locks/condvar/itron.rs} (98%) create mode 100644 library/std/src/sys/locks/condvar/mod.rs create mode 100644 library/std/src/sys/locks/mod.rs rename library/std/src/sys/{pal/itron/mutex.rs => locks/mutex/itron.rs} (85%) create mode 100644 library/std/src/sys/locks/mutex/mod.rs create mode 100644 library/std/src/sys/locks/rwlock/mod.rs rename library/std/src/sys/{pal/solid/rwlock.rs => locks/rwlock/solid.rs} (99%) diff --git a/library/std/src/sys/pal/itron/condvar.rs b/library/std/src/sys/locks/condvar/itron.rs similarity index 98% rename from library/std/src/sys/pal/itron/condvar.rs rename to library/std/src/sys/locks/condvar/itron.rs index 7a47cc6696a34..4c6f5e9dad269 100644 --- a/library/std/src/sys/pal/itron/condvar.rs +++ b/library/std/src/sys/locks/condvar/itron.rs @@ -1,5 +1,7 @@ //! POSIX conditional variable implementation based on user-space wait queues. -use super::{abi, error::expect_success_aborting, spin::SpinMutex, task, time::with_tmos_strong}; +use crate::sys::pal::itron::{ + abi, error::expect_success_aborting, spin::SpinMutex, task, time::with_tmos_strong, +}; use crate::{mem::replace, ptr::NonNull, sys::locks::Mutex, time::Duration}; // The implementation is inspired by the queue-based implementation shown in diff --git a/library/std/src/sys/locks/condvar/mod.rs b/library/std/src/sys/locks/condvar/mod.rs new file mode 100644 index 0000000000000..0bb93d6482a6b --- /dev/null +++ b/library/std/src/sys/locks/condvar/mod.rs @@ -0,0 +1,6 @@ +cfg_if::cfg_if! { + if #[cfg(target_os = "solid_asp3")] { + mod itron; + pub use itron::Condvar; + } +} diff --git a/library/std/src/sys/locks/mod.rs b/library/std/src/sys/locks/mod.rs new file mode 100644 index 0000000000000..0bdc4a1e1db81 --- /dev/null +++ b/library/std/src/sys/locks/mod.rs @@ -0,0 +1,7 @@ +mod condvar; +mod mutex; +mod rwlock; + +pub use condvar::Condvar; +pub use mutex::Mutex; +pub use rwlock::RwLock; diff --git a/library/std/src/sys/pal/itron/mutex.rs b/library/std/src/sys/locks/mutex/itron.rs similarity index 85% rename from library/std/src/sys/pal/itron/mutex.rs rename to library/std/src/sys/locks/mutex/itron.rs index 1f6cc41947602..a134eb2d1beca 100644 --- a/library/std/src/sys/pal/itron/mutex.rs +++ b/library/std/src/sys/locks/mutex/itron.rs @@ -1,6 +1,6 @@ //! Mutex implementation backed by μITRON mutexes. Assumes `acre_mtx` and //! `TA_INHERIT` are available. -use super::{ +use crate::sys::pal::itron::{ abi, error::{expect_success, expect_success_aborting, fail, ItronError}, spin::SpinIdOnceCell, @@ -66,20 +66,3 @@ impl Drop for Mutex { } } } - -pub(super) struct MutexGuard<'a>(&'a Mutex); - -impl<'a> MutexGuard<'a> { - #[inline] - pub(super) fn lock(x: &'a Mutex) -> Self { - x.lock(); - Self(x) - } -} - -impl Drop for MutexGuard<'_> { - #[inline] - fn drop(&mut self) { - unsafe { self.0.unlock() }; - } -} diff --git a/library/std/src/sys/locks/mutex/mod.rs b/library/std/src/sys/locks/mutex/mod.rs new file mode 100644 index 0000000000000..f8a51c7c682d4 --- /dev/null +++ b/library/std/src/sys/locks/mutex/mod.rs @@ -0,0 +1,6 @@ +cfg_if::cfg_if! { + if #[cfg(target_os = "solid_asp3")] { + mod itron; + pub use itron::Mutex; + } +} diff --git a/library/std/src/sys/locks/rwlock/mod.rs b/library/std/src/sys/locks/rwlock/mod.rs new file mode 100644 index 0000000000000..ad4dfdb80a71a --- /dev/null +++ b/library/std/src/sys/locks/rwlock/mod.rs @@ -0,0 +1,6 @@ +cfg_if::cfg_if! { + if #[cfg(target_os = "solid_asp3")] { + mod solid; + pub use solid::RwLock; + } +} diff --git a/library/std/src/sys/pal/solid/rwlock.rs b/library/std/src/sys/locks/rwlock/solid.rs similarity index 99% rename from library/std/src/sys/pal/solid/rwlock.rs rename to library/std/src/sys/locks/rwlock/solid.rs index ecb4eb83b9b05..9bf6f5dbb731e 100644 --- a/library/std/src/sys/pal/solid/rwlock.rs +++ b/library/std/src/sys/locks/rwlock/solid.rs @@ -1,5 +1,5 @@ //! A readers-writer lock implementation backed by the SOLID kernel extension. -use super::{ +use crate::sys::pal::{ abi, itron::{ error::{expect_success, expect_success_aborting, fail, ItronError}, diff --git a/library/std/src/sys/mod.rs b/library/std/src/sys/mod.rs index fae21636897b8..d77ac7eb027dc 100644 --- a/library/std/src/sys/mod.rs +++ b/library/std/src/sys/mod.rs @@ -6,6 +6,7 @@ mod pal; mod personality; pub mod cmath; +pub mod locks; pub mod os_str; pub mod path; diff --git a/library/std/src/sys/pal/solid/abi/fs.rs b/library/std/src/sys/pal/solid/abi/fs.rs index 32800bd9a9d0a..49526f4c9cd4d 100644 --- a/library/std/src/sys/pal/solid/abi/fs.rs +++ b/library/std/src/sys/pal/solid/abi/fs.rs @@ -1,9 +1,8 @@ //! `solid_fs.h` use crate::os::raw::{c_char, c_int, c_uchar}; pub use libc::{ - blksize_t, dev_t, ino_t, off_t, stat, time_t, O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, - O_TRUNC, O_WRONLY, SEEK_CUR, SEEK_END, SEEK_SET, S_IEXEC, S_IFBLK, S_IFCHR, S_IFDIR, S_IFIFO, - S_IFMT, S_IFREG, S_IREAD, S_IWRITE, + ino_t, off_t, stat, time_t, O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY, + SEEK_CUR, SEEK_END, SEEK_SET, S_IFBLK, S_IFCHR, S_IFDIR, S_IFIFO, S_IFMT, S_IFREG, S_IWRITE, }; pub const O_ACCMODE: c_int = 0x3; diff --git a/library/std/src/sys/pal/solid/abi/sockets.rs b/library/std/src/sys/pal/solid/abi/sockets.rs index eb06a6dd927e6..11c430360ce88 100644 --- a/library/std/src/sys/pal/solid/abi/sockets.rs +++ b/library/std/src/sys/pal/solid/abi/sockets.rs @@ -1,5 +1,5 @@ use crate::os::raw::{c_char, c_uint, c_void}; -pub use libc::{c_int, c_long, size_t, ssize_t, suseconds_t, time_t, timeval}; +pub use libc::{c_int, c_long, size_t, ssize_t, timeval}; pub const SOLID_NET_ERR_BASE: c_int = -2000; pub const EINPROGRESS: c_int = SOLID_NET_ERR_BASE - libc::EINPROGRESS; diff --git a/library/std/src/sys/pal/solid/mod.rs b/library/std/src/sys/pal/solid/mod.rs index be8e00339021f..9ada7d130f050 100644 --- a/library/std/src/sys/pal/solid/mod.rs +++ b/library/std/src/sys/pal/solid/mod.rs @@ -2,19 +2,17 @@ #![allow(missing_docs, nonstandard_style)] #![deny(unsafe_op_in_unsafe_fn)] -mod abi; +pub mod abi; #[path = "../itron"] -mod itron { - pub(super) mod abi; - pub mod condvar; - pub(super) mod error; - pub mod mutex; - pub(super) mod spin; - pub(super) mod task; +pub mod itron { + pub mod abi; + pub mod error; + pub mod spin; + pub mod task; pub mod thread; pub mod thread_parking; - pub(super) mod time; + pub mod time; use super::unsupported; } @@ -41,14 +39,6 @@ pub mod thread_local_key; pub use self::itron::thread_parking; pub mod time; -mod rwlock; - -pub mod locks { - pub use super::itron::condvar::*; - pub use super::itron::mutex::*; - pub use super::rwlock::*; -} - // SAFETY: must be called only once during runtime initialization. // NOTE: this is not guaranteed to run, for example when Rust code is called externally. pub unsafe fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) {} From c2d0f8452ff7ec35fa798d783a15510fc5549c90 Mon Sep 17 00:00:00 2001 From: joboet Date: Thu, 15 Feb 2024 17:09:51 +0100 Subject: [PATCH 2/9] std: move locks to `sys` on SGX --- library/std/src/sys/locks/condvar/mod.rs | 5 ++++- .../{pal/sgx/condvar.rs => locks/condvar/sgx.rs} | 3 +-- library/std/src/sys/locks/mutex/mod.rs | 7 +++++-- .../sys/{pal/sgx/mutex.rs => locks/mutex/sgx.rs} | 2 +- library/std/src/sys/locks/rwlock/mod.rs | 5 ++++- .../sys/{pal/sgx/rwlock.rs => locks/rwlock/sgx.rs} | 7 +++---- library/std/src/sys/pal/sgx/mod.rs | 13 +------------ 7 files changed, 19 insertions(+), 23 deletions(-) rename library/std/src/sys/{pal/sgx/condvar.rs => locks/condvar/sgx.rs} (94%) rename library/std/src/sys/{pal/sgx/mutex.rs => locks/mutex/sgx.rs} (94%) rename library/std/src/sys/{pal/sgx/rwlock.rs => locks/rwlock/sgx.rs} (99%) diff --git a/library/std/src/sys/locks/condvar/mod.rs b/library/std/src/sys/locks/condvar/mod.rs index 0bb93d6482a6b..1825fc25ff3bd 100644 --- a/library/std/src/sys/locks/condvar/mod.rs +++ b/library/std/src/sys/locks/condvar/mod.rs @@ -1,5 +1,8 @@ cfg_if::cfg_if! { - if #[cfg(target_os = "solid_asp3")] { + if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] { + mod sgx; + pub use sgx::Condvar; + } else if #[cfg(target_os = "solid_asp3")] { mod itron; pub use itron::Condvar; } diff --git a/library/std/src/sys/pal/sgx/condvar.rs b/library/std/src/sys/locks/condvar/sgx.rs similarity index 94% rename from library/std/src/sys/pal/sgx/condvar.rs rename to library/std/src/sys/locks/condvar/sgx.rs index aa1174664aeb0..cabd3250275a8 100644 --- a/library/std/src/sys/pal/sgx/condvar.rs +++ b/library/std/src/sys/locks/condvar/sgx.rs @@ -1,9 +1,8 @@ use crate::sys::locks::Mutex; +use crate::sys::pal::waitqueue::{SpinMutex, WaitQueue, WaitVariable}; use crate::sys_common::lazy_box::{LazyBox, LazyInit}; use crate::time::Duration; -use super::waitqueue::{SpinMutex, WaitQueue, WaitVariable}; - /// FIXME: `UnsafeList` is not movable. struct AllocatedCondvar(SpinMutex>); diff --git a/library/std/src/sys/locks/mutex/mod.rs b/library/std/src/sys/locks/mutex/mod.rs index f8a51c7c682d4..d61d3883bc9f8 100644 --- a/library/std/src/sys/locks/mutex/mod.rs +++ b/library/std/src/sys/locks/mutex/mod.rs @@ -1,6 +1,9 @@ cfg_if::cfg_if! { - if #[cfg(target_os = "solid_asp3")] { + if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] { + mod sgx; + pub use sgx::Mutex; + } else if #[cfg(target_os = "solid_asp3")] { mod itron; - pub use itron::Mutex; + pub use itron::Condvar; } } diff --git a/library/std/src/sys/pal/sgx/mutex.rs b/library/std/src/sys/locks/mutex/sgx.rs similarity index 94% rename from library/std/src/sys/pal/sgx/mutex.rs rename to library/std/src/sys/locks/mutex/sgx.rs index 0dbf020ebe06a..d37bd02adf8dd 100644 --- a/library/std/src/sys/pal/sgx/mutex.rs +++ b/library/std/src/sys/locks/mutex/sgx.rs @@ -1,4 +1,4 @@ -use super::waitqueue::{try_lock_or_false, SpinMutex, WaitQueue, WaitVariable}; +use crate::sys::pal::waitqueue::{try_lock_or_false, SpinMutex, WaitQueue, WaitVariable}; use crate::sys_common::lazy_box::{LazyBox, LazyInit}; /// FIXME: `UnsafeList` is not movable. diff --git a/library/std/src/sys/locks/rwlock/mod.rs b/library/std/src/sys/locks/rwlock/mod.rs index ad4dfdb80a71a..5e2b0044090ed 100644 --- a/library/std/src/sys/locks/rwlock/mod.rs +++ b/library/std/src/sys/locks/rwlock/mod.rs @@ -1,5 +1,8 @@ cfg_if::cfg_if! { - if #[cfg(target_os = "solid_asp3")] { + if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] { + mod sgx; + pub use sgx::RwLock; + } else if #[cfg(target_os = "solid_asp3")] { mod solid; pub use solid::RwLock; } diff --git a/library/std/src/sys/pal/sgx/rwlock.rs b/library/std/src/sys/locks/rwlock/sgx.rs similarity index 99% rename from library/std/src/sys/pal/sgx/rwlock.rs rename to library/std/src/sys/locks/rwlock/sgx.rs index ebae1cff0ee17..136dea597bb0c 100644 --- a/library/std/src/sys/pal/sgx/rwlock.rs +++ b/library/std/src/sys/locks/rwlock/sgx.rs @@ -1,13 +1,12 @@ #[cfg(test)] mod tests; +use crate::alloc::Layout; use crate::num::NonZero; -use crate::sys_common::lazy_box::{LazyBox, LazyInit}; - -use super::waitqueue::{ +use crate::sys::pal::waitqueue::{ try_lock_or_false, NotifiedTcs, SpinMutex, SpinMutexGuard, WaitQueue, WaitVariable, }; -use crate::alloc::Layout; +use crate::sys_common::lazy_box::{LazyBox, LazyInit}; struct AllocatedRwLock { readers: SpinMutex>>>, diff --git a/library/std/src/sys/pal/sgx/mod.rs b/library/std/src/sys/pal/sgx/mod.rs index 46f3e5b401da5..8ef3495884f2a 100644 --- a/library/std/src/sys/pal/sgx/mod.rs +++ b/library/std/src/sys/pal/sgx/mod.rs @@ -9,8 +9,6 @@ use crate::io::ErrorKind; use crate::sync::atomic::{AtomicBool, Ordering}; pub mod abi; -mod waitqueue; - pub mod alloc; pub mod args; pub mod env; @@ -31,16 +29,7 @@ pub mod thread; pub mod thread_local_key; pub mod thread_parking; pub mod time; - -mod condvar; -mod mutex; -mod rwlock; - -pub mod locks { - pub use super::condvar::*; - pub use super::mutex::*; - pub use super::rwlock::*; -} +pub mod waitqueue; // SAFETY: must be called only once during runtime initialization. // NOTE: this is not guaranteed to run, for example when Rust code is called externally. From 5e343e76e85c7e11afdcf9d76f0b355bbb38153d Mon Sep 17 00:00:00 2001 From: joboet Date: Thu, 15 Feb 2024 17:14:44 +0100 Subject: [PATCH 3/9] std: move locks to `sys` on teeos --- library/std/src/sys/locks/condvar/mod.rs | 3 + .../condvar.rs => locks/condvar/teeos.rs} | 0 library/std/src/sys/locks/mutex/mod.rs | 5 +- library/std/src/sys/locks/mutex/pthread.rs | 148 ++++++++++++++++++ library/std/src/sys/locks/rwlock/mod.rs | 3 + .../locks/rwlock.rs => locks/rwlock/teeos.rs} | 0 library/std/src/sys/pal/teeos/locks/mod.rs | 8 - library/std/src/sys/pal/teeos/mod.rs | 1 - 8 files changed, 158 insertions(+), 10 deletions(-) rename library/std/src/sys/{pal/teeos/locks/condvar.rs => locks/condvar/teeos.rs} (100%) create mode 100644 library/std/src/sys/locks/mutex/pthread.rs rename library/std/src/sys/{pal/teeos/locks/rwlock.rs => locks/rwlock/teeos.rs} (100%) delete mode 100644 library/std/src/sys/pal/teeos/locks/mod.rs diff --git a/library/std/src/sys/locks/condvar/mod.rs b/library/std/src/sys/locks/condvar/mod.rs index 1825fc25ff3bd..74494cdf66d79 100644 --- a/library/std/src/sys/locks/condvar/mod.rs +++ b/library/std/src/sys/locks/condvar/mod.rs @@ -5,5 +5,8 @@ cfg_if::cfg_if! { } else if #[cfg(target_os = "solid_asp3")] { mod itron; pub use itron::Condvar; + } else if #[cfg(target_os = "teeos")] { + mod teeos; + pub use teeos::Condvar; } } diff --git a/library/std/src/sys/pal/teeos/locks/condvar.rs b/library/std/src/sys/locks/condvar/teeos.rs similarity index 100% rename from library/std/src/sys/pal/teeos/locks/condvar.rs rename to library/std/src/sys/locks/condvar/teeos.rs diff --git a/library/std/src/sys/locks/mutex/mod.rs b/library/std/src/sys/locks/mutex/mod.rs index d61d3883bc9f8..b94608c849f7a 100644 --- a/library/std/src/sys/locks/mutex/mod.rs +++ b/library/std/src/sys/locks/mutex/mod.rs @@ -1,5 +1,8 @@ cfg_if::cfg_if! { - if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] { + if #[cfg(target_os = "teeos")] { + mod pthread; + pub use pthread::{Mutex, raw}; + } else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] { mod sgx; pub use sgx::Mutex; } else if #[cfg(target_os = "solid_asp3")] { diff --git a/library/std/src/sys/locks/mutex/pthread.rs b/library/std/src/sys/locks/mutex/pthread.rs new file mode 100644 index 0000000000000..ee0794334fbe3 --- /dev/null +++ b/library/std/src/sys/locks/mutex/pthread.rs @@ -0,0 +1,148 @@ +use crate::cell::UnsafeCell; +use crate::io::Error; +use crate::mem::{forget, MaybeUninit}; +use crate::sys::cvt_nz; +use crate::sys_common::lazy_box::{LazyBox, LazyInit}; + +struct AllocatedMutex(UnsafeCell); + +pub struct Mutex { + inner: LazyBox, +} + +#[inline] +pub unsafe fn raw(m: &Mutex) -> *mut libc::pthread_mutex_t { + m.inner.0.get() +} + +unsafe impl Send for AllocatedMutex {} +unsafe impl Sync for AllocatedMutex {} + +impl LazyInit for AllocatedMutex { + fn init() -> Box { + let mutex = Box::new(AllocatedMutex(UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER))); + + // Issue #33770 + // + // A pthread mutex initialized with PTHREAD_MUTEX_INITIALIZER will have + // a type of PTHREAD_MUTEX_DEFAULT, which has undefined behavior if you + // try to re-lock it from the same thread when you already hold a lock + // (https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_init.html). + // This is the case even if PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL + // (https://github.com/rust-lang/rust/issues/33770#issuecomment-220847521) -- in that + // case, `pthread_mutexattr_settype(PTHREAD_MUTEX_DEFAULT)` will of course be the same + // as setting it to `PTHREAD_MUTEX_NORMAL`, but not setting any mode will result in + // a Mutex where re-locking is UB. + // + // In practice, glibc takes advantage of this undefined behavior to + // implement hardware lock elision, which uses hardware transactional + // memory to avoid acquiring the lock. While a transaction is in + // progress, the lock appears to be unlocked. This isn't a problem for + // other threads since the transactional memory will abort if a conflict + // is detected, however no abort is generated when re-locking from the + // same thread. + // + // Since locking the same mutex twice will result in two aliasing &mut + // references, we instead create the mutex with type + // PTHREAD_MUTEX_NORMAL which is guaranteed to deadlock if we try to + // re-lock it from the same thread, thus avoiding undefined behavior. + unsafe { + let mut attr = MaybeUninit::::uninit(); + cvt_nz(libc::pthread_mutexattr_init(attr.as_mut_ptr())).unwrap(); + let attr = PthreadMutexAttr(&mut attr); + cvt_nz(libc::pthread_mutexattr_settype( + attr.0.as_mut_ptr(), + libc::PTHREAD_MUTEX_NORMAL, + )) + .unwrap(); + cvt_nz(libc::pthread_mutex_init(mutex.0.get(), attr.0.as_ptr())).unwrap(); + } + + mutex + } + + fn destroy(mutex: Box) { + // We're not allowed to pthread_mutex_destroy a locked mutex, + // so check first if it's unlocked. + if unsafe { libc::pthread_mutex_trylock(mutex.0.get()) == 0 } { + unsafe { libc::pthread_mutex_unlock(mutex.0.get()) }; + drop(mutex); + } else { + // The mutex is locked. This happens if a MutexGuard is leaked. + // In this case, we just leak the Mutex too. + forget(mutex); + } + } + + fn cancel_init(_: Box) { + // In this case, we can just drop it without any checks, + // since it cannot have been locked yet. + } +} + +impl Drop for AllocatedMutex { + #[inline] + fn drop(&mut self) { + let r = unsafe { libc::pthread_mutex_destroy(self.0.get()) }; + if cfg!(target_os = "dragonfly") { + // On DragonFly pthread_mutex_destroy() returns EINVAL if called on a + // mutex that was just initialized with libc::PTHREAD_MUTEX_INITIALIZER. + // Once it is used (locked/unlocked) or pthread_mutex_init() is called, + // this behaviour no longer occurs. + debug_assert!(r == 0 || r == libc::EINVAL); + } else { + debug_assert_eq!(r, 0); + } + } +} + +impl Mutex { + #[inline] + pub const fn new() -> Mutex { + Mutex { inner: LazyBox::new() } + } + + #[inline] + pub unsafe fn lock(&self) { + #[cold] + #[inline(never)] + fn fail(r: i32) -> ! { + let error = Error::from_raw_os_error(r); + panic!("failed to lock mutex: {error}"); + } + + let r = libc::pthread_mutex_lock(raw(self)); + // As we set the mutex type to `PTHREAD_MUTEX_NORMAL` above, we expect + // the lock call to never fail. Unfortunately however, some platforms + // (Solaris) do not conform to the standard, and instead always provide + // deadlock detection. How kind of them! Unfortunately that means that + // we need to check the error code here. To save us from UB on other + // less well-behaved platforms in the future, we do it even on "good" + // platforms like macOS. See #120147 for more context. + if r != 0 { + fail(r) + } + } + + #[inline] + pub unsafe fn unlock(&self) { + let r = libc::pthread_mutex_unlock(raw(self)); + debug_assert_eq!(r, 0); + } + + #[inline] + pub unsafe fn try_lock(&self) -> bool { + libc::pthread_mutex_trylock(raw(self)) == 0 + } +} + +pub(super) struct PthreadMutexAttr<'a>(pub &'a mut MaybeUninit); + +impl Drop for PthreadMutexAttr<'_> { + fn drop(&mut self) { + unsafe { + let result = libc::pthread_mutexattr_destroy(self.0.as_mut_ptr()); + debug_assert_eq!(result, 0); + } + } +} diff --git a/library/std/src/sys/locks/rwlock/mod.rs b/library/std/src/sys/locks/rwlock/mod.rs index 5e2b0044090ed..7de4a9d50a82a 100644 --- a/library/std/src/sys/locks/rwlock/mod.rs +++ b/library/std/src/sys/locks/rwlock/mod.rs @@ -5,5 +5,8 @@ cfg_if::cfg_if! { } else if #[cfg(target_os = "solid_asp3")] { mod solid; pub use solid::RwLock; + } else if #[cfg(target_os = "teeos")] { + mod teeos; + pub use teeos::RwLock; } } diff --git a/library/std/src/sys/pal/teeos/locks/rwlock.rs b/library/std/src/sys/locks/rwlock/teeos.rs similarity index 100% rename from library/std/src/sys/pal/teeos/locks/rwlock.rs rename to library/std/src/sys/locks/rwlock/teeos.rs diff --git a/library/std/src/sys/pal/teeos/locks/mod.rs b/library/std/src/sys/pal/teeos/locks/mod.rs deleted file mode 100644 index c58e9c7fd4541..0000000000000 --- a/library/std/src/sys/pal/teeos/locks/mod.rs +++ /dev/null @@ -1,8 +0,0 @@ -pub mod condvar; -#[path = "../../unix/locks/pthread_mutex.rs"] -pub mod mutex; -pub mod rwlock; - -pub(crate) use condvar::Condvar; -pub(crate) use mutex::Mutex; -pub(crate) use rwlock::RwLock; diff --git a/library/std/src/sys/pal/teeos/mod.rs b/library/std/src/sys/pal/teeos/mod.rs index 7953104486c56..51ef96a69a0f9 100644 --- a/library/std/src/sys/pal/teeos/mod.rs +++ b/library/std/src/sys/pal/teeos/mod.rs @@ -13,7 +13,6 @@ pub mod alloc; pub mod args; #[path = "../unsupported/env.rs"] pub mod env; -pub mod locks; //pub mod fd; #[path = "../unsupported/fs.rs"] pub mod fs; From 491d1a76646123e1d68e32ab4e52ddbe6065d82a Mon Sep 17 00:00:00 2001 From: joboet Date: Thu, 15 Feb 2024 17:40:33 +0100 Subject: [PATCH 4/9] std: move locks to `sys` on UNIX and other futex platforms --- .../condvar/futex.rs} | 2 +- library/std/src/sys/locks/condvar/mod.rs | 17 +- .../condvar/pthread.rs} | 8 +- .../mutex/fuchsia.rs} | 0 .../futex_mutex.rs => locks/mutex/futex.rs} | 0 library/std/src/sys/locks/mutex/mod.rs | 19 ++- .../futex_rwlock.rs => locks/rwlock/futex.rs} | 0 library/std/src/sys/locks/rwlock/mod.rs | 17 +- .../queue_rwlock.rs => locks/rwlock/queue.rs} | 0 library/std/src/sys/pal/hermit/mod.rs | 10 -- library/std/src/sys/pal/unix/locks/mod.rs | 31 ---- .../src/sys/pal/unix/locks/pthread_mutex.rs | 148 ------------------ library/std/src/sys/pal/unix/mod.rs | 1 - library/std/src/sys/pal/wasi/mod.rs | 10 -- library/std/src/sys/pal/wasm/mod.rs | 10 -- 15 files changed, 55 insertions(+), 218 deletions(-) rename library/std/src/sys/{pal/unix/locks/futex_condvar.rs => locks/condvar/futex.rs} (98%) rename library/std/src/sys/{pal/unix/locks/pthread_condvar.rs => locks/condvar/pthread.rs} (97%) rename library/std/src/sys/{pal/unix/locks/fuchsia_mutex.rs => locks/mutex/fuchsia.rs} (100%) rename library/std/src/sys/{pal/unix/locks/futex_mutex.rs => locks/mutex/futex.rs} (100%) rename library/std/src/sys/{pal/unix/locks/futex_rwlock.rs => locks/rwlock/futex.rs} (100%) rename library/std/src/sys/{pal/unix/locks/queue_rwlock.rs => locks/rwlock/queue.rs} (100%) delete mode 100644 library/std/src/sys/pal/unix/locks/mod.rs delete mode 100644 library/std/src/sys/pal/unix/locks/pthread_mutex.rs diff --git a/library/std/src/sys/pal/unix/locks/futex_condvar.rs b/library/std/src/sys/locks/condvar/futex.rs similarity index 98% rename from library/std/src/sys/pal/unix/locks/futex_condvar.rs rename to library/std/src/sys/locks/condvar/futex.rs index 4bd65dd25c292..3ad93ce07f753 100644 --- a/library/std/src/sys/pal/unix/locks/futex_condvar.rs +++ b/library/std/src/sys/locks/condvar/futex.rs @@ -1,6 +1,6 @@ -use super::Mutex; use crate::sync::atomic::{AtomicU32, Ordering::Relaxed}; use crate::sys::futex::{futex_wait, futex_wake, futex_wake_all}; +use crate::sys::locks::Mutex; use crate::time::Duration; pub struct Condvar { diff --git a/library/std/src/sys/locks/condvar/mod.rs b/library/std/src/sys/locks/condvar/mod.rs index 74494cdf66d79..8bae14b57656b 100644 --- a/library/std/src/sys/locks/condvar/mod.rs +++ b/library/std/src/sys/locks/condvar/mod.rs @@ -1,5 +1,20 @@ cfg_if::cfg_if! { - if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] { + if #[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + target_os = "openbsd", + target_os = "dragonfly", + target_os = "fuchsia", + all(target_family = "wasm", target_feature = "atomics"), + target_os = "hermit", + ))] { + mod futex; + pub use futex::Condvar; + } else if #[cfg(target_family = "unix")] { + mod pthread; + pub use pthread::Condvar; + } else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] { mod sgx; pub use sgx::Condvar; } else if #[cfg(target_os = "solid_asp3")] { diff --git a/library/std/src/sys/pal/unix/locks/pthread_condvar.rs b/library/std/src/sys/locks/condvar/pthread.rs similarity index 97% rename from library/std/src/sys/pal/unix/locks/pthread_condvar.rs rename to library/std/src/sys/locks/condvar/pthread.rs index 2dc1b0c601e78..094738d5a3f2c 100644 --- a/library/std/src/sys/pal/unix/locks/pthread_condvar.rs +++ b/library/std/src/sys/locks/condvar/pthread.rs @@ -1,7 +1,7 @@ use crate::cell::UnsafeCell; use crate::ptr; use crate::sync::atomic::{AtomicPtr, Ordering::Relaxed}; -use crate::sys::locks::{pthread_mutex, Mutex}; +use crate::sys::locks::{mutex, Mutex}; #[cfg(not(target_os = "nto"))] use crate::sys::time::TIMESPEC_MAX; #[cfg(target_os = "nto")] @@ -112,7 +112,7 @@ impl Condvar { #[inline] pub unsafe fn wait(&self, mutex: &Mutex) { - let mutex = pthread_mutex::raw(mutex); + let mutex = mutex::raw(mutex); self.verify(mutex); let r = libc::pthread_cond_wait(raw(self), mutex); debug_assert_eq!(r, 0); @@ -134,7 +134,7 @@ impl Condvar { pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool { use crate::sys::time::Timespec; - let mutex = pthread_mutex::raw(mutex); + let mutex = mutex::raw(mutex); self.verify(mutex); #[cfg(not(target_os = "nto"))] @@ -170,7 +170,7 @@ impl Condvar { use crate::sys::time::SystemTime; use crate::time::Instant; - let mutex = pthread_mutex::raw(mutex); + let mutex = mutex::raw(mutex); self.verify(mutex); // OSX implementation of `pthread_cond_timedwait` is buggy diff --git a/library/std/src/sys/pal/unix/locks/fuchsia_mutex.rs b/library/std/src/sys/locks/mutex/fuchsia.rs similarity index 100% rename from library/std/src/sys/pal/unix/locks/fuchsia_mutex.rs rename to library/std/src/sys/locks/mutex/fuchsia.rs diff --git a/library/std/src/sys/pal/unix/locks/futex_mutex.rs b/library/std/src/sys/locks/mutex/futex.rs similarity index 100% rename from library/std/src/sys/pal/unix/locks/futex_mutex.rs rename to library/std/src/sys/locks/mutex/futex.rs diff --git a/library/std/src/sys/locks/mutex/mod.rs b/library/std/src/sys/locks/mutex/mod.rs index b94608c849f7a..170eb6be98c9c 100644 --- a/library/std/src/sys/locks/mutex/mod.rs +++ b/library/std/src/sys/locks/mutex/mod.rs @@ -1,5 +1,22 @@ cfg_if::cfg_if! { - if #[cfg(target_os = "teeos")] { + if #[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + target_os = "openbsd", + target_os = "dragonfly", + all(target_family = "wasm", target_feature = "atomics"), + target_os = "hermit", + ))] { + mod futex; + pub use futex::Mutex; + } else if #[cfg(target_os = "fuchsia")] { + mod fuchsia; + pub use fuchsia::Mutex; + } else if #[cfg(any( + target_family = "unix", + target_os = "teeos", + ))] { mod pthread; pub use pthread::{Mutex, raw}; } else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] { diff --git a/library/std/src/sys/pal/unix/locks/futex_rwlock.rs b/library/std/src/sys/locks/rwlock/futex.rs similarity index 100% rename from library/std/src/sys/pal/unix/locks/futex_rwlock.rs rename to library/std/src/sys/locks/rwlock/futex.rs diff --git a/library/std/src/sys/locks/rwlock/mod.rs b/library/std/src/sys/locks/rwlock/mod.rs index 7de4a9d50a82a..96a086fc64b3b 100644 --- a/library/std/src/sys/locks/rwlock/mod.rs +++ b/library/std/src/sys/locks/rwlock/mod.rs @@ -1,5 +1,20 @@ cfg_if::cfg_if! { - if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] { + if #[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + target_os = "openbsd", + target_os = "dragonfly", + target_os = "fuchsia", + all(target_family = "wasm", target_feature = "atomics"), + target_os = "hermit", + ))] { + mod futex; + pub use futex::RwLock; + } else if #[cfg(target_family = "unix")] { + mod queue; + pub use queue::RwLock; + } else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] { mod sgx; pub use sgx::RwLock; } else if #[cfg(target_os = "solid_asp3")] { diff --git a/library/std/src/sys/pal/unix/locks/queue_rwlock.rs b/library/std/src/sys/locks/rwlock/queue.rs similarity index 100% rename from library/std/src/sys/pal/unix/locks/queue_rwlock.rs rename to library/std/src/sys/locks/rwlock/queue.rs diff --git a/library/std/src/sys/pal/hermit/mod.rs b/library/std/src/sys/pal/hermit/mod.rs index 57cc656e266a1..ada408107dc36 100644 --- a/library/std/src/sys/pal/hermit/mod.rs +++ b/library/std/src/sys/pal/hermit/mod.rs @@ -39,16 +39,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; use crate::os::hermit::abi; diff --git a/library/std/src/sys/pal/unix/locks/mod.rs b/library/std/src/sys/pal/unix/locks/mod.rs deleted file mode 100644 index a49247310b54c..0000000000000 --- a/library/std/src/sys/pal/unix/locks/mod.rs +++ /dev/null @@ -1,31 +0,0 @@ -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")] { - 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_condvar; - mod queue_rwlock; - pub(crate) use pthread_mutex::Mutex; - pub(crate) use queue_rwlock::RwLock; - pub(crate) use pthread_condvar::Condvar; - } -} diff --git a/library/std/src/sys/pal/unix/locks/pthread_mutex.rs b/library/std/src/sys/pal/unix/locks/pthread_mutex.rs deleted file mode 100644 index ee0794334fbe3..0000000000000 --- a/library/std/src/sys/pal/unix/locks/pthread_mutex.rs +++ /dev/null @@ -1,148 +0,0 @@ -use crate::cell::UnsafeCell; -use crate::io::Error; -use crate::mem::{forget, MaybeUninit}; -use crate::sys::cvt_nz; -use crate::sys_common::lazy_box::{LazyBox, LazyInit}; - -struct AllocatedMutex(UnsafeCell); - -pub struct Mutex { - inner: LazyBox, -} - -#[inline] -pub unsafe fn raw(m: &Mutex) -> *mut libc::pthread_mutex_t { - m.inner.0.get() -} - -unsafe impl Send for AllocatedMutex {} -unsafe impl Sync for AllocatedMutex {} - -impl LazyInit for AllocatedMutex { - fn init() -> Box { - let mutex = Box::new(AllocatedMutex(UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER))); - - // Issue #33770 - // - // A pthread mutex initialized with PTHREAD_MUTEX_INITIALIZER will have - // a type of PTHREAD_MUTEX_DEFAULT, which has undefined behavior if you - // try to re-lock it from the same thread when you already hold a lock - // (https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_init.html). - // This is the case even if PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL - // (https://github.com/rust-lang/rust/issues/33770#issuecomment-220847521) -- in that - // case, `pthread_mutexattr_settype(PTHREAD_MUTEX_DEFAULT)` will of course be the same - // as setting it to `PTHREAD_MUTEX_NORMAL`, but not setting any mode will result in - // a Mutex where re-locking is UB. - // - // In practice, glibc takes advantage of this undefined behavior to - // implement hardware lock elision, which uses hardware transactional - // memory to avoid acquiring the lock. While a transaction is in - // progress, the lock appears to be unlocked. This isn't a problem for - // other threads since the transactional memory will abort if a conflict - // is detected, however no abort is generated when re-locking from the - // same thread. - // - // Since locking the same mutex twice will result in two aliasing &mut - // references, we instead create the mutex with type - // PTHREAD_MUTEX_NORMAL which is guaranteed to deadlock if we try to - // re-lock it from the same thread, thus avoiding undefined behavior. - unsafe { - let mut attr = MaybeUninit::::uninit(); - cvt_nz(libc::pthread_mutexattr_init(attr.as_mut_ptr())).unwrap(); - let attr = PthreadMutexAttr(&mut attr); - cvt_nz(libc::pthread_mutexattr_settype( - attr.0.as_mut_ptr(), - libc::PTHREAD_MUTEX_NORMAL, - )) - .unwrap(); - cvt_nz(libc::pthread_mutex_init(mutex.0.get(), attr.0.as_ptr())).unwrap(); - } - - mutex - } - - fn destroy(mutex: Box) { - // We're not allowed to pthread_mutex_destroy a locked mutex, - // so check first if it's unlocked. - if unsafe { libc::pthread_mutex_trylock(mutex.0.get()) == 0 } { - unsafe { libc::pthread_mutex_unlock(mutex.0.get()) }; - drop(mutex); - } else { - // The mutex is locked. This happens if a MutexGuard is leaked. - // In this case, we just leak the Mutex too. - forget(mutex); - } - } - - fn cancel_init(_: Box) { - // In this case, we can just drop it without any checks, - // since it cannot have been locked yet. - } -} - -impl Drop for AllocatedMutex { - #[inline] - fn drop(&mut self) { - let r = unsafe { libc::pthread_mutex_destroy(self.0.get()) }; - if cfg!(target_os = "dragonfly") { - // On DragonFly pthread_mutex_destroy() returns EINVAL if called on a - // mutex that was just initialized with libc::PTHREAD_MUTEX_INITIALIZER. - // Once it is used (locked/unlocked) or pthread_mutex_init() is called, - // this behaviour no longer occurs. - debug_assert!(r == 0 || r == libc::EINVAL); - } else { - debug_assert_eq!(r, 0); - } - } -} - -impl Mutex { - #[inline] - pub const fn new() -> Mutex { - Mutex { inner: LazyBox::new() } - } - - #[inline] - pub unsafe fn lock(&self) { - #[cold] - #[inline(never)] - fn fail(r: i32) -> ! { - let error = Error::from_raw_os_error(r); - panic!("failed to lock mutex: {error}"); - } - - let r = libc::pthread_mutex_lock(raw(self)); - // As we set the mutex type to `PTHREAD_MUTEX_NORMAL` above, we expect - // the lock call to never fail. Unfortunately however, some platforms - // (Solaris) do not conform to the standard, and instead always provide - // deadlock detection. How kind of them! Unfortunately that means that - // we need to check the error code here. To save us from UB on other - // less well-behaved platforms in the future, we do it even on "good" - // platforms like macOS. See #120147 for more context. - if r != 0 { - fail(r) - } - } - - #[inline] - pub unsafe fn unlock(&self) { - let r = libc::pthread_mutex_unlock(raw(self)); - debug_assert_eq!(r, 0); - } - - #[inline] - pub unsafe fn try_lock(&self) -> bool { - libc::pthread_mutex_trylock(raw(self)) == 0 - } -} - -pub(super) struct PthreadMutexAttr<'a>(pub &'a mut MaybeUninit); - -impl Drop for PthreadMutexAttr<'_> { - fn drop(&mut self) { - unsafe { - let result = libc::pthread_mutexattr_destroy(self.0.as_mut_ptr()); - debug_assert_eq!(result, 0); - } - } -} diff --git a/library/std/src/sys/pal/unix/mod.rs b/library/std/src/sys/pal/unix/mod.rs index 976a437c17ff9..04b8c5ca91604 100644 --- a/library/std/src/sys/pal/unix/mod.rs +++ b/library/std/src/sys/pal/unix/mod.rs @@ -20,7 +20,6 @@ pub mod io; pub mod kernel_copy; #[cfg(target_os = "l4re")] mod l4re; -pub mod locks; pub mod memchr; #[cfg(not(target_os = "l4re"))] pub mod net; diff --git a/library/std/src/sys/pal/wasi/mod.rs b/library/std/src/sys/pal/wasi/mod.rs index 116878ee99681..5de2e0e7d63dc 100644 --- a/library/std/src/sys/pal/wasi/mod.rs +++ b/library/std/src/sys/pal/wasi/mod.rs @@ -44,16 +44,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; - } } else { #[path = "../unsupported/locks/mod.rs"] pub mod locks; diff --git a/library/std/src/sys/pal/wasm/mod.rs b/library/std/src/sys/pal/wasm/mod.rs index 567555118d707..910a54b2e0109 100644 --- a/library/std/src/sys/pal/wasm/mod.rs +++ b/library/std/src/sys/pal/wasm/mod.rs @@ -43,16 +43,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"] From 6ee45102fe4f2d7db1b2c60bcac93213b83a4578 Mon Sep 17 00:00:00 2001 From: joboet Date: Thu, 15 Feb 2024 17:47:42 +0100 Subject: [PATCH 5/9] std: move locks to `sys` on Windows --- library/std/src/sys/locks/condvar/mod.rs | 3 +++ .../windows/locks/condvar.rs => locks/condvar/windows.rs} | 2 +- library/std/src/sys/locks/mutex/mod.rs | 3 +++ .../{pal/windows/locks/mutex.rs => locks/mutex/windows.rs} | 0 library/std/src/sys/locks/rwlock/mod.rs | 3 +++ .../windows/locks/rwlock.rs => locks/rwlock/windows.rs} | 0 library/std/src/sys/pal/windows/locks/mod.rs | 6 ------ library/std/src/sys/pal/windows/mod.rs | 1 - 8 files changed, 10 insertions(+), 8 deletions(-) rename library/std/src/sys/{pal/windows/locks/condvar.rs => locks/condvar/windows.rs} (95%) rename library/std/src/sys/{pal/windows/locks/mutex.rs => locks/mutex/windows.rs} (100%) rename library/std/src/sys/{pal/windows/locks/rwlock.rs => locks/rwlock/windows.rs} (100%) delete mode 100644 library/std/src/sys/pal/windows/locks/mod.rs diff --git a/library/std/src/sys/locks/condvar/mod.rs b/library/std/src/sys/locks/condvar/mod.rs index 8bae14b57656b..889fcbaaaf11d 100644 --- a/library/std/src/sys/locks/condvar/mod.rs +++ b/library/std/src/sys/locks/condvar/mod.rs @@ -14,6 +14,9 @@ cfg_if::cfg_if! { } else if #[cfg(target_family = "unix")] { mod pthread; pub use pthread::Condvar; + } else if #[cfg(target_os = "windows")] { + mod windows; + pub use windows::Condvar; } else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] { mod sgx; pub use sgx::Condvar; diff --git a/library/std/src/sys/pal/windows/locks/condvar.rs b/library/std/src/sys/locks/condvar/windows.rs similarity index 95% rename from library/std/src/sys/pal/windows/locks/condvar.rs rename to library/std/src/sys/locks/condvar/windows.rs index 953bcc27dee0d..28a288335d2fb 100644 --- a/library/std/src/sys/pal/windows/locks/condvar.rs +++ b/library/std/src/sys/locks/condvar/windows.rs @@ -27,7 +27,7 @@ impl Condvar { let r = c::SleepConditionVariableSRW( self.inner.get(), mutex::raw(mutex), - crate::sys::pal::windows::dur2timeout(dur), + crate::sys::pal::dur2timeout(dur), 0, ); if r == 0 { diff --git a/library/std/src/sys/locks/mutex/mod.rs b/library/std/src/sys/locks/mutex/mod.rs index 170eb6be98c9c..2c4c2f4ef488b 100644 --- a/library/std/src/sys/locks/mutex/mod.rs +++ b/library/std/src/sys/locks/mutex/mod.rs @@ -19,6 +19,9 @@ cfg_if::cfg_if! { ))] { mod pthread; pub use pthread::{Mutex, raw}; + } else if #[cfg(target_os = "windows")] { + mod windows; + pub use windows::{Mutex, raw}; } else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] { mod sgx; pub use sgx::Mutex; diff --git a/library/std/src/sys/pal/windows/locks/mutex.rs b/library/std/src/sys/locks/mutex/windows.rs similarity index 100% rename from library/std/src/sys/pal/windows/locks/mutex.rs rename to library/std/src/sys/locks/mutex/windows.rs diff --git a/library/std/src/sys/locks/rwlock/mod.rs b/library/std/src/sys/locks/rwlock/mod.rs index 96a086fc64b3b..35fff36c25df6 100644 --- a/library/std/src/sys/locks/rwlock/mod.rs +++ b/library/std/src/sys/locks/rwlock/mod.rs @@ -14,6 +14,9 @@ cfg_if::cfg_if! { } else if #[cfg(target_family = "unix")] { mod queue; pub use queue::RwLock; + } else if #[cfg(target_os = "windows")] { + mod windows; + pub use windows::RwLock; } else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] { mod sgx; pub use sgx::RwLock; diff --git a/library/std/src/sys/pal/windows/locks/rwlock.rs b/library/std/src/sys/locks/rwlock/windows.rs similarity index 100% rename from library/std/src/sys/pal/windows/locks/rwlock.rs rename to library/std/src/sys/locks/rwlock/windows.rs diff --git a/library/std/src/sys/pal/windows/locks/mod.rs b/library/std/src/sys/pal/windows/locks/mod.rs deleted file mode 100644 index 0e0f9eccb2137..0000000000000 --- a/library/std/src/sys/pal/windows/locks/mod.rs +++ /dev/null @@ -1,6 +0,0 @@ -mod condvar; -mod mutex; -mod rwlock; -pub use condvar::Condvar; -pub use mutex::Mutex; -pub use rwlock::RwLock; diff --git a/library/std/src/sys/pal/windows/mod.rs b/library/std/src/sys/pal/windows/mod.rs index 726a4509f280f..b47d213df343a 100644 --- a/library/std/src/sys/pal/windows/mod.rs +++ b/library/std/src/sys/pal/windows/mod.rs @@ -19,7 +19,6 @@ pub mod env; pub mod fs; pub mod handle; pub mod io; -pub mod locks; pub mod memchr; pub mod net; pub mod os; From f77c4d57fce469b5cdee5f7b871ffe3ee4f633ef Mon Sep 17 00:00:00 2001 From: joboet Date: Thu, 15 Feb 2024 17:52:24 +0100 Subject: [PATCH 6/9] std: move locks to `sys` on xous --- library/std/src/sys/locks/condvar/mod.rs | 3 +++ .../{pal/xous/locks/condvar.rs => locks/condvar/xous.rs} | 2 +- library/std/src/sys/locks/mutex/mod.rs | 5 ++++- .../sys/{pal/xous/locks/mutex.rs => locks/mutex/xous.rs} | 0 library/std/src/sys/locks/rwlock/mod.rs | 3 +++ .../sys/{pal/xous/locks/rwlock.rs => locks/rwlock/xous.rs} | 0 library/std/src/sys/pal/xous/locks/mod.rs | 7 ------- library/std/src/sys/pal/xous/mod.rs | 1 - 8 files changed, 11 insertions(+), 10 deletions(-) rename library/std/src/sys/{pal/xous/locks/condvar.rs => locks/condvar/xous.rs} (99%) rename library/std/src/sys/{pal/xous/locks/mutex.rs => locks/mutex/xous.rs} (100%) rename library/std/src/sys/{pal/xous/locks/rwlock.rs => locks/rwlock/xous.rs} (100%) delete mode 100644 library/std/src/sys/pal/xous/locks/mod.rs diff --git a/library/std/src/sys/locks/condvar/mod.rs b/library/std/src/sys/locks/condvar/mod.rs index 889fcbaaaf11d..425b88c1bf08b 100644 --- a/library/std/src/sys/locks/condvar/mod.rs +++ b/library/std/src/sys/locks/condvar/mod.rs @@ -26,5 +26,8 @@ cfg_if::cfg_if! { } else if #[cfg(target_os = "teeos")] { mod teeos; pub use teeos::Condvar; + } else if #[cfg(target_os = "xous")] { + mod xous; + pub use xous::Condvar; } } diff --git a/library/std/src/sys/pal/xous/locks/condvar.rs b/library/std/src/sys/locks/condvar/xous.rs similarity index 99% rename from library/std/src/sys/pal/xous/locks/condvar.rs rename to library/std/src/sys/locks/condvar/xous.rs index 510235046e195..0e51449e0afa4 100644 --- a/library/std/src/sys/pal/xous/locks/condvar.rs +++ b/library/std/src/sys/locks/condvar/xous.rs @@ -1,6 +1,6 @@ -use super::mutex::Mutex; use crate::os::xous::ffi::{blocking_scalar, scalar}; use crate::os::xous::services::{ticktimer_server, TicktimerScalar}; +use crate::sys::locks::Mutex; use crate::time::Duration; use core::sync::atomic::{AtomicUsize, Ordering}; diff --git a/library/std/src/sys/locks/mutex/mod.rs b/library/std/src/sys/locks/mutex/mod.rs index 2c4c2f4ef488b..cb229d50e558d 100644 --- a/library/std/src/sys/locks/mutex/mod.rs +++ b/library/std/src/sys/locks/mutex/mod.rs @@ -27,6 +27,9 @@ cfg_if::cfg_if! { pub use sgx::Mutex; } else if #[cfg(target_os = "solid_asp3")] { mod itron; - pub use itron::Condvar; + pub use itron::Mutex; + } else if #[cfg(target_os = "xous")] { + mod xous; + pub use xous::Mutex; } } diff --git a/library/std/src/sys/pal/xous/locks/mutex.rs b/library/std/src/sys/locks/mutex/xous.rs similarity index 100% rename from library/std/src/sys/pal/xous/locks/mutex.rs rename to library/std/src/sys/locks/mutex/xous.rs diff --git a/library/std/src/sys/locks/rwlock/mod.rs b/library/std/src/sys/locks/rwlock/mod.rs index 35fff36c25df6..9d656e57bb395 100644 --- a/library/std/src/sys/locks/rwlock/mod.rs +++ b/library/std/src/sys/locks/rwlock/mod.rs @@ -26,5 +26,8 @@ cfg_if::cfg_if! { } else if #[cfg(target_os = "teeos")] { mod teeos; pub use teeos::RwLock; + } else if #[cfg(target_os = "xous")] { + mod xous; + pub use xous::RwLock; } } diff --git a/library/std/src/sys/pal/xous/locks/rwlock.rs b/library/std/src/sys/locks/rwlock/xous.rs similarity index 100% rename from library/std/src/sys/pal/xous/locks/rwlock.rs rename to library/std/src/sys/locks/rwlock/xous.rs diff --git a/library/std/src/sys/pal/xous/locks/mod.rs b/library/std/src/sys/pal/xous/locks/mod.rs deleted file mode 100644 index f3c5c5d9fb0ce..0000000000000 --- a/library/std/src/sys/pal/xous/locks/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -mod condvar; -mod mutex; -mod rwlock; - -pub use condvar::*; -pub use mutex::*; -pub use rwlock::*; diff --git a/library/std/src/sys/pal/xous/mod.rs b/library/std/src/sys/pal/xous/mod.rs index c9bad4ef019b5..7914a255aeaa1 100644 --- a/library/std/src/sys/pal/xous/mod.rs +++ b/library/std/src/sys/pal/xous/mod.rs @@ -9,7 +9,6 @@ pub mod env; pub mod fs; #[path = "../unsupported/io.rs"] pub mod io; -pub mod locks; pub mod net; pub mod os; #[path = "../unsupported/pipe.rs"] From 21fef03da217282b484035505c1bc09a175a2eeb Mon Sep 17 00:00:00 2001 From: joboet Date: Thu, 15 Feb 2024 18:00:24 +0100 Subject: [PATCH 7/9] std: move locks to `sys` on platforms without threads --- library/std/src/sys/locks/condvar/mod.rs | 3 +++ .../locks/condvar.rs => locks/condvar/no_threads.rs} | 0 library/std/src/sys/locks/mutex/mod.rs | 3 +++ .../locks/mutex.rs => locks/mutex/no_threads.rs} | 0 library/std/src/sys/locks/rwlock/mod.rs | 3 +++ .../locks/rwlock.rs => locks/rwlock/no_threads.rs} | 0 library/std/src/sys/pal/uefi/mod.rs | 2 -- library/std/src/sys/pal/unsupported/locks/mod.rs | 6 ------ library/std/src/sys/pal/unsupported/mod.rs | 1 - library/std/src/sys/pal/wasi/mod.rs | 5 +---- library/std/src/sys/pal/wasm/mod.rs | 2 -- library/std/src/sys/pal/zkvm/mod.rs | 2 -- 12 files changed, 10 insertions(+), 17 deletions(-) rename library/std/src/sys/{pal/unsupported/locks/condvar.rs => locks/condvar/no_threads.rs} (100%) rename library/std/src/sys/{pal/unsupported/locks/mutex.rs => locks/mutex/no_threads.rs} (100%) rename library/std/src/sys/{pal/unsupported/locks/rwlock.rs => locks/rwlock/no_threads.rs} (100%) delete mode 100644 library/std/src/sys/pal/unsupported/locks/mod.rs diff --git a/library/std/src/sys/locks/condvar/mod.rs b/library/std/src/sys/locks/condvar/mod.rs index 425b88c1bf08b..126a42a2a4c00 100644 --- a/library/std/src/sys/locks/condvar/mod.rs +++ b/library/std/src/sys/locks/condvar/mod.rs @@ -29,5 +29,8 @@ cfg_if::cfg_if! { } else if #[cfg(target_os = "xous")] { mod xous; pub use xous::Condvar; + } else { + mod no_threads; + pub use no_threads::Condvar; } } diff --git a/library/std/src/sys/pal/unsupported/locks/condvar.rs b/library/std/src/sys/locks/condvar/no_threads.rs similarity index 100% rename from library/std/src/sys/pal/unsupported/locks/condvar.rs rename to library/std/src/sys/locks/condvar/no_threads.rs diff --git a/library/std/src/sys/locks/mutex/mod.rs b/library/std/src/sys/locks/mutex/mod.rs index cb229d50e558d..710cb91fb1473 100644 --- a/library/std/src/sys/locks/mutex/mod.rs +++ b/library/std/src/sys/locks/mutex/mod.rs @@ -31,5 +31,8 @@ cfg_if::cfg_if! { } else if #[cfg(target_os = "xous")] { mod xous; pub use xous::Mutex; + } else { + mod no_threads; + pub use no_threads::Mutex; } } diff --git a/library/std/src/sys/pal/unsupported/locks/mutex.rs b/library/std/src/sys/locks/mutex/no_threads.rs similarity index 100% rename from library/std/src/sys/pal/unsupported/locks/mutex.rs rename to library/std/src/sys/locks/mutex/no_threads.rs diff --git a/library/std/src/sys/locks/rwlock/mod.rs b/library/std/src/sys/locks/rwlock/mod.rs index 9d656e57bb395..0564f1fe6fab9 100644 --- a/library/std/src/sys/locks/rwlock/mod.rs +++ b/library/std/src/sys/locks/rwlock/mod.rs @@ -29,5 +29,8 @@ cfg_if::cfg_if! { } else if #[cfg(target_os = "xous")] { mod xous; pub use xous::RwLock; + } else { + mod no_threads; + pub use no_threads::RwLock; } } diff --git a/library/std/src/sys/pal/unsupported/locks/rwlock.rs b/library/std/src/sys/locks/rwlock/no_threads.rs similarity index 100% rename from library/std/src/sys/pal/unsupported/locks/rwlock.rs rename to library/std/src/sys/locks/rwlock/no_threads.rs diff --git a/library/std/src/sys/pal/uefi/mod.rs b/library/std/src/sys/pal/uefi/mod.rs index 5a96b8f1c3a07..ff8e3bd32adfe 100644 --- a/library/std/src/sys/pal/uefi/mod.rs +++ b/library/std/src/sys/pal/uefi/mod.rs @@ -19,8 +19,6 @@ pub mod env; pub mod fs; #[path = "../unsupported/io.rs"] pub mod io; -#[path = "../unsupported/locks/mod.rs"] -pub mod locks; #[path = "../unsupported/net.rs"] pub mod net; #[path = "../unsupported/once.rs"] diff --git a/library/std/src/sys/pal/unsupported/locks/mod.rs b/library/std/src/sys/pal/unsupported/locks/mod.rs deleted file mode 100644 index 0e0f9eccb2137..0000000000000 --- a/library/std/src/sys/pal/unsupported/locks/mod.rs +++ /dev/null @@ -1,6 +0,0 @@ -mod condvar; -mod mutex; -mod rwlock; -pub use condvar::Condvar; -pub use mutex::Mutex; -pub use rwlock::RwLock; diff --git a/library/std/src/sys/pal/unsupported/mod.rs b/library/std/src/sys/pal/unsupported/mod.rs index 88f939cbab924..9ce275ee72d58 100644 --- a/library/std/src/sys/pal/unsupported/mod.rs +++ b/library/std/src/sys/pal/unsupported/mod.rs @@ -5,7 +5,6 @@ pub mod args; pub mod env; pub mod fs; pub mod io; -pub mod locks; pub mod net; pub mod once; pub mod os; diff --git a/library/std/src/sys/pal/wasi/mod.rs b/library/std/src/sys/pal/wasi/mod.rs index 5de2e0e7d63dc..084b8e0e21639 100644 --- a/library/std/src/sys/pal/wasi/mod.rs +++ b/library/std/src/sys/pal/wasi/mod.rs @@ -43,10 +43,7 @@ pub mod thread_local_key; pub mod time; cfg_if::cfg_if! { - if #[cfg(target_feature = "atomics")] { - } else { - #[path = "../unsupported/locks/mod.rs"] - pub mod locks; + if #[cfg(not(target_feature = "atomics"))] { #[path = "../unsupported/once.rs"] pub mod once; #[path = "../unsupported/thread_parking.rs"] diff --git a/library/std/src/sys/pal/wasm/mod.rs b/library/std/src/sys/pal/wasm/mod.rs index 910a54b2e0109..40b15120e6daa 100644 --- a/library/std/src/sys/pal/wasm/mod.rs +++ b/library/std/src/sys/pal/wasm/mod.rs @@ -48,8 +48,6 @@ cfg_if::cfg_if! { #[path = "atomics/thread.rs"] pub mod thread; } else { - #[path = "../unsupported/locks/mod.rs"] - pub mod locks; #[path = "../unsupported/once.rs"] pub mod once; #[path = "../unsupported/thread.rs"] diff --git a/library/std/src/sys/pal/zkvm/mod.rs b/library/std/src/sys/pal/zkvm/mod.rs index e859269831aa9..016c977dc33d6 100644 --- a/library/std/src/sys/pal/zkvm/mod.rs +++ b/library/std/src/sys/pal/zkvm/mod.rs @@ -33,8 +33,6 @@ pub mod thread_local_key; #[path = "../unsupported/time.rs"] pub mod time; -#[path = "../unsupported/locks/mod.rs"] -pub mod locks; #[path = "../unsupported/thread.rs"] pub mod thread; From 4ec5b980f9bf257bb1b0ef5fddf8194dfb55034d Mon Sep 17 00:00:00 2001 From: joboet Date: Fri, 16 Feb 2024 08:22:23 +0100 Subject: [PATCH 8/9] update license reference --- .reuse/dep5 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.reuse/dep5 b/.reuse/dep5 index 2c3adf1bfa1ba..6c39025b5de0d 100644 --- a/.reuse/dep5 +++ b/.reuse/dep5 @@ -52,7 +52,7 @@ Copyright: 2019 The Crossbeam Project Developers The Rust Project Developers (see https://thanks.rust-lang.org) License: MIT OR Apache-2.0 -Files: library/std/src/sys/pal/unix/locks/fuchsia_mutex.rs +Files: library/std/src/sys/locks/mutex/fuchsia.rs Copyright: 2016 The Fuchsia Authors The Rust Project Developers (see https://thanks.rust-lang.org) License: BSD-2-Clause AND (MIT OR Apache-2.0) From 1b1b6dd95f805daa1503627ad0c7288f7889616a Mon Sep 17 00:00:00 2001 From: joboet Date: Fri, 16 Feb 2024 08:24:05 +0100 Subject: [PATCH 9/9] update debuginfo test --- tests/debuginfo/mutex.rs | 2 +- tests/debuginfo/rwlock-read.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/debuginfo/mutex.rs b/tests/debuginfo/mutex.rs index 7a58c5c2224be..ab9fb8b7e81d9 100644 --- a/tests/debuginfo/mutex.rs +++ b/tests/debuginfo/mutex.rs @@ -10,7 +10,7 @@ // // cdb-command:dx m,d // cdb-check:m,d [Type: std::sync::mutex::Mutex] -// cdb-check: [...] inner [Type: std::sys::pal::windows::locks::mutex::Mutex] +// cdb-check: [...] inner [Type: std::sys::locks::mutex::windows::Mutex] // cdb-check: [...] poison [Type: std::sync::poison::Flag] // cdb-check: [...] data : 0 [Type: core::cell::UnsafeCell] diff --git a/tests/debuginfo/rwlock-read.rs b/tests/debuginfo/rwlock-read.rs index 4ed1ebd0b37c4..7e9838871ba43 100644 --- a/tests/debuginfo/rwlock-read.rs +++ b/tests/debuginfo/rwlock-read.rs @@ -16,7 +16,7 @@ // cdb-command:dx r // cdb-check:r [Type: std::sync::rwlock::RwLockReadGuard] // cdb-check: [...] data : NonNull([...]: 0) [Type: core::ptr::non_null::NonNull] -// cdb-check: [...] inner_lock : [...] [Type: std::sys::pal::windows::locks::rwlock::RwLock *] +// cdb-check: [...] inner_lock : [...] [Type: std::sys::locks::rwlock::windows::RwLock *] #[allow(unused_variables)]