src/executor.rs

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

107 lines · 2867 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            _SENDER = Some(sender);
 50            _FUTURE_QUEUE = Some(queue.clone());
 51
 52            let fd_hook = weechat.hook_fd(
 53                receiver,
 54                FdHookMode::Read,
 55                executor_cb,
 56                Some(queue),
 57            );
 58            _FUTURE_HOOK = Some(fd_hook);
 59        }
 60    }
 61
 62    let weechat_notify = unsafe {
 63        if let Some(s) = &_SENDER {
 64            s.clone()
 65        } else {
 66            panic!("Future queue wasn't initialized")
 67        }
 68    };
 69
 70    let queue: FutureQueue = unsafe {
 71        if let Some(q) = &_FUTURE_QUEUE {
 72            q.clone()
 73        } else {
 74            panic!("Future queue wasn't initialized")
 75        }
 76    };
 77
 78    let schedule = move |task| {
 79        let mut weechat_notify = weechat_notify
 80            .lock()
 81            .expect("Weechat notification sender lock is poisoned");
 82        let mut queue = queue.lock().expect(
 83            "Lock of the future queue of the Weechat executor is poisoned",
 84        );
 85
 86        queue.push_back(task);
 87        weechat_notify
 88            .send(())
 89            .expect("Can't notify Weechat to run a future");
 90    };
 91
 92    let (task, handle) = async_task::spawn_local(future, schedule, ());
 93    task.schedule();
 94    handle
 95}
 96
 97pub fn cleanup_executor() {
 98    unsafe {
 99        let hook = _FUTURE_HOOK.take();
100        // Drop our fd hook so it doesn't get called because we dropped the
101        // sender.
102        drop(hook);
103
104        _SENDER.take();
105        _FUTURE_QUEUE.take();
106    }
107}