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
This commit is contained in:
laizy 2025-05-19 17:08:04 +08:00 committed by GitHub
parent 875d1ef263
commit 70b0c4d63d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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<EmitterKey, Callback>(
Arc<Mutex<SubscriberSetState<EmitterKey, Callback>>>,
Rc<RefCell<SubscriberSetState<EmitterKey, Callback>>>,
);
impl<EmitterKey, Callback> Clone for SubscriberSet<EmitterKey, Callback> {
@ -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<EmitterKey, Callback>) {
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<Item = Callback> + use<EmitterKey, Callback> {
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) {