use dope_sys::{
io::reactor::complete::flags::{Flow, Meta as _},
kernel,
probe::{self, Cause, Outcome as _, State, Stopped},
table::token,
};
use crate::{
Ring, error,
reactor::{
ring::{
Checked, Identity, ProbeFault,
wait::{self, Waiting},
},
sq,
},
};
impl<S, T: token::Codec> probe::raw::Wait<S> for Waiting<S, T> {
type Ring = Ring<T>;
type Owned = sq::Owned<T>;
type Probe = Identity<T>;
type Outcome = Checked<T>;
type Error = kernel::Errno<error::Enter>;
fn submit(
mut state: S,
ring: impl for<'a> FnOnce(&'a mut S) -> &'a mut Ring<T>,
entry: &sq::Owned<T>,
probe: Identity<T>,
) -> Result<Self, Stopped<S, ProbeFault<T>>> {
if let Err(cause) = wait::submit(ring(&mut state), entry, probe) {
return Err(Stopped { state, cause });
}
Ok(Self { state, probe })
}
fn resume(
stopped: Stopped<Self, ProbeFault<T>>,
ring: impl for<'a> FnOnce(&'a mut S) -> &'a mut Ring<T>,
mut report: impl FnMut(ProbeFault<T>),
) -> Result<Self, Stopped<Self, ProbeFault<T>>> {
let Stopped { mut state, cause } = stopped;
let unread = cause.unread();
report(cause);
let result = match unread {
Some(observed) => wait::acknowledge(ring(&mut state.state), state.probe, observed),
None => Ok(()),
};
Stopped::attach(state, result).map(|(state, ())| state)
}
fn finish(
mut self,
ring: impl for<'a> FnOnce(&'a mut S) -> &'a mut Ring<T>,
) -> Result<(S, Checked<T>), Stopped<S, ProbeFault<T>>> {
let result = wait::next(ring(&mut self.state), self.probe);
Stopped::attach(self.state, result)
}
fn next(
mut self,
ring: impl for<'a> FnOnce(&'a mut S) -> &'a mut Ring<T>,
) -> Result<(State<S, Self>, Checked<T>), Stopped<State<S, Self>, ProbeFault<T>>> {
let result = wait::next(ring(&mut self.state), self.probe);
let (waiting, done) = Stopped::attach(self, result).map_err(Stopped::active)?;
let state = match done.meta().flow() {
Flow::Last => State::Idle(waiting.state),
Flow::More => State::Active(waiting),
};
Ok((state, done))
}
fn cancel(
mut self,
ring: impl for<'a> Fn(&'a mut S) -> &'a mut Ring<T>,
) -> Result<S, Stopped<State<S, Self>, ProbeFault<T>>> {
let probe = self.probe;
let target = T::probe(probe);
let complete = T::cancel_probe(probe);
let vacancy = match wait::reserve(ring(&mut self.state), probe) {
Ok(vacancy) => vacancy,
Err(cause) => return Err(Stopped::active(Stopped { state: self, cause })),
};
vacancy.cancel::<false>(target, complete);
let (state, done) = self.next(ring)?;
match state {
State::Idle(state) => {
let (state, ()) = Stopped::attach(state, done.cancel().map_err(Cause::Check))
.map_err(Stopped::idle)?;
Ok(state)
}
State::Active(state) => Err(Stopped {
state: State::Active(state),
cause: Cause::Check(done.nonterminal()),
}),
}
}
unsafe fn inspect<E>(
mut self,
check: impl FnOnce(&mut S) -> Result<(), E>,
) -> Result<Self, Stopped<Self, E>> {
let result = check(&mut self.state);
Stopped::attach(self, result).map(|(state, ())| state)
}
}