use std::{mem, pin::Pin};
#[cfg(target_os = "macos")]
use o3::buffer::storage::provided::fixed;
#[cfg(target_os = "macos")]
use crate::Engine;
use crate::{
driver::{
life::{
Lifecycle, Operation, Scope, State, domain,
native::{self, drain, resolver::Reclaim},
recovery::{self, Phase},
},
storage,
},
io,
};
pub(super) struct Closed {
_seal: (),
}
#[cfg(target_os = "macos")]
impl fixed::Release<Engine, crate::Buffer> for Closed {
fn release(&self, _: &mut Engine, _item: crate::Buffer) {}
}
pub(in crate::driver) enum Final {}
impl Final {
pub(super) fn suspend<'a, 'd, Report: io::Report>(
lifecycle: &'a mut Lifecycle<'d, Report>,
) -> native::resolver::Resolver<'a, 'd, Report, Self> {
lifecycle.resuming = true;
Self::start(lifecycle)
}
pub(super) fn start<'a, 'd, Report: io::Report>(
lifecycle: &'a mut Lifecycle<'d, Report>,
) -> native::resolver::Resolver<'a, 'd, Report, Self> {
native::Access::new(&mut lifecycle.local).reclaim(usize::MAX);
drain::routes(lifecycle.context());
recovery::retire(lifecycle, Phase::Quiesce)
}
}
impl<'a, 'd, Report: io::Report> native::resolver::Resolver<'a, 'd, Report, Final> {
fn drain(self) -> Self {
native::Access::new(&mut self.lifecycle.local).reclaim(usize::MAX);
drain::routes(self.lifecycle.context());
self
}
pub(in crate::driver::life) fn reap(self) {
recovery::retire(self.drain().lifecycle, Phase::Finish);
}
pub(super) fn resume(self) {
recovery::rearm(self.drain());
}
}
impl Reclaim for Final {}
const _: () = {
assert!(mem::size_of::<Closed>() == 0);
#[cfg(target_os = "macos")]
assert!(
mem::size_of::<fixed::End<'static, Engine, Closed>>()
== mem::size_of::<fixed::Loan<'static, Engine>>()
);
};
#[cfg(target_os = "macos")]
type End<'d> = fixed::End<'d, Engine, Closed>;
#[cfg(target_os = "linux")]
type End<'d> = Closed;
impl Closed {
fn retire<'d, Report: io::Report>(lifecycle: &mut Lifecycle<'d, Report>) -> Self {
if mem::take(&mut lifecycle.pending) {
lifecycle.report.report(io::Reclaimed::Shutdown);
}
Final::start(lifecycle).reap();
Self { _seal: () }
}
pub(super) fn reject<'d, S, Q, E, O, Report: io::Report>(
mut state: State<'d, Report>,
source: &Q,
cause: E,
op: O,
) -> (Result<O::Output, domain::Rejected<E, O>>, End<'d>)
where
S: storage::Plan,
Q: ?Sized,
O: Operation<S, Q, Report>,
{
op.rejected(source);
let end = Self::retire(&mut state.lifecycle);
#[cfg(target_os = "macos")]
let end = state.lifecycle.local.backend.finish(end);
let error = domain::Rejected {
cause,
operation: op,
};
(Err(error), end)
}
pub(super) fn run<'p, 'd: 'p, S, Q, O, Report: io::Report>(
mut state: State<'d, Report>,
store: Pin<&S::Output<'p, 'd>>,
source: &'d Q,
op: O,
) -> (O::Output, End<'d>)
where
S: storage::Plan + 'd,
Q: ?Sized + 'd,
O: Operation<S, Q, Report> + 'd,
{
let output = op.run(Scope::new(&mut state), storage::Loan::new(store), source);
let end = Self::retire(&mut state.lifecycle);
#[cfg(target_os = "macos")]
let end = state.lifecycle.local.backend.finish(end);
(output, end)
}
}