From 70b0c4d63d47db94d0cdb3373f9c31ea66699e11 Mon Sep 17 00:00:00 2001 From: laizy <4203231+laizy@users.noreply.github.com> Date: Mon, 19 May 2025 17:08:04 +0800 Subject: [PATCH] gpui: Replace Mutex with RefCell for SubscriberSet (#30907) `SubscriberSet` is `!Send` and `!Sync` because the `active` field of `Subscriber` is `Rc`. Release Notes: - N/A --- crates/gpui/src/subscription.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/crates/gpui/src/subscription.rs b/crates/gpui/src/subscription.rs index 44d5331014..a584f1a45f 100644 --- a/crates/gpui/src/subscription.rs +++ b/crates/gpui/src/subscription.rs @@ -1,10 +1,14 @@ use collections::{BTreeMap, BTreeSet}; -use parking_lot::Mutex; -use std::{cell::Cell, fmt::Debug, mem, rc::Rc, sync::Arc}; +use std::{ + cell::{Cell, RefCell}, + fmt::Debug, + mem, + rc::Rc, +}; use util::post_inc; pub(crate) struct SubscriberSet( - Arc>>, + Rc>>, ); impl Clone for SubscriberSet { @@ -30,7 +34,7 @@ where Callback: 'static, { pub fn new() -> Self { - Self(Arc::new(Mutex::new(SubscriberSetState { + Self(Rc::new(RefCell::new(SubscriberSetState { subscribers: Default::default(), dropped_subscribers: Default::default(), next_subscriber_id: 0, @@ -47,7 +51,7 @@ where callback: Callback, ) -> (Subscription, impl FnOnce() + use) { let active = Rc::new(Cell::new(false)); - let mut lock = self.0.lock(); + let mut lock = self.0.borrow_mut(); let subscriber_id = post_inc(&mut lock.next_subscriber_id); lock.subscribers .entry(emitter_key.clone()) @@ -64,7 +68,7 @@ where let subscription = Subscription { unsubscribe: Some(Box::new(move || { - let mut lock = this.lock(); + let mut lock = this.borrow_mut(); let Some(subscribers) = lock.subscribers.get_mut(&emitter_key) else { // remove was called with this emitter_key return; @@ -92,7 +96,7 @@ where &self, emitter: &EmitterKey, ) -> impl IntoIterator + use { - let subscribers = self.0.lock().subscribers.remove(emitter); + let subscribers = self.0.borrow_mut().subscribers.remove(emitter); subscribers .unwrap_or_default() .map(|s| s.into_values()) @@ -115,7 +119,7 @@ where { let Some(mut subscribers) = self .0 - .lock() + .borrow_mut() .subscribers .get_mut(emitter) .and_then(|s| s.take()) @@ -130,7 +134,7 @@ where true } }); - let mut lock = self.0.lock(); + let mut lock = self.0.borrow_mut(); // Add any new subscribers that were added while invoking the callback. if let Some(Some(new_subscribers)) = lock.subscribers.remove(emitter) {