1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-22 22:42:48 +03:00

tweak executor trait for compat with x11

This commit is contained in:
Wez Furlong 2019-03-04 23:13:37 +00:00
parent 89dff14c4a
commit 1f9d95d57f
5 changed files with 30 additions and 12 deletions

View File

@ -5,18 +5,23 @@ use std::sync::{Arc, Condvar, Mutex};
type NextFunc<T> = SendBoxFnOnce<'static, (Result<T, Error>,)>;
pub type SpawnFunc = SendBoxFnOnce<'static, ()>;
pub trait Executor: Sync + Send {
pub trait Executor: Send {
fn execute(&self, f: SpawnFunc);
fn clone_executor(&self) -> Box<Executor>;
}
impl Executor for Arc<Executor> {
impl Executor for Box<Executor> {
fn execute(&self, f: SpawnFunc) {
Executor::execute(&**self, f)
}
fn clone_executor(&self) -> Box<Executor> {
Executor::clone_executor(&**self)
}
}
/// An executor for spawning futures into the rayon global
/// thread pool
#[derive(Clone)]
pub struct RayonExecutor {}
impl RayonExecutor {
@ -29,6 +34,9 @@ impl Executor for RayonExecutor {
fn execute(&self, f: SpawnFunc) {
rayon::spawn(|| f.call());
}
fn clone_executor(&self) -> Box<Executor> {
Box::new(RayonExecutor::new())
}
}
enum PromiseState<T> {

View File

@ -46,6 +46,7 @@ pub fn channel<T: Send>(proxy: EventsLoopProxy) -> (GuiSender<T>, Receiver<T>) {
(GuiSender { tx, proxy }, rx)
}
#[derive(Clone)]
pub struct GlutinGuiExecutor {
tx: Arc<GuiSender<SpawnFunc>>,
}
@ -54,6 +55,11 @@ impl Executor for GlutinGuiExecutor {
fn execute(&self, f: SpawnFunc) {
self.tx.send(f).expect("GlutinExecutor execute failed");
}
fn clone_executor(&self) -> Box<Executor> {
Box::new(GlutinGuiExecutor {
tx: Arc::clone(&self.tx),
})
}
}
/// This struct holds references to Windows.
@ -95,7 +101,7 @@ impl GlutinGuiSystem {
}
impl GuiSystem for GlutinGuiSystem {
fn gui_executor(&self) -> Arc<Executor + Sync + Send> {
fn gui_executor(&self) -> Box<Executor> {
self.event_loop.gui_executor()
}
@ -168,8 +174,8 @@ impl GuiEventLoop {
res
}
fn gui_executor(&self) -> Arc<Executor + Sync + Send> {
Arc::new(GlutinGuiExecutor {
fn gui_executor(&self) -> Box<Executor> {
Box::new(GlutinGuiExecutor {
tx: self.gui_tx.clone(),
})
}

View File

@ -95,7 +95,7 @@ pub trait GuiSystem {
tab: &Rc<Tab>,
) -> Result<(), Error>;
fn gui_executor(&self) -> Arc<Executor + Sync + Send>;
fn gui_executor(&self) -> Box<Executor>;
}
pub mod glutinloop;

View File

@ -34,6 +34,11 @@ impl Executor for X11GuiExecutor {
fn execute(&self, f: SpawnFunc) {
self.tx.send(f).expect("GlutinExecutor execute failed");
}
fn clone_executor(&self) -> Box<Executor> {
Box::new(X11GuiExecutor {
tx: self.tx.clone(),
})
}
}
pub struct GuiEventLoop {

View File

@ -5,7 +5,6 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::io::Read;
use std::rc::Rc;
use std::sync::Arc;
use std::thread;
use term::TerminalHost;
use termwiz::hyperlink::Hyperlink;
@ -17,14 +16,14 @@ pub struct Mux {
tabs: RefCell<HashMap<TabId, Rc<Tab>>>,
}
fn read_from_tab_pty(executor: Arc<Executor>, tab_id: TabId, mut reader: Box<std::io::Read>) {
fn read_from_tab_pty(executor: Box<Executor>, tab_id: TabId, mut reader: Box<std::io::Read>) {
const BUFSIZE: usize = 32 * 1024;
let mut buf = [0; BUFSIZE];
loop {
match reader.read(&mut buf) {
Ok(size) if size == 0 => {
eprintln!("read_pty EOF: tab_id {}", tab_id);
Future::with_executor(Arc::clone(&executor), move || {
Future::with_executor(executor.clone_executor(), move || {
let mux = Mux::get().unwrap();
mux.remove_tab(tab_id);
Ok(())
@ -33,7 +32,7 @@ fn read_from_tab_pty(executor: Arc<Executor>, tab_id: TabId, mut reader: Box<std
}
Ok(size) => {
let data = buf[0..size].to_vec();
Future::with_executor(Arc::clone(&executor), move || {
Future::with_executor(executor.clone_executor(), move || {
let mux = Mux::get().unwrap();
if let Some(tab) = mux.get_tab(tab_id) {
tab.advance_bytes(
@ -48,7 +47,7 @@ fn read_from_tab_pty(executor: Arc<Executor>, tab_id: TabId, mut reader: Box<std
}
Err(err) => {
eprintln!("read_pty failed: tab {} {:?}", tab_id, err);
Future::with_executor(Arc::clone(&executor), move || {
Future::with_executor(executor.clone_executor(), move || {
let mux = Mux::get().unwrap();
mux.remove_tab(tab_id);
Ok(())
@ -116,7 +115,7 @@ impl Mux {
self.tabs.borrow().get(&tab_id).map(Rc::clone)
}
pub fn add_tab(&self, executor: Arc<Executor>, tab: &Rc<Tab>) -> Result<(), Error> {
pub fn add_tab(&self, executor: Box<Executor>, tab: &Rc<Tab>) -> Result<(), Error> {
self.tabs.borrow_mut().insert(tab.tab_id(), Rc::clone(tab));
let reader = tab.reader()?;