Skip to content

Commit

Permalink
Minor cleanups. (#8)
Browse files Browse the repository at this point in the history
Tidy up some documentation, and add `#[inline]` to `RawMutex` and
`RawRwLock`'s APIs.
  • Loading branch information
sunfishcode authored Dec 4, 2023
1 parent b9c1b96 commit d249418
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 29 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

Linux futex-based implementations of [`Mutex`], [`RwLock`], [`Condvar`],
[`Once`], and [`OnceLock`], as well as [`RawMutex`], [`RawRwLock`], and
[`RawCondvar`], derived from the futex code in std, factored out to
a standalone `no_std` crate using [`rustix`] to do the futex and [`lock_api`]
to provide most of the public API.
[`RawCondvar`], derived from the futex code in std, factored out to a
standalone `no_std` crate using [`rustix`] to do the futex and [`lock_api`] to
provide most of the public `Mutex` and `RwLock` API.

`lock_api` does not support poisoning, so support for poisoning is omitted.

Expand All @@ -29,11 +29,11 @@ these `AtomicU32`s are not documented, except that all these types'

[`Mutex`]: https://docs.rs/rustix-futex-sync/latest/rustix_futex_sync/type.Mutex.html
[`RwLock`]: https://docs.rs/rustix-futex-sync/latest/rustix_futex_sync/type.RwLock.html
[`Condvar`]: https://docs.rs/rustix-futex-sync/latest/rustix_futex_sync/type.Condvar.html
[`Once`]: https://docs.rs/rustix-futex-sync/latest/rustix_futex_sync/type.Once.html
[`OnceLock`]: https://docs.rs/rustix-futex-sync/latest/rustix_futex_sync/type.OnceLock.html
[`RawMutex`]: https://docs.rs/rustix-futex-sync/latest/rustix_futex_sync/type.RawMutex.html
[`RawRwLock`]: https://docs.rs/rustix-futex-sync/latest/rustix_futex_sync/type.RawRwLock.html
[`Condvar`]: https://docs.rs/rustix-futex-sync/latest/rustix_futex_sync/struct.Condvar.html
[`Once`]: https://docs.rs/rustix-futex-sync/latest/rustix_futex_sync/struct.Once.html
[`OnceLock`]: https://docs.rs/rustix-futex-sync/latest/rustix_futex_sync/struct.OnceLock.html
[`RawMutex`]: https://docs.rs/rustix-futex-sync/latest/rustix_futex_sync/struct.RawMutex.html
[`RawRwLock`]: https://docs.rs/rustix-futex-sync/latest/rustix_futex_sync/struct.RawRwLock.html
[`RawCondvar`]: https://docs.rs/rustix-futex-sync/latest/rustix_futex_sync/type.RawCondvar.html
[`rustix`]: https://github.com/bytecodealliance/rustix#readme
[`lock_api`]: https://crates.io/crates/lock_api
2 changes: 0 additions & 2 deletions src/futex_condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use core::time::Duration;
use super::RawMutex;
use lock_api::RawMutex as _;

pub type MovableCondvar = Condvar;

#[repr(transparent)]
pub struct Condvar {
// The value of this atomic is simply incremented on every notification.
Expand Down
2 changes: 0 additions & 2 deletions src/futex_mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use core::sync::atomic::{
};
use super::wait_wake::{futex_wait_timespec, futex_wake};

pub type MovableMutex = Mutex;

#[repr(transparent)]
pub struct Mutex {
/// 0: unlocked
Expand Down
2 changes: 0 additions & 2 deletions src/futex_rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use core::sync::atomic::{
};
use super::wait_wake::{futex_wait_timespec, futex_wake, futex_wake_all};

pub type MovableRwLock = RwLock;

#[repr(C)]
pub struct RwLock {
// The state consists of a 30-bit reader counter, a 'readers waiting' flag, and a 'writers waiting' flag.
Expand Down
28 changes: 15 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// Re-export this so that our users can use the same version we do.
pub use lock_api;

pub type RawCondvar = futex_condvar::MovableCondvar;

// Export convenient `Mutex` and `RwLock` types.
pub type Mutex<T> = lock_api::Mutex<RawMutex, T>;
pub type RwLock<T> = lock_api::RwLock<RawRwLock, T>;
Expand All @@ -27,9 +25,8 @@ pub use once_lock::OnceLock;
// Export the condvar types.
pub use condvar::{Condvar, WaitTimeoutResult};

// The following is derived from Rust's
// library/std/src/sys/unix/locks/mod.rs at revision
// 6fd7e9010db6be7605241c39eab7c5078ee2d5bd.
// Export the raw condvar types.
pub type RawCondvar = futex_condvar::Condvar;

// std's implementation code.
mod condvar;
Expand All @@ -41,10 +38,6 @@ mod once;
mod once_lock;
mod wait_wake;

// Use the raw lock types from std's implementation.
use futex_mutex::MovableMutex;
use futex_rwlock::MovableRwLock;

/// An implementation of [`lock_api::RawMutex`].
///
/// All of this `RawMutex`'s methods are in its implementation of
Expand All @@ -55,7 +48,7 @@ use futex_rwlock::MovableRwLock;
/// use rustix_futex_sync::lock_api::RawMutex as _;
/// ```
#[repr(transparent)]
pub struct RawMutex(MovableMutex);
pub struct RawMutex(futex_mutex::Mutex);

/// An implementation of [`lock_api::RawRwLock`].
///
Expand All @@ -67,23 +60,26 @@ pub struct RawMutex(MovableMutex);
/// use rustix_futex_sync::lock_api::RawRwLock as _;
/// ```
#[repr(C)]
pub struct RawRwLock(MovableRwLock);
pub struct RawRwLock(futex_rwlock::RwLock);

// Implement the raw lock traits for our wrappers.

unsafe impl lock_api::RawMutex for RawMutex {
type GuardMarker = lock_api::GuardNoSend;

const INIT: Self = Self(MovableMutex::new());
const INIT: Self = Self(futex_mutex::Mutex::new());

#[inline]
fn lock(&self) {
self.0.lock()
}

#[inline]
fn try_lock(&self) -> bool {
self.0.try_lock()
}

#[inline]
unsafe fn unlock(&self) {
self.0.unlock()
}
Expand All @@ -92,28 +88,34 @@ unsafe impl lock_api::RawMutex for RawMutex {
unsafe impl lock_api::RawRwLock for RawRwLock {
type GuardMarker = lock_api::GuardNoSend;

const INIT: Self = Self(MovableRwLock::new());
const INIT: Self = Self(futex_rwlock::RwLock::new());

#[inline]
fn lock_shared(&self) {
self.0.read()
}

#[inline]
fn try_lock_shared(&self) -> bool {
self.0.try_read()
}

#[inline]
unsafe fn unlock_shared(&self) {
self.0.read_unlock()
}

#[inline]
fn lock_exclusive(&self) {
self.0.write()
}

#[inline]
fn try_lock_exclusive(&self) -> bool {
self.0.try_write()
}

#[inline]
unsafe fn unlock_exclusive(&self) {
self.0.write_unlock()
}
Expand Down
5 changes: 3 additions & 2 deletions tests/repr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// rustix_futex_sync documents some details of the representation of some of
/// its public types.
//! rustix_futex_sync documents some details of the representation of some of
//! its public types.
use core::mem::{align_of, size_of, transmute};
use rustix_futex_sync::lock_api::{RawMutex as _, RawRwLock as _};
use rustix_futex_sync::{Condvar, Once, RawCondvar, RawMutex, RawRwLock};
Expand Down

0 comments on commit d249418

Please sign in to comment.