|
| 1 | +//! Nintendo 3DS-specific extensions to primitives in the [`std::thread`] module. |
| 2 | +//! |
| 3 | +//! All 3DS models have at least two CPU cores available to spawn threads on: |
| 4 | +//! The application core (appcore) and the system core (syscore). The New 3DS |
| 5 | +//! has an additional two cores, the first of which can also run user-created |
| 6 | +//! threads. |
| 7 | +//! |
| 8 | +//! Threads spawned on the appcore are cooperative rather than preemptive. This |
| 9 | +//! means that threads must explicitly yield control to other threads (whether |
| 10 | +//! via synchronization primitives or explicit calls to `yield_now`) when they |
| 11 | +//! are not actively performing work. Failure to do so may result in control |
| 12 | +//! flow being stuck in an inactive thread while the other threads are powerless |
| 13 | +//! to continue their work. |
| 14 | +//! |
| 15 | +//! However, it is possible to spawn one fully preemptive thread on the syscore |
| 16 | +//! by using a service call to reserve a slice of time for a thread to run. |
| 17 | +//! Attempting to run more than one thread at a time on the syscore will result |
| 18 | +//! in an error. |
| 19 | +//! |
| 20 | +//! [`std::thread`]: crate::thread |
| 21 | +
|
| 22 | +#![unstable(feature = "horizon_thread_ext", issue = "none")] |
| 23 | + |
| 24 | +/// Extensions on [`std::thread::Builder`] for the Nintendo 3DS. |
| 25 | +/// |
| 26 | +/// [`std::thread::Builder`]: crate::thread::Builder |
| 27 | +pub trait BuilderExt: Sized { |
| 28 | + /// Sets the priority level for the new thread. |
| 29 | + /// |
| 30 | + /// Low values gives the thread higher priority. For userland apps, this has |
| 31 | + /// to be within the range of 0x18 to 0x3F inclusive. The main thread |
| 32 | + /// usually has a priority of 0x30, but not always. |
| 33 | + fn priority(mut self, priority: i32) -> Self; |
| 34 | + |
| 35 | + /// Sets the ID of the processor the thread should be run on. Threads on the |
| 36 | + /// 3DS are only preemptive if they are on the system core. Otherwise they |
| 37 | + /// are cooperative (must yield to let other threads run). |
| 38 | + /// |
| 39 | + /// Processor IDs are labeled starting from 0. On Old3DS it must be <2, and |
| 40 | + /// on New3DS it must be <4. Pass -1 to execute the thread on all CPUs and |
| 41 | + /// -2 to execute the thread on the default CPU (set in the application's |
| 42 | + /// Exheader). |
| 43 | + /// |
| 44 | + /// * Processor #0 is the application core. It is always possible to create |
| 45 | + /// a thread on this core. |
| 46 | + /// * Processor #1 is the system core. If the CPU time limit is set, it is |
| 47 | + /// possible to create a single thread on this core. |
| 48 | + /// * Processor #2 is New3DS exclusive. Normal applications can create |
| 49 | + /// threads on this core only if the built application has proper external setup. |
| 50 | + /// * Processor #3 is New3DS exclusive. Normal applications cannot create |
| 51 | + /// threads on this core. |
| 52 | + fn processor_id(mut self, processor_id: i32) -> Self; |
| 53 | +} |
| 54 | + |
| 55 | +impl BuilderExt for crate::thread::Builder { |
| 56 | + fn priority(mut self, priority: i32) -> Self { |
| 57 | + self.native_options.priority = Some(priority); |
| 58 | + self |
| 59 | + } |
| 60 | + |
| 61 | + fn processor_id(mut self, processor_id: i32) -> Self { |
| 62 | + self.native_options.processor_id = Some(processor_id); |
| 63 | + self |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/// Get the current thread's priority level. Lower values correspond to higher |
| 68 | +/// priority levels. |
| 69 | +pub fn current_priority() -> i32 { |
| 70 | + let thread_id = unsafe { libc::pthread_self() }; |
| 71 | + let mut policy = 0; |
| 72 | + let mut sched_param = libc::sched_param { sched_priority: 0 }; |
| 73 | + |
| 74 | + let result = unsafe { libc::pthread_getschedparam(thread_id, &mut policy, &mut sched_param) }; |
| 75 | + assert_eq!(result, 0); |
| 76 | + |
| 77 | + sched_param.sched_priority |
| 78 | +} |
| 79 | + |
| 80 | +/// Get the current thread's processor ID. |
| 81 | +/// |
| 82 | +/// * Processor #0 is the application core. |
| 83 | +/// * Processor #1 is the system core. |
| 84 | +/// * Processor #2 is New3DS exclusive. |
| 85 | +/// * Processor #3 is New3DS exclusive. |
| 86 | +pub fn current_processor() -> i32 { |
| 87 | + unsafe { libc::pthread_getprocessorid_np() } |
| 88 | +} |
0 commit comments