Skip to content

Commit

Permalink
Add a portable-atomic feature (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
notgull committed Apr 3, 2023
1 parent a9595d3 commit e909945
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
uses: taiki-e/install-action@cargo-hack
- run: rustup target add thumbv7m-none-eabi
- run: cargo hack build --target thumbv7m-none-eabi --no-default-features --no-dev-deps
- run: cargo hack build --target thumbv7m-none-eabi --no-default-features --no-dev-deps --features portable-atomic

msrv:
runs-on: ubuntu-latest
Expand Down
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@ exclude = ["/.*"]
[features]
default = ["std"]
std = ["parking"]
portable-atomic = ["portable-atomic-util", "portable_atomic_crate"]

[dependencies]
parking = { version = "2.0.0", optional = true }
portable-atomic-util = { version = "0.1.1", default-features = false, optional = true, features = ["alloc"] }

[dependencies.portable_atomic_crate]
package = "portable-atomic"
version = "1.2.0"
default-features = false
optional = true

[dev-dependencies]
futures-lite = "1.12.0"
Expand Down
31 changes: 23 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@
//! listener.as_mut().wait();
//! }
//! ```
//!
//! # Features
//!
//! - The `portable-atomic` feature enables the use of the [`portable-atomic`] crate to provide
//! atomic operations on platforms that don't support them.
//!
//! [`portable-atomic`]: https://crates.io/crates/portable-atomic

#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
Expand All @@ -71,11 +78,11 @@ mod sys;

use alloc::boxed::Box;

use core::borrow::Borrow;
use core::fmt;
use core::future::Future;
use core::marker::PhantomPinned;
use core::mem::ManuallyDrop;
use core::ops::Deref;
use core::pin::Pin;
use core::ptr;
use core::task::{Context, Poll, Waker};
Expand Down Expand Up @@ -623,7 +630,7 @@ impl Future for EventListener {
}
}

struct Listener<B: Borrow<Inner> + Unpin> {
struct Listener<B: Deref<Target = Inner> + Unpin> {
/// The reference to the original event.
event: B,

Expand All @@ -634,10 +641,10 @@ struct Listener<B: Borrow<Inner> + Unpin> {
_pin: PhantomPinned,
}

unsafe impl<B: Borrow<Inner> + Unpin + Send> Send for Listener<B> {}
unsafe impl<B: Borrow<Inner> + Unpin + Sync> Sync for Listener<B> {}
unsafe impl<B: Deref<Target = Inner> + Unpin + Send> Send for Listener<B> {}
unsafe impl<B: Deref<Target = Inner> + Unpin + Sync> Sync for Listener<B> {}

impl<B: Borrow<Inner> + Unpin> Listener<B> {
impl<B: Deref<Target = Inner> + Unpin> Listener<B> {
/// Pin-project this listener.
fn project(self: Pin<&mut Self>) -> (&Inner, Pin<&mut Option<sys::Listener>>) {
// SAFETY: `event` is `Unpin`, and `listener`'s pin status is preserved
Expand All @@ -646,7 +653,7 @@ impl<B: Borrow<Inner> + Unpin> Listener<B> {
event, listener, ..
} = self.get_unchecked_mut();

((*event).borrow(), Pin::new_unchecked(listener))
(&*event, Pin::new_unchecked(listener))
}
}

Expand Down Expand Up @@ -779,7 +786,7 @@ impl<B: Borrow<Inner> + Unpin> Listener<B> {
}
}

impl<B: Borrow<Inner> + Unpin> Drop for Listener<B> {
impl<B: Deref<Target = Inner> + Unpin> Drop for Listener<B> {
fn drop(&mut self) {
// If we're being dropped, we need to remove ourself from the list.
let (inner, listener) = unsafe { Pin::new_unchecked(self).project() };
Expand Down Expand Up @@ -915,10 +922,18 @@ fn full_fence() {

/// Synchronization primitive implementation.
mod sync {
pub(super) use alloc::sync::Arc;
pub(super) use core::cell;

#[cfg(not(feature = "portable-atomic"))]
pub(super) use alloc::sync::Arc;
#[cfg(not(feature = "portable-atomic"))]
pub(super) use core::sync::atomic;

#[cfg(feature = "portable-atomic")]
pub(super) use portable_atomic_crate as atomic;
#[cfg(feature = "portable-atomic")]
pub(super) use portable_atomic_util::Arc;

#[cfg(feature = "std")]
pub(super) use std::sync::{Mutex, MutexGuard};

Expand Down

0 comments on commit e909945

Please sign in to comment.