Skip to content

Commit

Permalink
Auto merge of #95173 - m-ou-se:sys-locks-module, r=dtolnay
Browse files Browse the repository at this point in the history
Move std::sys::{mutex, condvar, rwlock} to std::sys::locks.

This cleans up the the std::sys modules a bit by putting the locks in a single module called `locks` rather than spread over the three modules `mutex`, `condvar`, and `rwlock`. This makes it easier to organise lock implementations, which helps with #93740.
  • Loading branch information
bors committed Mar 23, 2022
2 parents 7b0bf9e + 733153f commit 36748cf
Show file tree
Hide file tree
Showing 32 changed files with 133 additions and 100 deletions.
2 changes: 1 addition & 1 deletion library/std/src/sys/hermit/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::ffi::c_void;
use crate::ptr;
use crate::sync::atomic::{AtomicUsize, Ordering::SeqCst};
use crate::sys::hermit::abi;
use crate::sys::mutex::Mutex;
use crate::sys::locks::Mutex;
use crate::time::Duration;

// The implementation is inspired by Andrew D. Birrell's paper
Expand Down
13 changes: 10 additions & 3 deletions library/std/src/sys/hermit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ pub mod alloc;
pub mod args;
#[path = "../unix/cmath.rs"]
pub mod cmath;
pub mod condvar;
pub mod env;
pub mod fd;
pub mod fs;
#[path = "../unsupported/io.rs"]
pub mod io;
pub mod memchr;
pub mod mutex;
pub mod net;
pub mod os;
#[path = "../unix/os_str.rs"]
Expand All @@ -40,14 +38,23 @@ pub mod path;
pub mod pipe;
#[path = "../unsupported/process.rs"]
pub mod process;
pub mod rwlock;
pub mod stdio;
pub mod thread;
pub mod thread_local_dtor;
#[path = "../unsupported/thread_local_key.rs"]
pub mod thread_local_key;
pub mod time;

mod condvar;
mod mutex;
mod rwlock;

pub mod locks {
pub use super::condvar::*;
pub use super::mutex::*;
pub use super::rwlock::*;
}

use crate::io::ErrorKind;

#[allow(unused_extern_crates)]
Expand Down
3 changes: 1 addition & 2 deletions library/std/src/sys/hermit/rwlock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::cell::UnsafeCell;
use crate::sys::condvar::Condvar;
use crate::sys::mutex::Mutex;
use crate::sys::locks::{Condvar, Mutex};

