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 <noreply@anthropic.com>
This commit is contained in:
Generated
+9
@@ -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"
|
||||
|
||||
+1
-1
@@ -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]
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user