pi-async-executor 0.1.0

默认执行器的统一时钟与线程通知基础
Documentation
//! 预先分配的取消接续节点;只在本模块(module)消费原生队列,不保存异步计算(Future)。
use crate::parking::ThreadNotifier;
use cordyceps::{
    mpsc_queue::{Consumer, Links},
    Linked,
};
use pi_async_task_queue::{management::QueueCleanupProgress, notify::QueueNotifier};
use std::{
    num::NonZeroUsize,
    sync::{
        atomic::{AtomicU8, AtomicUsize},
        Arc,
    },
};

// 静态回返协议;具体节点保存核心登记,本模块只负责排队和责任接续。
// Handle不Clone;原登记可通过另一Arc包装共享,绝不由此复制已入队资格。
// on_drop是原生指针往返可重建的同一函数地址,只用于冷路径单位结算;不指向用户回调。
// 成功出队后先结束Consumer/links引用,再处理核心取消;允许节点安全重用和原址存储。
// state仅AUTO_HELP/REQUEST,不设置永久CLOSED,迟到真实发布仍须帮助,不重开业务来源。
// units是已登记发布、队列保管及临时处理的真实接续持有数,不是业务定时登记数量。
// Consumer内确认清旧请求,归还后实际处理/单位结算,再AcqRel核新请求;独立出口在
// AUTO_HELP时进入帮助登记一次,内部空重查不制造请求。完整Empty+units零优先于请求。
// 只创建时分配stub/共享对象;publish及每次重试只移动原Handle,原生Linked不调用用户代码。
// 用户Report清理和核心resume在Consumer外;Owner退出前实际定时来源已停止或已由真实本地活动者接续。

pub(super) trait RetryWork:
    Send + Sync + Sized + 'static + Linked<Links<Self>, Handle = RetryHandle<Self>>
{
    fn resume(this: &Arc<Self>);
}
pub(super) struct RetryHandle<W> {
    pub(super) value: Arc<W>,
    pub(super) on_drop: Option<fn(&W)>,
}
pub(super) struct RetryShared<W: Linked<Links<W>, Handle = RetryHandle<W>>> {
    queue: cordyceps::MpscQueue<W>,
    state: AtomicU8,
    units: AtomicUsize,
    notifier: ThreadNotifier,
    batch: NonZeroUsize,
}
pub(super) struct RetryOwner<W: RetryWork> {
    shared: Arc<RetryShared<W>>,
}
struct RetryConsumerGuard<'a, W: RetryWork> {
    consumer: Option<Consumer<'a, W>>,
}
struct RetryCompletionGuard<'a, W: RetryWork> {
    shared: &'a RetryShared<W>,
    armed: bool,
}
struct RetryCleanupGuard<'a, W: RetryWork> {
    shared: &'a RetryShared<W>,
    current: Option<RetryHandle<W>>,
    resume_tail: bool,
}
const RETRY_AUTO_HELP: u8 = 1;
const RETRY_REQUEST: u8 = 2;
enum RetryStep {
    Complete,
    MoreWork,
    Deferred,
    Busy,
}
impl<W> Drop for RetryHandle<W> {
    fn drop(&mut self) {
        retry_handle_drop_inner(self)
    }
}
impl<W: RetryWork> RetryOwner<W> {
    pub(super) fn new(stub: RetryHandle<W>, notifier: ThreadNotifier, batch: NonZeroUsize) -> Self {
        retry_owner_new_inner(stub, notifier, batch)
    }
    pub(super) fn shared(&self) -> Arc<RetryShared<W>> {
        retry_owner_shared_inner(self)
    }
    pub(super) fn process(&mut self) -> QueueCleanupProgress {
        retry_owner_process_inner(self)
    }
}
impl<W: RetryWork> Drop for RetryOwner<W> {
    fn drop(&mut self) {
        retry_owner_drop_inner(self)
    }
}
impl<W: RetryWork> RetryShared<W> {
    // 只接收同源且已由具体节点原子取得一次链接资格的原Handle;不公开此入口。
    pub(super) fn publish(&self, value: RetryHandle<W>) {
        retry_publish_inner(self, value)
    }
    // 原Handle在发布登记前不带单位,into_ptr后仍保相同结算回返;Stub永不计单位。
    fn confirm(&self) {
        if retry_confirm_inner(self) {
            self.assist();
        }
    }
    fn assist(&self) -> RetryStep {
        retry_request_inner(self);
        loop {
            match self.step(true) {
                RetryStep::MoreWork => continue,
                result @ (RetryStep::Complete | RetryStep::Deferred | RetryStep::Busy) => {
                    return result
                }
            }
        }
    }
    fn step(&self, helping: bool) -> RetryStep {
        retry_step_inner(self, helping)
    }
    pub(super) fn begin_close(&self) -> QueueCleanupProgress {
        retry_begin_close_inner(self)
    }
    pub(super) fn has_work(&self) -> bool {
        retry_has_work_inner(self)
    }
}
impl<W: Linked<Links<W>, Handle = RetryHandle<W>>> RetryShared<W> {
    pub(super) fn return_unit(&self) {
        retry_return_unit_inner(self)
    }
}
impl<W: RetryWork> Drop for RetryConsumerGuard<'_, W> {
    fn drop(&mut self) {
        retry_consumer_drop_inner(self)
    }
}
impl<W: RetryWork> Drop for RetryCompletionGuard<'_, W> {
    fn drop(&mut self) {
        retry_completion_drop_inner(self)
    }
}
impl<W: RetryWork> Drop for RetryCleanupGuard<'_, W> {
    fn drop(&mut self) {
        retry_cleanup_drop_inner(self)
    }
}
include!("retry_inner.rs");