src/executor.rs

5c1cf0dafd71169d39428b82706d761dcb59012e
weechat-matrix-rs/src/executor.rs history · blame · raw

109 lines · 2922 bytes

  1use async_task::JoinHandle;
  2use pipe_channel::{channel, Receiver, Sender};
  3use std::collections::VecDeque;
  4use std::future::Future;
  5use std::sync::{Arc, Mutex};
  6
  7use weechat::{FdHook, FdHookMode, Weechat};
  8
  9fn executor_cb(future_queue: &FutureQueue, receiver: &mut Receiver<()>) {
 10    receiver
 11        .recv()
 12        .expect("Executor channel has been dropped before unhooking");
 13    let mut queue = future_queue
 14        .lock()
 15        .expect("Future queue has been dropped before unhooking");
 16
 17    let task = queue.pop_front();
 18
 19    // Drop the lock here so we can spawn new futures from the currently running
 20    // one.
 21    drop(queue);
 22
 23    if let Some(task) = task {
 24        task.run();
 25    }
 26}
 27
 28type Job = async_task::Task<()>;
 29
 30static mut _FUTURE_HOOK: Option<FdHook<FutureQueue, Receiver<()>>> = None;
 31static mut _SENDER: Option<Arc<Mutex<Sender<()>>>> = None;
 32static mut _FUTURE_QUEUE: Option<FutureQueue> = None;
 33
 34type FutureQueue = Arc<Mutex<VecDeque<Job>>>;
 35
 36pub fn spawn_weechat<F, R>(future: F) -> JoinHandle<R, ()>
 37where
 38    F: Future<Output = R> + 'static,
 39    R: 'static,
 40{
 41    let weechat = unsafe { Weechat::weechat() };
 42
 43    unsafe {
 44        if _FUTURE_HOOK.is_none() {
 45            let (sender, receiver) = channel();
 46            let sender = Arc::new(Mutex::new(sender));
 47            let queue = Arc::new(Mutex::new(VecDeque::new()));
 48
 49            weechat.print(&format!("{:?}", _SENDER));
 50
 51            _SENDER = Some(sender);
 52            _FUTURE_QUEUE = Some(queue.clone());
 53
 54            let fd_hook = weechat.hook_fd(
 55                receiver,
 56                FdHookMode::Read,
 57                executor_cb,
 58                Some(queue),
 59            );
 60            _FUTURE_HOOK = Some(fd_hook);
 61        }
 62    }
 63
 64    let weechat_notify = unsafe {
 65        if let Some(s) = &_SENDER {
 66            s.clone()
 67        } else {
 68            panic!("Future queue wasn't initialized")
 69        }
 70    };
 71
 72    let queue: FutureQueue = unsafe {
 73        if let Some(q) = &_FUTURE_QUEUE {
 74            q.clone()
 75        } else {
 76            panic!("Future queue wasn't initialized")
 77        }
 78    };
 79
 80    let schedule = move |task| {
 81        let mut weechat_notify = weechat_notify
 82            .lock()
 83            .expect("Weechat notification sender lock is poisoned");
 84        let mut queue = queue.lock().expect(
 85            "Lock of the future queue of the Weechat executor is poisoned",
 86        );
 87
 88        queue.push_back(task);
 89        weechat_notify
 90            .send(())
 91            .expect("Can't notify Weechat to run a future");
 92    };
 93
 94    let (task, handle) = async_task::spawn_local(future, schedule, ());
 95    task.schedule();
 96    handle
 97}
 98
 99pub fn cleanup_executor() {
100    unsafe {
101        let hook = _FUTURE_HOOK.take();
102        // Drop our fd hook so it doesn't get called because we dropped the
103        // sender.
104        drop(hook);
105
106        _SENDER.take();
107        _FUTURE_QUEUE.take();
108    }
109}