1use dope_sys::{io::reactor::event, thread::signal::Source};
2use std::{
3 cell::Cell,
4 convert::Infallible,
5 ops::{self, ControlFlow},
6 os::fd,
7 task::Poll,
8};
9
10use crate::{
11 Backend, Errno,
12 driver::{
13 life::domain,
14 schedule::ready::wake::{self, store::Slot},
15 settings,
16 },
17 executor::{
18 self,
19 drive::{self, clock},
20 root, session,
21 },
22};
23
24pub struct Signal<'process> {
26 source: Source<'process, Backend>,
27 result: Cell<Option<Result<(), Errno>>>,
28}
29
30impl<'process> Signal<'process> {
31 pub const fn new(source: Source<'process, Backend>) -> Self {
32 Self {
33 source,
34 result: Cell::new(None),
35 }
36 }
37
38 pub fn result(&self) -> Option<Result<(), Errno>> {
39 self.result.get()
40 }
41}
42
43impl fd::AsFd for Signal<'_> {
44 fn as_fd(&self) -> fd::BorrowedFd<'_> {
45 self.source.as_fd()
46 }
47}
48
49impl executor::Startup for &mut Signal<'_> {
50 type Error = Infallible;
51 type Target = Infallible;
52
53 fn failed(target: &Self::Target) {
54 match *target {}
55 }
56
57 fn observed(&self, _request: executor::Requested<Self>) -> ControlFlow<()> {
58 if self.result.get().is_none() {
59 self.result.set(Some(self.source.wait()));
60 }
61 ControlFlow::Break(())
62 }
63}
64
65impl<'source, 'process> executor::Build for &'source mut Signal<'process> {
66 type Shutdown = Self;
67
68 fn try_build<'p>(
69 self,
70 config: settings::Config,
71 place: &'p mut domain::Place<Self::Shutdown>,
72 ) -> Result<domain::Generation<'p, Self>, executor::Error<'p>> {
73 domain::Generation::try_registered(
74 config,
75 self,
76 <Self as executor::Startup>::INTEREST,
77 place,
78 )
79 }
80}
81
82pub struct Wait<'signal, 'd> {
84 result: &'signal Cell<Option<Result<(), Errno>>>,
85 wake: &'signal Slot<'d>,
86}
87
88impl<'d> Wait<'_, 'd> {
89 pub fn poll(&mut self, wake: wake::Waker<'d>) -> Poll<Result<(), Errno>> {
90 match self.result.get() {
91 Some(result) => Poll::Ready(result),
92 None => {
93 self.wake.set(wake);
94 Poll::Pending
95 }
96 }
97 }
98}
99
100pub struct App<'signal, 'app, 'env: 'd, 'd: 'app, D: executor::raw::Layout<'d>, Report, Q> {
102 app: &'signal mut session::App<'app, 'env, 'd, D, Report, Q>,
103 wake: &'signal Slot<'d>,
104}
105
106impl<'app, 'env: 'd, 'd: 'app, 'source, 'process, D, Report: event::Report<Event = crate::Reported>>
107 App<'_, 'app, 'env, 'd, D, Report, &'source mut Signal<'process>>
108where
109 D: executor::raw::App<'d>,
110{
111 pub fn with<R>(
112 app: &mut session::App<'app, 'env, 'd, D, Report, &'source mut Signal<'process>>,
113 run: impl for<'signal> FnOnce(
114 App<'signal, 'app, 'env, 'd, D, Report, &'source mut Signal<'process>>,
115 Wait<'signal, 'd>,
116 ) -> R,
117 ) -> R {
118 let wake = Slot::empty();
119 let source = app.core.source;
120 let result = &source.result;
121 run(
122 App { app, wake: &wake },
123 Wait {
124 result,
125 wake: &wake,
126 },
127 )
128 }
129}
130
131impl<'app, 'env: 'd, 'd: 'app, D, Report: event::Report<Event = crate::Reported>, Q>
132 App<'_, 'app, 'env, 'd, D, Report, Q>
133where
134 D: executor::raw::App<'d>,
135 Q: executor::Startup,
136{
137 pub fn drive<R>(&mut self, root: R) -> Result<R::Output, drive::Error>
138 where
139 R: root::Root<'d>,
140 {
141 let source = self.app.core.source;
142 self.app
143 .poll_root(clock::wall(), root, &(source, self.wake))?
144 .complete()
145 }
146}
147
148impl<'app, 'env: 'd, 'd: 'app, D: executor::raw::Layout<'d>, Report, Q> ops::Deref
149 for App<'_, 'app, 'env, 'd, D, Report, Q>
150{
151 type Target = session::App<'app, 'env, 'd, D, Report, Q>;
152
153 fn deref(&self) -> &Self::Target {
154 self.app
155 }
156}
157
158impl<'d, D: executor::raw::Layout<'d>, Report, Q> ops::DerefMut
159 for App<'_, '_, '_, 'd, D, Report, Q>
160{
161 fn deref_mut(&mut self) -> &mut Self::Target {
162 self.app
163 }
164}