Skip to content

Commit a54da5a

Browse files
committed
gpui: Do not use a single shared parker within a Dispatcher
This caused issues with #40172, as it made Zed execute and block on tad few more background tasks. Parker is ~cheap to create, hence we should be ok to just create it at the time it is needed.
1 parent 81425be commit a54da5a

File tree

7 files changed

+17
-97
lines changed

7 files changed

+17
-97
lines changed

crates/gpui/src/executor.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ impl BackgroundExecutor {
210210
}
211211
let deadline = timeout.map(|timeout| Instant::now() + timeout);
212212

213-
let unparker = self.dispatcher.unparker();
213+
let parker = parking::Parker::new();
214+
let unparker = parker.unparker();
214215
let waker = waker_fn(move || {
215216
unparker.unpark();
216217
});
@@ -222,10 +223,14 @@ impl BackgroundExecutor {
222223
Poll::Pending => {
223224
let timeout =
224225
deadline.map(|deadline| deadline.saturating_duration_since(Instant::now()));
225-
if !self.dispatcher.park(timeout)
226-
&& deadline.is_some_and(|deadline| deadline < Instant::now())
227-
{
228-
return Err(future);
226+
if let Some(timeout) = timeout {
227+
if !parker.park_timeout(timeout)
228+
&& deadline.is_some_and(|deadline| deadline < Instant::now())
229+
{
230+
return Err(future);
231+
}
232+
} else {
233+
parker.park();
229234
}
230235
}
231236
}
@@ -242,6 +247,8 @@ impl BackgroundExecutor {
242247
) -> Result<Fut::Output, impl Future<Output = Fut::Output> + use<Fut>> {
243248
use std::sync::atomic::AtomicBool;
244249

250+
use parking::Parker;
251+
245252
let mut future = Box::pin(future);
246253
if timeout == Some(Duration::ZERO) {
247254
return Err(future);
@@ -255,7 +262,8 @@ impl BackgroundExecutor {
255262
} else {
256263
usize::MAX
257264
};
258-
let unparker = self.dispatcher.unparker();
265+
let parker = Parker::new();
266+
let unparker = parker.unparker();
259267
let awoken = Arc::new(AtomicBool::new(false));
260268
let waker = waker_fn({
261269
let awoken = awoken.clone();
@@ -297,7 +305,7 @@ impl BackgroundExecutor {
297305
"parked with nothing left to run{waiting_message}{backtrace_message}",
298306
)
299307
}
300-
self.dispatcher.park(None);
308+
parker.park();
301309
}
302310
}
303311
}

crates/gpui/src/platform.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ use async_task::Runnable;
4848
use futures::channel::oneshot;
4949
use image::codecs::gif::GifDecoder;
5050
use image::{AnimationDecoder as _, Frame};
51-
use parking::Unparker;
5251
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
5352
use schemars::JsonSchema;
5453
use seahash::SeaHasher;
@@ -564,8 +563,6 @@ pub trait PlatformDispatcher: Send + Sync {
564563
fn dispatch(&self, runnable: Runnable, label: Option<TaskLabel>);
565564
fn dispatch_on_main_thread(&self, runnable: Runnable);
566565
fn dispatch_after(&self, duration: Duration, runnable: Runnable);
567-
fn park(&self, timeout: Option<Duration>) -> bool;
568-
fn unparker(&self) -> Unparker;
569566
fn now(&self) -> Instant {
570567
Instant::now()
571568
}

crates/gpui/src/platform/linux/dispatcher.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use calloop::{
55
channel::{self, Sender},
66
timer::TimeoutAction,
77
};
8-
use parking::{Parker, Unparker};
9-
use parking_lot::Mutex;
108
use std::{
119
thread,
1210
time::{Duration, Instant},
@@ -19,7 +17,6 @@ struct TimerAfter {
1917
}
2018

2119
pub(crate) struct LinuxDispatcher {
22-
parker: Mutex<Parker>,
2320
main_sender: Sender<Runnable>,
2421
timer_sender: Sender<TimerAfter>,
2522
background_sender: flume::Sender<Runnable>,
@@ -92,7 +89,6 @@ impl LinuxDispatcher {
9289
background_threads.push(timer_thread);
9390

9491
Self {
95-
parker: Mutex::new(Parker::new()),
9692
main_sender,
9793
timer_sender,
9894
background_sender,
@@ -130,17 +126,4 @@ impl PlatformDispatcher for LinuxDispatcher {
130126
.send(TimerAfter { duration, runnable })
131127
.ok();
132128
}
133-
134-
fn park(&self, timeout: Option<Duration>) -> bool {
135-
if let Some(timeout) = timeout {
136-
self.parker.lock().park_timeout(timeout)
137-
} else {
138-
self.parker.lock().park();
139-
true
140-
}
141-
}
142-
143-
fn unparker(&self) -> Unparker {
144-
self.parker.lock().unparker()
145-
}
146129
}

crates/gpui/src/platform/mac/dispatcher.rs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ use objc::{
99
runtime::{BOOL, YES},
1010
sel, sel_impl,
1111
};
12-
use parking::{Parker, Unparker};
13-
use parking_lot::Mutex;
1412
use std::{
1513
ffi::c_void,
1614
ptr::{NonNull, addr_of},
17-
sync::Arc,
1815
time::Duration,
1916
};
2017

@@ -29,23 +26,7 @@ pub(crate) fn dispatch_get_main_queue() -> dispatch_queue_t {
2926
addr_of!(_dispatch_main_q) as *const _ as dispatch_queue_t
3027
}
3128

32-
pub(crate) struct MacDispatcher {
33-
parker: Arc<Mutex<Parker>>,
34-
}
35-
36-
impl Default for MacDispatcher {
37-
fn default() -> Self {
38-
Self::new()
39-
}
40-
}
41-
42-
impl MacDispatcher {
43-
pub fn new() -> Self {
44-
MacDispatcher {
45-
parker: Arc::new(Mutex::new(Parker::new())),
46-
}
47-
}
48-
}
29+
pub(crate) struct MacDispatcher;
4930

5031
impl PlatformDispatcher for MacDispatcher {
5132
fn is_main_thread(&self) -> bool {
@@ -86,19 +67,6 @@ impl PlatformDispatcher for MacDispatcher {
8667
);
8768
}
8869
}
89-
90-
fn park(&self, timeout: Option<Duration>) -> bool {
91-
if let Some(timeout) = timeout {
92-
self.parker.lock().park_timeout(timeout)
93-
} else {
94-
self.parker.lock().park();
95-
true
96-
}
97-
}
98-
99-
fn unparker(&self) -> Unparker {
100-
self.parker.lock().unparker()
101-
}
10270
}
10371

10472
extern "C" fn trampoline(runnable: *mut c_void) {

crates/gpui/src/platform/mac/platform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl Default for MacPlatform {
187187

188188
impl MacPlatform {
189189
pub(crate) fn new(headless: bool) -> Self {
190-
let dispatcher = Arc::new(MacDispatcher::new());
190+
let dispatcher = Arc::new(MacDispatcher);
191191

192192
#[cfg(feature = "font-kit")]
193193
let text_system = Arc::new(crate::MacTextSystem::new());

crates/gpui/src/platform/test/dispatcher.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::{PlatformDispatcher, TaskLabel};
22
use async_task::Runnable;
33
use backtrace::Backtrace;
44
use collections::{HashMap, HashSet, VecDeque};
5-
use parking::{Parker, Unparker};
65
use parking_lot::Mutex;
76
use rand::prelude::*;
87
use std::{
@@ -22,8 +21,6 @@ struct TestDispatcherId(usize);
2221
pub struct TestDispatcher {
2322
id: TestDispatcherId,
2423
state: Arc<Mutex<TestDispatcherState>>,
25-
parker: Arc<Mutex<Parker>>,
26-
unparker: Unparker,
2724
}
2825

2926
struct TestDispatcherState {
@@ -45,7 +42,6 @@ struct TestDispatcherState {
4542

4643
impl TestDispatcher {
4744
pub fn new(random: StdRng) -> Self {
48-
let (parker, unparker) = parking::pair();
4945
let state = TestDispatcherState {
5046
random,
5147
foreground: HashMap::default(),
@@ -66,8 +62,6 @@ impl TestDispatcher {
6662
TestDispatcher {
6763
id: TestDispatcherId(0),
6864
state: Arc::new(Mutex::new(state)),
69-
parker: Arc::new(Mutex::new(parker)),
70-
unparker,
7165
}
7266
}
7367

@@ -251,8 +245,6 @@ impl Clone for TestDispatcher {
251245
Self {
252246
id: TestDispatcherId(id),
253247
state: self.state.clone(),
254-
parker: self.parker.clone(),
255-
unparker: self.unparker.clone(),
256248
}
257249
}
258250
}
@@ -276,7 +268,6 @@ impl PlatformDispatcher for TestDispatcher {
276268
state.background.push(runnable);
277269
}
278270
}
279-
self.unparker.unpark();
280271
}
281272

282273
fn dispatch_on_main_thread(&self, runnable: Runnable) {
@@ -286,7 +277,6 @@ impl PlatformDispatcher for TestDispatcher {
286277
.entry(self.id)
287278
.or_default()
288279
.push_back(runnable);
289-
self.unparker.unpark();
290280
}
291281

292282
fn dispatch_after(&self, duration: std::time::Duration, runnable: Runnable) {
@@ -297,14 +287,6 @@ impl PlatformDispatcher for TestDispatcher {
297287
};
298288
state.delayed.insert(ix, (next_time, runnable));
299289
}
300-
fn park(&self, _: Option<std::time::Duration>) -> bool {
301-
self.parker.lock().park();
302-
true
303-
}
304-
305-
fn unparker(&self) -> Unparker {
306-
self.unparker.clone()
307-
}
308290

309291
fn as_test(&self) -> Option<&TestDispatcher> {
310292
Some(self)

crates/gpui/src/platform/windows/dispatcher.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use std::{
55

66
use async_task::Runnable;
77
use flume::Sender;
8-
use parking::Parker;
9-
use parking_lot::Mutex;
108
use util::ResultExt;
119
use windows::{
1210
System::Threading::{
@@ -24,7 +22,6 @@ use crate::{
2422

2523
pub(crate) struct WindowsDispatcher {
2624
main_sender: Sender<Runnable>,
27-
parker: Mutex<Parker>,
2825
main_thread_id: ThreadId,
2926
platform_window_handle: SafeHwnd,
3027
validation_number: usize,
@@ -36,13 +33,11 @@ impl WindowsDispatcher {
3633
platform_window_handle: HWND,
3734
validation_number: usize,
3835
) -> Self {
39-
let parker = Mutex::new(Parker::new());
4036
let main_thread_id = current().id();
4137
let platform_window_handle = platform_window_handle.into();
4238

4339
WindowsDispatcher {
4440
main_sender,
45-
parker,
4641
main_thread_id,
4742
platform_window_handle,
4843
validation_number,
@@ -112,17 +107,4 @@ impl PlatformDispatcher for WindowsDispatcher {
112107
fn dispatch_after(&self, duration: Duration, runnable: Runnable) {
113108
self.dispatch_on_threadpool_after(runnable, duration);
114109
}
115-
116-
fn park(&self, timeout: Option<Duration>) -> bool {
117-
if let Some(timeout) = timeout {
118-
self.parker.lock().park_timeout(timeout)
119-
} else {
120-
self.parker.lock().park();
121-
true
122-
}
123-
}
124-
125-
fn unparker(&self) -> parking::Unparker {
126-
self.parker.lock().unparker()
127-
}
128110
}

0 commit comments

Comments
 (0)