Skip to content

Commit 493a903

Browse files
committed
Bump main thread priority on windows
1 parent 4010994 commit 493a903

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_lsp_server/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ env_logger = { version = "0.7.1", default-features = false }
3030
ra_cargo_watch = { path = "../ra_cargo_watch" }
3131
either = "1.5"
3232

33+
[target.'cfg(windows)'.dependencies]
34+
winapi = "0.3"
35+
3336
[dev-dependencies]
3437
tempfile = "3"
3538
test_utils = { path = "../test_utils" }

crates/ra_lsp_server/src/main_loop.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ pub fn main_loop(
5757
) -> Result<()> {
5858
log::info!("server_config: {:#?}", config);
5959

60+
// Windows scheduler implements priority boosts: if thread waits for an
61+
// event (like a condvar), and event fires, priority of the thread is
62+
// temporary bumped. This optimization backfires in our case: each time the
63+
// `main_loop` schedules a task to run on a threadpool, the worker threads
64+
// gets a higher priority, and (on a machine with fewer cores) displaces the
65+
// main loop! We work-around this by marking the main loop as a
66+
// higher-priority thread.
67+
//
68+
// https://docs.microsoft.com/en-us/windows/win32/procthread/scheduling-priorities
69+
// https://docs.microsoft.com/en-us/windows/win32/procthread/priority-boosts
70+
// https://github.com/rust-analyzer/rust-analyzer/issues/2835
71+
#[cfg(windows)]
72+
unsafe {
73+
use winapi::um::processthreadsapi::*;
74+
let thread = GetCurrentThread();
75+
let thread_priority_above_normal = 1;
76+
SetThreadPriority(thread, thread_priority_above_normal);
77+
}
78+
6079
let mut loop_state = LoopState::default();
6180
let mut world_state = {
6281
let feature_flags = {

0 commit comments

Comments
 (0)