use crate::common::CommonError;
use std::future::Future;
use std::sync::{Condvar, Mutex};
use tokio::runtime::{Handle, Runtime};
pub(crate) struct OwnedRuntime {
handle: Handle,
state: Mutex<RuntimeState>,
changed: Condvar,
}
struct RuntimeState {
runtime: Option<Runtime>,
retiring: bool,
result: Option<Result<(), CommonError>>,
}
impl OwnedRuntime {
pub(super) fn new(runtime: Runtime) -> Self {
Self {
handle: runtime.handle().clone(),
state: Mutex::new(RuntimeState {
runtime: Some(runtime),
retiring: false,
result: None,
}),
changed: Condvar::new(),
}
}
pub(crate) fn handle(&self) -> &Handle {
&self.handle
}
pub(crate) fn block_on<F: Future>(&self, future: F) -> F::Output {
self.handle.block_on(future)
}
pub(crate) fn shutdown_blocking(&self) -> Result<(), CommonError> {
if Handle::try_current().is_ok() {
return Err(CommonError::RuntimeError);
}
let mut state = self.state.lock().unwrap_or_else(|error| error.into_inner());
loop {
if let Some(result) = state.result {
return result;
}
if !state.retiring {
state.retiring = true;
let runtime = state.runtime.take();
drop(state);
let result = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
drop(runtime)
})) {
Ok(()) => Ok(()),
Err(payload) => {
crate::common::retirement::discard_panic_payload(payload);
Err(CommonError::RuntimeError)
}
};
state = self.state.lock().unwrap_or_else(|error| error.into_inner());
state.result = Some(result);
self.changed.notify_all();
return result;
}
state = self
.changed
.wait(state)
.unwrap_or_else(|error| error.into_inner());
}
}
}
impl Drop for OwnedRuntime {
fn drop(&mut self) {
let state = self
.state
.get_mut()
.unwrap_or_else(|error| error.into_inner());
let Some(runtime) = state.runtime.take() else {
return;
};
if Handle::try_current().is_ok() {
runtime.shutdown_background();
} else {
drop(runtime);
}
}
}