use crate::drive::{AdmissionRule, ConcurrentMode};
use pi_async_task::{definitions::TaskOwnerId, Runnable, TaskThread};
use pi_async_task_queue::{
external::ExternalTake,
BatchAttempt, ExternalQueueManager, ExternalTaskQueue, InternalTaskQueue, QueueCleanupProgress,
QueuePushError, QueuePushErrorKind, TaskQueue,
};
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
#[doc(hidden)]
#[allow(private_bounds)]
pub trait ExternalBinding: ExternalOwnerOperations<Shared = Self::Handle> {
type Handle: ExternalAccess;
}
#[doc(hidden)]
#[allow(private_bounds)]
pub trait ExternalAdmission<A: AdmissionRule>: ExternalBinding + ExternalAdmissionPair<A> {}
pub(crate) trait ExternalAdmissionPair<A: AdmissionRule> {}
impl<E: ExternalQueueManager, A: AdmissionRule> ExternalAdmissionPair<A> for E {}
impl ExternalAdmissionPair<ConcurrentMode> for LocalTaskSource {}
impl<E, A> ExternalAdmission<A> for E
where
E: ExternalBinding + ExternalAdmissionPair<A>,
A: AdmissionRule,
{
}
#[doc(hidden)]
#[allow(private_bounds)]
pub trait ExternalAccess: ExternalAccessOperations + Clone + Send + Sync + 'static {}
#[doc(hidden)]
pub struct LocalTaskSource {
before_close: Option<Box<dyn FnOnce()>>,
}
#[doc(hidden)]
#[derive(Clone)]
pub struct LocalTaskSourceAccess(Arc<AtomicUsize>);
#[doc(hidden)]
pub struct LocalTaskQueue(TaskQueue<true>);
impl LocalTaskQueue {
pub(crate) fn new(owner: &TaskThread) -> Self {
Self(TaskQueue::new_with_unbound(owner))
}
pub(crate) fn push_ordinary(
&mut self,
task: Runnable,
) -> pi_result::RawResult<(), QueuePushError> {
self.0.push_back(task)
}
}
impl InternalTaskQueue for LocalTaskQueue {
fn try_push_internal_front(
&mut self,
task: Runnable,
) -> pi_result::RawResult<(), QueuePushError> {
self.0.push_front(task)
}
fn try_push_internal_back(
&mut self,
task: Runnable,
) -> pi_result::RawResult<(), QueuePushError> {
local_task_queue_push_internal_inner(self, task)
}
fn pop_internal_front(&mut self) -> Option<Runnable> {
self.0.pop_front()
}
fn internal_len(&self) -> usize {
self.0.len()
}
fn internal_owner_id(&self) -> Option<TaskOwnerId> {
Some(self.0.owner_id())
}
fn is_internal_closed(&self) -> bool {
self.0.is_internal_closed()
}
fn clear_internal(&mut self) -> pi_result::Result<QueueCleanupProgress> {
self.0.clear_internal()
}
fn close_internal(&mut self) -> pi_result::Result<QueueCleanupProgress> {
self.0.close_internal()
}
}
pub(crate) trait ExternalOwnerOperations: Sized {
type Shared: ExternalAccess;
fn shared_external(&self) -> Self::Shared;
fn close_external(&mut self) -> pi_result::Result<QueueCleanupProgress>;
}
pub(crate) trait ExternalAccessOperations {
const COMBINED: bool;
fn external_count(&self) -> usize;
fn take_external(&self) -> ExternalTake;
fn local_pending(&self) -> Option<&LocalTaskSourceAccess> {
None
}
}
impl<E: ExternalQueueManager> ExternalBinding for E {
type Handle = E::Handle;
}
impl<E: ExternalQueueManager> ExternalOwnerOperations for E {
type Shared = E::Handle;
fn shared_external(&self) -> Self::Shared {
self.handle()
}
fn close_external(&mut self) -> pi_result::Result<QueueCleanupProgress> {
external_close_inner(self)
}
}
impl<E: ExternalTaskQueue> ExternalAccess for E {}
impl<E: ExternalTaskQueue> ExternalAccessOperations for E {
const COMBINED: bool = false;
fn external_count(&self) -> usize {
self.len()
}
fn take_external(&self) -> ExternalTake {
self.try_take_one_or_batch()
}
}
impl LocalTaskSource {
pub(crate) fn new(before_close: Box<dyn FnOnce()>) -> Self {
Self {
before_close: Some(before_close),
}
}
fn revoke(&mut self) {
local_task_source_revoke_inner(self);
}
}
impl Drop for LocalTaskSource {
fn drop(&mut self) {
self.revoke();
}
}
impl ExternalBinding for LocalTaskSource {
type Handle = LocalTaskSourceAccess;
}
impl ExternalOwnerOperations for LocalTaskSource {
type Shared = LocalTaskSourceAccess;
fn shared_external(&self) -> Self::Shared {
LocalTaskSourceAccess(Arc::new(AtomicUsize::new(0)))
}
fn close_external(&mut self) -> pi_result::Result<QueueCleanupProgress> {
self.revoke();
Ok(QueueCleanupProgress::Complete)
}
}
impl LocalTaskSourceAccess {
pub(crate) fn accepted(&self) {
self.0.fetch_add(1, Ordering::Release);
}
pub(crate) fn selected(&self) {
self.0.fetch_sub(1, Ordering::Release);
}
pub(crate) fn cleared(&self) {
self.0.store(0, Ordering::Release);
}
}
impl ExternalAccess for LocalTaskSourceAccess {}
impl ExternalAccessOperations for LocalTaskSourceAccess {
const COMBINED: bool = true;
fn external_count(&self) -> usize {
self.0.load(Ordering::Acquire)
}
fn take_external(&self) -> ExternalTake {
ExternalTake::Batch(BatchAttempt::Empty)
}
fn local_pending(&self) -> Option<&LocalTaskSourceAccess> {
Some(self)
}
}
include!("external_binding_inner.rs");