Skip to content

Commit

Permalink
Update to rustix 0.38.35 and the new futex API. (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfishcode authored Aug 27, 2024
1 parent 6320a39 commit 4f2ad30
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 45 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ include = ["src", "Cargo.toml", "COPYRIGHT", "LICENSE*", "/*.md"]
rust-version = "1.70"

[dependencies]
rustix = { version = "0.38.34", default-features = false, features = ["thread", "time"] }
rustix = { version = "0.38.35", default-features = false, features = ["thread", "time"] }
lock_api = { version = "0.4.7", default-features = false, optional = true }

# Special dependencies used in rustc-dep-of-std mode.
Expand All @@ -38,4 +38,3 @@ rustc-dep-of-std = [

[package.metadata.docs.rs]
features = ["atomic_usize"]
rustdoc-args = ["--cfg", "doc_cfg"]
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![doc = include_str!("../README.md")]
#![no_std]
#![cfg_attr(doc_cfg, feature(doc_cfg))]
#![cfg_attr(docsrs, feature(doc_cfg))]

// Re-export this so that our users can use the same version we do.
#[cfg(feature = "lock_api")]
Expand Down Expand Up @@ -29,11 +29,11 @@ pub type MappedRwLockReadGuard<'a, T> = lock_api::MappedRwLockReadGuard<'a, RawR
pub type MappedRwLockWriteGuard<'a, T> = lock_api::MappedRwLockWriteGuard<'a, RawRwLock, T>;
#[cfg(feature = "lock_api")]
#[cfg(feature = "atomic_usize")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "atomic_usize")))]
#[cfg_attr(docsrs, doc(cfg(feature = "atomic_usize")))]
pub type ReentrantMutex<G, T> = lock_api::ReentrantMutex<RawMutex, G, T>;
#[cfg(feature = "lock_api")]
#[cfg(feature = "atomic_usize")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "atomic_usize")))]
#[cfg_attr(docsrs, doc(cfg(feature = "atomic_usize")))]
pub type ReentrantMutexGuard<'a, G, T> = lock_api::ReentrantMutexGuard<'a, RawMutex, G, T>;

// Export the once types.
Expand Down
56 changes: 16 additions & 40 deletions src/wait_wake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
//! library/std/src/sys/pal/unix/futex.rs at revision
//! b58f647d5488dce73bba517907c44af2c2a618c4.
use core::num::NonZeroU32;
use core::sync::atomic::AtomicU32;
use core::time::Duration;
use rustix::thread::{FutexFlags, FutexOperation};
use rustix::thread::futex;
use rustix::time::{ClockId, Timespec};

/// Wait for a futex_wake operation to wake us.
Expand Down Expand Up @@ -36,7 +37,6 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
/// This allows callers that don't need the timeout to pass `None` and avoid
/// statically depending on `clock_gettime`.
pub fn futex_wait_timespec(futex: &AtomicU32, expected: u32, timespec: Option<Timespec>) -> bool {
use core::ptr::{null, null_mut};
use core::sync::atomic::Ordering::Relaxed;

loop {
Expand All @@ -45,19 +45,18 @@ pub fn futex_wait_timespec(futex: &AtomicU32, expected: u32, timespec: Option<Ti
return true;
}

let r = unsafe {
// Use FUTEX_WAIT_BITSET rather than FUTEX_WAIT to be able to give an
// absolute time rather than a relative time.
rustix::thread::futex(
futex.as_ptr(),
FutexOperation::WaitBitset,
FutexFlags::PRIVATE,
let r =
// Use `FUTEX_WAIT_BITSET` rather than `FUTEX_WAIT` to be able to
// give an absolute time rather than a relative time.
futex::wait_bitset(
futex,
futex::Flags::PRIVATE,
expected,
timespec.as_ref().map_or(null(), |t| t as *const _),
null_mut(),
!0u32, // A full bitmask, to make it behave like a regular FUTEX_WAIT.
timespec,
// A full bitmask, to make it behave like a regular `FUTEX_WAIT`.
NonZeroU32::MAX
)
};
;

match r {
Err(rustix::io::Errno::TIMEDOUT) => return false,
Expand All @@ -72,36 +71,13 @@ pub fn futex_wait_timespec(futex: &AtomicU32, expected: u32, timespec: Option<Ti
/// Returns true if this actually woke up such a thread,
/// or false if no thread was waiting on this futex.
pub fn futex_wake(futex: &AtomicU32) -> bool {
use core::ptr::{null, null_mut};
unsafe {
match rustix::thread::futex(
futex.as_ptr(),
FutexOperation::Wake,
FutexFlags::PRIVATE,
1,
null(),
null_mut(),
0,
) {
Err(_) | Ok(0) => false,
_ => true,
}
match futex::wake(futex, futex::Flags::PRIVATE, 1) {
Err(_) | Ok(0) => false,
_ => true,
}
}

/// Wake up all threads that are waiting on futex_wait on this futex.
pub fn futex_wake_all(futex: &AtomicU32) {
use core::ptr::{null, null_mut};
unsafe {
rustix::thread::futex(
futex.as_ptr(),
FutexOperation::Wake,
FutexFlags::PRIVATE,
i32::MAX as u32,
null(),
null_mut(),
0,
)
.ok();
}
futex::wake(futex, futex::Flags::PRIVATE, i32::MAX as u32).ok();
}

0 comments on commit 4f2ad30

Please sign in to comment.