Skip to content

Commit

Permalink
Use try_lock in diff_handle for Diff gutter (#11092)
Browse files Browse the repository at this point in the history
* Use `try_lock` in `diff_handle` for Diff gutter
- Add the method `try_load() -> Option<Diff>` to `DiffHandle`, using `try_lock` to avoid deadlocks.
- Use said method in `gutter.rs diff()`, which will use a blank `GutterFn` instead when met with a locked `Diff`.

* Revert changes

* Replace `Mutex` with `RwLock` in `Diff`

---------

Co-authored-by: Kaniel Kirby <[email protected]>
  • Loading branch information
kanielrkirby and Kaniel Kirby authored Aug 9, 2024
1 parent aaaafb8 commit f028268
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions helix-vcs/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;
use helix_core::Rope;
use helix_event::RenderLockGuard;
use imara_diff::Algorithm;
use parking_lot::{Mutex, MutexGuard};
use parking_lot::{RwLock, RwLockReadGuard};
use tokio::sync::mpsc::{unbounded_channel, UnboundedSender};
use tokio::task::JoinHandle;
use tokio::time::Instant;
Expand Down Expand Up @@ -37,7 +37,7 @@ struct DiffInner {
#[derive(Clone, Debug)]
pub struct DiffHandle {
channel: UnboundedSender<Event>,
diff: Arc<Mutex<DiffInner>>,
diff: Arc<RwLock<DiffInner>>,
inverted: bool,
}

Expand All @@ -48,7 +48,7 @@ impl DiffHandle {

fn new_with_handle(diff_base: Rope, doc: Rope) -> (DiffHandle, JoinHandle<()>) {
let (sender, receiver) = unbounded_channel();
let diff: Arc<Mutex<DiffInner>> = Arc::default();
let diff: Arc<RwLock<DiffInner>> = Arc::default();
let worker = DiffWorker {
channel: receiver,
diff: diff.clone(),
Expand All @@ -70,7 +70,7 @@ impl DiffHandle {

pub fn load(&self) -> Diff {
Diff {
diff: self.diff.lock(),
diff: self.diff.read(),
inverted: self.inverted,
}
}
Expand Down Expand Up @@ -164,7 +164,7 @@ impl Hunk {
/// non-overlapping order
#[derive(Debug)]
pub struct Diff<'a> {
diff: MutexGuard<'a, DiffInner>,
diff: RwLockReadGuard<'a, DiffInner>,
inverted: bool,
}

Expand Down
6 changes: 3 additions & 3 deletions helix-vcs/src/diff/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;

use helix_core::{Rope, RopeSlice};
use imara_diff::intern::InternedInput;
use parking_lot::Mutex;
use parking_lot::RwLock;
use tokio::sync::mpsc::UnboundedReceiver;
use tokio::sync::Notify;
use tokio::time::{timeout, timeout_at, Duration};
Expand All @@ -21,7 +21,7 @@ mod test;

pub(super) struct DiffWorker {
pub channel: UnboundedReceiver<Event>,
pub diff: Arc<Mutex<DiffInner>>,
pub diff: Arc<RwLock<DiffInner>>,
pub new_hunks: Vec<Hunk>,
pub diff_finished_notify: Arc<Notify>,
}
Expand Down Expand Up @@ -73,7 +73,7 @@ impl DiffWorker {
/// `self.new_hunks` is always empty after this function runs.
/// To improve performance this function tries to reuse the allocation of the old diff previously stored in `self.line_diffs`
fn apply_hunks(&mut self, diff_base: Rope, doc: Rope) {
let mut diff = self.diff.lock();
let mut diff = self.diff.write();
diff.diff_base = diff_base;
diff.doc = doc;
swap(&mut diff.hunks, &mut self.new_hunks);
Expand Down
2 changes: 1 addition & 1 deletion helix-vcs/src/diff/worker/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl DiffHandle {
// dropping the channel terminates the task
drop(self.channel);
handle.await.unwrap();
let diff = diff.lock();
let diff = diff.read();
Vec::clone(&diff.hunks)
}
}
Expand Down

0 comments on commit f028268

Please sign in to comment.