wasm cleanup
This commit is contained in:
@@ -69,8 +69,8 @@ cfg_if! {
|
||||
pub use async_lock::Mutex as AsyncMutex;
|
||||
pub use async_lock::MutexGuard as AsyncMutexGuard;
|
||||
pub use no_std_net::{ SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs, IpAddr, Ipv4Addr, Ipv6Addr };
|
||||
pub type SystemPinBoxFuture<T> = PinBox<dyn Future<Output = T> + 'static>;
|
||||
pub type SystemPinBoxFutureLifetime<'a, T> = PinBox<dyn Future<Output = T> + 'a>;
|
||||
pub type SystemPinBoxFuture<T> = PinBox<dyn Future<Output = T> + Send + 'static>;
|
||||
pub type SystemPinBoxFutureLifetime<'a, T> = PinBox<dyn Future<Output = T> + Send + 'a>;
|
||||
pub use async_executors::JoinHandle as LowLevelJoinHandle;
|
||||
} else {
|
||||
pub use std::string::String;
|
||||
|
||||
@@ -125,88 +125,81 @@ where
|
||||
}
|
||||
|
||||
// Possibly spawn the future possibly returning the value of the last execution
|
||||
cfg_if! {
|
||||
if #[cfg(target_arch = "wasm32")] {
|
||||
pub async fn single_spawn(
|
||||
&self,
|
||||
future: impl Future<Output = T> + 'static,
|
||||
) -> Result<(Option<T>,bool), ()> {
|
||||
let mut out: Option<T> = None;
|
||||
pub async fn single_spawn_local(
|
||||
&self,
|
||||
future: impl Future<Output = T> + 'static,
|
||||
) -> Result<(Option<T>, bool), ()> {
|
||||
let mut out: Option<T> = None;
|
||||
|
||||
// See if we have a result we can return
|
||||
let maybe_jh = match self.try_lock() {
|
||||
Ok(v) => v,
|
||||
Err(_) => {
|
||||
// If we are already polling somewhere else, don't hand back a result
|
||||
return Err(());
|
||||
}
|
||||
};
|
||||
let mut run = true;
|
||||
// See if we have a result we can return
|
||||
let maybe_jh = match self.try_lock() {
|
||||
Ok(v) => v,
|
||||
Err(_) => {
|
||||
// If we are already polling somewhere else, don't hand back a result
|
||||
return Err(());
|
||||
}
|
||||
};
|
||||
let mut run = true;
|
||||
|
||||
if maybe_jh.is_some() {
|
||||
let mut jh = maybe_jh.unwrap();
|
||||
if maybe_jh.is_some() {
|
||||
let mut jh = maybe_jh.unwrap();
|
||||
|
||||
// See if we finished, if so, return the value of the last execution
|
||||
if let Poll::Ready(r) = poll!(&mut jh) {
|
||||
out = Some(r);
|
||||
// Task finished, unlock with a new task
|
||||
} else {
|
||||
// Still running, don't run again, unlock with the current join handle
|
||||
run = false;
|
||||
self.unlock(Some(jh));
|
||||
}
|
||||
}
|
||||
|
||||
// Run if we should do that
|
||||
if run {
|
||||
self.unlock(Some(intf::spawn_local(future)));
|
||||
}
|
||||
|
||||
// Return the prior result if we have one
|
||||
Ok((out, run))
|
||||
// See if we finished, if so, return the value of the last execution
|
||||
if let Poll::Ready(r) = poll!(&mut jh) {
|
||||
out = Some(r);
|
||||
// Task finished, unlock with a new task
|
||||
} else {
|
||||
// Still running, don't run again, unlock with the current join handle
|
||||
run = false;
|
||||
self.unlock(Some(jh));
|
||||
}
|
||||
}
|
||||
|
||||
// Run if we should do that
|
||||
if run {
|
||||
self.unlock(Some(intf::spawn_local(future)));
|
||||
}
|
||||
|
||||
// Return the prior result if we have one
|
||||
Ok((out, run))
|
||||
}
|
||||
}
|
||||
cfg_if! {
|
||||
if #[cfg(not(target_arch = "wasm32"))] {
|
||||
impl<T> MustJoinSingleFuture<T>
|
||||
where
|
||||
T: 'static + Send,
|
||||
{
|
||||
pub async fn single_spawn(
|
||||
&self,
|
||||
future: impl Future<Output = T> + Send + 'static,
|
||||
) -> Result<(Option<T>, bool), ()> {
|
||||
let mut out: Option<T> = None;
|
||||
// See if we have a result we can return
|
||||
let maybe_jh = match self.try_lock() {
|
||||
Ok(v) => v,
|
||||
Err(_) => {
|
||||
// If we are already polling somewhere else, don't hand back a result
|
||||
return Err(());
|
||||
}
|
||||
};
|
||||
let mut run = true;
|
||||
if maybe_jh.is_some() {
|
||||
let mut jh = maybe_jh.unwrap();
|
||||
// See if we finished, if so, return the value of the last execution
|
||||
if let Poll::Ready(r) = poll!(&mut jh) {
|
||||
out = Some(r);
|
||||
// Task finished, unlock with a new task
|
||||
} else {
|
||||
// Still running, don't run again, unlock with the current join handle
|
||||
run = false;
|
||||
self.unlock(Some(jh));
|
||||
}
|
||||
}
|
||||
// Run if we should do that
|
||||
if run {
|
||||
self.unlock(Some(intf::spawn(future)));
|
||||
}
|
||||
// Return the prior result if we have one
|
||||
Ok((out, run))
|
||||
|
||||
impl<T> MustJoinSingleFuture<T>
|
||||
where
|
||||
T: 'static + Send,
|
||||
{
|
||||
pub async fn single_spawn(
|
||||
&self,
|
||||
future: impl Future<Output = T> + Send + 'static,
|
||||
) -> Result<(Option<T>, bool), ()> {
|
||||
let mut out: Option<T> = None;
|
||||
// See if we have a result we can return
|
||||
let maybe_jh = match self.try_lock() {
|
||||
Ok(v) => v,
|
||||
Err(_) => {
|
||||
// If we are already polling somewhere else, don't hand back a result
|
||||
return Err(());
|
||||
}
|
||||
};
|
||||
let mut run = true;
|
||||
if maybe_jh.is_some() {
|
||||
let mut jh = maybe_jh.unwrap();
|
||||
// See if we finished, if so, return the value of the last execution
|
||||
if let Poll::Ready(r) = poll!(&mut jh) {
|
||||
out = Some(r);
|
||||
// Task finished, unlock with a new task
|
||||
} else {
|
||||
// Still running, don't run again, unlock with the current join handle
|
||||
run = false;
|
||||
self.unlock(Some(jh));
|
||||
}
|
||||
}
|
||||
// Run if we should do that
|
||||
if run {
|
||||
self.unlock(Some(intf::spawn(future)));
|
||||
}
|
||||
// Return the prior result if we have one
|
||||
Ok((out, run))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,23 +3,13 @@ use crate::*;
|
||||
use core::sync::atomic::{AtomicU64, Ordering};
|
||||
use once_cell::sync::OnceCell;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(target_arch = "wasm32")] {
|
||||
type TickTaskRoutine<E> =
|
||||
dyn Fn(StopToken, u64, u64) -> PinBoxFuture<Result<(), E>> + 'static;
|
||||
} else {
|
||||
type TickTaskRoutine<E> =
|
||||
dyn Fn(StopToken, u64, u64) -> SendPinBoxFuture<Result<(), E>> + Send + Sync + 'static;
|
||||
}
|
||||
}
|
||||
type TickTaskRoutine<E> =
|
||||
dyn Fn(StopToken, u64, u64) -> SendPinBoxFuture<Result<(), E>> + Send + Sync + 'static;
|
||||
|
||||
/// Runs a single-future background processing task, attempting to run it once every 'tick period' microseconds.
|
||||
/// If the prior tick is still running, it will allow it to finish, and do another tick when the timer comes around again.
|
||||
/// One should attempt to make tasks short-lived things that run in less than the tick period if you want things to happen with regular periodicity.
|
||||
pub struct TickTask<
|
||||
#[cfg(target_arch = "wasm32")] E: 'static,
|
||||
#[cfg(not(target_arch = "wasm32"))] E: Send + 'static,
|
||||
> {
|
||||
pub struct TickTask<E: Send + 'static> {
|
||||
last_timestamp_us: AtomicU64,
|
||||
tick_period_us: u64,
|
||||
routine: OnceCell<Box<TickTaskRoutine<E>>>,
|
||||
@@ -27,11 +17,7 @@ pub struct TickTask<
|
||||
single_future: MustJoinSingleFuture<Result<(), E>>,
|
||||
}
|
||||
|
||||
impl<
|
||||
#[cfg(target_arch = "wasm32")] E: 'static,
|
||||
#[cfg(not(target_arch = "wasm32"))] E: Send + 'static,
|
||||
> TickTask<E>
|
||||
{
|
||||
impl<E: Send + 'static> TickTask<E> {
|
||||
pub fn new_us(tick_period_us: u64) -> Self {
|
||||
Self {
|
||||
last_timestamp_us: AtomicU64::new(0),
|
||||
|
||||
@@ -167,7 +167,7 @@ pub fn listen_address_to_socket_addrs(listen_address: &str) -> EyreResult<Vec<So
|
||||
cfg_if! {
|
||||
if #[cfg(target_arch = "wasm32")] {
|
||||
use core::str::FromStr;
|
||||
vec![SocketAddr::from_str(listen_address).wrap_err("Unable to parse address")?]
|
||||
vec![SocketAddr::from_str(listen_address).map_err(|e| io_error_other!(e)).wrap_err("Unable to parse address")?]
|
||||
} else {
|
||||
listen_address
|
||||
.to_socket_addrs()
|
||||
|
||||
Reference in New Issue
Block a user