Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutex is not needed; Unused THREAD_SENDERS #1860

Merged
merged 1 commit into from
May 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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