revl: Add sched() to thread::Builder
PATCH 1/1 revl: Add sched() to thread::Builder
When spawning threads with Builder, it wasn't possible to set the scheduling policy of the spawned thread as you don't have access to the Thread object after spawning. Added that option to Builder.
---
src/thread.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)
src/thread.rs +18 -3
@@ -11,6 +11,7 @@ use std::ptr;use std::os::raw::c_int;use std::io::Error ;use std::ffi::CString ; +use evl_sys::SchedPolicy ;use evl_sys::{ evl_attach_thread, evl_unblock_thread,
@@ -29,6 +30,7 @@ pub struct Builder { visible: bool, observable: bool, unicast: bool, + sched: sched::SchedAttrs, }impl Builder {
@@ -53,6 +55,7 @@ impl Builder { visible: false, observable: false, unicast: false, + sched: sched::get_zero_attrs(), } }/// Set the thread name. This name must conform to the [naming
@@ -106,6 +109,11 @@ impl Builder { self.unicast =true ; self } +// Set scheduling policy for the thread +pub fn sched (mut self, policy:impl sched::PolicyParam ) ->Self { + self.sched = policy.to_attr (); + self + }/// Attach the calling thread to the EVL core, consuming the /// builder. ///
@@ -257,11 +265,24 @@ impl Thread {evl_attach_thread (c_flags, ptr::null ()) } }; +// evl_attach_thread() returns a valid file descriptor or -errno. -match ret { -0 .. =>return Ok (Thread (ret)), - _ =>return Err (Error ::from_raw_os_error (-ret)), +let thread =Thread (ret); + +if builder.sched.0 .sched_policyas i32 !=0 { +let c_attrs_ptr: *const evl_sched_attrs = &builder.sched.0 ; +let ret:c_int =unsafe {evl_set_schedattr (thread.0 , c_attrs_ptr) }; +if ret !=0 { +return Err (Error ::from_raw_os_error (-ret)); } + } +Ok (thread) }/// Unblock the target thread.
///
Signature · 1 line
2.34.1
Looks like I accidentally submitted the wrong patch. Here's the correct one:
src/thread.rs +0 -0
@@ -29,6 +29,7 @@ pub struct Builder {
visible: bool,
observable: bool,
unicast: bool,
+ sched: sched::SchedAttrs,
}
impl Builder {
@@ -53,6 +54,7 @@ impl Builder {
visible: false,
observable: false,
unicast: false,
+ sched: sched::get_zero_attrs(),
}
}
/// Set the thread name. This name must conform to the [naming @@ -106,6 +108,11 @@ impl Builder {
self.unicast = true;
self
}
+ // Set scheduling policy for the thread
+ pub fn sched(mut self, policy: impl sched::PolicyParam) -> Self { + self.sched = policy.to_attr();
+ self
+ }
/// Attach the calling thread to the EVL core, consuming the /// builder.
///
@@ -257,11 +264,23 @@ impl Thread {
evl_attach_thread(c_flags, ptr::null())
}
};
+
// evl_attach_thread() returns a valid file descriptor or -errno. - match ret {
- 0.. => return Ok(Thread(ret)),
- _ => return Err(Error::from_raw_os_error(-ret)),
- };
+ if ret < 0 {
+ return Err(Error::from_raw_os_error(-ret));
+ }
+
+ let thread = Thread(ret);
+
+ if builder.sched.0.sched_policy as i32 != 0 {
+ let c_attrs_ptr: *const evl_sched_attrs = &builder.sched.0; + let ret: c_int = unsafe { evl_set_schedattr(thread.0, c_attrs_ptr) }; + if ret != 0 {
+ return Err(Error::from_raw_os_error(-ret));
+ }
+ }
+
+ Ok(thread)
}
/// Unblock the target thread.
///
Quoted 77 lines · Jussi Viiri
When spawning threads with Builder, it wasn't possible to set the scheduling policy of the spawned thread as you don't have access to the Thread object after spawning. Added that option to Builder.
Signed-off-by Jussi Viiri <[email protected]>---
src/thread.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)src/thread.rs +18
-4@@ -11,6 +11,7 @@ use std::ptr;use std::os::raw::c_int;use std::io::Error ;use std::ffi::CString ; +use evl_sys::SchedPolicy ;use evl_sys::{ evl_attach_thread, evl_unblock_thread,@@ -29,6 +30,7 @@ pub struct Builder { visible: bool, observable: bool, unicast: bool, + sched: sched::SchedAttrs, }impl Builder {@@ -53,6 +55,7 @@ impl Builder { visible: false, observable: false, unicast: false, + sched: sched::get_zero_attrs(), } }/// Set the thread name. This name must conform to the [naming @@ -106,6 +109,11 @@ impl Builder { self.unicast =true ; self } +// Set scheduling policy for the thread +pub fn sched (mut self, policy:impl sched::PolicyParam ) ->Self { + self.sched = policy.to_attr (); + self + }/// Attach the calling thread to the EVL core, consuming the /// builder. /// @@ -257,11 +265,24 @@ impl Thread {evl_attach_thread (c_flags, ptr::null ()) } }; +// evl_attach_thread() returns a valid file descriptor or -errno. -match ret { -0 .. =>return Ok (Thread (ret)), - _ =>return Err (Error ::from_raw_os_error (-ret)), +let thread =Thread (ret); + +if builder.sched.0 .sched_policyas i32 !=0 { +let c_attrs_ptr: *const evl_sched_attrs = &builder.sched.0 ; +let ret:c_int =unsafe {evl_set_schedattr (thread.0 , c_attrs_ptr) }; +if ret !=0 { +return Err (Error ::from_raw_os_error (-ret)); } + } +Ok (thread) }/// Unblock the target thread. /// --2.34.1
Quoted 22 lines · Jussi Viiri
When spawning threads with Builder, it wasn't possible to set the scheduling policy of the spawned thread as you don't have access to the Thread object after spawning. Added that option to Builder.
Signed-off-by Jussi Viiri <[email protected]>---
src/thread.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)src/thread.rs +1
-0@@ -11,6 +11,7 @@ use std::ptr;use std::os::raw::c_int;use std::io::Error ;use std::ffi::CString ; +use evl_sys::SchedPolicy ;use evl_sys::{ evl_attach_thread, evl_unblock_thread,
src/thread.rs pub struct Builder { · line 33
Quoted 4 lines
src/thread.rs +1
-0@@ -29,6 +30,7 @@ pub struct Builder { visible: bool, observable: bool, unicast: bool, + sched: sched::SchedAttrs,
sched: Option<SchedAttrs>,
I would rather use an option here, zero as a sched_policy currently means SCHED_OTHER/NORMAL. Assuming zero is the 'unset' policy value would cause this policy setting to be ignored by Thread::attach().
Granted, we are missing the definition of SchedOther as a unit type (and its PolicyParam trait implementation) in sched.rs as well, so that we may apply SCHED_OTHER to the caller (SCHED_OTHER is an alias to (SCHED_WEAK,0) for the EVL core).
src/thread.rs impl Builder { · line 58
Quoted 7 lines
src/thread.rs +1
-0}impl Builder {@@ -53,6 +55,7 @@ impl Builder { visible: false, observable: false, unicast: false, + sched: sched::get_zero_attrs(),
sched: None,
Quoted 18 lines
src/thread.rs +5
-0} }/// Set the thread name. This name must conform to the [naming @@ -106,6 +109,11 @@ impl Builder { self.unicast =true ; self } +// Set scheduling policy for the thread +pub fn sched (mut self, policy:impl sched::PolicyParam ) ->Self { + self.sched = policy.to_attr (); + self + }/// Attach the calling thread to the EVL core, consuming the /// builder. /// @@ -257,11 +265,24 @@ impl Thread {evl_attach_thread (c_flags, ptr::null ()) }
src/thread.rs impl Thread { · line 273
Quoted 10 lines
src/thread.rs +5
-3}; +// evl_attach_thread() returns a valid file descriptor or -errno. -match ret { -0 .. =>return Ok (Thread (ret)), - _ =>return Err (Error ::from_raw_os_error (-ret)), +let thread =Thread (ret); + +if builder.sched.0 .sched_policyas i32 !=0 { +let c_attrs_ptr: *const evl_sched_attrs = &builder.sched.0 ;
Consequently, we could have something a bit more idiomatic instead:
if let Some(attrs) = builder.sched {
let c_attrs_ptr: *const evl_sched_attrs = &attrs.0;
...
}Signature · 1 line
Philippe.
Quoted 1 line · Jussi Viiri
When spawning threads with Builder, it wasn't possible to set the scheduling policy of the spawned thread as you don't have access to the Thread object after spawning. Added that option to Builder.
Thread::attach() returns Result<Self, Error>, so you could still invoke Thread::attach().unwrap().set_sched(...) afterwards, I believe.
Signature · 1 line
Philippe.