From 0e025df8e1869ea604842799b4d89b0065e4b10f Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 19 Oct 2026 02:13:51 +0200 Subject: [PATCH] Server thread: ahead of the workers on macOS and Windows, as vanilla's spin MinecraftServer.spin raises its server thread (Thread.setPriority(8)) when there are more than four processors. The new horizon-threads crate makes the same request for the dedicated server's thread and the integrated server's: user-interactive QoS on macOS, above-normal priority on Windows, nothing on Linux (an unprivileged thread can't raise itself). Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 9 +++ Cargo.toml | 2 +- crates/horizon-client/Cargo.toml | 2 + crates/horizon-client/src/game/dedicated.rs | 2 + crates/horizon-client/src/game/integrated.rs | 2 + crates/horizon-threads/Cargo.toml | 16 +++++ crates/horizon-threads/src/lib.rs | 66 ++++++++++++++++++++ 7 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 crates/horizon-threads/Cargo.toml create mode 100644 crates/horizon-threads/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 94e2052e3..c9eb13c20 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1429,6 +1429,7 @@ dependencies = [ "horizon-shaderpack", "horizon-telemetry", "horizon-theme", + "horizon-threads", "horizon-world", "log", "miniz_oxide 0.8.9", @@ -1605,6 +1606,14 @@ dependencies = [ name = "horizon-theme" version = "0.1.0" +[[package]] +name = "horizon-threads" +version = "0.1.0" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + [[package]] name = "horizon-world" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 30a30386d..85b30a130 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] exclude = ["launcher", "crates/horizon-telemetry-server"] -members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-world", "crates/voxel-kernels", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-shaderpack", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"] +members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/voxel-kernels", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-shaderpack", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"] resolver = "2" [workspace.package] diff --git a/crates/horizon-client/Cargo.toml b/crates/horizon-client/Cargo.toml index 82dca390d..2f193e3f9 100644 --- a/crates/horizon-client/Cargo.toml +++ b/crates/horizon-client/Cargo.toml @@ -10,6 +10,8 @@ description = "Horizon native development client" [dependencies] horizon-assets = { path = "../horizon-assets" } horizon-core = { path = "../horizon-core" } +# The server thread ahead of the workers (MinecraftServer.spin's Thread.setPriority). +horizon-threads = { path = "../horizon-threads" } horizon-world = { path = "../horizon-world" } horizon-protocol = { path = "../horizon-protocol" } # server.properties, eula.txt and the user lists, shared with horizon-server. diff --git a/crates/horizon-client/src/game/dedicated.rs b/crates/horizon-client/src/game/dedicated.rs index 640318529..8495ea129 100644 --- a/crates/horizon-client/src/game/dedicated.rs +++ b/crates/horizon-client/src/game/dedicated.rs @@ -427,6 +427,8 @@ const BROADCAST: &[&str] = &[ impl Game<'_> { /// DedicatedServer.initServer and MinecraftServer.runServer. pub(super) fn run_dedicated(&mut self) -> Result<()> { + // This thread ticks the server (MinecraftServer.spin's priority). + horizon_threads::favour_current(); self.join_world_rest()?; let Setup { props, diff --git a/crates/horizon-client/src/game/integrated.rs b/crates/horizon-client/src/game/integrated.rs index 2b8ffb166..bfbc8d66c 100644 --- a/crates/horizon-client/src/game/integrated.rs +++ b/crates/horizon-client/src/game/integrated.rs @@ -216,6 +216,8 @@ impl Game<'_> { let thread = std::thread::Builder::new() .name("Server thread".into()) .spawn(move || { + // MinecraftServer.spin's Thread.setPriority(8). + horizon_threads::favour_current(); let result = serve( &res, pack, diff --git a/crates/horizon-threads/Cargo.toml b/crates/horizon-threads/Cargo.toml new file mode 100644 index 000000000..35f7e3222 --- /dev/null +++ b/crates/horizon-threads/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "horizon-threads" +version = "0.1.0" +edition.workspace = true +license.workspace = true +rust-version = "1.85" +description = "Scheduling hints for the game's threads: the OS calls the crates that forbid unsafe code can't make" + +[target.'cfg(target_vendor = "apple")'.dependencies] +libc = "0.2" + +[target.'cfg(windows)'.dependencies] +windows-sys = { version = "0.61", features = ["Win32_System_Threading"] } + +[lints] +workspace = true diff --git a/crates/horizon-threads/src/lib.rs b/crates/horizon-threads/src/lib.rs new file mode 100644 index 000000000..6436a4729 --- /dev/null +++ b/crates/horizon-threads/src/lib.rs @@ -0,0 +1,66 @@ +//! Scheduling hints for the game's threads: the few OS calls that the other +//! crates, which forbid unsafe code, can't make themselves. + +/// Ask the OS to run the calling thread ahead of the process's other +/// threads, as vanilla's `MinecraftServer.spin` does for its server thread +/// (`Thread.setPriority(8)` when there are more than four processors), so +/// world generation and chunk encoding on every other core don't hold a tick +/// back: user-interactive quality of service on macOS, above-normal priority +/// on Windows. Nothing elsewhere: Linux lets an unprivileged thread lower its +/// priority but not raise it (and the JVM leaves it there too). Whether the +/// OS took it. +pub fn favour_current() -> bool { + if std::thread::available_parallelism().map_or(1, |n| n.get()) <= 4 { + return false; + } + favour() +} + +#[cfg(target_vendor = "apple")] +fn favour() -> bool { + // SAFETY: sets the calling thread's own QoS class; no pointers. + unsafe { + libc::pthread_set_qos_class_self_np(libc::qos_class_t::QOS_CLASS_USER_INTERACTIVE, 0) == 0 + } +} + +#[cfg(windows)] +fn favour() -> bool { + use windows_sys::Win32::System::Threading::{ + GetCurrentThread, SetThreadPriority, THREAD_PRIORITY_ABOVE_NORMAL, + }; + // SAFETY: GetCurrentThread's pseudo handle needs no closing. + unsafe { SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL) != 0 } +} + +#[cfg(not(any(target_vendor = "apple", windows)))] +fn favour() -> bool { + false +} + +#[cfg(test)] +mod tests { + /// The hint holds on the platforms that take it. + #[test] + fn favouring_a_thread_takes_where_the_os_allows_it() { + let took = std::thread::spawn(super::favour).join().unwrap(); + assert_eq!(took, cfg!(any(target_vendor = "apple", windows))); + #[cfg(target_vendor = "apple")] + std::thread::spawn(|| { + assert!(super::favour()); + let mut class = libc::qos_class_t::QOS_CLASS_UNSPECIFIED; + let mut relative = 0; + // SAFETY: reads the calling thread's own QoS class into locals. + let read = unsafe { + libc::pthread_get_qos_class_np(libc::pthread_self(), &mut class, &mut relative) + }; + assert_eq!(read, 0); + assert!(matches!( + class, + libc::qos_class_t::QOS_CLASS_USER_INTERACTIVE + )); + }) + .join() + .unwrap(); + } +}