From 435f7c3374284d1f5b2e3f1f728e77225a48ec81 Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Wed, 9 Feb 2022 21:06:00 -0800 Subject: [PATCH 1/6] Add some extensions to pthread for armv6k-nintendo-3ds The pthread_attr_t type can have priority and affinity values set. pthread_getpriority returns the priority of the current thread. These are needed to enable std thread support. std can't link directly with libctru so we go through pthread as an intermediate interface. --- src/unix/newlib/horizon/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/unix/newlib/horizon/mod.rs b/src/unix/newlib/horizon/mod.rs index a154d72707b9c..e638734db5f66 100644 --- a/src/unix/newlib/horizon/mod.rs +++ b/src/unix/newlib/horizon/mod.rs @@ -190,5 +190,11 @@ extern "C" { value: *mut ::c_void, ) -> ::c_int; + pub fn pthread_attr_setpriority(attr: *mut ::pthread_attr_t, priority: ::c_int) -> ::c_int; + + pub fn pthread_attr_setaffinity(attr: *mut ::pthread_attr_t, affinity: ::c_int) -> ::c_int; + + pub fn pthread_getpriority() -> ::c_int; + pub fn gethostid() -> ::c_long; } From eef23c7017332352afd02bfaaed8a0576830a9f7 Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Fri, 11 Feb 2022 21:23:57 -0800 Subject: [PATCH 2/6] Change to more standard priority function interfaces --- src/unix/newlib/horizon/mod.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/unix/newlib/horizon/mod.rs b/src/unix/newlib/horizon/mod.rs index e638734db5f66..56d35b1af12e5 100644 --- a/src/unix/newlib/horizon/mod.rs +++ b/src/unix/newlib/horizon/mod.rs @@ -51,6 +51,10 @@ s! { pub sun_family: ::sa_family_t, pub sun_path: [::c_char; 104usize], } + + pub struct sched_param { + pub sched_priority: ::c_int, + } } pub const SIGEV_NONE: ::c_int = 1; @@ -190,7 +194,15 @@ extern "C" { value: *mut ::c_void, ) -> ::c_int; - pub fn pthread_attr_setpriority(attr: *mut ::pthread_attr_t, priority: ::c_int) -> ::c_int; + pub fn pthread_attr_getschedparam( + attr: *const ::pthread_attr_t, + param: *mut sched_param, + ) -> ::c_int; + + pub fn pthread_attr_setschedparam( + attr: *mut ::pthread_attr_t, + param: *const sched_param, + ) -> ::c_int; pub fn pthread_attr_setaffinity(attr: *mut ::pthread_attr_t, affinity: ::c_int) -> ::c_int; From cf928081204e323e831353324b3ef14561fa8e45 Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Sat, 12 Feb 2022 19:14:36 -0800 Subject: [PATCH 3/6] Change wording to "ideal processor" and use "np" suffix Also adds a get version, to keep consistency. --- src/unix/newlib/horizon/mod.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/unix/newlib/horizon/mod.rs b/src/unix/newlib/horizon/mod.rs index 56d35b1af12e5..617ae8f6b959c 100644 --- a/src/unix/newlib/horizon/mod.rs +++ b/src/unix/newlib/horizon/mod.rs @@ -204,7 +204,15 @@ extern "C" { param: *const sched_param, ) -> ::c_int; - pub fn pthread_attr_setaffinity(attr: *mut ::pthread_attr_t, affinity: ::c_int) -> ::c_int; + pub fn pthread_attr_getidealprocessor_np( + attr: *const ::pthread_attr_t, + ideal_processor: *mut ::c_int, + ) -> ::c_int; + + pub fn pthread_attr_setidealprocessor_np( + attr: *mut ::pthread_attr_t, + ideal_processor: ::c_int, + ) -> ::c_int; pub fn pthread_getpriority() -> ::c_int; From 9405ad6b7702bd35fcb9725aa28795421b1626d2 Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Sat, 12 Feb 2022 19:59:00 -0800 Subject: [PATCH 4/6] Replace pthread_getpriority nonstandard function with standard ones I chose the scheduler priorities based on https://man7.org/linux/man-pages/man7/sched.7.html I think the cooperative app cores are most like SCHED_FIFO, while the sys core is similar to SCHED_RR. However, I don't think our pthread implementation would be able to accurately return the right policy since we need to know what processor the thread is running on, and the only API to get that gets the ID for the current thread. Since the pthread function passes in a thread ID, we are unable to always get the processor ID and thus the policy. In this case, I think we should just always return (and accept in set) SCHED_FIFO. I don't think this will be used anyways. --- src/unix/newlib/horizon/mod.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/unix/newlib/horizon/mod.rs b/src/unix/newlib/horizon/mod.rs index 617ae8f6b959c..bab91fde91acd 100644 --- a/src/unix/newlib/horizon/mod.rs +++ b/src/unix/newlib/horizon/mod.rs @@ -151,6 +151,10 @@ pub const FIONBIO: ::c_ulong = 1; pub const RTLD_DEFAULT: *mut ::c_void = 0 as *mut ::c_void; +// For pthread get/setschedparam +pub const SCHED_FIFO: ::c_int = 1; +pub const SCHED_RR: ::c_int = 2; + // Horizon OS works doesn't or can't hold any of this information safe_f! { pub {const} fn WIFSTOPPED(_status: ::c_int) -> bool { @@ -214,7 +218,17 @@ extern "C" { ideal_processor: ::c_int, ) -> ::c_int; - pub fn pthread_getpriority() -> ::c_int; + pub fn pthread_getschedparam( + native: ::pthread_t, + policy: *mut ::c_int, + param: *mut ::sched_param, + ) -> ::c_int; + + pub fn pthread_setschedparam( + native: ::pthread_t, + policy: ::c_int, + param: *const ::sched_param, + ) -> ::c_int; pub fn gethostid() -> ::c_long; } From d38b04a4ea065074379aedf1a11325c49dda8a1d Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Sun, 13 Feb 2022 18:42:37 -0800 Subject: [PATCH 5/6] Add nonstandard pthread_getprocessorid_np function --- src/unix/newlib/horizon/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/unix/newlib/horizon/mod.rs b/src/unix/newlib/horizon/mod.rs index bab91fde91acd..3967805e1bd40 100644 --- a/src/unix/newlib/horizon/mod.rs +++ b/src/unix/newlib/horizon/mod.rs @@ -229,6 +229,8 @@ extern "C" { policy: ::c_int, param: *const ::sched_param, ) -> ::c_int; + + pub fn pthread_getprocessorid_np() -> ::c_int; pub fn gethostid() -> ::c_long; } From b62064be98cc318d79ae9b19c6642e29130f59ec Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Mon, 14 Feb 2022 20:54:14 -0800 Subject: [PATCH 6/6] Rename "idealprocessor" attr functions to "processorid" --- src/unix/newlib/horizon/mod.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/unix/newlib/horizon/mod.rs b/src/unix/newlib/horizon/mod.rs index 3967805e1bd40..5884501daace4 100644 --- a/src/unix/newlib/horizon/mod.rs +++ b/src/unix/newlib/horizon/mod.rs @@ -208,14 +208,14 @@ extern "C" { param: *const sched_param, ) -> ::c_int; - pub fn pthread_attr_getidealprocessor_np( + pub fn pthread_attr_getprocessorid_np( attr: *const ::pthread_attr_t, - ideal_processor: *mut ::c_int, + processor_id: *mut ::c_int, ) -> ::c_int; - pub fn pthread_attr_setidealprocessor_np( + pub fn pthread_attr_setprocessorid_np( attr: *mut ::pthread_attr_t, - ideal_processor: ::c_int, + processor_id: ::c_int, ) -> ::c_int; pub fn pthread_getschedparam( @@ -229,7 +229,7 @@ extern "C" { policy: ::c_int, param: *const ::sched_param, ) -> ::c_int; - + pub fn pthread_getprocessorid_np() -> ::c_int; pub fn gethostid() -> ::c_long;