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