pub struct RWLock {
lock: Mutex,
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/itron/condvar.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! 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::{mem::replace, ptr::NonNull, sys::mutex::Mutex, time::Duration};
use crate::{mem::replace, ptr::NonNull, sys::locks::Mutex, time::Duration};

// The implementation is inspired by the queue-based implementation shown in
// Andrew D. Birrell's paper "Implementing Condition Variables with Semaphores"
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/sgx/condvar.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::sys::mutex::Mutex;
use crate::sys::locks::Mutex;
use crate::time::Duration;

use super::waitqueue::{SpinMutex, WaitQueue, WaitVariable};
Expand Down
13 changes: 10 additions & 3 deletions library/std/src/sys/sgx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ pub mod alloc;
pub mod args;
#[path = "../unix/cmath.rs"]
pub mod cmath;
pub mod condvar;
pub mod env;
pub mod fd;
#[path = "../unsupported/fs.rs"]
pub mod fs;
#[path = "../unsupported/io.rs"]
pub mod io;
pub mod memchr;
pub mod mutex;
pub mod net;
pub mod os;
#[path = "../unix/os_str.rs"]
Expand All @@ -33,12 +31,21 @@ pub mod path;
pub mod pipe;
#[path = "../unsupported/process.rs"]
pub mod process;
pub mod rwlock;
pub mod stdio;
pub mod thread;
pub mod thread_local_key;
pub mod time;

mod condvar;
mod mutex;
mod rwlock;

pub mod locks {
pub use super::condvar::*;
pub use super::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) {
Expand Down
11 changes: 9 additions & 2 deletions library/std/src/sys/solid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,21 @@ pub mod path;
pub mod pipe;
#[path = "../unsupported/process.rs"]
pub mod process;
pub mod rwlock;
pub mod stdio;
pub use self::itron::{condvar, mutex, thread};
pub use self::itron::thread;
pub mod memchr;
pub mod thread_local_dtor;
pub mod thread_local_key;
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) {}
Expand Down
8 changes: 8 additions & 0 deletions library/std/src/sys/unix/locks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod pthread_condvar;
mod pthread_mutex;
mod pthread_remutex;
mod pthread_rwlock;
pub use pthread_condvar::{Condvar, MovableCondvar};
pub use pthread_mutex::{MovableMutex, Mutex};
pub use pthread_remutex::ReentrantMutex;
pub use pthread_rwlock::{MovableRWLock, RWLock};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cell::UnsafeCell;
use crate::sys::mutex::{self, Mutex};
use crate::sys::locks::{pthread_mutex, Mutex};
use crate::time::Duration;

pub struct Condvar {
Expand Down Expand Up @@ -79,7 +79,7 @@ impl Condvar {

#[inline]
pub unsafe fn wait(&self, mutex: &Mutex) {
let r = libc::pthread_cond_wait(self.inner.get(), mutex::raw(mutex));
let r = libc::pthread_cond_wait(self.inner.get(), pthread_mutex::raw(mutex));
debug_assert_eq!(r, 0);
}

Expand Down Expand Up @@ -111,7 +111,7 @@ impl Condvar {
let timeout =
sec.map(|s| libc::timespec { tv_sec: s, tv_nsec: nsec as _ }).unwrap_or(TIMESPEC_MAX);

let r = libc::pthread_cond_timedwait(self.inner.get(), mutex::raw(mutex), &timeout);
let r = libc::pthread_cond_timedwait(self.inner.get(), pthread_mutex::raw(mutex), &timeout);
assert!(r == libc::ETIMEDOUT || r == 0);
r == 0
}
Expand Down Expand Up @@ -169,7 +169,7 @@ impl Condvar {
.unwrap_or(TIMESPEC_MAX);

// And wait!
let r = libc::pthread_cond_timedwait(self.inner.get(), mutex::raw(mutex), &timeout);
let r = libc::pthread_cond_timedwait(self.inner.get(), pthread_mutex::raw(mutex), &timeout);
debug_assert!(r == libc::ETIMEDOUT || r == 0);

// ETIMEDOUT is not a totally reliable method of determining timeout due
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,49 +90,7 @@ impl Mutex {
}
}

pub struct ReentrantMutex {
inner: UnsafeCell<libc::pthread_mutex_t>,
}

unsafe impl Send for ReentrantMutex {}
unsafe impl Sync for ReentrantMutex {}

impl ReentrantMutex {
pub const unsafe fn uninitialized() -> ReentrantMutex {
ReentrantMutex { inner: UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER) }
}

pub unsafe fn init(&self) {
let mut attr = MaybeUninit::<libc::pthread_mutexattr_t>::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_RECURSIVE))
.unwrap();
cvt_nz(libc::pthread_mutex_init(self.inner.get(), attr.0.as_ptr())).unwrap();
}

pub unsafe fn lock(&self) {
let result = libc::pthread_mutex_lock(self.inner.get());
debug_assert_eq!(result, 0);
}

#[inline]
pub unsafe fn try_lock(&self) -> bool {
libc::pthread_mutex_trylock(self.inner.get()) == 0
}

pub unsafe fn unlock(&self) {
let result = libc::pthread_mutex_unlock(self.inner.get());
debug_assert_eq!(result, 0);
}

pub unsafe fn destroy(&self) {
let result = libc::pthread_mutex_destroy(self.inner.get());
debug_assert_eq!(result, 0);
}
}

struct PthreadMutexAttr<'a>(&'a mut MaybeUninit<libc::pthread_mutexattr_t>);
pub(super) struct PthreadMutexAttr<'a>(pub &'a mut MaybeUninit<libc::pthread_mutexattr_t>);

impl Drop for PthreadMutexAttr<'_> {
fn drop(&mut self) {
Expand Down
46 changes: 46 additions & 0 deletions library/std/src/sys/unix/locks/pthread_remutex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use super::pthread_mutex::PthreadMutexAttr;
use crate::cell::UnsafeCell;
use crate::mem::MaybeUninit;
use crate::sys::cvt_nz;

pub struct ReentrantMutex {
inner: UnsafeCell<libc::pthread_mutex_t>,
}

unsafe impl Send for ReentrantMutex {}
unsafe impl Sync for ReentrantMutex {}

impl ReentrantMutex {
pub const unsafe fn uninitialized() -> ReentrantMutex {
ReentrantMutex { inner: UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER) }
}

pub unsafe fn init(&self) {
let mut attr = MaybeUninit::<libc::pthread_mutexattr_t>::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_RECURSIVE))
.unwrap();
cvt_nz(libc::pthread_mutex_init(self.inner.get(), attr.0.as_ptr())).unwrap();
}

