pi-async-executor 0.1.3

默认执行器的统一时钟与线程通知基础
Documentation
fn prepare_default_timer_inner<C, L>(
    runtime: RuntimeId,
    clock: C,
    notifier: ThreadNotifier,
    options: CancelTimerOptions,
) -> pi_result::Result<PreparedDefaultTimer<C, L>>
where
    C: ExecutorClock<Instant = GlobalManualInstant>,
    L: LocalTimerSubmit<DefaultTimerItem<C, L>>,
{
    let queue = CancelTimerQueue::prepare(notifier.clone(), options)?;
    let retries = new_retry_owner(notifier, options.work_batch_size);
    let retry_shared = retries.shared();
    let shared = Arc::new(TimerSourceShared {
        runtime,
        clock,
        backend: queue.handle(),
        retries: retry_shared,
        owner_exiting: std::sync::atomic::AtomicBool::new(false),
        local: std::marker::PhantomData,
    });
    let access = DefaultTimerAccess { shared };
    Ok(PreparedDefaultTimer {
        queue: Some(queue),
        access,
        retry: Some(retries),
    })
}

fn prepared_default_timer_access_inner<C, L>(
    this: &PreparedDefaultTimer<C, L>,
) -> DefaultTimerAccess<C, L>
where
    C: ExecutorClock<Instant = GlobalManualInstant>,
    L: LocalTimerSubmit<DefaultTimerItem<C, L>>,
{
    this.access.clone()
}

fn bind_default_timer_inner<C, L>(
    mut this: PreparedDefaultTimer<C, L>,
    notifier: &ThreadNotifier,
    now: GlobalManualInstant,
    may_admit: impl Fn(TaskId) -> Option<bool> + 'static,
) -> pi_result::Result<DefaultTimerDriver<C, L>>
where
    C: ExecutorClock<Instant = GlobalManualInstant>,
    L: LocalTimerSubmit<DefaultTimerItem<C, L>>,
{
    if this
        .access
        .shared
        .owner_exiting
        .load(std::sync::atomic::Ordering::Acquire)
    {
        return Err(TimerQueueError::Closed.into_report());
    }
    let Some(prepared_queue) = this.queue.take() else {
        return Err(TimerQueueError::Closed.into_report());
    };
    let Some(retry) = this.retry.take() else {
        this.access
            .shared
            .owner_exiting
            .store(true, std::sync::atomic::Ordering::Release);
        drop(prepared_queue);
        return Err(TimerQueueError::Closed.into_report());
    };
    let bound_queue = match prepared_queue.bind(now) {
        Ok(queue) => queue,
        Err(error) => {
            this.access
                .shared
                .owner_exiting
                .store(true, std::sync::atomic::Ordering::Release);
            let _ = this.access.shared.retries.begin_close();
            drop(retry);
            return Err(error);
        }
    };
    let validate = (|| -> pi_result::Result<()> {
        if !matches!(
            pi_async_task_queue::TimerTaskQueue::status(&bound_queue),
            TimerQueueStatus::Accepting
        ) {
            return Err(TimerQueueError::Closed.into_report());
        }
        if !bound_queue.uses_notifier(notifier) {
            return Err(pi_result::Error::new(pi_result::ErrorKind::InvalidInput)
                .attach("预备定时来源(prepared timer source)与目标线程通知句柄不一致"));
        }
        let current = this.access.shared.clock.now()?;
        pi_async_task_queue::TimerTaskQueue::next_check_at(&bound_queue, &current)
            .map_err(TimerQueueError::into_report)?;
        Ok(())
    })();
    if let Err(error) = validate {
        this.access
            .shared
            .owner_exiting
            .store(true, std::sync::atomic::Ordering::Release);
        drop(bound_queue);
        let _ = this.access.shared.retries.begin_close();
        drop(retry);
        return Err(error);
    }
    let core = Rc::new(TimerLocalCore {
        queue: RefCell::new(Some(bound_queue)),
        retry: RefCell::new(retry),
        shared: Arc::clone(&this.access.shared),
        thread: std::thread::current().id(),
        close_error: RefCell::new(None),
        admission: Some(TimerAdmission {
            may_admit: Box::new(may_admit),
        }),
    });
    Ok(DefaultTimerDriver { core })
}

fn prepared_default_timer_drop_inner<C, L>(this: &mut PreparedDefaultTimer<C, L>)
where
    C: ExecutorClock<Instant = GlobalManualInstant>,
    L: LocalTimerSubmit<DefaultTimerItem<C, L>>,
{
    if this.queue.is_none() && this.retry.is_none() {
        return;
    }
    this.access
        .shared
        .owner_exiting
        .store(true, std::sync::atomic::Ordering::Release);
    drop(this.queue.take());
    if let Some(retry) = this.retry.take() {
        let _ = this.access.shared.retries.begin_close();
        drop(retry);
    }
}