Skip to content

Commit df845c5

Browse files
authored
Merge pull request #46 from Meziu/feature/std-threads
Use std threads support
2 parents 3dedcd9 + 8113950 commit df845c5

8 files changed

Lines changed: 127 additions & 1070 deletions

File tree

ctru-rs/examples/futures-basic.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
//! The example also implements clean shutdown by using a oneshot channel to end the future, thus
66
//! ending the executor and the thread it runs on.
77
8+
#![feature(horizon_thread_ext)]
9+
810
use ctru::console::Console;
911
use ctru::services::hid::KeyPad;
1012
use ctru::services::{Apt, Hid};
1113
use ctru::Gfx;
1214
use futures::StreamExt;
15+
use std::os::horizon::thread::BuilderExt;
1316

1417
fn main() {
1518
ctru::init();
@@ -26,8 +29,8 @@ fn main() {
2629

2730
let (exit_sender, mut exit_receiver) = futures::channel::oneshot::channel();
2831
let (mut timer_sender, mut timer_receiver) = futures::channel::mpsc::channel(0);
29-
let executor_thread = ctru::thread::Builder::new()
30-
.affinity(1)
32+
let executor_thread = std::thread::Builder::new()
33+
.processor_id(1)
3134
.spawn(move || {
3235
let mut executor = futures::executor::LocalPool::new();
3336

ctru-rs/examples/futures-tokio.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
#![feature(horizon_thread_ext)]
2+
13
use ctru::console::Console;
24
use ctru::services::hid::KeyPad;
35
use ctru::services::{Apt, Hid};
46
use ctru::Gfx;
7+
use std::os::horizon::thread::BuilderExt;
58
use std::time::Duration;
69

710
fn main() {
@@ -23,11 +26,9 @@ fn main() {
2326
.build()
2427
.expect("Couldn't build runtime");
2528

26-
let runtime_thread = ctru::thread::Builder::new()
29+
let runtime_thread = std::thread::Builder::new()
2730
// Run on the system core
28-
.affinity(1)
29-
// Use a bigger stack size. Default is 0x1000 but we'd easily overflow that.
30-
.stack_size(0x200000)
31+
.processor_id(1)
3132
.spawn(move || {
3233
runtime.block_on(async move {
3334
let mut wake_time = tokio::time::Instant::now() + Duration::from_secs(1);

ctru-rs/examples/thread-basic.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
#![feature(horizon_thread_ext)]
2+
13
use ctru::console::Console;
24
use ctru::gfx::Gfx;
35
use ctru::services::apt::Apt;
46
use ctru::services::hid::{Hid, KeyPad};
5-
use ctru::thread;
6-
7+
use std::os::horizon::thread::BuilderExt;
78
use std::time::Duration;
89

910
fn main() {
@@ -14,19 +15,19 @@ fn main() {
1415
let gfx = Gfx::default();
1516
let _console = Console::init(gfx.top_screen.borrow_mut());
1617

17-
let prio = thread::current().priority();
18+
let prio = std::os::horizon::thread::current_priority();
1819
println!("Main thread prio: {}\n", prio);
1920

2021
for ix in 0..3 {
21-
thread::Builder::new()
22+
std::thread::Builder::new()
2223
.priority(prio - 1)
2324
.spawn(move || {
2425
let sleep_duration: u64 = 1000 + ix * 250;
2526
let mut i = 0;
2627
loop {
2728
println!("Thread{ix} says {i}");
2829
i += 1;
29-
thread::sleep(Duration::from_millis(sleep_duration));
30+
std::thread::sleep(Duration::from_millis(sleep_duration));
3031
}
3132
})
3233
.unwrap();

ctru-rs/examples/thread-info.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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::BuilderExt;
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+
.processor_id(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+
})
35+
.unwrap()
36+
.join()
37+
.unwrap();
38+
39+
println!("sys thread exited");
40+
println!("\nPress Start to exit");
41+
42+
while apt.main_loop() {
43+
hid.scan_input();
44+
45+
if hid.keys_down().contains(KeyPad::KEY_START) {
46+
break;
47+
}
48+
49+
gfx.flush_buffers();
50+
gfx.swap_buffers();
51+
gfx.wait_for_vblank();
52+
}
53+
}
54+
55+
fn print_processor(thread_name: &str) {
56+
println!(
57+
"{thread_name} processor: {}",
58+
std::os::horizon::thread::current_processor()
59+
);
60+
}
61+
62+
fn print_priority(thread_name: &str) {
63+
println!(
64+
"{thread_name} priority: {:#x}",
65+
std::os::horizon::thread::current_priority()
66+
);
67+
}
68+
69+
fn print_affinity_mask(thread_name: &str) {
70+
let mut affinity_mask = [0u8; 1];
71+
let result = unsafe {
72+
ctru_sys::svcGetThreadAffinityMask(
73+
affinity_mask.as_mut_ptr(),
74+
ctru_sys::CUR_THREAD_HANDLE,
75+
2,
76+
)
77+
};
78+
79+
if ctru_sys::R_FAILED(result) {
80+
println!("Error getting affinity mask:");
81+
println!("result level = {}", ctru_sys::R_LEVEL(result));
82+
println!("result summary = {}", ctru_sys::R_SUMMARY(result));
83+
println!("result description = {}", ctru_sys::R_DESCRIPTION(result));
84+
return;
85+
}
86+
87+
let affinity_value = affinity_mask[0];
88+
println!("{thread_name} affinity: {affinity_value:#x?}");
89+
}
90+
91+
fn print_thread_id(thread_name: &str) {
92+
let mut thread_id = 0;
93+
let result = unsafe { ctru_sys::svcGetThreadId(&mut thread_id, ctru_sys::CUR_THREAD_HANDLE) };
94+
95+
if ctru_sys::R_FAILED(result) {
96+
println!("Error getting thread ID:");
97+
println!("result level = {}", ctru_sys::R_LEVEL(result));
98+
println!("result summary = {}", ctru_sys::R_SUMMARY(result));
99+
println!("result description = {}", ctru_sys::R_DESCRIPTION(result));
100+
return;
101+
}
102+
103+
println!("{thread_name} ID: {thread_id:#x?}")
104+
}

ctru-rs/examples/thread-locals.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
#![feature(horizon_thread_ext)]
2+
13
use ctru::console::Console;
24
use ctru::services::hid::KeyPad;
35
use ctru::services::{Apt, Hid};
46
use ctru::Gfx;
57
use std::cell::RefCell;
8+
use std::os::horizon::thread::BuilderExt;
69

710
std::thread_local! {
811
static MY_LOCAL: RefCell<&'static str> = RefCell::new("initial value");
@@ -28,8 +31,8 @@ fn main() {
2831
println!("Value on main thread after mutation: {}", local.borrow());
2932
});
3033

31-
ctru::thread::Builder::new()
32-
.affinity(1)
34+
std::thread::Builder::new()
35+
.processor_id(1)
3336
.spawn(move || {
3437
MY_LOCAL.with(|local| {
3538
println!("Initial value on second thread: {}", local.borrow());

ctru-rs/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ pub fn init() {
2828

2929
use std::panic::PanicInfo;
3030

31-
let main_thread = thread::current().id();
31+
let main_thread = std::thread::current().id();
3232

3333
// Panic Hook setup
3434
let default_hook = std::panic::take_hook();
3535
let new_hook = Box::new(move |info: &PanicInfo| {
3636
default_hook(info);
3737

3838
// Only for panics in the main thread
39-
if main_thread == thread::current().id() && console::Console::exists() {
39+
if main_thread == std::thread::current().id() && console::Console::exists() {
4040
println!("\nPress SELECT to exit the software");
4141
let hid = services::hid::Hid::init().unwrap();
4242

@@ -57,7 +57,6 @@ pub mod error;
5757
pub mod gfx;
5858
pub mod services;
5959
pub mod srv;
60-
pub mod thread;
6160

6261
cfg_if::cfg_if! {
6362
if #[cfg(all(feature = "romfs", romfs_exists))] {

ctru-rs/src/test_runner.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,4 @@ mod link_fix {
115115
extern "C" fn sigemptyset(_arg1: *mut libc::sigset_t) -> ::libc::c_int {
116116
-1
117117
}
118-
119-
#[no_mangle]
120-
extern "C" fn sysconf(_name: libc::c_int) -> libc::c_long {
121-
-1
122-
}
123118
}

0 commit comments

Comments
 (0)