-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Partially implement thread scheduling attributes API proposal #101222
Closed
ian-h-chamberlain
wants to merge
2
commits into
rust-lang:master
from
ian-h-chamberlain:feature/thread-scheduling-proposal
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
|
||
pub mod fs; | ||
pub(crate) mod raw; | ||
pub mod thread; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//! Horizon-specific extensions for working with the [`std::thread`] module. | ||
//! | ||
//! [`std::thread`]: crate::thread | ||
|
||
#![unstable(feature = "thread_scheduling", issue = "none")] | ||
|
||
use crate::fmt; | ||
|
||
/// The relative priority of the thread. See the `libctru` docs for the `prio` | ||
/// parameter of [`threadCreate`] for details on valid values. | ||
/// | ||
/// [`threadCreate`]: https://libctru.devkitpro.org/thread_8h.html#a38c873d8cb02de7f5eca848fe68183ee | ||
pub struct Priority(pub(crate) libc::c_int); | ||
|
||
impl TryFrom<i32> for Priority { | ||
type Error = (); | ||
|
||
fn try_from(value: i32) -> Result<Self, Self::Error> { | ||
match value { | ||
0x18..=0x3F => Ok(Self(value)), | ||
_ => Err(()), | ||
} | ||
} | ||
} | ||
|
||
impl crate::fmt::Debug for Priority { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("Priority").finish_non_exhaustive() | ||
} | ||
} | ||
|
||
/// The CPU(s) on which to spawn the thread. See the `libctru` docs for the | ||
/// `core_id` parameter of [`threadCreate`] for details on valid values. | ||
/// | ||
/// [`threadCreate`]: https://libctru.devkitpro.org/thread_8h.html#a38c873d8cb02de7f5eca848fe68183ee | ||
pub struct Affinity(pub(crate) libc::c_int); | ||
|
||
impl Default for Affinity { | ||
fn default() -> Self { | ||
Self(-2) | ||
} | ||
} | ||
|
||
impl TryFrom<i32> for Affinity { | ||
type Error = (); | ||
|
||
fn try_from(value: i32) -> Result<Self, Self::Error> { | ||
match value { | ||
-2..=4 => Ok(Self(value)), | ||
_ => Err(()), | ||
} | ||
} | ||
} | ||
|
||
impl crate::fmt::Debug for Affinity { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("Affinity").finish_non_exhaustive() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ pub mod fs; | |
pub mod net; | ||
pub mod process; | ||
pub mod raw; | ||
pub mod thread; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//! Linux-specific extensions for working with the [`std::thread`] module. | ||
//! | ||
//! [`std::thread`]: crate::thread | ||
|
||
#![unstable(feature = "thread_scheduling", issue = "none")] | ||
|
||
use crate::fmt; | ||
|
||
/// The relative scheduling priority of a thread, corresponding to the | ||
/// `sched_priority` scheduling parameter. | ||
/// | ||
/// Refer to the man page for [`pthread_attr_setschedparam(3)`] for more details. | ||
/// | ||
/// [`pthread_attr_setschedparam(3)`]: https://man7.org/linux/man-pages/man3/pthread_attr_setschedparam.3.html | ||
pub struct Priority(pub(crate) libc::c_int); | ||
|
||
impl Priority { | ||
/// Create an integer priority. | ||
pub fn new(priority: i32) -> Self { | ||
Self(priority) | ||
} | ||
} | ||
|
||
impl crate::fmt::Debug for Priority { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("Priority").finish_non_exhaustive() | ||
} | ||
} | ||
|
||
/// The CPU affinity mask of a thread, which determines what CPUs a thread is | ||
/// eligible to run on. | ||
/// | ||
/// Refer to the man page for [`pthread_attr_setaffinity_np(3)`] for more details. | ||
/// | ||
/// [`pthread_attr_setaffinity_np(3)`]: https://man7.org/linux/man-pages/man3/pthread_attr_setaffinity_np.3.html | ||
pub struct Affinity(pub(crate) libc::cpu_set_t); | ||
|
||
impl Affinity { | ||
/// Create an affinity mask with no CPUs in it. | ||
/// See the man page entry for [`CPU_ZERO`] for more details. | ||
/// | ||
/// [`CPU_ZERO`]: https://man7.org/linux/man-pages/man3/CPU_SET.3.html | ||
pub fn new() -> Self { | ||
unsafe { | ||
let mut set = crate::mem::zeroed(); | ||
libc::CPU_ZERO(&mut set); | ||
Self(set) | ||
} | ||
} | ||
|
||
/// Add a CPU to the affinity mask. | ||
/// See the man page entry for [`CPU_SET`] for more details. | ||
/// | ||
/// [`CPU_SET`]: https://man7.org/linux/man-pages/man3/CPU_SET.3.html | ||
pub fn set(&mut self, cpu: usize) { | ||
unsafe { | ||
libc::CPU_SET(cpu, &mut self.0); | ||
} | ||
} | ||
} | ||
|
||
impl crate::fmt::Debug for Affinity { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("Affinity").finish_non_exhaustive() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be structs instead of type definitions in case this is implemented for the target in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a private type, I think it should be possible to change its type freely without any consequences to user code (alias or otherwise). However if it was re-exported as
pub
(per your other comment) then I agree it would be better asstruct Priority(())
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah, this is sys and not actually public