remove mutexes

This commit is contained in:
Aleksey Kladov 2019-01-26 17:04:00 +03:00
parent 012ea3fac6
commit bf98fc609e

View file

@ -9,7 +9,6 @@ use crossbeam_channel::{Receiver, Sender, unbounded, RecvError, select};
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use thread_worker::WorkerHandle; use thread_worker::WorkerHandle;
use walkdir::WalkDir; use walkdir::WalkDir;
use parking_lot::Mutex;
use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher as _Watcher}; use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher as _Watcher};
use crate::{RootConfig, Roots, VfsRoot}; use crate::{RootConfig, Roots, VfsRoot};
@ -83,9 +82,9 @@ impl Worker {
let watcher = notify::watcher(notify_sender, WATCHER_DELAY) let watcher = notify::watcher(notify_sender, WATCHER_DELAY)
.map_err(|e| log::error!("failed to spawn notify {}", e)) .map_err(|e| log::error!("failed to spawn notify {}", e))
.ok(); .ok();
let ctx = WatcherCtx { let mut ctx = WatcherCtx {
roots, roots,
watcher: Arc::new(Mutex::new(watcher)), watcher,
sender: output_sender, sender: output_sender,
}; };
let thread = thread::spawn(move || { let thread = thread::spawn(move || {
@ -101,18 +100,18 @@ impl Worker {
// closed, we should shutdown everything. // closed, we should shutdown everything.
recv(input_receiver) -> t => match t { recv(input_receiver) -> t => match t {
Err(RecvError) => break, Err(RecvError) => break,
Ok(Task::AddRoot { root, config }) => watch_root(&ctx, root, Arc::clone(&config)), Ok(Task::AddRoot { root, config }) => watch_root(&mut ctx, root, Arc::clone(&config)),
}, },
// Watcher send us changes. If **this** channel is // Watcher send us changes. If **this** channel is
// closed, the watcher has died, which indicates a bug // closed, the watcher has died, which indicates a bug
// -- escalate! // -- escalate!
recv(watcher_receiver) -> event => match event { recv(watcher_receiver) -> event => match event {
Err(RecvError) => panic!("watcher is dead"), Err(RecvError) => panic!("watcher is dead"),
Ok((path, change)) => WatcherCtx::handle_change(&ctx, path, change).unwrap(), Ok((path, change)) => WatcherCtx::handle_change(&mut ctx, path, change).unwrap(),
}, },
} }
} }
drop(ctx.watcher.lock().take()); drop(ctx.watcher.take());
drop(ctx); drop(ctx);
let res2 = thread.join(); let res2 = thread.join();
match &res2 { match &res2 {
@ -142,10 +141,9 @@ impl Worker {
} }
} }
fn watch_root(woker: &WatcherCtx, root: VfsRoot, config: Arc<RootConfig>) { fn watch_root(woker: &mut WatcherCtx, root: VfsRoot, config: Arc<RootConfig>) {
let mut guard = woker.watcher.lock();
log::debug!("loading {} ...", config.root.as_path().display()); log::debug!("loading {} ...", config.root.as_path().display());
let files = watch_recursive(guard.as_mut(), config.root.as_path(), &*config) let files = watch_recursive(woker.watcher.as_mut(), config.root.as_path(), &*config)
.into_iter() .into_iter()
.filter_map(|path| { .filter_map(|path| {
let abs_path = path.to_path(&config.root); let abs_path = path.to_path(&config.root);
@ -160,10 +158,9 @@ fn watch_root(woker: &WatcherCtx, root: VfsRoot, config: Arc<RootConfig>) {
log::debug!("... loaded {}", config.root.as_path().display()); log::debug!("... loaded {}", config.root.as_path().display());
} }
#[derive(Clone)]
struct WatcherCtx { struct WatcherCtx {
roots: Arc<Roots>, roots: Arc<Roots>,
watcher: Arc<Mutex<Option<RecommendedWatcher>>>, watcher: Option<RecommendedWatcher>,
sender: Sender<TaskResult>, sender: Sender<TaskResult>,
} }
@ -198,7 +195,7 @@ fn convert_notify_event(event: DebouncedEvent, sender: &Sender<(PathBuf, ChangeK
} }
impl WatcherCtx { impl WatcherCtx {
fn handle_change(&self, path: PathBuf, kind: ChangeKind) -> Result<()> { fn handle_change(&mut self, path: PathBuf, kind: ChangeKind) -> Result<()> {
let (root, rel_path) = match self.roots.find(&path) { let (root, rel_path) = match self.roots.find(&path) {
None => return Ok(()), None => return Ok(()),
Some(it) => it, Some(it) => it,
@ -208,8 +205,7 @@ impl WatcherCtx {
ChangeKind::Create => { ChangeKind::Create => {
let mut paths = Vec::new(); let mut paths = Vec::new();
if path.is_dir() { if path.is_dir() {
let mut guard = self.watcher.lock(); paths.extend(watch_recursive(self.watcher.as_mut(), &path, &config));
paths.extend(watch_recursive(guard.as_mut(), &path, &config));
} else { } else {
paths.push(rel_path); paths.push(rel_path);
} }