use crate::stream::{recv, send};
use core::{mem::MaybeUninit, ops};
use std::sync::Arc;
impl super::Handle for tokio::runtime::Handle {
#[inline]
fn spawn_recv_shutdown(&self, shutdown: recv::application::Shutdown) {
self.spawn(async move {
tokio::time::timeout(core::time::Duration::from_secs(1), shutdown).await
});
}
#[inline]
fn spawn_send_shutdown(&self, shutdown: send::application::Shutdown) {
self.spawn(async move {
tokio::time::timeout(core::time::Duration::from_secs(1), shutdown).await
});
}
}
#[derive(Clone)]
pub struct Shared(Arc<SharedInner>);
impl Shared {
#[inline]
pub fn handle(&self) -> super::ArcHandle {
self.0.clone()
}
}
impl ops::Deref for Shared {
type Target = tokio::runtime::Handle;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<tokio::runtime::Runtime> for Shared {
fn from(rt: tokio::runtime::Runtime) -> Self {
Self(Arc::new(SharedInner(MaybeUninit::new(rt))))
}
}
struct SharedInner(MaybeUninit<tokio::runtime::Runtime>);
impl ops::Deref for SharedInner {
type Target = tokio::runtime::Handle;
#[inline]
fn deref(&self) -> &Self::Target {
unsafe { (self.0).assume_init_ref().handle() }
}
}
impl super::Handle for SharedInner {
#[inline]
fn spawn_recv_shutdown(&self, shutdown: recv::application::Shutdown) {
(**self).spawn_recv_shutdown(shutdown)
}
#[inline]
fn spawn_send_shutdown(&self, shutdown: send::application::Shutdown) {
(**self).spawn_send_shutdown(shutdown)
}
}
impl Drop for SharedInner {
fn drop(&mut self) {
let rt = unsafe { self.0.assume_init_read() };
std::thread::spawn(move || {
rt.shutdown_timeout(core::time::Duration::from_secs(10));
});
}
}