Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 74bc2a4

Browse files
committed
Wrap platform-specific QoS in r-a-specific “thread intent”
1 parent d0b001e commit 74bc2a4

File tree

13 files changed

+354
-295
lines changed

13 files changed

+354
-295
lines changed

crates/flycheck/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl FlycheckHandle {
9090
) -> FlycheckHandle {
9191
let actor = FlycheckActor::new(id, sender, config, workspace_root);
9292
let (sender, receiver) = unbounded::<StateChange>();
93-
let thread = stdx::thread::Builder::new(stdx::thread::QoSClass::Utility)
93+
let thread = stdx::thread::Builder::new(stdx::thread::ThreadIntent::Worker)
9494
.name("Flycheck".to_owned())
9595
.spawn(move || actor.run(receiver))
9696
.expect("failed to spawn thread");
@@ -409,7 +409,7 @@ impl CargoHandle {
409409

410410
let (sender, receiver) = unbounded();
411411
let actor = CargoActor::new(sender, stdout, stderr);
412-
let thread = stdx::thread::Builder::new(stdx::thread::QoSClass::Utility)
412+
let thread = stdx::thread::Builder::new(stdx::thread::ThreadIntent::Worker)
413413
.name("CargoHandle".to_owned())
414414
.spawn(move || actor.run())
415415
.expect("failed to spawn thread");

crates/ide/src/prime_caches.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub(crate) fn parallel_prime_caches(
8181
let worker = prime_caches_worker.clone();
8282
let db = db.snapshot();
8383

84-
stdx::thread::Builder::new(stdx::thread::QoSClass::Utility)
84+
stdx::thread::Builder::new(stdx::thread::ThreadIntent::Worker)
8585
.allow_leak(true)
8686
.spawn(move || Cancelled::catch(|| worker(db)))
8787
.expect("failed to spawn thread");

crates/rust-analyzer/src/bin/main.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,15 @@ fn try_main(flags: flags::RustAnalyzer) -> Result<()> {
7979
return Ok(());
8080
}
8181

82-
// rust-analyzer’s “main thread” is actually a secondary thread
83-
// with an increased stack size at the User Initiated QoS class.
84-
// We use this QoS class because any delay in the main loop
82+
// rust-analyzer’s “main thread” is actually
83+
// a secondary latency-sensitive thread with an increased stack size.
84+
// We use this thread intent because any delay in the main loop
8585
// will make actions like hitting enter in the editor slow.
86-
// rust-analyzer does not block the editor’s render loop,
87-
// so we don’t use User Interactive.
88-
with_extra_thread("LspServer", stdx::thread::QoSClass::UserInitiated, run_server)?;
86+
with_extra_thread(
87+
"LspServer",
88+
stdx::thread::ThreadIntent::LatencySensitive,
89+
run_server,
90+
)?;
8991
}
9092
flags::RustAnalyzerCmd::Parse(cmd) => cmd.run()?,
9193
flags::RustAnalyzerCmd::Symbols(cmd) => cmd.run()?,
@@ -143,10 +145,10 @@ const STACK_SIZE: usize = 1024 * 1024 * 8;
143145
/// space.
144146
fn with_extra_thread(
145147
thread_name: impl Into<String>,
146-
qos_class: stdx::thread::QoSClass,
148+
thread_intent: stdx::thread::ThreadIntent,
147149
f: impl FnOnce() -> Result<()> + Send + 'static,
148150
) -> Result<()> {
149-
let handle = stdx::thread::Builder::new(qos_class)
151+
let handle = stdx::thread::Builder::new(thread_intent)
150152
.name(thread_name.into())
151153
.stack_size(STACK_SIZE)
152154
.spawn(f)?;

crates/rust-analyzer/src/dispatch.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{fmt, panic, thread};
44
use ide::Cancelled;
55
use lsp_server::ExtractError;
66
use serde::{de::DeserializeOwned, Serialize};
7-
use stdx::thread::QoSClass;
7+
use stdx::thread::ThreadIntent;
88

99
use crate::{
1010
global_state::{GlobalState, GlobalStateSnapshot},
@@ -104,7 +104,7 @@ impl<'a> RequestDispatcher<'a> {
104104
None => return self,
105105
};
106106

107-
self.global_state.task_pool.handle.spawn(QoSClass::Utility, {
107+
self.global_state.task_pool.handle.spawn(ThreadIntent::Worker, {
108108
let world = self.global_state.snapshot();
109109
move || {
110110
let result = panic::catch_unwind(move || {
@@ -135,7 +135,7 @@ impl<'a> RequestDispatcher<'a> {
135135
R::Params: DeserializeOwned + panic::UnwindSafe + Send + fmt::Debug,
136136
R::Result: Serialize,
137137
{
138-
self.on_with_qos::<R>(QoSClass::Utility, f)
138+
self.on_with_thread_intent::<R>(ThreadIntent::Worker, f)
139139
}
140140

141141
/// Dispatches a latency-sensitive request onto the thread pool.
@@ -148,7 +148,7 @@ impl<'a> RequestDispatcher<'a> {
148148
R::Params: DeserializeOwned + panic::UnwindSafe + Send + fmt::Debug,
149149
R::Result: Serialize,
150150
{
151-
self.on_with_qos::<R>(QoSClass::UserInitiated, f)
151+
self.on_with_thread_intent::<R>(ThreadIntent::LatencySensitive, f)
152152
}
153153

154154
pub(crate) fn finish(&mut self) {
@@ -163,9 +163,9 @@ impl<'a> RequestDispatcher<'a> {
163163
}
164164
}
165165

166-
fn on_with_qos<R>(
166+
fn on_with_thread_intent<R>(
167167
&mut self,
168-
qos_class: QoSClass,
168+
intent: ThreadIntent,
169169
f: fn(GlobalStateSnapshot, R::Params) -> Result<R::Result>,
170170
) -> &mut Self
171171
where
@@ -178,7 +178,7 @@ impl<'a> RequestDispatcher<'a> {
178178
None => return self,
179179
};
180180

181-
self.global_state.task_pool.handle.spawn(qos_class, {
181+
self.global_state.task_pool.handle.spawn(intent, {
182182
let world = self.global_state.snapshot();
183183
move || {
184184
let result = panic::catch_unwind(move || {

crates/rust-analyzer/src/handlers/notification.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
291291
}
292292
Ok(())
293293
};
294-
state.task_pool.handle.spawn_with_sender(stdx::thread::QoSClass::Utility, move |_| {
294+
state.task_pool.handle.spawn_with_sender(stdx::thread::ThreadIntent::Worker, move |_| {
295295
if let Err(e) = std::panic::catch_unwind(task) {
296296
tracing::error!("flycheck task panicked: {e:?}")
297297
}

crates/rust-analyzer/src/main_loop.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ impl GlobalState {
397397
tracing::debug!(%cause, "will prime caches");
398398
let num_worker_threads = self.config.prime_caches_num_threads();
399399

400-
self.task_pool.handle.spawn_with_sender(stdx::thread::QoSClass::Utility, {
400+
self.task_pool.handle.spawn_with_sender(stdx::thread::ThreadIntent::Worker, {
401401
let analysis = self.snapshot().analysis;
402402
move |sender| {
403403
sender.send(Task::PrimeCaches(PrimeCachesProgress::Begin)).unwrap();
@@ -680,7 +680,7 @@ impl GlobalState {
680680
.on_sync::<lsp_ext::OnTypeFormatting>(handlers::handle_on_type_formatting)
681681
// We can’t run latency-sensitive request handlers which do semantic
682682
// analysis on the main thread because that would block other
683-
// requests. Instead, we run these request handlers on higher QoS
683+
// requests. Instead, we run these request handlers on higher priority
684684
// threads in the threadpool.
685685
.on_latency_sensitive::<lsp_types::request::Completion>(handlers::handle_completion)
686686
.on_latency_sensitive::<lsp_types::request::ResolveCompletionItem>(
@@ -789,8 +789,8 @@ impl GlobalState {
789789
let snapshot = self.snapshot();
790790

791791
// Diagnostics are triggered by the user typing
792-
// so we want computing them to run at the User Initiated QoS.
793-
self.task_pool.handle.spawn(stdx::thread::QoSClass::UserInitiated, move || {
792+
// so we run them on a latency sensitive thread.
793+
self.task_pool.handle.spawn(stdx::thread::ThreadIntent::LatencySensitive, move || {
794794
let _p = profile::span("publish_diagnostics");
795795
let diagnostics = subscriptions
796796
.into_iter()

crates/rust-analyzer/src/reload.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use ide_db::{
2727
use itertools::Itertools;
2828
use proc_macro_api::{MacroDylib, ProcMacroServer};
2929
use project_model::{PackageRoot, ProjectWorkspace, WorkspaceBuildScripts};
30-
use stdx::format_to;
30+
use stdx::{format_to, thread::ThreadIntent};
3131
use syntax::SmolStr;
3232
use triomphe::Arc;
3333
use vfs::{file_set::FileSetConfig, AbsPath, AbsPathBuf, ChangeKind};
@@ -185,7 +185,7 @@ impl GlobalState {
185185
pub(crate) fn fetch_workspaces(&mut self, cause: Cause) {
186186
tracing::info!(%cause, "will fetch workspaces");
187187

188-
self.task_pool.handle.spawn_with_sender(stdx::thread::QoSClass::Utility, {
188+
self.task_pool.handle.spawn_with_sender(ThreadIntent::Worker, {
189189
let linked_projects = self.config.linked_projects();
190190
let detached_files = self.config.detached_files().to_vec();
191191
let cargo_config = self.config.cargo();
@@ -260,7 +260,7 @@ impl GlobalState {
260260
tracing::info!(%cause, "will fetch build data");
261261
let workspaces = Arc::clone(&self.workspaces);
262262
let config = self.config.cargo();
263-
self.task_pool.handle.spawn_with_sender(stdx::thread::QoSClass::Utility, move |sender| {
263+
self.task_pool.handle.spawn_with_sender(ThreadIntent::Worker, move |sender| {
264264
sender.send(Task::FetchBuildData(BuildDataProgress::Begin)).unwrap();
265265

266266
let progress = {
@@ -280,7 +280,7 @@ impl GlobalState {
280280
let dummy_replacements = self.config.dummy_replacements().clone();
281281
let proc_macro_clients = self.proc_macro_clients.clone();
282282

283-
self.task_pool.handle.spawn_with_sender(stdx::thread::QoSClass::Utility, move |sender| {
283+
self.task_pool.handle.spawn_with_sender(ThreadIntent::Worker, move |sender| {
284284
sender.send(Task::LoadProcMacros(ProcMacroProgress::Begin)).unwrap();
285285

286286
let dummy_replacements = &dummy_replacements;

crates/rust-analyzer/src/task_pool.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! It is used in [`crate::global_state::GlobalState`] throughout the main loop.
33
44
use crossbeam_channel::Sender;
5-
use stdx::thread::{Pool, QoSClass};
5+
use stdx::thread::{Pool, ThreadIntent};
66

77
pub(crate) struct TaskPool<T> {
88
sender: Sender<T>,
@@ -14,23 +14,23 @@ impl<T> TaskPool<T> {
1414
TaskPool { sender, pool: Pool::new(threads) }
1515
}
1616

17-
pub(crate) fn spawn<F>(&mut self, qos_class: QoSClass, task: F)
17+
pub(crate) fn spawn<F>(&mut self, intent: ThreadIntent, task: F)
1818
where
1919
F: FnOnce() -> T + Send + 'static,
2020
T: Send + 'static,
2121
{
22-
self.pool.spawn(qos_class, {
22+
self.pool.spawn(intent, {
2323
let sender = self.sender.clone();
2424
move || sender.send(task()).unwrap()
2525
})
2626
}
2727

28-
pub(crate) fn spawn_with_sender<F>(&mut self, qos_class: QoSClass, task: F)
28+
pub(crate) fn spawn_with_sender<F>(&mut self, intent: ThreadIntent, task: F)
2929
where
3030
F: FnOnce(Sender<T>) + Send + 'static,
3131
T: Send + 'static,
3232
{
33-
self.pool.spawn(qos_class, {
33+
self.pool.spawn(intent, {
3434
let sender = self.sender.clone();
3535
move || task(sender)
3636
})

crates/rust-analyzer/tests/slow-tests/support.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl Server {
165165
fn new(dir: TestDir, config: Config) -> Server {
166166
let (connection, client) = Connection::memory();
167167

168-
let _thread = stdx::thread::Builder::new(stdx::thread::QoSClass::Utility)
168+
let _thread = stdx::thread::Builder::new(stdx::thread::ThreadIntent::Worker)
169169
.name("test server".to_string())
170170
.spawn(move || main_loop(config, connection).unwrap())
171171
.expect("failed to spawn a thread");

0 commit comments

Comments
 (0)