1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use futures::channel::{mpsc, oneshot};
use futures::select;
use futures::stream::FuturesUnordered;
use futures::FutureExt;
use futures::StreamExt;
use std::cell::RefCell;
use std::future::Future;
use std::mem::take;
use std::pin::Pin;
use std::rc::Rc;

use crate::Disposable;

/// Event loop that is used internally.
pub struct EventLoop {
    state: Rc<RefCell<BusState>>,
    tx: mpsc::Sender<Message>,
}

impl EventLoop {
    /// Returns a new loop and its handler. The handler should be send to the
    /// dedicated Polyhorn thread while the reference-counted loop itself can be
    /// shared among all threads.
    pub fn new() -> (EventLoop, EventLoopHandler) {
        let state = Rc::new(RefCell::new(BusState::default()));

        let (tx, rx) = mpsc::channel::<Message>(1024);

        (
            EventLoop {
                state: state.clone(),
                tx,
            },
            EventLoopHandler { state, rx },
        )
    }

    pub fn queue<F>(&self, task: F) -> Disposable
    where
        F: Future<Output = ()> + 'static,
    {
        // This will always succeed since the handler does not hold a persistent
        // reference to the pending state.
        let mut pending = self.state.borrow_mut();

        let (tx, rx) = oneshot::channel();

        pending.additions.push(Box::pin(async move {
            let mut rx = rx.fuse();
            let mut task = Box::pin(task).fuse();

            select! {
                _ = rx => {},
                _ = task => {},
            };

            Some(Message::Refresh)
        }));

        self.tx.clone().try_send(Message::Refresh).unwrap();

        Disposable::new(Token { tx: Some(tx) })
    }

    pub fn queue_retain<F>(&self, task: F)
    where
        F: Future<Output = ()> + 'static,
    {
        // This will always succeed since the handler does not hold a persistent
        // reference to the pending state.
        let mut pending = self.state.borrow_mut();

        pending.additions.push(Box::pin(async move {
            task.await;

            Some(Message::Refresh)
        }));

        self.tx.clone().try_send(Message::Refresh).unwrap();
    }
}

impl Drop for EventLoop {
    fn drop(&mut self) {
        let _ = self.tx.try_send(Message::Terminate);
    }
}

enum Message {
    Refresh,
    Terminate,
}

#[derive(Default)]
struct BusState {
    additions: Vec<Pin<Box<dyn Future<Output = Option<Message>>>>>,
}

pub struct EventLoopHandler {
    state: Rc<RefCell<BusState>>,
    rx: mpsc::Receiver<Message>,
}

impl EventLoopHandler {
    pub async fn main(mut self) {
        let mut tasks = FuturesUnordered::<Pin<Box<dyn Future<Output = Option<Message>>>>>::new();

        loop {
            select! {
                message = self.rx.next() => {
                    match message {
                        Some(Message::Refresh) => {
                            let pending = take(&mut self.state.borrow_mut().additions);
                            tasks.extend(pending);
                        }
                        Some(Message::Terminate) => break,
                        None => {},
                    }
                },
                _ = tasks.next() => {},
            };
        }
    }
}

struct Token {
    tx: Option<oneshot::Sender<()>>,
}

impl Drop for Token {
    fn drop(&mut self) {
        if let Some(tx) = self.tx.take() {
            // Note: we ignore the result. The result will be an error if the
            // future has already finished, in which case the receiver is
            // dropped.
            let _ = tx.send(());
        }
    }
}