Add signal reset()

This commit is contained in:
Dario Nieuwenhuis 2020-11-08 18:41:22 +01:00
parent def225b982
commit a2735a716c

View File

@ -26,15 +26,20 @@ impl<T: Send> Signal<T> {
} }
pub fn signal(&self, val: T) { pub fn signal(&self, val: T) {
unsafe { cortex_m::interrupt::free(|_| unsafe {
cortex_m::interrupt::free(|_| { let state = &mut *self.state.get();
let state = &mut *self.state.get(); match mem::replace(state, State::Signaled(val)) {
match mem::replace(state, State::Signaled(val)) { State::Waiting(waker) => waker.wake(),
State::Waiting(waker) => waker.wake(), _ => {}
_ => {} }
} })
}) }
}
pub fn reset(&self) {
cortex_m::interrupt::free(|_| unsafe {
let state = &mut *self.state.get();
*state = State::None
})
} }
pub fn wait<'a>(&'a self) -> impl Future<Output = T> + 'a { pub fn wait<'a>(&'a self) -> impl Future<Output = T> + 'a {
@ -50,22 +55,20 @@ impl<'a, T: Send> Future for WaitFuture<'a, T> {
type Output = T; type Output = T;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
unsafe { cortex_m::interrupt::free(|_| unsafe {
cortex_m::interrupt::free(|_| { let state = &mut *self.signal.state.get();
let state = &mut *self.signal.state.get(); match state {
match state { State::None => {
State::None => { *state = State::Waiting(cx.waker().clone());
*state = State::Waiting(cx.waker().clone()); Poll::Pending
Poll::Pending
}
State::Waiting(w) if w.will_wake(cx.waker()) => Poll::Pending,
State::Waiting(_) => panic!("waker overflow"),
State::Signaled(_) => match mem::replace(state, State::None) {
State::Signaled(res) => Poll::Ready(res),
_ => unreachable!(),
},
} }
}) State::Waiting(w) if w.will_wake(cx.waker()) => Poll::Pending,
} State::Waiting(_) => panic!("waker overflow"),
State::Signaled(_) => match mem::replace(state, State::None) {
State::Signaled(res) => Poll::Ready(res),
_ => unreachable!(),
},
}
})
} }
} }