use core::cmp::Reverse;
use core::future::Future;
use core::pin::{pin, Pin};
use core::sync::atomic::{AtomicBool, Ordering};
use core::task::{Context, Poll, Waker};
use core::time::Duration;
use std::boxed::Box;
use std::collections::{BinaryHeap, VecDeque};
use std::sync::{Arc, Condvar, Mutex, OnceLock};
use std::task::Wake;
use std::time::Instant;
use super::backend::{BoxFuture, SchedulerBackend};
use super::scheduler::SchedulerCapabilities;
use super::task::{Task, TaskShared};
pub fn block_on<F: Future>(fut: F) -> F::Output {
struct ThreadWaker {
ready: Mutex<bool>,
cv: Condvar,
}
impl Wake for ThreadWaker {
fn wake(self: Arc<Self>) {
self.wake_by_ref();
}
fn wake_by_ref(self: &Arc<Self>) {
if let Ok(mut ready) = self.ready.lock() {
*ready = true;
self.cv.notify_one();
}
}
}
let signal = Arc::new(ThreadWaker {
ready: Mutex::new(false),
cv: Condvar::new(),
});
let waker = Waker::from(signal.clone());
let mut cx = Context::from_waker(&waker);
let mut fut = pin!(fut);
loop {
match fut.as_mut().poll(&mut cx) {
Poll::Ready(out) => return out,
Poll::Pending => {
if let Ok(mut ready) = signal.ready.lock() {
while !*ready {
ready = match signal.cv.wait(ready) {
Ok(g) => g,
Err(poisoned) => poisoned.into_inner(),
};
}
*ready = false;
}
}
}
}
}
#[cfg(not(target_arch = "wasm32"))]
struct TimerEntry {
deadline: Instant,
seq: u64,
waker: Waker,
}
#[cfg(not(target_arch = "wasm32"))]
impl PartialEq for TimerEntry {
fn eq(&self, other: &Self) -> bool {
self.deadline == other.deadline && self.seq == other.seq
}
}
#[cfg(not(target_arch = "wasm32"))]
impl Eq for TimerEntry {}
#[cfg(not(target_arch = "wasm32"))]
impl PartialOrd for TimerEntry {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
#[cfg(not(target_arch = "wasm32"))]
impl Ord for TimerEntry {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.deadline
.cmp(&other.deadline)
.then_with(|| self.seq.cmp(&other.seq))
}
}
#[cfg(not(target_arch = "wasm32"))]
struct TimerDriver {
state: Mutex<(u64, BinaryHeap<Reverse<TimerEntry>>)>,
cv: Condvar,
}
#[cfg(not(target_arch = "wasm32"))]
static TIMER_DRIVER: OnceLock<Arc<TimerDriver>> = OnceLock::new();
#[cfg(not(target_arch = "wasm32"))]
fn timer_driver() -> &'static Arc<TimerDriver> {
TIMER_DRIVER.get_or_init(|| {
let driver = Arc::new(TimerDriver {
state: Mutex::new((0, BinaryHeap::new())),
cv: Condvar::new(),
});
let bg = Arc::clone(&driver);
let _ = std::thread::Builder::new()
.name(std::string::String::from("velox-async-timer"))
.stack_size(128 * 1024)
.spawn(move || loop {
let waker_to_fire = {
let mut guard = bg.state.lock().unwrap_or_else(|e| e.into_inner());
loop {
let Some(Reverse(top)) = guard.1.peek() else {
guard = bg.cv.wait(guard).unwrap_or_else(|e| e.into_inner());
continue;
};
let now = Instant::now();
if top.deadline <= now {
let Reverse(entry) = guard.1.pop().unwrap();
break entry.waker;
}
let wait_dur = top.deadline.saturating_duration_since(now);
let (next_guard, _) = bg
.cv
.wait_timeout(guard, wait_dur)
.unwrap_or_else(|e| e.into_inner());
guard = next_guard;
}
};
waker_to_fire.wake();
});
driver
})
}
struct NativeTimer {
deadline: Instant,
registered: bool,
}
impl NativeTimer {
#[inline]
fn after(duration: Duration) -> Self {
Self {
deadline: Instant::now() + duration,
registered: false,
}
}
fn poll_native(&mut self, cx: &mut Context<'_>) -> Poll<()> {
if Instant::now() >= self.deadline {
return Poll::Ready(());
}
#[cfg(not(target_arch = "wasm32"))]
{
if !self.registered {
self.registered = true;
let driver = timer_driver();
let mut guard = driver.state.lock().unwrap_or_else(|e| e.into_inner());
guard.0 = guard.0.wrapping_add(1);
let seq = guard.0;
let is_earlier = guard
.1
.peek()
.map(|Reverse(top)| self.deadline < top.deadline)
.unwrap_or(true);
guard.1.push(Reverse(TimerEntry {
deadline: self.deadline,
seq,
waker: cx.waker().clone(),
}));
if is_earlier {
driver.cv.notify_one();
}
}
Poll::Pending
}
#[cfg(target_arch = "wasm32")]
{
let _ = cx;
Poll::Ready(())
}
}
}
enum TimerInner {
Native(NativeTimer),
Custom(BoxFuture<'static, ()>),
}
pub struct Timer {
inner: TimerInner,
}
impl Timer {
#[inline]
pub fn after(duration: Duration) -> Self {
Self {
inner: TimerInner::Native(NativeTimer::after(duration)),
}
}
#[inline]
pub fn from_future(fut: BoxFuture<'static, ()>) -> Self {
Self {
inner: TimerInner::Custom(fut),
}
}
}
impl Unpin for Timer {}
impl Future for Timer {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match &mut self.get_mut().inner {
TimerInner::Native(native) => native.poll_native(cx),
TimerInner::Custom(custom) => custom.as_mut().poll(cx),
}
}
}
#[cfg(not(target_arch = "wasm32"))]
struct BgJob {
future: Mutex<Option<BoxFuture<'static, ()>>>,
scheduled: AtomicBool,
}
#[cfg(not(target_arch = "wasm32"))]
impl BgJob {
fn enqueue(self: &Arc<Self>) {
if !self.scheduled.swap(true, Ordering::AcqRel) {
let pool = bg_pool();
let mut q = pool.queue.lock().unwrap_or_else(|e| e.into_inner());
q.push_back(Arc::clone(self));
pool.cv.notify_one();
}
}
fn run(self: &Arc<Self>) {
self.scheduled.store(false, Ordering::Release);
let mut fut = {
let mut slot = self.future.lock().unwrap_or_else(|e| e.into_inner());
let Some(f) = slot.take() else {
return;
};
f
};
let waker = Waker::from(Arc::clone(self));
let mut cx = Context::from_waker(&waker);
match fut.as_mut().poll(&mut cx) {
Poll::Ready(()) => {}
Poll::Pending => {
let mut slot = self.future.lock().unwrap_or_else(|e| e.into_inner());
*slot = Some(fut);
}
}
}
}
#[cfg(not(target_arch = "wasm32"))]
impl Wake for BgJob {
fn wake(self: Arc<Self>) {
self.enqueue();
}
fn wake_by_ref(self: &Arc<Self>) {
self.enqueue();
}
}
#[cfg(not(target_arch = "wasm32"))]
struct BackgroundPool {
queue: Mutex<VecDeque<Arc<BgJob>>>,
cv: Condvar,
}
#[cfg(not(target_arch = "wasm32"))]
static BG_POOL: OnceLock<Arc<BackgroundPool>> = OnceLock::new();
#[cfg(not(target_arch = "wasm32"))]
fn bg_pool() -> &'static Arc<BackgroundPool> {
BG_POOL.get_or_init(|| {
let pool = Arc::new(BackgroundPool {
queue: Mutex::new(VecDeque::new()),
cv: Condvar::new(),
});
let worker_count = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(2)
.clamp(2, 4);
for idx in 0..worker_count {
let p = Arc::clone(&pool);
let _ = std::thread::Builder::new()
.name(std::format!("velox-bg-{idx}"))
.stack_size(256 * 1024)
.spawn(move || loop {
let job = {
let mut q = p.queue.lock().unwrap_or_else(|e| e.into_inner());
while q.is_empty() {
q = p.cv.wait(q).unwrap_or_else(|e| e.into_inner());
}
q.pop_front()
};
if let Some(job) = job {
job.run();
}
});
}
pool
})
}
pub struct NativeStdScheduler {
epoch: Instant,
}
static GLOBAL_NATIVE_SCHEDULER: OnceLock<Arc<dyn SchedulerBackend>> = OnceLock::new();
impl NativeStdScheduler {
pub fn new() -> Self {
Self {
epoch: Instant::now(),
}
}
pub fn global() -> Arc<dyn SchedulerBackend> {
Arc::clone(GLOBAL_NATIVE_SCHEDULER.get_or_init(|| Arc::new(Self::new())))
}
}
impl Default for NativeStdScheduler {
fn default() -> Self {
Self::new()
}
}
impl SchedulerBackend for NativeStdScheduler {
#[inline]
fn name(&self) -> &'static str {
"velox-native-std"
}
#[inline]
fn capabilities(&self) -> SchedulerCapabilities {
SchedulerCapabilities {
name: self.name(),
supports_background_threads: cfg!(not(target_arch = "wasm32")),
supports_dynamic_spawn: true,
deterministic_clock: false,
}
}
fn spawn_background(&self, fut: BoxFuture<'static, ()>) {
#[cfg(not(target_arch = "wasm32"))]
{
let job = Arc::new(BgJob {
future: Mutex::new(Some(fut)),
scheduled: AtomicBool::new(false),
});
job.enqueue();
}
#[cfg(target_arch = "wasm32")]
{
block_on(fut);
}
}
#[inline]
fn timer(&self, duration: Duration) -> Timer {
Timer::after(duration)
}
#[inline]
fn now_ms(&self) -> u64 {
self.epoch.elapsed().as_millis().min(u64::MAX as u128) as u64
}
}
struct CancellableFuture<T, Fut> {
fut: Option<Pin<Box<Fut>>>,
shared: Arc<TaskShared<T>>,
cancel_waker: Arc<Mutex<Option<Waker>>>,
}
impl<T, Fut> Unpin for CancellableFuture<T, Fut> {}
impl<T, Fut> Future for CancellableFuture<T, Fut>
where
T: Send + 'static,
Fut: Future<Output = T> + Send + 'static,
{
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
if this.shared.is_cancelled() {
this.fut = None;
let mut slot = this.cancel_waker.lock().unwrap_or_else(|e| e.into_inner());
*slot = None;
return Poll::Ready(());
}
{
let mut slot = this.cancel_waker.lock().unwrap_or_else(|e| e.into_inner());
let should_update = slot
.as_ref()
.map(|prev| !prev.will_wake(cx.waker()))
.unwrap_or(true);
if should_update {
*slot = Some(cx.waker().clone());
}
}
let Some(fut) = this.fut.as_mut() else {
return Poll::Ready(());
};
match fut.as_mut().poll(cx) {
Poll::Ready(output) => {
this.fut = None;
{
let mut slot = this.cancel_waker.lock().unwrap_or_else(|e| e.into_inner());
*slot = None;
}
if !this.shared.is_cancelled() {
this.shared.complete(output);
}
Poll::Ready(())
}
Poll::Pending => {
if this.shared.is_cancelled() {
this.fut = None;
let mut slot = this.cancel_waker.lock().unwrap_or_else(|e| e.into_inner());
*slot = None;
return Poll::Ready(());
}
Poll::Pending
}
}
}
}
#[derive(Clone)]
pub struct BackgroundExecutor {
backend: Arc<dyn SchedulerBackend>,
}
impl Default for BackgroundExecutor {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl core::fmt::Debug for BackgroundExecutor {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("BackgroundExecutor")
.field("scheduler", &self.backend.name())
.finish()
}
}
impl BackgroundExecutor {
#[inline]
pub fn new() -> Self {
Self {
backend: NativeStdScheduler::global(),
}
}
#[inline]
pub fn with_backend(backend: Arc<dyn SchedulerBackend>) -> Self {
Self { backend }
}
#[inline]
pub fn backend(&self) -> &Arc<dyn SchedulerBackend> {
&self.backend
}
#[inline]
pub fn scheduler_name(&self) -> &'static str {
self.backend.name()
}
#[inline]
pub fn capabilities(&self) -> SchedulerCapabilities {
self.backend.capabilities()
}
#[inline]
pub fn now_ms(&self) -> u64 {
self.backend.now_ms()
}
pub fn spawn<T, Fut>(&self, fut: Fut) -> Task<T>
where
T: Send + 'static,
Fut: Future<Output = T> + Send + 'static,
{
let cancelled = Arc::new(AtomicBool::new(false));
let cancel_waker: Arc<Mutex<Option<Waker>>> = Arc::new(Mutex::new(None));
let cancel_waker_cb = Arc::clone(&cancel_waker);
let on_cancel: Arc<dyn Fn() + Send + Sync> = Arc::new(move || {
let waker = {
let mut slot = cancel_waker_cb.lock().unwrap_or_else(|e| e.into_inner());
slot.take()
};
if let Some(w) = waker {
w.wake();
}
});
let shared = TaskShared::new(cancelled, Some(on_cancel));
let wrapped = CancellableFuture {
fut: Some(Box::pin(fut)),
shared: Arc::clone(&shared),
cancel_waker,
};
self.backend.spawn_background(Box::pin(wrapped));
Task::from_shared(shared)
}
#[inline]
pub fn timer(&self, duration: Duration) -> Timer {
self.backend.timer(duration)
}
}