use super::definitions::{
CancelTimerKey, TaggedRegistrationId, TimerLinks, TimerListEnds, MAX_DEADLINE_NANOS,
};
use super::shared::{TimerRequest, TimerShared};
use crate::management::QueueCleanupProgress;
use crate::notify::QueueNotifier;
use crate::timer::{TimerAdvanceProgress, TimerQueueError, TimerSubmitError, TimerTake};
use indexmap::IndexMap;
use pi_clock_manual::global::GlobalManualInstant;
use std::collections::VecDeque;
use std::num::NonZeroU64;
pub(super) struct TimerLocalState<Item: 'static> {
native: pi_cancel_timer::Timer<TaggedRegistrationId, 100, 60, 3, 1>,
reference: GlobalManualInstant,
groups: [TimerGroupStorage<Item>; 2],
cleanup: TimerListEnds,
staged: VecDeque<Box<TimerRequest<Item>>>,
clear: TimerClearState<Item>,
}
struct TimerGroupStorage<Item: 'static> {
entries: IndexMap<NonZeroU64, TimerRegistration<Item>>,
due: TimerListEnds,
due_len: usize,
}
struct TimerRegistration<Item: 'static> {
item: Item,
phase: TimerRegistrationPhase,
}
enum TimerRegistrationPhase {
Native { key: pi_slot_wheel::TimerKey },
Due { links: TimerLinks },
PendingCleanup { links: TimerLinks },
}
enum TimerClearState<Item: 'static> {
Available { marker: Box<TimerRequest<Item>> },
WaitingPublishers { old_group: bool, marker: Box<TimerRequest<Item>> },
Queued { old_group: bool },
Reached { old_group: bool, marker: Box<TimerRequest<Item>> },
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum TimerLocalCleanupMode {
Ordinary,
Clear,
Close,
OwnerExit,
}
struct TimerLocalCleanupGuard<'a, Item: 'static, N: QueueNotifier> {
shared: &'a TimerShared<Item, N>,
local: &'a mut TimerLocalState<Item>,
mode: TimerLocalCleanupMode,
current_debt: bool,
unsettled: usize,
armed: bool,
}
impl<Item: 'static> TimerLocalState<Item> {
pub(super) fn new(now: GlobalManualInstant, marker: Box<TimerRequest<Item>>) -> Self {
timer_local_new_inner(now, marker)
}
fn validate_now(&self, now: GlobalManualInstant) -> pi_result::RawResult<(), TimerQueueError> {
timer_validate_now_inner(self, now)
}
fn validate_deadline(deadline: GlobalManualInstant) -> pi_result::RawResult<u64, TimerQueueError> {
timer_validate_deadline_inner(deadline)
}
fn reserve_registration(&mut self, group: bool) -> pi_result::RawResult<(), TimerQueueError> {
timer_reserve_registration_inner(self, group)
}
fn adopt_front<N: QueueNotifier>(
&mut self,
shared: &TimerShared<Item, N>,
) -> pi_result::RawResult<bool, TimerQueueError> {
timer_adopt_front_inner(self, shared)
}
fn discover_due<N: QueueNotifier>(
&mut self,
shared: &TimerShared<Item, N>,
target: GlobalManualInstant,
) -> pi_result::RawResult<bool, TimerQueueError> {
timer_discover_due_inner(self, shared, target)
}
fn mark_cancel(&mut self, key: CancelTimerKey) {
timer_mark_cancel_inner(self, key)
}
fn clear_step<N: QueueNotifier>(
&mut self,
shared: &TimerShared<Item, N>,
) -> pi_result::RawResult<QueueCleanupProgress, TimerQueueError> {
timer_clear_step_inner(self, shared)
}
fn ready_for_source_exit(&self) -> bool {
timer_local_ready_for_exit_inner(self)
}
pub(super) fn try_push<N: QueueNotifier>(
&mut self,
shared: &TimerShared<Item, N>,
deadline: GlobalManualInstant,
item: Item,
) -> pi_result::RawResult<CancelTimerKey, TimerSubmitError<Item>> {
let mut cleanup = TimerLocalCleanupGuard::new(shared, self, TimerLocalCleanupMode::Ordinary);
let result = timer_local_push_inner(&mut cleanup, deadline, item);
cleanup.finish();
result
}
pub(super) fn request_cancel<N: QueueNotifier>(
&mut self,
shared: &TimerShared<Item, N>,
key: &CancelTimerKey,
) -> pi_result::RawResult<(), TimerQueueError> {
let mut cleanup = TimerLocalCleanupGuard::new(shared, self, TimerLocalCleanupMode::Ordinary);
let result = timer_local_cancel_inner(&mut cleanup, key);
cleanup.finish();
result
}
pub(super) fn advance<N: QueueNotifier>(
&mut self,
shared: &TimerShared<Item, N>,
now: GlobalManualInstant,
) -> pi_result::RawResult<TimerAdvanceProgress, TimerQueueError> {
self.validate_now(now)?;
let mut cleanup = TimerLocalCleanupGuard::new(shared, self, TimerLocalCleanupMode::Ordinary);
let result = timer_local_advance_inner(&mut cleanup, now);
cleanup.finish();
result
}
pub(super) fn take_one<N: QueueNotifier>(
&mut self,
shared: &TimerShared<Item, N>,
) -> pi_result::RawResult<TimerTake<Item>, TimerQueueError> {
let mut cleanup = TimerLocalCleanupGuard::new(shared, self, TimerLocalCleanupMode::Ordinary);
let result = timer_local_take_one_inner(&mut cleanup);
cleanup.finish();
result
}
fn prepare_all<N: QueueNotifier>(
&mut self,
shared: &TimerShared<Item, N>,
) -> pi_result::RawResult<Option<Vec<Item>>, TimerQueueError> {
timer_prepare_all_inner(self, shared)
}
pub(super) fn take_all<N: QueueNotifier>(
&mut self,
shared: &TimerShared<Item, N>,
) -> pi_result::RawResult<TimerTake<Vec<Item>>, TimerQueueError> {
let mut cleanup = TimerLocalCleanupGuard::new(shared, self, TimerLocalCleanupMode::Ordinary);
let result = match cleanup.local.prepare_all(shared) {
Ok(Some(values)) => Ok(cleanup.commit_all(values)),
Ok(None) => Ok(TimerTake::WouldBlock),
Err(TimerQueueError::Busy) => Ok(TimerTake::Busy),
Err(TimerQueueError::Closed) => Ok(TimerTake::Closed),
Err(error) => Err(error),
};
cleanup.finish();
result
}
pub(super) fn process_cleanup<N: QueueNotifier>(
&mut self,
shared: &TimerShared<Item, N>,
) -> pi_result::Result<QueueCleanupProgress> {
let mut cleanup = TimerLocalCleanupGuard::new(shared, self, TimerLocalCleanupMode::Ordinary);
let result = timer_local_process_cleanup_inner(&mut cleanup);
cleanup.finish_progress(result)
}
pub(super) fn clear<N: QueueNotifier>(
&mut self,
shared: &TimerShared<Item, N>,
) -> pi_result::Result<QueueCleanupProgress> {
let mut cleanup = TimerLocalCleanupGuard::new(shared, self, TimerLocalCleanupMode::Clear);
let result = timer_local_clear_inner(&mut cleanup);
cleanup.finish_progress(result)
}
pub(super) fn close<N: QueueNotifier>(
&mut self,
shared: &TimerShared<Item, N>,
) -> pi_result::Result<QueueCleanupProgress> {
shared.stop_accepting();
let mut cleanup = TimerLocalCleanupGuard::new(shared, self, TimerLocalCleanupMode::Close);
let result = timer_local_close_inner(&mut cleanup);
cleanup.finish_progress(result)
}
pub(super) fn next_check_at<N: QueueNotifier>(
&self,
shared: &TimerShared<Item, N>,
now: GlobalManualInstant,
) -> pi_result::RawResult<Option<GlobalManualInstant>, TimerQueueError> {
self.validate_now(now)?;
timer_next_check_inner(self, shared, now)
}
pub(super) fn drop_owner_local<N: QueueNotifier>(&mut self, shared: &TimerShared<Item, N>) {
let mut cleanup = TimerLocalCleanupGuard::new(shared, self, TimerLocalCleanupMode::OwnerExit);
timer_local_owner_exit_inner(&mut cleanup);
cleanup.finish();
}
}
impl<'a, Item: 'static, N: QueueNotifier> TimerLocalCleanupGuard<'a, Item, N> {
fn new(
shared: &'a TimerShared<Item, N>,
local: &'a mut TimerLocalState<Item>,
mode: TimerLocalCleanupMode,
) -> Self {
timer_local_guard_new_inner(shared, local, mode)
}
fn drop_one(&mut self) -> bool {
match self.take_cleanup() {
Some(item) => {
drop(item);
self.settle_current();
true
}
None => false,
}
}
fn take_cleanup(&mut self) -> Option<Item> {
timer_take_cleanup_inner(self)
}
fn settle_current(&mut self) {
timer_settle_current_inner(self)
}
fn commit_all(&mut self, values: Vec<Item>) -> TimerTake<Vec<Item>> {
timer_commit_all_inner(self, values)
}
fn finish(&mut self) {
timer_local_guard_finish_inner(self)
}
fn finish_progress(
&mut self,
result: pi_result::Result<QueueCleanupProgress>,
) -> pi_result::Result<QueueCleanupProgress> {
self.finish();
match result {
Ok(QueueCleanupProgress::Complete) if self.shared.snapshot().stopped => {
if !self.local.ready_for_source_exit() {
return Ok(QueueCleanupProgress::MoreWork);
}
match self.shared.try_confirm_closed(&mut self.local.staged) {
Ok(true) => Ok(QueueCleanupProgress::Complete),
Ok(false) | Err(TimerQueueError::Busy) => Ok(QueueCleanupProgress::MoreWork),
Err(error) => Err(error.into_report()),
}
}
other => other,
}
}
}
impl<Item: 'static, N: QueueNotifier> Drop for TimerLocalCleanupGuard<'_, Item, N> {
fn drop(&mut self) {
timer_local_guard_drop_inner(self);
if self.mode == TimerLocalCleanupMode::OwnerExit {
self.shared.confirm_exit();
}
}
}
include!("local_inner.rs");