From 47f27fed13669c10afab5162fac40cade8b6ee26 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 4 Mar 2019 11:30:03 +0000 Subject: [PATCH] add executor to the gui systems --- src/guiloop/glutinloop.rs | 44 +++++++++++++++++++++++++++++++++++++++ src/guiloop/mod.rs | 4 ++++ src/guiloop/x11.rs | 43 ++++++++++++++++++++++++++++++++++++++ src/promise.rs | 2 +- 4 files changed, 92 insertions(+), 1 deletion(-) diff --git a/src/guiloop/glutinloop.rs b/src/guiloop/glutinloop.rs index 40285f8dd..cfde25069 100644 --- a/src/guiloop/glutinloop.rs +++ b/src/guiloop/glutinloop.rs @@ -8,6 +8,7 @@ use crate::guicommon::tabs::Tab; use crate::guicommon::window::TerminalWindow; use crate::guiloop::SessionTerminated; use crate::mux::{Mux, PtyEvent, PtyEventSender}; +use crate::promise::{Executor, SpawnFunc}; use crate::spawn_tab; use failure::Error; use futures::future; @@ -18,6 +19,7 @@ use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; use std::sync::mpsc::{self, Receiver, SyncSender, TryRecvError}; +use std::sync::Arc; use std::thread; use std::time::{Duration, SystemTime}; @@ -67,6 +69,16 @@ pub fn channel(proxy: EventsLoopProxy) -> (GuiSender, Receiver) { (GuiSender { tx, proxy }, rx) } +pub struct GlutinGuiExecutor { + tx: Arc>, +} + +impl Executor for GlutinGuiExecutor { + fn execute(&self, f: SpawnFunc) { + self.tx.send(f).expect("GlutinExecutor execute failed"); + } +} + /// This struct holds references to Windows. /// The primary mapping is from `WindowId` -> `GliumTerminalWindow`. #[derive(Default)] @@ -83,6 +95,8 @@ pub struct GuiEventLoop { core: futurecore::Core, poll_tx: GuiSender, poll_rx: Receiver, + gui_tx: Arc>, + gui_rx: Receiver, tick_rx: Receiver<()>, mux: Rc, } @@ -117,6 +131,12 @@ impl GuiSystem for GlutinGuiSystem { Box::new(self.event_loop.poll_tx.clone()) } + fn gui_executor(&self) -> Arc { + Arc::new(GlutinGuiExecutor { + tx: self.event_loop.gui_tx.clone(), + }) + } + fn run_forever(&self) -> Result<(), Error> { // This convoluted run() signature is present because of this issue: // https://github.com/tomaka/winit/issues/413 @@ -136,6 +156,7 @@ impl GuiSystem for GlutinGuiSystem { myself.run_event_loop()?; myself.process_poll()?; + myself.process_gui_exec()?; myself.process_tick()?; } } @@ -161,6 +182,7 @@ impl GuiEventLoop { mux.set_spawner(core.get_spawner()); let (poll_tx, poll_rx) = channel(event_loop.create_proxy()); + let (gui_tx, gui_rx) = channel(event_loop.create_proxy()); // The glutin/glium plumbing has no native tick/timer stuff, so // we implement one using a thread. Nice. @@ -176,6 +198,8 @@ impl GuiEventLoop { core, poll_tx, poll_rx, + gui_rx, + gui_tx: Arc::new(gui_tx), tick_rx, event_loop: RefCell::new(event_loop), windows: Rc::new(RefCell::new(Default::default())), @@ -315,6 +339,26 @@ impl GuiEventLoop { } } + fn process_gui_exec(&self) -> Result<(), Error> { + let start = SystemTime::now(); + loop { + match start.elapsed() { + Ok(elapsed) if elapsed > MAX_POLL_LOOP_DURATION => { + return Ok(()); + } + Err(_) => { + return Ok(()); + } + _ => {} + } + match self.gui_rx.try_recv() { + Ok(func) => func.call(), + Err(TryRecvError::Empty) => return Ok(()), + Err(err) => bail!("poll_rx disconnected {:?}", err), + } + } + } + fn process_tick(&self) -> Result<(), Error> { loop { match self.tick_rx.try_recv() { diff --git a/src/guiloop/mod.rs b/src/guiloop/mod.rs index 5ad6f1757..987227284 100644 --- a/src/guiloop/mod.rs +++ b/src/guiloop/mod.rs @@ -3,8 +3,10 @@ use crate::config::Config; use crate::font::FontConfiguration; use crate::guicommon::tabs::Tab; use crate::mux::{Mux, PtyEventSender}; +use crate::promise::Executor; use failure::Error; use std::rc::Rc; +use std::sync::Arc; #[derive(Debug, Deserialize, Clone, Copy)] pub enum GuiSelection { @@ -71,6 +73,8 @@ pub trait GuiSystem { ) -> Result<(), Error>; fn pty_sender(&self) -> Box; + + fn gui_executor(&self) -> Arc; } pub mod glutinloop; diff --git a/src/guiloop/x11.rs b/src/guiloop/x11.rs index 6ecd6f9cd..eafbb5c5d 100644 --- a/src/guiloop/x11.rs +++ b/src/guiloop/x11.rs @@ -5,6 +5,7 @@ use crate::guicommon::tabs::Tab; use crate::guicommon::window::TerminalWindow; use crate::guiloop::GuiSystem; use crate::mux::{Mux, PtyEvent, PtyEventSender}; +use crate::promise::{Executor, SpawnFunc}; use crate::spawn_tab; use crate::xwindows::xwin::X11TerminalWindow; use crate::xwindows::Connection; @@ -47,6 +48,16 @@ struct Windows { by_id: HashMap, } +pub struct X11GuiExecutor { + tx: GuiSender, +} + +impl Executor for X11GuiExecutor { + fn execute(&self, f: SpawnFunc) { + self.tx.send(f).expect("GlutinExecutor execute failed"); + } +} + pub struct GuiEventLoop { poll: Poll, pub conn: Rc, @@ -55,12 +66,15 @@ pub struct GuiEventLoop { interval: Duration, pty_rx: GuiReceiver, pty_tx: GuiSender, + gui_rx: GuiReceiver, + gui_tx: GuiSender, mux: Rc, } const TOK_CORE: usize = 0xffff_ffff; const TOK_PTY: usize = 0xffff_fffe; const TOK_XCB: usize = 0xffff_fffc; +const TOK_GUI_EXEC: usize = 0xffff_fffd; pub struct X11GuiSystem { event_loop: Rc, @@ -76,6 +90,13 @@ impl super::GuiSystem for X11GuiSystem { fn pty_sender(&self) -> Box { Box::new(self.event_loop.pty_tx.clone()) } + + fn gui_executor(&self) -> Arc { + Arc::new(X11GuiExecutor { + tx: self.event_loop.gui_tx.clone(), + }) + } + fn run_forever(&self) -> Result<(), Error> { self.event_loop.run() } @@ -112,12 +133,22 @@ impl GuiEventLoop { let (pty_tx, pty_rx) = channel(); poll.register(&pty_rx, Token(TOK_PTY), Ready::readable(), PollOpt::level())?; + let (gui_tx, gui_rx) = channel(); + poll.register( + &gui_rx, + Token(TOK_GUI_EXEC), + Ready::readable(), + PollOpt::level(), + )?; + Ok(Self { conn, poll, core, pty_rx, pty_tx, + gui_tx, + gui_rx, interval: Duration::from_millis(50), windows: Rc::new(RefCell::new(Default::default())), mux: Rc::clone(mux), @@ -134,6 +165,7 @@ impl GuiEventLoop { let tok_core = Token(TOK_CORE); let tok_xcb = Token(TOK_XCB); let tok_pty = Token(TOK_PTY); + let tok_gui = Token(TOK_GUI_EXEC); self.conn.flush(); let mut last_interval = Instant::now(); @@ -159,6 +191,8 @@ impl GuiEventLoop { self.process_queued_xcb()?; } else if t == tok_pty { self.process_pty_event()?; + } else if t == tok_gui { + self.process_gui_exec()?; } else { } } @@ -255,6 +289,15 @@ impl GuiEventLoop { Ok(()) } + fn process_gui_exec(&self) -> Result<(), Error> { + match self.gui_rx.try_recv() { + Ok(func) => func.call(), + Err(TryRecvError::Empty) => return Ok(()), + Err(err) => bail!("poll_rx disconnected {:?}", err), + } + Ok(()) + } + fn schedule_window_close(&self, window_id: WindowId) -> Result<(), Error> { eprintln!("schedule_window_close {:?}", window_id); diff --git a/src/promise.rs b/src/promise.rs index 10bb32141..dc6dd8e71 100644 --- a/src/promise.rs +++ b/src/promise.rs @@ -3,7 +3,7 @@ use failure::Error; use std::sync::{Arc, Condvar, Mutex}; type NextFunc = SendBoxFnOnce<'static, (Result,)>; -type SpawnFunc = SendBoxFnOnce<'static, ()>; +pub type SpawnFunc = SendBoxFnOnce<'static, ()>; pub trait Executor { fn execute(&self, f: SpawnFunc);