use std::collections::VecDeque;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
#[cfg(not(target_family = "wasm"))]
use mln_actor::RunLoop;
use parking_lot::Mutex;
type Task<S> = Box<dyn FnOnce(&mut S) + Send>;
pub(crate) struct Queue<S> {
inner: Arc<Inner<S>>,
}
struct Inner<S> {
tasks: Mutex<VecDeque<Task<S>>>,
waker: Mutex<Option<Arc<dyn Fn() + Send + Sync>>>,
stopped: AtomicBool,
}
impl<S> Clone for Queue<S> {
fn clone(&self) -> Self {
Queue { inner: Arc::clone(&self.inner) }
}
}
impl<S> Default for Queue<S> {
fn default() -> Self {
Queue {
inner: Arc::new(Inner {
tasks: Mutex::new(VecDeque::new()),
waker: Mutex::new(None),
stopped: AtomicBool::new(false),
}),
}
}
}
impl<S: 'static> Queue<S> {
pub(crate) fn post(&self, task: impl FnOnce(&mut S) + Send + 'static) {
self.inner.tasks.lock().push_back(Box::new(task));
self.wake();
}
pub(crate) fn set_waker(&self, waker: Option<Arc<dyn Fn() + Send + Sync>>) {
*self.inner.waker.lock() = waker;
}
pub(crate) fn wake(&self) {
let waker = self.inner.waker.lock().clone();
if let Some(waker) = waker {
waker();
}
}
pub(crate) fn run_once(&self, state: &mut S) -> bool {
let mut ran = false;
loop {
let Some(task) = self.inner.tasks.lock().pop_front() else { return ran };
task(state);
ran = true;
}
}
pub(crate) fn has_pending(&self) -> bool {
!self.inner.tasks.lock().is_empty()
}
pub(crate) fn stop(&self) {
let inner = Arc::clone(&self.inner);
self.post(move |_| inner.stopped.store(true, Ordering::SeqCst));
}
pub(crate) fn is_stopped(&self) -> bool {
self.inner.stopped.load(Ordering::SeqCst)
}
#[cfg(not(target_family = "wasm"))]
pub(crate) fn run_until_stopped(&self, run_loop: &RunLoop, state: &mut S) {
let handle = run_loop.handle();
self.set_waker(Some(Arc::new(move || handle.wake())));
loop {
run_loop.run_once();
self.run_once(state);
if self.is_stopped() {
return;
}
if !run_loop.has_pending() && !self.has_pending() {
run_loop.wait_for_work();
}
}
}
}