use crate::timer::internal::tokio_runtime_shutdown_guard::TokioRuntimeShutdownGuard;
use crate::timer::internal::tokio_runtime_shutdown_state::TokioRuntimeShutdownState;
use std::sync::Arc;
use tokio::sync::{
futures::OwnedNotified,
oneshot,
};
#[derive(Debug)]
pub(crate) struct TokioRuntimeLiveness {
shutdown: Arc<TokioRuntimeShutdownState>,
_sentinel_release: oneshot::Sender<()>,
}
impl TokioRuntimeLiveness {
#[must_use]
pub(crate) fn new() -> (Self, oneshot::Receiver<()>) {
let (sentinel_release, release_notification) = oneshot::channel();
let liveness = Self {
shutdown: Arc::new(TokioRuntimeShutdownState::new()),
_sentinel_release: sentinel_release,
};
(liveness, release_notification)
}
pub(crate) fn start(&self, release_notification: oneshot::Receiver<()>) {
let shutdown_guard =
TokioRuntimeShutdownGuard::new(Arc::clone(&self.shutdown));
tokio::spawn(async move {
let _shutdown_guard = shutdown_guard;
let _ = release_notification.await;
});
}
#[must_use]
#[inline]
pub(crate) fn is_shutdown(&self) -> bool {
self.shutdown.is_shutdown()
}
pub(crate) fn shutdown_notification(&self) -> OwnedNotified {
self.shutdown.notification()
}
}