From a2735a716c72ec9098297a4ef3120a8e12b5e61d Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Sun, 8 Nov 2020 18:41:22 +0100 Subject: [PATCH] Add signal reset() --- embassy/src/util/signal.rs | 53 ++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/embassy/src/util/signal.rs b/embassy/src/util/signal.rs index 783725247..f28166d3c 100644 --- a/embassy/src/util/signal.rs +++ b/embassy/src/util/signal.rs @@ -26,15 +26,20 @@ impl Signal { } pub fn signal(&self, val: T) { - unsafe { - cortex_m::interrupt::free(|_| { - let state = &mut *self.state.get(); - match mem::replace(state, State::Signaled(val)) { - State::Waiting(waker) => waker.wake(), - _ => {} - } - }) - } + cortex_m::interrupt::free(|_| unsafe { + let state = &mut *self.state.get(); + match mem::replace(state, State::Signaled(val)) { + 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 + 'a { @@ -50,22 +55,20 @@ impl<'a, T: Send> Future for WaitFuture<'a, T> { type Output = T; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - unsafe { - cortex_m::interrupt::free(|_| { - let state = &mut *self.signal.state.get(); - match state { - State::None => { - *state = State::Waiting(cx.waker().clone()); - 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!(), - }, + cortex_m::interrupt::free(|_| unsafe { + let state = &mut *self.signal.state.get(); + match state { + State::None => { + *state = State::Waiting(cx.waker().clone()); + 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!(), + }, + } + }) } }