mirror of
https://github.com/rtic-rs/rtic.git
synced 2024-11-27 14:04:56 +01:00
.
This commit is contained in:
parent
7b2b7c64cc
commit
faf03f414b
1 changed files with 76 additions and 48 deletions
|
@ -1,8 +1,11 @@
|
||||||
//! A channel operating on bitflags.
|
//! A channel operating on bitflags.
|
||||||
|
|
||||||
|
use core::{future::poll_fn, task::Poll};
|
||||||
|
|
||||||
|
use atomic::AtomicType;
|
||||||
use bitflags::{Bits, Flags};
|
use bitflags::{Bits, Flags};
|
||||||
use core::sync::atomic::{AtomicU16, AtomicU8, Ordering};
|
use portable_atomic::Ordering;
|
||||||
use portable_atomic::{AtomicI16, AtomicI32, AtomicI64, AtomicI8, AtomicU32, AtomicU64};
|
use rtic_common::waker_registration::CriticalSectionWakerRegistration as CSWaker;
|
||||||
|
|
||||||
/// A channel for setting and clearing `bitflags` concurrently.
|
/// A channel for setting and clearing `bitflags` concurrently.
|
||||||
pub struct BitChannel<T: Flags>
|
pub struct BitChannel<T: Flags>
|
||||||
|
@ -11,6 +14,7 @@ where
|
||||||
T::Bits: AtomicType,
|
T::Bits: AtomicType,
|
||||||
{
|
{
|
||||||
atomic: <T::Bits as AtomicType>::Atomic,
|
atomic: <T::Bits as AtomicType>::Atomic,
|
||||||
|
waker: CSWaker,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> BitChannel<T>
|
impl<T> BitChannel<T>
|
||||||
|
@ -22,25 +26,50 @@ where
|
||||||
pub const fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
BitChannel {
|
BitChannel {
|
||||||
atomic: T::Bits::ATOMIC_ZERO,
|
atomic: T::Bits::ATOMIC_ZERO,
|
||||||
|
waker: CSWaker::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set `bitflag`s.
|
/// Set `bitflag`s.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn send(&self, flags: T) {
|
pub fn send(&self, flags: T) {
|
||||||
T::Bits::fetch_or(&self.atomic, flags.bits(), Ordering::Relaxed);
|
T::Bits::fetch_or(&self.atomic, flags.bits(), Ordering::Acquire);
|
||||||
|
|
||||||
|
self.waker.wake();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Receive the current value of the `bitflags` and reset all flags.
|
/// Receive the current value of the `bitflags` and reset all flags. This can be accessed
|
||||||
|
/// concurrently but not all receivers will see the flags, only the first one will.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn recv(&self) -> T {
|
pub fn recv(&self) -> T {
|
||||||
<T as Flags>::from_bits_retain(T::Bits::fetch_and(
|
<T as Flags>::from_bits_retain(T::Bits::fetch_and(
|
||||||
&self.atomic,
|
&self.atomic,
|
||||||
T::Bits::EMPTY,
|
T::Bits::EMPTY,
|
||||||
Ordering::Relaxed,
|
Ordering::Release,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Wait for new values, the return is guaranteed non-empty.
|
||||||
|
pub async fn wait(&self) -> T {
|
||||||
|
poll_fn(|cx| {
|
||||||
|
self.waker.register(cx.waker());
|
||||||
|
let val = self.recv();
|
||||||
|
|
||||||
|
if val.is_empty() {
|
||||||
|
Poll::Pending
|
||||||
|
} else {
|
||||||
|
Poll::Ready(val)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod atomic {
|
||||||
|
use portable_atomic::{
|
||||||
|
AtomicI16, AtomicI32, AtomicI64, AtomicI8, AtomicU16, AtomicU32, AtomicU64, AtomicU8,
|
||||||
|
Ordering,
|
||||||
|
};
|
||||||
|
|
||||||
/// Generic atomic trait, allows for taking any `bitflags::Bits` as an atomic.
|
/// Generic atomic trait, allows for taking any `bitflags::Bits` as an atomic.
|
||||||
pub trait AtomicType: Sized {
|
pub trait AtomicType: Sized {
|
||||||
|
@ -85,8 +114,7 @@ atomic_type_impl!(AtomicI8, i8);
|
||||||
atomic_type_impl!(AtomicI16, i16);
|
atomic_type_impl!(AtomicI16, i16);
|
||||||
atomic_type_impl!(AtomicI32, i32);
|
atomic_type_impl!(AtomicI32, i32);
|
||||||
atomic_type_impl!(AtomicI64, i64);
|
atomic_type_impl!(AtomicI64, i64);
|
||||||
|
}
|
||||||
// etc...
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
Loading…
Reference in a new issue