Skip to content

Commit

Permalink
Don't wait lock on configure event
Browse files Browse the repository at this point in the history
  • Loading branch information
daa84 committed Jul 31, 2018
1 parent 2edaea7 commit f034d34
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
27 changes: 27 additions & 0 deletions src/nvim/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ impl<'a> NeovimRef<'a> {
NeovimRef::SingleThreaded(nvim)
}

fn try_nvim_async(nvim_async: &'a NeovimClientAsync) -> Option<NeovimRef<'a>> {
let guard = nvim_async.nvim.try_lock();

if let Ok(guard) = guard {
Some(NeovimRef::MultiThreaded(guard))
} else {
None
}
}

fn from_nvim_async(nvim_async: &'a NeovimClientAsync) -> Option<NeovimRef<'a>> {
let guard = nvim_async.nvim.lock().unwrap();

Expand Down Expand Up @@ -77,6 +87,10 @@ impl NeovimClientAsync {
pub fn borrow(&self) -> Option<NeovimRef> {
NeovimRef::from_nvim_async(self)
}

pub fn try_borrow(&self) -> Option<NeovimRef> {
NeovimRef::try_nvim_async(self)
}
}

impl Clone for NeovimClientAsync {
Expand Down Expand Up @@ -146,6 +160,19 @@ impl NeovimClient {
self.state.get() == NeovimClientState::InitInProgress
}

/// In case neovimref locked in another thread
/// this method can return None
pub fn try_nvim(&self) -> Option<NeovimRef> {
let nvim = self.nvim.borrow_mut();
if nvim.is_some() {
Some(NeovimRef::from_nvim(RefMut::map(nvim, |n| {
n.as_mut().unwrap()
})))
} else {
self.nvim_async.try_borrow()
}
}

pub fn nvim(&self) -> Option<NeovimRef> {
let nvim = self.nvim.borrow_mut();
if nvim.is_some() {
Expand Down
11 changes: 7 additions & 4 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,15 +435,18 @@ impl State {
let resize_timer = self.resize_timer.clone();

let resize_id = gtk::timeout_add(200, move || {
resize_timer.set(None);

if let Some(mut nvim) = nvim.nvim() {
if let Some(mut nvim) = nvim.try_nvim() {
debug!("ui_try_resize {}/{}", columns, rows);
resize_timer.set(None);

nvim.ui_try_resize_async(columns as u64, rows as u64)
.cb(|r| r.report_err())
.call();

return Continue(false);
}
Continue(false)

Continue(true)
});

self.resize_timer.set(Some(resize_id));
Expand Down

0 comments on commit f034d34

Please sign in to comment.