Skip to content

Commit

Permalink
PollWatcher: remove Mutex::lock() when emit event
Browse files Browse the repository at this point in the history
  • Loading branch information
visig9 committed May 22, 2022
1 parent f128369 commit 574c7a5
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/poll.rs
Expand Up @@ -23,13 +23,13 @@ mod data {
};
use filetime::FileTime;
use std::{
cell::RefCell,
collections::{hash_map::RandomState, HashMap},
fmt::{self, Debug},
fs::{self, File, Metadata},
hash::{BuildHasher, Hasher},
io::{self, Read},
path::{Path, PathBuf},
sync::{Arc, Mutex},
time::Instant,
};
use walkdir::WalkDir;
Expand Down Expand Up @@ -360,21 +360,23 @@ mod data {
}

/// Thin wrapper for outer event handler, for easy to use.
struct EventEmitter(Arc<Mutex<dyn EventHandler>>);
struct EventEmitter(
// Use `RefCell` to make sure `emit()` only need shared borrow of self (&self).
// Use `Box` to make sure EventEmitter is Sized.
Box<RefCell<dyn EventHandler>>,
);

impl EventEmitter {
fn new<F: EventHandler>(event_handler: F) -> Self {
Self(Arc::new(Mutex::new(event_handler)))
Self(Box::new(RefCell::new(event_handler)))
}

/// Emit single event.
fn emit(&self, event: crate::Result<Event>) {
// FIXME: inconsistent: some place mutex poison cause panic, some place just ignore.
if let Ok(mut guard) = self.0.lock() {
guard.handle_event(event);
}
self.0.borrow_mut().handle_event(event);
}

/// Emit event.
fn emit_ok(&self, event: Event) {
self.emit(Ok(event))
}
Expand Down

0 comments on commit 574c7a5

Please sign in to comment.