Proposal
Supersedes #71. This proposal is based on the discussion there and on Zulip about that proposal.
cc @AzureMarker @joshtriplett as they were involved in the earlier discussion / proposal as well.
Problem statement
Exposing OS thread scheduling options has been requested for quite a long time. Some third-party crates have been created to supplement the std functionality, but they seem to mostly work by either duplicating std APIs or only work for the current thread after spawning.
This proposal aims to enable std::os extension traits that can modify thread::Builder to set some of these properties before a thread is spawned, without committing to a higher-level cross-platform API (but ideally leaving room for one to be designed and implemented in the future).
Motivation, use-cases
Setting thread affinity and priority has a variety of motivations. For platforms that use a cooperative thread scheduler, setting CPU affinity and priority may be necessary to ensure that threads are not starved or cause deadlocks. High-performance / realtime applications may need fine grained control over their threads to meet performance requirements.
In principle, I believe this proposal should enable implementation of the necessary APIs to exert low-level OS-specific control over threads, which should pave the way for a more general-purpose cross-platform solution for these use cases.
Solution sketches
I have taken some of the changes from previous attempts and implemented them in a fork here, trying to show the minimum viable API surface:
std::os::linux allows for setting affinity with libc::cpu_set_t (OS-specific structure).
std::os::horizon allows for setting priority with libc::c_int (OS-specific meaning).
- Other platforms are unaffected. It should be possible to expand the extension traits as needed.
The new public API surface area is fairly small but can be expanded on a per-platform basis as support is added / desired:
std::os::horizon::thread
/// Horizon-specific extension trait for [`thread::Builder`](crate::thread::Builder).
pub trait BuilderExt {
/// Set the priority of the thread to be spawned.
///
/// See <https://www.3dbrew.org/wiki/Multi-threading#Threads> for details
/// about the meaning / valid values of the `priority` parameter.
fn priority(self, priority: libc::c_int) -> Self;
}
impl BuilderExt for crate::thread::Builder {}
std::os::linux::thread
/// Linux-specific extensions for [`thread::Builder`](crate::thread::Builder).
pub trait BuilderExt {
/// Set the CPU affinity (which cores to run on) for the thread to be spawned.
///
/// See <https://man7.org/linux/man-pages/man3/CPU_SET.3.html> for more details
/// about how to construct the `cpu_set` parameter.
fn affinity(self, cpu_set: libc::cpu_set_t) -> Self;
}
impl BuilderExt for crate::thread::Builder {}
Draft of an implementation here:
rust-lang/rust@master...ian-h-chamberlain:rust:feature/thread-schedule-os-ext
Links and related work
Proposal
Supersedes #71. This proposal is based on the discussion there and on Zulip about that proposal.
cc @AzureMarker @joshtriplett as they were involved in the earlier discussion / proposal as well.
Problem statement
Exposing OS thread scheduling options has been requested for quite a long time. Some third-party crates have been created to supplement the
stdfunctionality, but they seem to mostly work by either duplicatingstdAPIs or only work for the current thread after spawning.This proposal aims to enable
std::osextension traits that can modifythread::Builderto set some of these properties before a thread is spawned, without committing to a higher-level cross-platform API (but ideally leaving room for one to be designed and implemented in the future).Motivation, use-cases
Setting thread affinity and priority has a variety of motivations. For platforms that use a cooperative thread scheduler, setting CPU affinity and priority may be necessary to ensure that threads are not starved or cause deadlocks. High-performance / realtime applications may need fine grained control over their threads to meet performance requirements.
In principle, I believe this proposal should enable implementation of the necessary APIs to exert low-level OS-specific control over threads, which should pave the way for a more general-purpose cross-platform solution for these use cases.
Solution sketches
I have taken some of the changes from previous attempts and implemented them in a fork here, trying to show the minimum viable API surface:
std::os::linuxallows for setting affinity withlibc::cpu_set_t(OS-specific structure).std::os::horizonallows for setting priority withlibc::c_int(OS-specific meaning).The new public API surface area is fairly small but can be expanded on a per-platform basis as support is added / desired:
std::os::horizon::threadstd::os::linux::threadDraft of an implementation here:
rust-lang/rust@master...ian-h-chamberlain:rust:feature/thread-schedule-os-ext
Links and related work
thread-prioritycrate (duplicatesstd::thread::BuilderAPI to allow setting before spawn)core_affinitycrate (only allows set/get of current thread's affinity)armv6k-nintendo-3dsextension traits:std::threadsupport for the Nintendo 3DS rust#98514