mux: Mux is now Send+Sync

This commit is contained in:
Wez Furlong committed 2022-12-19 11:54:02 -07:00
1 parent ee2aac0902
commit 478cc59be3
11 files changed
+128 -126

No files matched your search

+2 -2
View File
@@ -9,7 +9,7 @@ use mux::window::{Window, WindowId};
use mux::Mux;
use portable_pty::CommandBuilder;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::Arc;
use wezterm_dynamic::{FromDynamic, ToDynamic};
use wezterm_term::TerminalSize;
@@ -21,7 +21,7 @@ pub use pane::MuxPane;
pub use tab::MuxTab;
pub use window::MuxWindow;
fn get_mux() -> mlua::Result<Rc<Mux>> {
fn get_mux() -> mlua::Result<Arc<Mux>> {
Mux::get()
.ok_or_else(|| mlua::Error::external("cannot get Mux: not running on the mux thread?"))
}
+1 -1
View File
@@ -6,7 +6,7 @@ use std::sync::Arc;
pub struct MuxPane(pub PaneId);
impl MuxPane {
pub fn resolve<'a>(&self, mux: &'a Rc<Mux>) -> mlua::Result<Arc<dyn Pane>> {
pub fn resolve<'a>(&self, mux: &'a Arc<Mux>) -> mlua::Result<Arc<dyn Pane>> {
mux.get_pane(self.0)
.ok_or_else(|| mlua::Error::external(format!("pane id {} not found in mux", self.0)))
}
+1 -1
View File
@@ -5,7 +5,7 @@ use std::sync::Arc;
pub struct MuxTab(pub TabId);
impl MuxTab {
pub fn resolve<'a>(&self, mux: &'a Rc<Mux>) -> mlua::Result<Arc<Tab>> {
pub fn resolve<'a>(&self, mux: &'a Arc<Mux>) -> mlua::Result<Arc<Tab>> {
mux.get_tab(self.0)
.ok_or_else(|| mlua::Error::external(format!("tab id {} not found in mux", self.0)))
}
+5 -2
View File
@@ -5,14 +5,17 @@ use parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
pub struct MuxWindow(pub WindowId);
impl MuxWindow {
pub fn resolve<'a>(&self, mux: &'a Rc<Mux>) -> mlua::Result<MappedRwLockReadGuard<'a, Window>> {
pub fn resolve<'a>(
&self,
mux: &'a Arc<Mux>,
) -> mlua::Result<MappedRwLockReadGuard<'a, Window>> {
mux.get_window(self.0)
.ok_or_else(|| mlua::Error::external(format!("window id {} not found in mux", self.0)))
}
pub fn resolve_mut<'a>(
&self,
mux: &'a Rc<Mux>,
mux: &'a Arc<Mux>,
) -> mlua::Result<MappedRwLockWriteGuard<'a, Window>> {
mux.get_window_mut(self.0)
.ok_or_else(|| mlua::Error::external(format!("window id {} not found in mux", self.0)))
+10 -20
View File
@@ -12,15 +12,13 @@ use libc::{SOL_SOCKET, SO_RCVBUF, SO_SNDBUF};
use log::error;
use metrics::histogram;
use parking_lot::{
MappedRwLockReadGuard, MappedRwLockWriteGuard, RwLock, RwLockReadGuard, RwLockWriteGuard,
MappedRwLockReadGuard, MappedRwLockWriteGuard, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard,
};
use percent_encoding::percent_decode_str;
use portable_pty::{CommandBuilder, ExitStatus, PtySize};
use std::cell::RefCell;
use std::collections::HashMap;
use std::convert::TryInto;
use std::io::{Read, Write};
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread;
@@ -90,7 +88,7 @@ pub struct Mux {
default_domain: RwLock<Option<Arc<dyn Domain>>>,
domains: RwLock<HashMap<DomainId, Arc<dyn Domain>>>,
domains_by_name: RwLock<HashMap<String, Arc<dyn Domain>>>,
subscribers: RwLock<HashMap<usize, Box<dyn Fn(MuxNotification) -> bool>>>,
subscribers: RwLock<HashMap<usize, Box<dyn Fn(MuxNotification) -> bool + Send + Sync>>>,
banner: RwLock<Option<String>>,
clients: RwLock<HashMap<ClientId, ClientInfo>>,
identity: RwLock<Option<Arc<ClientId>>>,
@@ -312,8 +310,8 @@ fn read_from_pane_pty(
dead.store(true, Ordering::Relaxed);
}
thread_local! {
static MUX: RefCell<Option<Rc<Mux>>> = RefCell::new(None);
lazy_static::lazy_static! {
static ref MUX: Mutex<Option<Arc<Mux>>> = Mutex::new(None);
}
pub struct MuxWindowBuilder {
@@ -543,7 +541,7 @@ impl Mux {
pub fn subscribe<F>(&self, subscriber: F)
where
F: Fn(MuxNotification) -> bool + 'static,
F: Fn(MuxNotification) -> bool + 'static + Send + Sync,
{
let sub_id = SUB_ID.fetch_add(1, Ordering::Relaxed);
self.subscribers
@@ -597,24 +595,16 @@ impl Mux {
.insert(domain.domain_name().to_string(), Arc::clone(domain));
}
pub fn set_mux(mux: &Rc<Mux>) {
MUX.with(|m| {
*m.borrow_mut() = Some(Rc::clone(mux));
});
pub fn set_mux(mux: &Arc<Mux>) {
MUX.lock().replace(Arc::clone(mux));
}
pub fn shutdown() {
MUX.with(|m| drop(m.borrow_mut().take()));
MUX.lock().take();
}
pub fn get() -> Option<Rc<Mux>> {
let mut res = None;
MUX.with(|m| {
if let Some(mux) = &*m.borrow() {
res = Some(Rc::clone(mux));
}
});
res
pub fn get() -> Option<Arc<Mux>> {
MUX.lock().as_ref().map(Arc::clone)
}
pub fn get_pane(&self, pane_id: PaneId) -> Option<Arc<dyn Pane>> {
+19 -18
View File
@@ -770,30 +770,31 @@ struct LocalPaneNotifHandler {
impl AlertHandler for LocalPaneNotifHandler {
fn alert(&mut self, alert: Alert) {
if let Some(mux) = Mux::get() {
match &alert {
Alert::WindowTitleChanged(title) => {
if let Some((_domain, window_id, _tab_id)) = mux.resolve_pane_id(self.pane_id) {
if let Some(mut window) = mux.get_window_mut(window_id) {
window.set_title(title);
let pane_id = self.pane_id;
promise::spawn::spawn_into_main_thread(async move {
if let Some(mux) = Mux::get() {
match &alert {
Alert::WindowTitleChanged(title) => {
if let Some((_domain, window_id, _tab_id)) = mux.resolve_pane_id(pane_id) {
if let Some(mut window) = mux.get_window_mut(window_id) {
window.set_title(title);
}
}
}
}
Alert::TabTitleChanged(title) => {
if let Some((_domain, _window_id, tab_id)) = mux.resolve_pane_id(self.pane_id) {
if let Some(tab) = mux.get_tab(tab_id) {
tab.set_title(title.as_deref().unwrap_or(""));
Alert::TabTitleChanged(title) => {
if let Some((_domain, _window_id, tab_id)) = mux.resolve_pane_id(pane_id) {
if let Some(tab) = mux.get_tab(tab_id) {
tab.set_title(title.as_deref().unwrap_or(""));
}
}
}
_ => {}
}
_ => {}
}
mux.notify(MuxNotification::Alert {
pane_id: self.pane_id,
alert,
});
}
mux.notify(MuxNotification::Alert { pane_id, alert });
}
})
.detach();
}
}
+76 -74
View File
@@ -45,82 +45,85 @@ impl GuiFrontEnd {
client_id: client_id.clone(),
});
let fe = Rc::downgrade(&front_end);
mux.subscribe(move |n| {
if let Some(fe) = fe.upgrade() {
match n {
MuxNotification::WindowWorkspaceChanged(_)
| MuxNotification::ActiveWorkspaceChanged(_)
| MuxNotification::WindowCreated(_)
| MuxNotification::WindowRemoved(_) => {
promise::spawn::spawn(async move {
let fe = crate::frontend::front_end();
if !fe.is_switching_workspace() {
fe.reconcile_workspace();
}
})
.detach();
}
MuxNotification::TabAddedToWindow { .. } => {}
MuxNotification::PaneRemoved(_) => {}
MuxNotification::WindowInvalidated(_) => {}
MuxNotification::PaneOutput(_) => {}
MuxNotification::PaneAdded(_) => {}
MuxNotification::Alert {
pane_id: _,
alert:
Alert::ToastNotification {
title,
body,
focus: _,
},
} => {
let message = if title.is_none() { "" } else { &body };
let title = title.as_ref().unwrap_or(&body);
// FIXME: if notification.focus is true, we should do
// something here to arrange to focus pane_id when the
// notification is clicked
persistent_toast_notification(title, message);
}
MuxNotification::Alert {
pane_id: _,
alert: Alert::Bell,
} => {
// Handled via TermWindowNotif; NOP it here.
}
MuxNotification::Alert {
pane_id: _,
alert:
Alert::OutputSinceFocusLost
| Alert::PaletteChanged
| Alert::CurrentWorkingDirectoryChanged
| Alert::WindowTitleChanged(_)
| Alert::TabTitleChanged(_)
| Alert::IconTitleChanged(_)
| Alert::SetUserVar { .. },
} => {}
MuxNotification::Empty => {
match n {
MuxNotification::WindowWorkspaceChanged(_)
| MuxNotification::ActiveWorkspaceChanged(_)
| MuxNotification::WindowCreated(_)
| MuxNotification::WindowRemoved(_) => {
promise::spawn::spawn_into_main_thread(async move {
let fe = crate::frontend::front_end();
if !fe.is_switching_workspace() {
fe.reconcile_workspace();
}
})
.detach();
}
MuxNotification::TabAddedToWindow { .. } => {}
MuxNotification::PaneRemoved(_) => {}
MuxNotification::WindowInvalidated(_) => {}
MuxNotification::PaneOutput(_) => {}
MuxNotification::PaneAdded(_) => {}
MuxNotification::Alert {
pane_id: _,
alert:
Alert::ToastNotification {
title,
body,
focus: _,
},
} => {
let message = if title.is_none() { "" } else { &body };
let title = title.as_ref().unwrap_or(&body);
// FIXME: if notification.focus is true, we should do
// something here to arrange to focus pane_id when the
// notification is clicked
persistent_toast_notification(title, message);
}
MuxNotification::Alert {
pane_id: _,
alert: Alert::Bell,
} => {
// Handled via TermWindowNotif; NOP it here.
}
MuxNotification::Alert {
pane_id: _,
alert:
Alert::OutputSinceFocusLost
| Alert::PaletteChanged
| Alert::CurrentWorkingDirectoryChanged
| Alert::WindowTitleChanged(_)
| Alert::TabTitleChanged(_)
| Alert::IconTitleChanged(_)
| Alert::SetUserVar { .. },
} => {}
MuxNotification::Empty => {
promise::spawn::spawn_into_main_thread(async move {
if mux::activity::Activity::count() == 0 {
log::trace!("Mux is now empty, terminate gui");
Connection::get().unwrap().terminate_message_loop();
}
}
MuxNotification::SaveToDownloads { name, data } => {
if !config::configuration().allow_download_protocols {
log::error!(
"Ignoring download request for {:?}, \
})
.detach();
}
MuxNotification::SaveToDownloads { name, data } => {
if !config::configuration().allow_download_protocols {
log::error!(
"Ignoring download request for {:?}, \
as allow_download_protocols=false",
name
);
} else if let Err(err) = crate::download::save_to_downloads(name, &*data) {
log::error!("save_to_downloads: {:#}", err);
}
name
);
} else if let Err(err) = crate::download::save_to_downloads(name, &*data) {
log::error!("save_to_downloads: {:#}", err);
}
MuxNotification::AssignClipboard {
pane_id,
selection,
clipboard,
} => {
}
MuxNotification::AssignClipboard {
pane_id,
selection,
clipboard,
} => {
promise::spawn::spawn_into_main_thread(async move {
let fe = crate::frontend::front_end();
log::trace!(
"set clipboard in pane {} {:?} {:?}",
pane_id,
@@ -139,13 +142,12 @@ impl GuiFrontEnd {
);
} else {
log::error!("Cannot assign clipboard as there are no windows");
}
}
};
})
.detach();
}
true
} else {
false
}
true
});
// Re-evaluate the config so that folks that are using
// `wezterm.gui.get_appearance()` can have that take effect
+4 -4
View File
@@ -491,7 +491,7 @@ enum Publish {
}
impl Publish {
pub fn resolve(mux: &Rc<Mux>, config: &ConfigHandle, always_new_process: bool) -> Self {
pub fn resolve(mux: &Arc<Mux>, config: &ConfigHandle, always_new_process: bool) -> Self {
if mux.default_domain().domain_name() != config.default_domain.as_deref().unwrap_or("local")
{
return Self::NoConnectNoPublish;
@@ -636,8 +636,8 @@ fn setup_mux(
config: &ConfigHandle,
default_domain_name: Option<&str>,
default_workspace_name: Option<&str>,
) -> anyhow::Result<Rc<Mux>> {
let mux = Rc::new(mux::Mux::new(Some(local_domain.clone())));
) -> anyhow::Result<Arc<Mux>> {
let mux = Arc::new(mux::Mux::new(Some(local_domain.clone())));
Mux::set_mux(&mux);
let client_id = Arc::new(mux::client::ClientId::new());
mux.register_client(client_id.clone());
@@ -671,7 +671,7 @@ fn build_initial_mux(
config: &ConfigHandle,
default_domain_name: Option<&str>,
default_workspace_name: Option<&str>,
) -> anyhow::Result<Rc<Mux>> {
) -> anyhow::Result<Arc<Mux>> {
let domain: Arc<dyn Domain> = Arc::new(LocalDomain::new("local")?);
setup_mux(domain, config, default_domain_name, default_workspace_name)
}
+7 -1
View File
@@ -1346,7 +1346,13 @@ impl TermWindow {
let dead = Arc::new(AtomicBool::new(false));
mux.subscribe(move |n| {
let mux_window_id = *mux_window_id.lock().unwrap();
Self::mux_pane_output_event_callback(n, &window, mux_window_id, &dead)
let window = window.clone();
let dead = dead.clone();
promise::spawn::spawn_into_main_thread(async move {
Self::mux_pane_output_event_callback(n, &window, mux_window_id, &dead)
})
.detach();
true
});
}
+1 -1
View File
@@ -176,7 +176,7 @@ fn run() -> anyhow::Result<()> {
};
let domain: Arc<dyn Domain> = Arc::new(LocalDomain::new("local")?);
let mux = Rc::new(mux::Mux::new(Some(domain.clone())));
let mux = Arc::new(mux::Mux::new(Some(domain.clone())));
Mux::set_mux(&mux);
let executor = promise::spawn::SimpleExecutor::new();
+2 -2
View File
@@ -14,7 +14,7 @@ use portable_pty::cmdbuilder::CommandBuilder;
use serde::Serializer as _;
use std::ffi::OsString;
use std::io::{Read, Write};
use std::rc::Rc;
use std::sync::Arc;
use tabout::{tabulate_output, Alignment, Column};
use umask::UmaskSaver;
use wezterm_client::client::{unix_connect_with_retry, Client};
@@ -1152,7 +1152,7 @@ async fn run_cli_async(config: config::ConfigHandle, cli: CliCommand) -> anyhow:
// ourselves into basically netcat.
drop(client);
let mux = Rc::new(mux::Mux::new(None));
let mux = Arc::new(mux::Mux::new(None));
Mux::set_mux(&mux);
let unix_dom = config.unix_domains.first().unwrap();
let target = unix_dom.target();