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::schedule::ready::wake::{self, store::Slot},
13 executor::{
14 self,
15 drive::{self, clock},
16 root, session,
17 },
18};
19
20pub struct Signal<'process> {
22 source: Source<'process, Backend>,
23 result: Cell<Option<Result<(), Errno>>>,
24}
25
26impl<'process> Signal<'process> {
27 pub const fn new(source: Source<'process, Backend>) -> Self {
28 Self {
29 source,
30 result: Cell::new(None),
31 }
32 }
33
34 pub fn result(&self) -> Option<Result<(), Errno>> {
35 self.result.get()
36 }
37}
38
39impl fd::AsFd for Signal<'_> {
40 fn as_fd(&self) -> fd::BorrowedFd<'_> {
41 self.source.as_fd()
42 }
43}
44
45impl executor::Startup for &mut Signal<'_> {
46 type Error = Infallible;
47 type Target = Infallible;
48
49 fn failed(target: &Self::Target) {
50 match *target {}
51 }
52
53 fn observed(&self, _request: executor::Requested<Self>) -> ControlFlow<()> {
54 if self.result.get().is_none() {
55 self.result.set(Some(self.source.wait()));
56 }
57 ControlFlow::Break(())
58 }
59}
60
61impl executor::Source for &mut Signal<'_> {}
62
63pub struct Wait<'signal, 'd> {
65 result: &'signal Cell<Option<Result<(), Errno>>>,
66 wake: &'signal Slot<'d>,
67}
68
69impl<'d> Wait<'_, 'd> {
70 pub fn poll(&mut self, wake: wake::Waker<'d>) -> Poll<Result<(), Errno>> {
71 match self.result.get() {
72 Some(result) => Poll::Ready(result),
73 None => {
74 self.wake.set(wake);
75 Poll::Pending
76 }
77 }
78 }
79}
80
81pub struct App<'signal, 'app, 'env: 'd, 'd: 'app, D: executor::raw::Layout<'d>, Report, Q> {
83 app: &'signal mut session::App<'app, 'env, 'd, D, Report, Q>,
84 wake: &'signal Slot<'d>,
85}
86
87impl<'app, 'env, 'd, 'source, 'process, D, Report: event::Report<Event = crate::Reported>>
88 App<'_, 'app, 'env, 'd, D, Report, &'source mut Signal<'process>>
89where
90 D: executor::raw::App<'d>,
91{
92 pub fn with<R>(
93 app: &mut session::App<'app, 'env, 'd, D, Report, &'source mut Signal<'process>>,
94 run: impl for<'signal> FnOnce(
95 App<'signal, 'app, 'env, 'd, D, Report, &'source mut Signal<'process>>,
96 Wait<'signal, 'd>,
97 ) -> R,
98 ) -> R {
99 let wake = Slot::empty();
100 let source = app.core.source;
101 let result = &source.result;
102 run(
103 App { app, wake: &wake },
104 Wait {
105 result,
106 wake: &wake,
107 },
108 )
109 }
110}
111
112impl<'d, D, Report: event::Report<Event = crate::Reported>, Q> App<'_, '_, '_, 'd, D, Report, Q>
113where
114 D: executor::raw::App<'d>,
115 Q: executor::Startup,
116{
117 pub fn drive<R>(&mut self, root: R) -> Result<R::Output, drive::Error>
118 where
119 R: root::Root<'d>,
120 {
121 let source = self.app.core.source;
122 self.app
123 .poll_root(clock::wall(), root, &(source, self.wake))?
124 .complete()
125 }
126}
127
128impl<'app, 'env, 'd, D: executor::raw::Layout<'d>, Report, Q> ops::Deref
129 for App<'_, 'app, 'env, 'd, D, Report, Q>
130{
131 type Target = session::App<'app, 'env, 'd, D, Report, Q>;
132
133 fn deref(&self) -> &Self::Target {
134 self.app
135 }
136}
137
138impl<'d, D: executor::raw::Layout<'d>, Report, Q> ops::DerefMut
139 for App<'_, '_, '_, 'd, D, Report, Q>
140{
141 fn deref_mut(&mut self) -> &mut Self::Target {
142 self.app
143 }
144}