2022-06-15 08:24:36 +00:00
|
|
|
//! A synchronization primitive for passing the latest value to a task.
|
2022-09-22 06:05:40 +00:00
|
|
|
use core::cell::Cell;
|
2022-09-22 14:42:49 +00:00
|
|
|
use core::future::{poll_fn, Future};
|
2021-09-10 23:53:53 +00:00
|
|
|
use core::task::{Context, Poll, Waker};
|
|
|
|
|
2022-09-24 09:08:46 +00:00
|
|
|
use crate::blocking_mutex::raw::RawMutex;
|
2022-09-22 06:05:40 +00:00
|
|
|
use crate::blocking_mutex::Mutex;
|
|
|
|
|
2022-06-25 22:53:35 +00:00
|
|
|
/// Single-slot signaling primitive.
|
2021-09-10 23:53:53 +00:00
|
|
|
///
|
2022-08-23 06:37:38 +00:00
|
|
|
/// This is similar to a [`Channel`](crate::channel::Channel) with a buffer size of 1, except
|
2022-06-25 22:53:35 +00:00
|
|
|
/// "sending" to it (calling [`Signal::signal`]) when full will overwrite the previous value instead
|
|
|
|
/// of waiting for the receiver to pop the previous value.
|
|
|
|
///
|
|
|
|
/// It is useful for sending data between tasks when the receiver only cares about
|
|
|
|
/// the latest data, and therefore it's fine to "lose" messages. This is often the case for "state"
|
|
|
|
/// updates.
|
|
|
|
///
|
2022-08-23 06:37:38 +00:00
|
|
|
/// For more advanced use cases, you might want to use [`Channel`](crate::channel::Channel) instead.
|
2022-06-25 22:53:35 +00:00
|
|
|
///
|
|
|
|
/// Signals are generally declared as `static`s and then borrowed as required.
|
2021-11-12 05:49:48 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2022-08-22 20:00:06 +00:00
|
|
|
/// use embassy_sync::signal::Signal;
|
2022-09-24 09:08:46 +00:00
|
|
|
/// use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
2021-11-12 05:49:48 +00:00
|
|
|
///
|
|
|
|
/// enum SomeCommand {
|
|
|
|
/// On,
|
|
|
|
/// Off,
|
|
|
|
/// }
|
|
|
|
///
|
2022-09-24 09:08:46 +00:00
|
|
|
/// static SOME_SIGNAL: Signal<CriticalSectionRawMutex, SomeCommand> = Signal::new();
|
2021-11-12 05:49:48 +00:00
|
|
|
/// ```
|
2022-09-24 09:08:46 +00:00
|
|
|
pub struct Signal<M, T>
|
2022-09-22 06:05:40 +00:00
|
|
|
where
|
2022-09-24 09:08:46 +00:00
|
|
|
M: RawMutex,
|
2022-09-22 06:05:40 +00:00
|
|
|
{
|
2022-09-24 09:08:46 +00:00
|
|
|
state: Mutex<M, Cell<State<T>>>,
|
2021-09-10 23:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum State<T> {
|
|
|
|
None,
|
|
|
|
Waiting(Waker),
|
|
|
|
Signaled(T),
|
|
|
|
}
|
|
|
|
|
2022-09-24 09:08:46 +00:00
|
|
|
impl<M, T> Signal<M, T>
|
2022-09-22 06:05:40 +00:00
|
|
|
where
|
2022-09-24 09:08:46 +00:00
|
|
|
M: RawMutex,
|
2022-09-22 06:05:40 +00:00
|
|
|
{
|
2022-06-25 22:53:35 +00:00
|
|
|
/// Create a new `Signal`.
|
2021-09-10 23:53:53 +00:00
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self {
|
2022-09-22 06:05:40 +00:00
|
|
|
state: Mutex::new(Cell::new(State::None)),
|
2021-09-10 23:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-11 23:24:04 +00:00
|
|
|
}
|
2021-09-10 23:53:53 +00:00
|
|
|
|
2022-11-22 13:55:10 +00:00
|
|
|
impl<M, T> Default for Signal<M, T>
|
|
|
|
where
|
|
|
|
M: RawMutex,
|
|
|
|
{
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 09:08:46 +00:00
|
|
|
impl<M, T: Send> Signal<M, T>
|
2022-09-22 06:05:40 +00:00
|
|
|
where
|
2022-09-24 09:08:46 +00:00
|
|
|
M: RawMutex,
|
2022-09-22 06:05:40 +00:00
|
|
|
{
|
2022-06-25 22:53:35 +00:00
|
|
|
/// Mark this Signal as signaled.
|
2021-09-10 23:53:53 +00:00
|
|
|
pub fn signal(&self, val: T) {
|
2022-09-22 06:05:40 +00:00
|
|
|
self.state.lock(|cell| {
|
|
|
|
let state = cell.replace(State::Signaled(val));
|
|
|
|
if let State::Waiting(waker) = state {
|
2021-09-10 23:53:53 +00:00
|
|
|
waker.wake();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-25 22:53:35 +00:00
|
|
|
/// Remove the queued value in this `Signal`, if any.
|
2021-09-10 23:53:53 +00:00
|
|
|
pub fn reset(&self) {
|
2022-09-22 06:05:40 +00:00
|
|
|
self.state.lock(|cell| cell.set(State::None));
|
2021-09-10 23:53:53 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 06:05:40 +00:00
|
|
|
fn poll_wait(&self, cx: &mut Context<'_>) -> Poll<T> {
|
|
|
|
self.state.lock(|cell| {
|
|
|
|
let state = cell.replace(State::None);
|
2021-09-10 23:53:53 +00:00
|
|
|
match state {
|
|
|
|
State::None => {
|
2022-09-22 06:05:40 +00:00
|
|
|
cell.set(State::Waiting(cx.waker().clone()));
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
State::Waiting(w) if w.will_wake(cx.waker()) => {
|
|
|
|
cell.set(State::Waiting(w));
|
2021-09-10 23:53:53 +00:00
|
|
|
Poll::Pending
|
|
|
|
}
|
2022-09-12 10:05:58 +00:00
|
|
|
State::Waiting(w) => {
|
2022-09-22 06:05:40 +00:00
|
|
|
cell.set(State::Waiting(cx.waker().clone()));
|
2022-09-12 10:05:58 +00:00
|
|
|
w.wake();
|
|
|
|
Poll::Pending
|
|
|
|
}
|
2022-09-22 06:05:40 +00:00
|
|
|
State::Signaled(res) => Poll::Ready(res),
|
2021-09-10 23:53:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Future that completes when this Signal has been signaled.
|
|
|
|
pub fn wait(&self) -> impl Future<Output = T> + '_ {
|
2022-09-22 14:42:49 +00:00
|
|
|
poll_fn(move |cx| self.poll_wait(cx))
|
2021-09-10 23:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// non-blocking method to check whether this signal has been signaled.
|
|
|
|
pub fn signaled(&self) -> bool {
|
2022-09-22 06:05:40 +00:00
|
|
|
self.state.lock(|cell| {
|
|
|
|
let state = cell.replace(State::None);
|
|
|
|
|
|
|
|
let res = matches!(state, State::Signaled(_));
|
|
|
|
|
|
|
|
cell.set(state);
|
|
|
|
|
|
|
|
res
|
|
|
|
})
|
2021-09-10 23:53:53 +00:00
|
|
|
}
|
|
|
|
}
|