Skip to content

Commit 353b6c1

Browse files
committed
Add thread-info example
1 parent 2d49cd9 commit 353b6c1

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

ctru-rs/examples/thread-info.rs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
//! Prints some interesting system info about the main and (spawned) system threads.
2+
3+
#![feature(horizon_thread_ext)]
4+
5+
use ctru::console::Console;
6+
use ctru::gfx::Gfx;
7+
use ctru::services::apt::Apt;
8+
use ctru::services::hid::{Hid, KeyPad};
9+
use std::os::horizon::thread::ThreadBuilderExt;
10+
11+
fn main() {
12+
ctru::init();
13+
let gfx = Gfx::default();
14+
let hid = Hid::init().expect("Couldn't obtain HID controller");
15+
let apt = Apt::init().expect("Couldn't obtain APT controller");
16+
let _console = Console::init(gfx.top_screen.borrow_mut());
17+
18+
// Give ourselves up to 30% of the system core's time
19+
apt.set_app_cpu_time_limit(30)
20+
.expect("Failed to enable system core");
21+
22+
print_processor("main thread");
23+
print_thread_id("main thread");
24+
print_priority("main thread");
25+
print_affinity_mask("main thread");
26+
27+
std::thread::Builder::new()
28+
.ideal_processor(1)
29+
.spawn(|| {
30+
print_processor("sys thread");
31+
print_thread_id("sys thread");
32+
print_priority("sys thread");
33+
print_affinity_mask("sys thread");
34+
print_thread_list();
35+
})
36+
.unwrap()
37+
.join()
38+
.unwrap();
39+
40+
println!("sys thread exited");
41+
print_thread_list();
42+
43+
println!("\nPress Start to exit");
44+
45+
while apt.main_loop() {
46+
hid.scan_input();
47+
48+
if hid.keys_down().contains(KeyPad::KEY_START) {
49+
break;
50+
}
51+
52+
gfx.flush_buffers();
53+
gfx.swap_buffers();
54+
gfx.wait_for_vblank();
55+
}
56+
}
57+
58+
fn print_processor(thread_name: &str) {
59+
println!(
60+
"{thread_name} processor: {}",
61+
std::os::horizon::thread::current_processor()
62+
);
63+
}
64+
65+
fn print_priority(thread_name: &str) {
66+
println!(
67+
"{thread_name} priority: {:#x}",
68+
std::os::horizon::thread::current_priority()
69+
);
70+
}
71+
72+
fn print_affinity_mask(thread_name: &str) {
73+
let mut affinity_mask = [0u8; 1];
74+
let result = unsafe {
75+
ctru_sys::svcGetThreadAffinityMask(
76+
affinity_mask.as_mut_ptr(),
77+
ctru_sys::CUR_THREAD_HANDLE,
78+
2,
79+
)
80+
};
81+
82+
if ctru_sys::R_FAILED(result) {
83+
println!("Error getting affinity mask:");
84+
println!("result level = {}", ctru_sys::R_LEVEL(result));
85+
println!("result summary = {}", ctru_sys::R_SUMMARY(result));
86+
println!("result description = {}", ctru_sys::R_DESCRIPTION(result));
87+
return;
88+
}
89+
90+
let affinity_value = affinity_mask[0];
91+
println!("{thread_name} affinity: {affinity_value:#x?}");
92+
}
93+
94+
fn print_thread_id(thread_name: &str) {
95+
let mut thread_id = 0;
96+
let result = unsafe { ctru_sys::svcGetThreadId(&mut thread_id, ctru_sys::CUR_THREAD_HANDLE) };
97+
98+
if ctru_sys::R_FAILED(result) {
99+
println!("Error getting thread ID:");
100+
println!("result level = {}", ctru_sys::R_LEVEL(result));
101+
println!("result summary = {}", ctru_sys::R_SUMMARY(result));
102+
println!("result description = {}", ctru_sys::R_DESCRIPTION(result));
103+
return;
104+
}
105+
106+
println!("{thread_name} ID: {thread_id:#x?}")
107+
}
108+
109+
fn print_thread_list() {
110+
let mut thread_ids = [0; 100];
111+
let mut thread_ids_count = 0;
112+
let result = unsafe {
113+
ctru_sys::svcGetThreadList(
114+
&mut thread_ids_count,
115+
thread_ids.as_mut_ptr(),
116+
thread_ids.len() as i32,
117+
ctru_sys::CUR_PROCESS_HANDLE,
118+
)
119+
};
120+
121+
if ctru_sys::R_FAILED(result) {
122+
println!("Error getting thread list:");
123+
println!("result level = {}", ctru_sys::R_LEVEL(result));
124+
println!("result summary = {}", ctru_sys::R_SUMMARY(result));
125+
println!("result description = {}", ctru_sys::R_DESCRIPTION(result));
126+
return;
127+
}
128+
129+
println!("Thread list:");
130+
for thread_id in thread_ids.into_iter().take(thread_ids_count as usize) {
131+
println!(" {thread_id:#x}");
132+
}
133+
}

0 commit comments

Comments
 (0)