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 locks to sys #121177

Merged
merged 9 commits into from
Feb 19, 2024
Merged
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 .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
36 changes: 36 additions & 0 deletions library/std/src/sys/locks/condvar/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
cfg_if::cfg_if! {
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(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;
} 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;
} else if #[cfg(target_os = "xous")] {
mod xous;
pub use xous::Condvar;
} else {
mod no_threads;
pub use no_threads::Condvar;
}
}
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -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);
Expand All @@ -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"))]
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<WaitVariable<()>>);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod condvar;
mod mutex;
mod rwlock;

pub use condvar::Condvar;
pub use mutex::Mutex;
pub use rwlock::RwLock;
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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() };
}
}
38 changes: 38 additions & 0 deletions library/std/src/sys/locks/mutex/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
cfg_if::cfg_if! {
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(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;
} else if #[cfg(target_os = "solid_asp3")] {
mod itron;
pub use itron::Mutex;
} else if #[cfg(target_os = "xous")] {
mod xous;
pub use xous::Mutex;
} else {
mod no_threads;
pub use no_threads::Mutex;
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
36 changes: 36 additions & 0 deletions library/std/src/sys/locks/rwlock/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
cfg_if::cfg_if! {
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(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;
} 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;
} else if #[cfg(target_os = "xous")] {
mod xous;
pub use xous::RwLock;
} else {
mod no_threads;
pub use no_threads::RwLock;
}
}
Original file line number Diff line number Diff line change
@@ -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<WaitVariable<Option<NonZero<usize>>>>,
Expand Down
Original file line number Diff line number Diff line change
@@ -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},
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod pal;
mod personality;

pub mod cmath;
pub mod locks;
pub mod os_str;
pub mod path;

Expand Down
10 changes: 0 additions & 10 deletions library/std/src/sys/pal/hermit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 1 addition & 12 deletions library/std/src/sys/pal/sgx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions library/std/src/sys/pal/solid/abi/fs.rs
Original file line number Diff line number Diff line change
@@ -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,
ChrisDenton marked this conversation as resolved.
Show resolved Hide resolved
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;
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/solid/abi/sockets.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
24 changes: 7 additions & 17 deletions library/std/src/sys/pal/solid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {}
Expand Down
8 changes: 0 additions & 8 deletions library/std/src/sys/pal/teeos/locks/mod.rs

This file was deleted.

1 change: 0 additions & 1 deletion library/std/src/sys/pal/teeos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading
Loading