Skip to content

Commit

Permalink
Mutex is not needed (neovide#1860)
Browse files Browse the repository at this point in the history
  • Loading branch information
juchiast authored and falcucci committed May 15, 2023
1 parent 2bc4d21 commit 1aa431d
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/event_aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
fmt::Debug,
};

use parking_lot::{Mutex, RwLock};
use parking_lot::RwLock;
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver};

use crate::channel_utils::*;
Expand All @@ -13,12 +13,8 @@ lazy_static! {
pub static ref EVENT_AGGREGATOR: EventAggregator = EventAggregator::default();
}

thread_local! {
static THREAD_SENDERS: RwLock<HashMap<TypeId, Box<dyn Any + Send>>> = RwLock::new(HashMap::new());
}

pub struct EventAggregator {
parent_senders: RwLock<HashMap<TypeId, Mutex<Box<dyn Any + Send>>>>,
parent_senders: RwLock<HashMap<TypeId, Box<dyn Any + Send + Sync>>>,
unclaimed_receivers: RwLock<HashMap<TypeId, Box<dyn Any + Send + Sync>>>,
}

Expand All @@ -35,13 +31,13 @@ impl EventAggregator {
fn get_sender<T: Any + Clone + Debug + Send>(&self) -> LoggingTx<T> {
match self.parent_senders.write().entry(TypeId::of::<T>()) {
Entry::Occupied(entry) => {
let sender = entry.get().lock();
let sender = entry.get();
sender.downcast_ref::<LoggingTx<T>>().unwrap().clone()
}
Entry::Vacant(entry) => {
let (sender, receiver) = unbounded_channel();
let logging_tx = LoggingTx::attach(sender, type_name::<T>().to_owned());
entry.insert(Mutex::new(Box::new(logging_tx.clone())));
entry.insert(Box::new(logging_tx.clone()));
self.unclaimed_receivers
.write()
.insert(TypeId::of::<T>(), Box::new(receiver));
Expand All @@ -67,7 +63,7 @@ impl EventAggregator {
match self.parent_senders.write().entry(type_id) {
Entry::Occupied(_) => panic!("EventAggregator: type already registered"),
Entry::Vacant(entry) => {
entry.insert(Mutex::new(Box::new(logging_sender)));
entry.insert(Box::new(logging_sender));
}
}

Expand Down

0 comments on commit 1aa431d

Please sign in to comment.