use core::future::Future;
use core::time::Duration;
#[cfg(feature = "platform-embassy")]
pub mod mpsc;
#[cfg(feature = "platform-tokio")]
pub use tokio::sync::mpsc;
pub mod atomic64;
pub mod net;
pub mod select;
pub use select::{select2, select3, Either, Select2, Select3, Which3};
#[cfg(feature = "platform-tokio")]
pub type BoundedSender<T> = tokio::sync::mpsc::Sender<T>;
#[cfg(feature = "platform-tokio")]
pub type BoundedReceiver<T> = tokio::sync::mpsc::Receiver<T>;
#[cfg(feature = "platform-tokio")]
pub use std::time::Instant;
#[cfg(feature = "platform-tokio")]
pub use tokio::sync::Notify;
#[cfg(feature = "platform-tokio")]
pub use tokio::task::JoinHandle;
#[cfg(feature = "platform-tokio")]
pub use tokio::time::error::Elapsed;
#[cfg(feature = "platform-tokio")]
pub use tokio_util::sync::CancellationToken;
#[cfg(feature = "platform-tokio")]
pub async fn sleep(dur: Duration) {
tokio::time::sleep(dur).await;
}
#[cfg(feature = "platform-tokio")]
pub async fn timeout<T>(dur: Duration, future: impl Future<Output = T>) -> Result<T, Elapsed> {
tokio::time::timeout(dur, future).await
}
#[cfg(feature = "platform-tokio")]
pub fn spawn<F>(future: F) -> JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
tokio::spawn(future)
}
#[cfg(feature = "platform-tokio")]
pub fn spawn_with_result<F, T>(future: F) -> JoinHandle<T>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
tokio::spawn(future)
}
#[cfg(feature = "platform-embassy")]
pub mod embassy_impl {
use alloc::boxed::Box;
use alloc::sync::Arc;
use core::future::Future;
use core::sync::atomic::{AtomicPtr, Ordering};
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::signal::Signal;
pub type SpawnFn = fn(Box<dyn Future<Output = ()> + Send>);
static SPAWN_FN: AtomicPtr<SpawnFn> = AtomicPtr::new(core::ptr::null_mut());
pub fn set_spawn_fn(f: SpawnFn) {
let leaked = alloc::boxed::Box::leak(alloc::boxed::Box::new(f));
SPAWN_FN.store(leaked as *mut SpawnFn, Ordering::Release);
}
#[derive(Clone)]
pub struct Notify {
signal: Arc<Signal<CriticalSectionRawMutex, ()>>,
}
impl Notify {
pub fn new() -> Self {
Self {
signal: Arc::new(Signal::new()),
}
}
pub fn notified(&self) -> impl Future<Output = ()> + '_ {
self.signal.wait()
}
pub fn notify_waiters(&self) {
self.signal.signal(());
}
pub fn notify_one(&self) {
self.signal.signal(());
}
}
impl Default for Notify {
fn default() -> Self {
Self::new()
}
}
pub(crate) fn with_spawn_fn<R>(f: impl FnOnce(SpawnFn) -> R) -> R {
critical_section::with(|_| {
let ptr = SPAWN_FN.load(Ordering::Acquire);
assert!(!ptr.is_null(), "platform: spawn fn not configured");
f(unsafe { *ptr })
})
}
}
#[cfg(feature = "platform-embassy")]
pub use embassy_impl::{set_spawn_fn, Notify as EmbassyNotify};
#[cfg(feature = "platform-embassy")]
pub use mpsc::bounded::{Receiver as BoundedReceiver, Sender as BoundedSender};
#[cfg(feature = "platform-embassy")]
pub use EmbassyNotify as Notify;
#[cfg(feature = "platform-embassy")]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Instant(embassy_time::Instant);
#[cfg(feature = "platform-embassy")]
impl Instant {
pub fn now() -> Self {
Self(embassy_time::Instant::now())
}
pub fn checked_duration_since(&self, earlier: Self) -> Option<core::time::Duration> {
if self.0 >= earlier.0 {
let d = self.0 - earlier.0;
let micros = d.as_micros();
Some(core::time::Duration::from_micros(micros))
} else {
None
}
}
}
#[cfg(feature = "platform-embassy")]
impl core::ops::Add<Duration> for Instant {
type Output = Instant;
fn add(self, rhs: Duration) -> Instant {
Self(
self.0
+ embassy_time::Duration::from_micros(rhs.as_micros().min(u64::MAX as u128) as u64),
)
}
}
#[cfg(feature = "platform-embassy")]
pub async fn sleep(dur: Duration) {
embassy_time::Timer::after(embassy_time::Duration::from_micros(
dur.as_micros().min(u64::MAX as u128) as u64,
))
.await;
}
#[cfg(feature = "platform-embassy")]
pub async fn timeout<T>(
dur: Duration,
future: impl Future<Output = T>,
) -> Result<T, embassy_time::TimeoutError> {
embassy_time::with_timeout(
embassy_time::Duration::from_micros(dur.as_micros().min(u64::MAX as u128) as u64),
future,
)
.await
}
#[cfg(feature = "platform-embassy")]
pub fn spawn<F>(future: F)
where
F: Future<Output = ()> + Send + 'static,
{
let boxed: alloc::boxed::Box<dyn Future<Output = ()> + Send> = alloc::boxed::Box::new(future);
embassy_impl::with_spawn_fn(|spawn| spawn(boxed));
}
#[cfg(feature = "platform-embassy")]
pub struct JoinHandle<T> {
rx: crate::platform::mpsc::BoundedReceiver<T>,
}
#[cfg(feature = "platform-embassy")]
impl<T: Send + 'static> Future for JoinHandle<T> {
type Output = T;
fn poll(
self: core::pin::Pin<&mut Self>,
cx: &mut core::task::Context<'_>,
) -> core::task::Poll<T> {
let this = self.get_mut();
let fut = this.rx.recv();
let mut fut = core::pin::pin!(fut);
core::future::Future::poll(fut.as_mut(), cx) .map(|opt| opt.expect("spawn_with_result sender alive"))
}
}
#[cfg(feature = "platform-embassy")]
pub fn spawn_with_result<F, T>(future: F) -> JoinHandle<T>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
let (tx, rx) = crate::platform::mpsc::channel::<T>(1);
spawn(async move {
let out = future.await;
let _ = tx.send(out);
});
JoinHandle { rx }
}
#[cfg(all(not(feature = "platform-tokio"), not(feature = "platform-embassy")))]
compile_error!(
"rsipstack: no platform backend enabled; enable \"platform-tokio\" (host) \
or \"platform-embassy\" (embedded, WP3)"
);
pub mod sync;
#[cfg(feature = "platform-embassy")]
pub mod token;
#[cfg(feature = "platform-embassy")]
pub use token::CancellationToken;