use dope_sys::{io::reactor::event, thread::signal::Source};
use std::{
cell::Cell,
convert::Infallible,
ops::{self, ControlFlow},
os::fd,
task::Poll,
};
use crate::{
Backend, Errno,
driver::{
life::domain,
schedule::ready::wake::{self, store::Slot},
settings,
},
executor::{
self,
drive::{self, clock},
root, session,
},
};
pub struct Signal<'process> {
source: Source<'process, Backend>,
result: Cell<Option<Result<(), Errno>>>,
}
impl<'process> Signal<'process> {
pub const fn new(source: Source<'process, Backend>) -> Self {
Self {
source,
result: Cell::new(None),
}
}
pub fn result(&self) -> Option<Result<(), Errno>> {
self.result.get()
}
}
impl fd::AsFd for Signal<'_> {
fn as_fd(&self) -> fd::BorrowedFd<'_> {
self.source.as_fd()
}
}
impl executor::Startup for &mut Signal<'_> {
type Error = Infallible;
type Target = Infallible;
fn failed(target: &Self::Target) {
match *target {}
}
fn observed(&self, _request: executor::Requested<Self>) -> ControlFlow<()> {
if self.result.get().is_none() {
self.result.set(Some(self.source.wait()));
}
ControlFlow::Break(())
}
}
impl<'source, 'process> executor::Build for &'source mut Signal<'process> {
type Shutdown = Self;
fn try_build<'p>(
self,
config: settings::Config,
place: &'p mut domain::Place<Self::Shutdown>,
) -> Result<domain::Generation<'p, Self>, executor::Error<'p>> {
domain::Generation::try_registered(
config,
self,
<Self as executor::Startup>::INTEREST,
place,
)
}
}
pub struct Wait<'signal, 'd> {
result: &'signal Cell<Option<Result<(), Errno>>>,
wake: &'signal Slot<'d>,
}
impl<'d> Wait<'_, 'd> {
pub fn poll(&mut self, wake: wake::Waker<'d>) -> Poll<Result<(), Errno>> {
match self.result.get() {
Some(result) => Poll::Ready(result),
None => {
self.wake.set(wake);
Poll::Pending
}
}
}
}
pub struct App<'signal, 'app, 'env: 'd, 'd: 'app, D: executor::raw::Layout<'d>, Report, Q> {
app: &'signal mut session::App<'app, 'env, 'd, D, Report, Q>,
wake: &'signal Slot<'d>,
}
impl<'app, 'env: 'd, 'd: 'app, 'source, 'process, D, Report: event::Report<Event = crate::Reported>>
App<'_, 'app, 'env, 'd, D, Report, &'source mut Signal<'process>>
where
D: executor::raw::App<'d>,
{
pub fn with<R>(
app: &mut session::App<'app, 'env, 'd, D, Report, &'source mut Signal<'process>>,
run: impl for<'signal> FnOnce(
App<'signal, 'app, 'env, 'd, D, Report, &'source mut Signal<'process>>,
Wait<'signal, 'd>,
) -> R,
) -> R {
let wake = Slot::empty();
let source = app.core.source;
let result = &source.result;
run(
App { app, wake: &wake },
Wait {
result,
wake: &wake,
},
)
}
}
impl<'app, 'env: 'd, 'd: 'app, D, Report: event::Report<Event = crate::Reported>, Q>
App<'_, 'app, 'env, 'd, D, Report, Q>
where
D: executor::raw::App<'d>,
Q: executor::Startup,
{
pub fn drive<R>(&mut self, root: R) -> Result<R::Output, drive::Error>
where
R: root::Root<'d>,
{
let source = self.app.core.source;
self.app
.poll_root(clock::wall(), root, &(source, self.wake))?
.complete()
}
}
impl<'app, 'env: 'd, 'd: 'app, D: executor::raw::Layout<'d>, Report, Q> ops::Deref
for App<'_, 'app, 'env, 'd, D, Report, Q>
{
type Target = session::App<'app, 'env, 'd, D, Report, Q>;
fn deref(&self) -> &Self::Target {
self.app
}
}
impl<'d, D: executor::raw::Layout<'d>, Report, Q> ops::DerefMut
for App<'_, '_, '_, 'd, D, Report, Q>
{
fn deref_mut(&mut self) -> &mut Self::Target {
self.app
}
}