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