forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Implement std threads support #10
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
Merged
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
06dc730
Support std threads again by using pthread
AzureMarker 44cc029
Fix some compile errors and update usage of pthread_getpriority
AzureMarker e775158
Remove reference to libctru API
AzureMarker b41ac82
Remove some unnecessary documentation
AzureMarker b7ab814
Change 3DS thread builder functions to be unstable
AzureMarker 8a3d0a3
Add a "native options" concept to thread builder and make an ext trait
AzureMarker 4338219
Add a few more details about 3DS thread scheduling
AzureMarker 8bcc638
Change default stack size to 2MiB (the normal default)
AzureMarker c77ce82
Rename affinity to "ideal processor" and use new libc functions
AzureMarker a66db15
Move current_priority to the os module and expose it under the feature
AzureMarker c383153
Add std::os::horizon::thread::current_processor
AzureMarker 10cb9c9
Rename "ideal_processor" to "processor_id"
AzureMarker 9119729
Rename ThreadBuilderExt to BuilderExt
AzureMarker 3b5e287
Catch pthread errors in `std::os::horizon::thread::current_priority`
AzureMarker 209d3c0
Update library/std/src/os/horizon/thread.rs
AzureMarker 2b5a7c1
Remove reference to a syscall
AzureMarker 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 hidden or 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 mod raw; | ||
| pub mod thread; | ||
This file contains hidden or 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,88 @@ | ||
| //! Nintendo 3DS-specific extensions to primitives in the [`std::thread`] module. | ||
| //! | ||
| //! All 3DS models have at least two CPU cores available to spawn threads on: | ||
| //! The application core (appcore) and the system core (syscore). The New 3DS | ||
| //! has an additional two cores, the first of which can also run user-created | ||
| //! threads. | ||
| //! | ||
| //! Threads spawned on the appcore are cooperative rather than preemptive. This | ||
| //! means that threads must explicitly yield control to other threads (whether | ||
| //! via synchronization primitives or explicit calls to `yield_now`) when they | ||
| //! are not actively performing work. Failure to do so may result in control | ||
| //! flow being stuck in an inactive thread while the other threads are powerless | ||
| //! to continue their work. | ||
| //! | ||
| //! However, it is possible to spawn one fully preemptive thread on the syscore | ||
| //! by using a service call (https://www.3dbrew.org/wiki/APT:SetApplicationCpuTimeLimit) | ||
| //! to reserve a slice of time for a thread to run. Attempting to run more than | ||
| //! one thread at a time on the syscore will result in an error. | ||
| //! | ||
| //! [`std::thread`]: crate::thread | ||
|
|
||
| #![unstable(feature = "horizon_thread_ext", issue = "none")] | ||
|
|
||
| /// Extensions on [`std::thread::Builder`] for the Nintendo 3DS. | ||
| /// | ||
| /// [`std::thread::Builder`]: crate::thread::Builder | ||
| pub trait BuilderExt: Sized { | ||
| /// Sets the priority level for the new thread. | ||
| /// | ||
| /// Low values gives the thread higher priority. For userland apps, this has | ||
| /// to be within the range of 0x18 to 0x3F inclusive. The main thread | ||
| /// usually has a priority of 0x30, but not always. | ||
| fn priority(mut self, priority: i32) -> Self; | ||
|
|
||
| /// Sets the ID of the processor the thread should be run on. Threads on the | ||
| /// 3DS are only preemptive if they are on the system core. Otherwise they | ||
| /// are cooperative (must yield to let other threads run). | ||
| /// | ||
| /// Processor IDs are labeled starting from 0. On Old3DS it must be <2, and | ||
| /// on New3DS it must be <4. Pass -1 to execute the thread on all CPUs and | ||
| /// -2 to execute the thread on the default CPU (set in the application's | ||
| /// Exheader). | ||
| /// | ||
| /// * Processor #0 is the application core. It is always possible to create | ||
| /// a thread on this core. | ||
| /// * Processor #1 is the system core. If the CPU time limit is set, it is | ||
| /// possible to create a single thread on this core. | ||
| /// * Processor #2 is New3DS exclusive. Normal applications can create | ||
| /// threads on this core if the exheader kernel flags bitmask has 0x2000 | ||
| /// set. | ||
|
AzureMarker marked this conversation as resolved.
Outdated
|
||
| /// * Processor #3 is New3DS exclusive. Normal applications cannot create | ||
| /// threads on this core. | ||
| fn processor_id(mut self, processor_id: i32) -> Self; | ||
| } | ||
|
|
||
| impl BuilderExt for crate::thread::Builder { | ||
| fn priority(mut self, priority: i32) -> Self { | ||
| self.native_options.priority = Some(priority); | ||
| self | ||
| } | ||
|
|
||
| fn processor_id(mut self, processor_id: i32) -> Self { | ||
| self.native_options.processor_id = Some(processor_id); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| /// Get the current thread's priority level. Lower values correspond to higher | ||
| /// priority levels. | ||
| pub fn current_priority() -> i32 { | ||
| let thread_id = unsafe { libc::pthread_self() }; | ||
| let mut policy = 0; | ||
| let mut sched_param = libc::sched_param { sched_priority: 0 }; | ||
|
|
||
| unsafe { libc::pthread_getschedparam(thread_id, &mut policy, &mut sched_param) }; | ||
|
|
||
| sched_param.sched_priority | ||
| } | ||
|
|
||
| /// Get the current thread's processor ID. | ||
| /// | ||
| /// * Processor #0 is the application core. | ||
| /// * Processor #1 is the system core. | ||
| /// * Processor #2 is New3DS exclusive. | ||
| /// * Processor #3 is New3DS exclusive. | ||
| pub fn current_processor() -> i32 { | ||
| unsafe { libc::pthread_getprocessorid_np() } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.