ntex_async_std/
signals.rs

1use std::{cell::RefCell, future::Future, pin::Pin, rc::Rc, task::Context, task::Poll};
2
3thread_local! {
4    static SRUN: RefCell<bool> = const { RefCell::new(false) };
5    static SHANDLERS: Rc<RefCell<Vec<oneshot::Sender<Signal>>>> = Default::default();
6}
7
8/// Different types of process signals
9#[derive(PartialEq, Eq, Clone, Copy, Debug)]
10pub enum Signal {
11    /// SIGHUP
12    Hup,
13    /// SIGINT
14    Int,
15    /// SIGTERM
16    Term,
17    /// SIGQUIT
18    Quit,
19}
20
21/// Register signal handler.
22///
23/// Signals are handled by oneshots, you have to re-register
24/// after each signal.
25pub fn signal() -> Option<oneshot::Receiver<Signal>> {
26    if !SRUN.with(|v| *v.borrow()) {
27        async_std::task::spawn_local(Signals::new());
28    }
29    SHANDLERS.with(|handlers| {
30        let (tx, rx) = oneshot::channel();
31        handlers.borrow_mut().push(tx);
32        Some(rx)
33    })
34}
35
36struct Signals {}
37
38impl Signals {
39    pub(super) fn new() -> Signals {
40        Self {}
41    }
42}
43
44impl Future for Signals {
45    type Output = ();
46
47    fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
48        Poll::Ready(())
49    }
50}