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,
},
};
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> {
pub(super) fn publish(&self, value: RetryHandle<W>) {
retry_publish_inner(self, value)
}
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");