pi-async-executor 0.1.2

默认执行器的统一时钟与线程通知基础
Documentation
fn real_member_owner_inner<E, I, T, L>(
    this: &ExecutorMemberAccess<E, I, T, L>,
) -> pi_async_task::definitions::TaskOwnerId
where
    E: ExternalTaskQueue,
    I: IdleTaskQueue,
    T: ExecutorTimer + 'static,
    L: LocalReadySubmit,
{
    this.handle.owner
}

fn real_member_return_inner<E, I, T, L>(
    this: &ExecutorMemberAccess<E, I, T, L>,
    task: Runnable,
) -> pi_result::RawResult<(), Runnable>
where
    E: ExternalTaskQueue,
    I: IdleTaskQueue,
    T: ExecutorTimer + 'static,
    L: LocalReadySubmit,
{
    if task.snapshot().owner != Some(this.handle.owner) {
        return Err(task);
    }
    this.handle.factory.return_to_schedule(task);
    Ok(())
}

fn executor_member_submit_idle_inner<E, I, T, L>(
    this: &ExecutorMemberAccess<E, I, T, L>,
    task: Runnable,
) -> pi_result::RawResult<(), pi_async_task_queue::idle::IdleSubmitError>
where
    E: ExternalTaskQueue,
    I: IdleTaskQueue,
    T: ExecutorTimer + 'static,
    L: LocalReadySubmit,
{
    use pi_async_task_queue::idle::{IdleQueueError, IdleSubmitError};

    let snapshot = task.snapshot();
    if snapshot.owner.is_some() || snapshot.admitted {
        return Err(IdleSubmitError {
            task,
            cause: IdleQueueError::Backend(
                pi_result::Error::new(pi_result::ErrorKind::InvalidInput)
                    .attach("成员空闲提交(member idle submit)只接受未绑定的普通任务(Task)"),
            ),
        });
    }
    if matches!(
        this.handle.state.status(),
        RuntimeStatus::Closing | RuntimeStatus::Closed
    ) {
        return Err(IdleSubmitError {
            task,
            cause: IdleQueueError::Closed,
        });
    }
    let mut guard = SourceNotifyGuard {
        notifier: &this.handle.notifier,
        armed: true,
    };
    let result = this.handle.idle.try_push(task);
    guard.armed = false;
    if result.is_ok() {
        pi_async_task_queue::notify::QueueNotifier::notify(&this.handle.notifier);
    }
    result
}

fn executor_member_create_local_with_notifier_inner<E, I, T, L, N, F, M>(
    this: &ExecutorMemberAccess<E, I, T, L>,
    local: &ExecutorLocal<T>,
    notifier: N,
    future: F,
    config: TaskConfig<M>,
) -> pi_result::RawResult<
    (UnsubmittedTask, TaskHandle<N, F::Output, M, LocalOutput>),
    CreateError<F, M>,
>
where
    E: ExternalTaskQueue,
    I: IdleTaskQueue,
    T: ExecutorTimer + 'static,
    L: LocalReadySubmit,
    N: TaskNotifier,
    F: Future + 'static,
    F::Output: 'static,
    M: Send + Sync + 'static,
{
    if local.runtime_id() != this.handle.factory.runtime_id() {
        return Err(CreateError {
            future,
            config,
            cause: CreateErrorKind::LocalContext(LocalRuntimeAccessError::TypeMismatch),
        });
    }
    let mut pending = Some((notifier, future, config));
    let registry = this.handle.registry.clone();
    let expected_owner = this.handle.owner;
    let created = local.with_owner(|owner| {
        let (notifier, future, config) = pending.take().expect("原值与配置只在此处取出一次");
        if owner.id() != expected_owner {
            return Err(CreateError {
                future,
                config,
                cause: CreateErrorKind::LocalContext(LocalRuntimeAccessError::TypeMismatch),
            });
        }
        let factory = this.handle.factory.with_notifier(notifier);
        factory.create_local(owner, future, config)
    });
    match created {
        Ok(result) => {
            let (task, handle) = result?;
            registry.register(handle.control_handle().query_handle());
            Ok((task, handle))
        }
        Err(_) => {
            let (_notifier, future, config) = pending.take().expect("未取得所属域时原值仍在本处");
            Err(CreateError {
                future,
                config,
                cause: CreateErrorKind::LocalContext(LocalRuntimeAccessError::ContextUnavailable),
            })
        }
    }
}

fn executor_member_spawn_timing_local_with_notifier_inner<E, I, T, L, N, A, F, M>(
    this: &ExecutorMemberAccess<E, I, T, L>,
    local: &ExecutorLocal<T>,
    notifier: N,
    access: A,
    future: F,
    delay: Duration,
    config: TaskConfig<M>,
) -> pi_result::RawResult<
    TaskHandle<TimedTaskNotifier<N, A>, F::Output, M, LocalOutput>,
    SpawnError<F, TimedTaskNotifier<N, A>, M, LocalOutput>,
>
where
    E: ExternalTaskQueue,
    I: IdleTaskQueue,
    T: ExecutorTimer + 'static,
    L: LocalReadySubmit,
    N: TaskNotifier,
    A: TimerAccess,
    F: Future + 'static,
    F::Output: 'static,
    M: Send + Sync + 'static,
{
    if local.runtime_id() != this.handle.factory.runtime_id() {
        return Err(SpawnError::Create(CreateError {
            future,
            config,
            cause: CreateErrorKind::LocalContext(LocalRuntimeAccessError::TypeMismatch),
        }));
    }
    let mut pending = Some((notifier, access, future, config));
    let registry = this.handle.registry.clone();
    let expected_owner = this.handle.owner;
    let outcome = local.with_owner(|owner| {
        let (notifier, access, future, config) =
            pending.take().expect("原值与配置只在此处取出一次");
        if owner.id() != expected_owner {
            return Err(SpawnError::Create(CreateError {
                future,
                config,
                cause: CreateErrorKind::LocalContext(LocalRuntimeAccessError::TypeMismatch),
            }));
        }
        let factory = this.handle.factory.with_notifier(notifier);
        pi_async_runtime_core::time::task_support::spawn_local_task_with_query(
            &factory,
            owner,
            access,
            future,
            delay,
            config,
            move |query| registry.register(query),
        )
    });
    match outcome {
        Ok(result) => result,
        Err(_) => {
            let (_notifier, _access, future, config) =
                pending.take().expect("未取得所属域时原值仍在本处");
            Err(SpawnError::Create(CreateError {
                future,
                config,
                cause: CreateErrorKind::LocalContext(LocalRuntimeAccessError::ContextUnavailable),
            }))
        }
    }
}