mod definitions;
mod value;
mod shared;
mod local;
pub use definitions::{CancelTimerKey, CancelTimerOptions};
use crate::management::QueueCleanupProgress;
use crate::notify::QueueNotifier;
use crate::timer::{
TimerAdvanceProgress, TimerCancelRequestError, TimerQueueError, TimerQueueHandle,
TimerQueueStatus, TimerSubmitError, TimerTake, TimerTaskQueue,
};
use local::TimerLocalState;
use pi_clock_manual::global::GlobalManualInstant;
use shared::TimerShared;
use std::marker::PhantomData;
use std::rc::Rc;
use std::sync::Arc;
pub struct CancelTimerQueue<Item: 'static, N: QueueNotifier> {
shared: Arc<TimerShared<Item, N>>,
local: TimerLocalState<Item>,
owner_thread: PhantomData<Rc<()>>,
}
pub struct CancelTimerQueueHandle<Item: 'static, N: QueueNotifier> {
shared: Arc<TimerShared<Item, N>>,
}
impl<Item: 'static, N: QueueNotifier> CancelTimerQueue<Item, N> {
pub fn new(
now: GlobalManualInstant,
notifier: N,
options: CancelTimerOptions,
) -> pi_result::Result<Self> {
let (shared, marker) = TimerShared::new(notifier, options)?;
let local = TimerLocalState::new(now, marker);
Ok(cancel_timer_new_inner(shared, local))
}
}
impl<Item: 'static, N: QueueNotifier> TimerTaskQueue for CancelTimerQueue<Item, N> {
type Item = Item;
type Instant = GlobalManualInstant;
type Key = CancelTimerKey;
type DueBatch = Vec<Item>;
type Handle = CancelTimerQueueHandle<Item, N>;
fn handle(&self) -> Self::Handle {
cancel_timer_handle_inner(Arc::clone(&self.shared))
}
fn try_push_local(
&mut self, deadline: Self::Instant, item: Item,
) -> pi_result::RawResult<Self::Key, TimerSubmitError<Item>> {
self.local.try_push(&self.shared, deadline, item)
}
fn request_cancel_local(
&mut self, key: &Self::Key,
) -> pi_result::RawResult<(), TimerCancelRequestError> {
self.local.request_cancel(&self.shared, key)
}
fn advance(
&mut self, now: &Self::Instant,
) -> pi_result::RawResult<TimerAdvanceProgress, TimerQueueError> {
self.local.advance(&self.shared, *now)
}
fn try_take_due_one(&mut self) -> pi_result::RawResult<TimerTake<Item>, TimerQueueError> {
self.local.take_one(&self.shared)
}
fn try_take_due_all(&mut self) -> pi_result::RawResult<TimerTake<Vec<Item>>, TimerQueueError> {
self.local.take_all(&self.shared)
}
fn process_cleanup(&mut self) -> pi_result::Result<QueueCleanupProgress> {
self.local.process_cleanup(&self.shared)
}
fn next_check_at(
&self, now: &Self::Instant,
) -> pi_result::RawResult<Option<Self::Instant>, TimerQueueError> {
self.local.next_check_at(&self.shared, *now)
}
fn status(&self) -> TimerQueueStatus {
self.shared.status()
}
fn len(&self) -> usize {
self.shared.len()
}
fn is_empty(&self) -> bool {
self.shared.is_empty()
}
fn clear(&mut self) -> pi_result::Result<QueueCleanupProgress> {
self.local.clear(&self.shared)
}
fn close(&mut self) -> pi_result::Result<QueueCleanupProgress> {
self.local.close(&self.shared)
}
}
impl<Item: 'static, N: QueueNotifier> Clone for CancelTimerQueueHandle<Item, N> {
fn clone(&self) -> Self {
cancel_timer_handle_inner(Arc::clone(&self.shared))
}
}
impl<Item: 'static, N: QueueNotifier> TimerQueueHandle for CancelTimerQueueHandle<Item, N> {
type Item = Item;
type Instant = GlobalManualInstant;
type Key = CancelTimerKey;
fn try_push(
&self, deadline: Self::Instant, item: Item,
) -> pi_result::RawResult<Self::Key, TimerSubmitError<Item>>
where
Item: Send + 'static,
Self::Instant: Send + 'static,
{
self.shared.try_push(deadline, item)
}
fn request_cancel(&self, key: &Self::Key) -> pi_result::RawResult<(), TimerCancelRequestError> {
self.shared.request_cancel(key)
}
fn status(&self) -> TimerQueueStatus {
self.shared.status()
}
fn len(&self) -> usize {
self.shared.len()
}
fn is_empty(&self) -> bool {
self.shared.is_empty()
}
}
impl<Item: 'static, N: QueueNotifier> Drop for CancelTimerQueue<Item, N> {
fn drop(&mut self) {
self.shared.begin_owner_exit();
self.local.drop_owner_local(&self.shared);
}
}
include!("cancel_timer_inner.rs");