pub unsafe fn lock(&self) {
let result = libc::pthread_mutex_lock(self.inner.get());
debug_assert_eq!(result, 0);
}

#[inline]
pub unsafe fn try_lock(&self) -> bool {
libc::pthread_mutex_trylock(self.inner.get()) == 0
}

pub unsafe fn unlock(&self) {
let result = libc::pthread_mutex_unlock(self.inner.get());
debug_assert_eq!(result, 0);
}

pub unsafe fn destroy(&self) {
let result = libc::pthread_mutex_destroy(self.inner.get());
debug_assert_eq!(result, 0);
}
}
File renamed without changes.
4 changes: 1 addition & 3 deletions library/std/src/sys/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub mod android;
pub mod args;
#[path = "../unix/cmath.rs"]
pub mod cmath;
pub mod condvar;
pub mod env;
pub mod fd;
pub mod fs;
Expand All @@ -24,8 +23,8 @@ pub mod io;
pub mod kernel_copy;
#[cfg(target_os = "l4re")]
mod l4re;
pub mod locks;
pub mod memchr;
pub mod mutex;
#[cfg(not(target_os = "l4re"))]
pub mod net;
#[cfg(target_os = "l4re")]
Expand All @@ -36,7 +35,6 @@ pub mod path;
pub mod pipe;
pub mod process;
pub mod rand;
pub mod rwlock;
pub mod stack_overflow;
pub mod stdio;
pub mod thread;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::sys::mutex::Mutex;
use crate::sys::locks::Mutex;
use crate::time::Duration;

pub struct Condvar {}
Expand Down
6 changes: 6 additions & 0 deletions library/std/src/sys/unsupported/locks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mod condvar;
mod mutex;
mod rwlock;
pub use condvar::{Condvar, MovableCondvar};
pub use mutex::{MovableMutex, Mutex, ReentrantMutex};
pub use rwlock::{MovableRWLock, RWLock};
File renamed without changes.
File renamed without changes.
4 changes: 1 addition & 3 deletions library/std/src/sys/unsupported/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ pub mod alloc;
pub mod args;
#[path = "../unix/cmath.rs"]
pub mod cmath;
pub mod condvar;
pub mod env;
pub mod fs;
pub mod io;
pub mod mutex;
pub mod locks;
pub mod net;
pub mod os;
#[path = "../unix/os_str.rs"]
Expand All @@ -17,7 +16,6 @@ pub mod os_str;
pub mod path;
pub mod pipe;
pub mod process;
pub mod rwlock;
pub mod stdio;
pub mod thread;
#[cfg(target_thread_local)]
Expand Down
8 changes: 2 additions & 6 deletions library/std/src/sys/wasi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ pub mod alloc;
pub mod args;
#[path = "../unix/cmath.rs"]
pub mod cmath;
#[path = "../unsupported/condvar.rs"]
pub mod condvar;
pub mod env;
pub mod fd;
pub mod fs;
pub mod io;
#[path = "../unsupported/mutex.rs"]
pub mod mutex;
#[path = "../unsupported/locks/mod.rs"]
pub mod locks;
pub mod net;
pub mod os;
#[path = "../unix/os_str.rs"]
Expand All @@ -40,8 +38,6 @@ pub mod path;
pub mod pipe;
#[path = "../unsupported/process.rs"]
pub mod process;
#[path = "../unsupported/rwlock.rs"]
pub mod rwlock;
pub mod stdio;
pub mod thread;
#[path = "../unsupported/thread_local_dtor.rs"]
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/wasm/atomics/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::arch::wasm32;
use crate::cmp;
use crate::mem;
use crate::sync::atomic::{AtomicUsize, Ordering::SeqCst};
use crate::sys::mutex::Mutex;
use crate::sys::locks::Mutex;
use crate::time::Duration;

pub struct Condvar {
Expand Down
3 changes: 1 addition & 2 deletions library/std/src/sys/wasm/atomics/rwlock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::cell::UnsafeCell;
use crate::sys::condvar::Condvar;
use crate::sys::mutex::Mutex;
use crate::sys::locks::{Condvar, Mutex};

pub struct RWLock {
lock: Mutex,
Expand Down
Loading

0 comments on commit 36748cf

Please sign in to comment.