add executor to the gui systems

This commit is contained in:
Wez Furlong committed 2019-03-04 11:30:03 +00:00
1 parent e8a4f8f94c
commit 47f27fed13
4 files changed
+92 -1

No files matched your search

+44
View File
@@ -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<T: Send>(proxy: EventsLoopProxy) -> (GuiSender<T>, Receiver<T>) {
(GuiSender { tx, proxy }, rx)
}
pub struct GlutinGuiExecutor {
tx: Arc<GuiSender<SpawnFunc>>,
}
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<PtyEvent>,
poll_rx: Receiver<PtyEvent>,
gui_tx: Arc<GuiSender<SpawnFunc>>,
gui_rx: Receiver<SpawnFunc>,
tick_rx: Receiver<()>,
mux: Rc<Mux>,
}
@@ -117,6 +131,12 @@ impl GuiSystem for GlutinGuiSystem {
Box::new(self.event_loop.poll_tx.clone())
}
fn gui_executor(&self) -> Arc<Executor> {
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() {
+4
View File
@@ -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<PtyEventSender>;
fn gui_executor(&self) -> Arc<Executor>;
}
pub mod glutinloop;
+43
View File
@@ -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<WindowId, X11TerminalWindow>,
}
pub struct X11GuiExecutor {
tx: GuiSender<SpawnFunc>,
}
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<Connection>,
@@ -55,12 +66,15 @@ pub struct GuiEventLoop {
interval: Duration,
pty_rx: GuiReceiver<PtyEvent>,
pty_tx: GuiSender<PtyEvent>,
gui_rx: GuiReceiver<SpawnFunc>,
gui_tx: GuiSender<SpawnFunc>,
mux: Rc<Mux>,
}
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<GuiEventLoop>,
@@ -76,6 +90,13 @@ impl super::GuiSystem for X11GuiSystem {
fn pty_sender(&self) -> Box<PtyEventSender> {
Box::new(self.event_loop.pty_tx.clone())
}
fn gui_executor(&self) -> Arc<Executor> {
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);
+1 -1
View File
@@ -3,7 +3,7 @@ use failure::Error;
use std::sync::{Arc, Condvar, Mutex};
type NextFunc<T> = SendBoxFnOnce<'static, (Result<T, Error>,)>;
type SpawnFunc = SendBoxFnOnce<'static, ()>;
pub type SpawnFunc = SendBoxFnOnce<'static, ()>;
pub trait Executor {
fn execute(&self, f: SpawnFunc);