Simpliify PeripheralMutex a bit.

This commit is contained in:
Dario Nieuwenhuis 2021-01-06 22:48:54 +01:00
parent 77bdb5428e
commit deb3c93892
2 changed files with 22 additions and 19 deletions

View File

@ -61,7 +61,7 @@ struct State<'a, U: Instance> {
/// - nrf52832: Section 15.2
/// - nrf52840: Section 6.1.2
pub struct BufferedUarte<'a, U: Instance> {
inner: PeripheralMutex<U::Interrupt, State<'a, U>>,
inner: PeripheralMutex<State<'a, U>>,
}
impl<'a, U: Instance> Unpin for BufferedUarte<'a, U> {}
@ -143,7 +143,6 @@ impl<'a, U: Instance> BufferedUarte<'a, U> {
BufferedUarte {
inner: PeripheralMutex::new(
irq,
State {
inner: uarte,
@ -155,11 +154,12 @@ impl<'a, U: Instance> BufferedUarte<'a, U> {
tx_state: TxState::Idle,
tx_waker: WakerRegistration::new(),
},
irq,
),
}
}
fn inner(self: Pin<&mut Self>) -> Pin<&mut PeripheralMutex<U::Interrupt, State<'a, U>>> {
fn inner(self: Pin<&mut Self>) -> Pin<&mut PeripheralMutex<State<'a, U>>> {
unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().inner) }
}
}
@ -173,7 +173,7 @@ impl<'a, U: Instance> Drop for BufferedUarte<'a, U> {
impl<'a, U: Instance> AsyncBufRead for BufferedUarte<'a, U> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> {
self.inner().with(|_irq, state| {
self.inner().with(|state, _irq| {
// Conservative compiler fence to prevent optimizations that do not
// take in to account actions by DMA. The fence has been placed here,
// before any DMA action has started
@ -203,7 +203,7 @@ impl<'a, U: Instance> AsyncBufRead for BufferedUarte<'a, U> {
}
fn consume(self: Pin<&mut Self>, amt: usize) {
self.inner().with(|irq, state| {
self.inner().with(|state, irq| {
trace!("consume {:?}", amt);
state.rx.pop(amt);
irq.pend();
@ -213,7 +213,7 @@ impl<'a, U: Instance> AsyncBufRead for BufferedUarte<'a, U> {
impl<'a, U: Instance> AsyncWrite for BufferedUarte<'a, U> {
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> {
self.inner().with(|irq, state| {
self.inner().with(|state, irq| {
trace!("poll_write: {:?}", buf.len());
let tx_buf = state.tx.push_buf();
@ -242,6 +242,8 @@ impl<'a, U: Instance> AsyncWrite for BufferedUarte<'a, U> {
}
impl<'a, U: Instance> PeripheralState for State<'a, U> {
type Interrupt = U::Interrupt;
fn on_interrupt(&mut self) {
trace!("irq: start");
let mut more_work = true;

View File

@ -6,25 +6,26 @@ use crate::fmt::*;
use crate::interrupt::OwnedInterrupt;
pub trait PeripheralState {
type Interrupt: OwnedInterrupt;
fn on_interrupt(&mut self);
}
pub struct PeripheralMutex<I: OwnedInterrupt, S: PeripheralState> {
inner: Option<(I, UnsafeCell<S>)>,
pub struct PeripheralMutex<S: PeripheralState> {
inner: Option<(UnsafeCell<S>, S::Interrupt)>,
not_send: PhantomData<*mut ()>,
}
impl<I: OwnedInterrupt, S: PeripheralState> PeripheralMutex<I, S> {
pub fn new(irq: I, state: S) -> Self {
impl<S: PeripheralState> PeripheralMutex<S> {
pub fn new(state: S, irq: S::Interrupt) -> Self {
Self {
inner: Some((irq, UnsafeCell::new(state))),
inner: Some((UnsafeCell::new(state), irq)),
not_send: PhantomData,
}
}
pub fn with<R>(self: Pin<&mut Self>, f: impl FnOnce(&mut I, &mut S) -> R) -> R {
pub fn with<R>(self: Pin<&mut Self>, f: impl FnOnce(&mut S, &mut S::Interrupt) -> R) -> R {
let this = unsafe { self.get_unchecked_mut() };
let (irq, state) = unwrap!(this.inner.as_mut());
let (state, irq) = unwrap!(this.inner.as_mut());
irq.disable();
compiler_fence(Ordering::SeqCst);
@ -43,7 +44,7 @@ impl<I: OwnedInterrupt, S: PeripheralState> PeripheralMutex<I, S> {
// Safety: it's OK to get a &mut to the state, since the irq is disabled.
let state = unsafe { &mut *state.get() };
let r = f(irq, state);
let r = f(state, irq);
compiler_fence(Ordering::SeqCst);
irq.enable();
@ -51,18 +52,18 @@ impl<I: OwnedInterrupt, S: PeripheralState> PeripheralMutex<I, S> {
r
}
pub fn free(self: Pin<&mut Self>) -> (I, S) {
pub fn free(self: Pin<&mut Self>) -> (S, S::Interrupt) {
let this = unsafe { self.get_unchecked_mut() };
let (irq, state) = unwrap!(this.inner.take());
let (state, irq) = unwrap!(this.inner.take());
irq.disable();
irq.remove_handler();
(irq, state.into_inner())
(state.into_inner(), irq)
}
}
impl<I: OwnedInterrupt, S: PeripheralState> Drop for PeripheralMutex<I, S> {
impl<S: PeripheralState> Drop for PeripheralMutex<S> {
fn drop(&mut self) {
if let Some((irq, state)) = &mut self.inner {
if let Some((state, irq)) = &mut self.inner {
irq.disable();
irq.remove_handler();
}