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

Implement a new algorithm to make this crate no_std #30

Closed
wants to merge 11 commits into from
Prev Previous commit
Next Next commit
fix msrv + loom compilation
notgull committed Sep 9, 2022
commit d3295ca6c7452fbb0a79badaf5218329749dfe1d
18 changes: 13 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -525,6 +525,7 @@ impl EventListener {
/// // There are no notification so this times out.
/// assert!(!listener.wait_timeout(Duration::from_secs(1)));
/// ```
#[cfg(not(loom))]
pub fn wait_timeout(self, duration: Duration) -> bool {
self.wait_internal(Instant::now().checked_add(duration))
}
@@ -545,6 +546,7 @@ impl EventListener {
/// // There are no notification so this times out.
/// assert!(!listener.wait_deadline(Instant::now() + Duration::from_secs(1)));
/// ```
#[cfg(not(loom))]
pub fn wait_deadline(self, deadline: Instant) -> bool {
self.wait_internal(Some(deadline))
}
@@ -568,9 +570,15 @@ impl EventListener {
// Park the thread and see if we have been notified.
match deadline {
Some(deadline) => {
if !parker.park_deadline(deadline) {
// The timeout elapsed. Return false.
break false;
#[cfg(loom)]
panic!("`wait_deadline` is not supported with loom");

#[cfg(not(loom))]
{
if !parker.park_deadline(deadline) {
// The timeout elapsed. Return false.
break false;
}
}
}
None => parker.park(),
@@ -705,7 +713,7 @@ impl Listener {
// We now hold the "lock" on the task slot. Write the task to the slot.
let task = self.task.with_mut(|slot| unsafe {
// If there already was a task, swap it out and wake it instead of replacing it.
if matches!(state, State::Task) {
if state == State::Task {
Some(ptr::replace(slot.cast(), (task)()))
} else {
ptr::write(slot.cast(), (task)());
@@ -886,7 +894,7 @@ impl Drop for Listener {
}

/// The state that a `Listener` can be in.
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(usize)]
enum State {
/// The listener was just created.
8 changes: 5 additions & 3 deletions src/sync.rs
Original file line number Diff line number Diff line change
@@ -46,9 +46,7 @@ mod sync_impl {
#[cfg(loom)]
mod sync_impl {
pub(crate) use loom::cell::UnsafeCell;
pub(crate) use loom::sync::atomic::{
fence, AtomicBool, AtomicPtr, AtomicU32, AtomicUsize, Ordering,
};
pub(crate) use loom::sync::atomic::{fence, AtomicPtr, AtomicUsize, Ordering};

/// Re-implementation of `parking::pair` based on loom.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we upstream this into https://github.com/smol-rs/parking ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! It should be a straightforward reimplementation in terms of Loom primitives.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, it's smol-rs/parking#8

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that it's upstreamed, can we use that version? (although I don't know how it would interact with dependencies...) Does cargo correctly work with git deps which also have versions (e.g. transforms them into crates.io entries on publishing)?

pub(crate) fn pair() -> (Parker, Unparker) {
@@ -69,13 +67,17 @@ mod sync_impl {
}

/// Re-implementation of `parking::Unparker` based on loom.
#[derive(Clone)]
pub(crate) struct Unparker(loom::thread::Thread);

impl Unparker {
pub(crate) fn unpark(&self) {
self.0.unpark();
}
}

#[allow(dead_code)]
pub(crate) trait AtomicWithMut {}
}

pub(crate) use sync_impl::*;