All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] revl: Add sched() to thread::Builder
@ 2022-09-21 18:39 ` Jussi Viiri
  2022-09-21 20:23   ` Jussi Viiri
                     ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Jussi Viiri @ 2022-09-21 18:39 UTC (permalink / raw)
  To: xenomai

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 <ilmai@iki.fi>

---

  src/thread.rs | 27 ++++++++++++++++++++++++---
  1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/src/thread.rs b/src/thread.rs
index d487658..b0300af 100644
--- a/src/thread.rs
+++ b/src/thread.rs
@@ -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_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.
      ///
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] revl: Add sched() to thread::Builder
  2022-09-21 18:39 ` [PATCH] revl: Add sched() to thread::Builder Jussi Viiri
@ 2022-09-21 20:23   ` Jussi Viiri
  2022-09-26  7:13   ` Philippe Gerum
  2022-09-26  7:46   ` Philippe Gerum
  2 siblings, 0 replies; 4+ messages in thread
From: Jussi Viiri @ 2022-09-21 20:23 UTC (permalink / raw)
  To: xenomai

Looks like I accidentally submitted the wrong patch. Here's the correct one:

--- a/src/thread.rs
+++ b/src/thread.rs
@@ -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.
      ///

On 21.9.2022 21.39, Jussi Viiri wrote:
> 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 <ilmai@iki.fi>
>
> ---
>
>    src/thread.rs | 27 ++++++++++++++++++++++++---
>    1 file changed, 24 insertions(+), 3 deletions(-)
>
> diff --git a/src/thread.rs b/src/thread.rs
> index d487658..b0300af 100644
> --- a/src/thread.rs
> +++ b/src/thread.rs
> @@ -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_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.
>        ///
> --
> 2.34.1
>
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] revl: Add sched() to thread::Builder
  2022-09-21 18:39 ` [PATCH] revl: Add sched() to thread::Builder Jussi Viiri
  2022-09-21 20:23   ` Jussi Viiri
@ 2022-09-26  7:13   ` Philippe Gerum
  2022-09-26  7:46   ` Philippe Gerum
  2 siblings, 0 replies; 4+ messages in thread
From: Philippe Gerum @ 2022-09-26  7:13 UTC (permalink / raw)
  To: Jussi Viiri; +Cc: xenomai


Jussi Viiri <ilmai@iki.fi> writes:

> 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 <ilmai@iki.fi>
>
> ---
>
>  src/thread.rs | 27 ++++++++++++++++++++++++---
>  1 file changed, 24 insertions(+), 3 deletions(-)
>
> diff --git a/src/thread.rs b/src/thread.rs
> index d487658..b0300af 100644
> --- a/src/thread.rs
> +++ b/src/thread.rs
> @@ -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,

       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).

>  }
>    impl Builder {
> @@ -53,6 +55,7 @@ impl Builder {
>              visible: false,
>              observable: false,
>              unicast: false,
> +            sched: sched::get_zero_attrs(),

               sched: None,

>          }
>      }
>      /// 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_policy as 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;
               ...
       }

-- 
Philippe.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] revl: Add sched() to thread::Builder
  2022-09-21 18:39 ` [PATCH] revl: Add sched() to thread::Builder Jussi Viiri
  2022-09-21 20:23   ` Jussi Viiri
  2022-09-26  7:13   ` Philippe Gerum
@ 2022-09-26  7:46   ` Philippe Gerum
  2 siblings, 0 replies; 4+ messages in thread
From: Philippe Gerum @ 2022-09-26  7:46 UTC (permalink / raw)
  To: Jussi Viiri; +Cc: xenomai


Jussi Viiri <ilmai@iki.fi> writes:

> 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.

-- 
Philippe.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-09-26  7:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <gTXdwUk_bKng2MQYwbdY-wCvewR2NZOyi9XxSpkGlR0GL4SerQi2RIneMs5e421_D69U1vjgdpCUDWfmQW56Bw==@protonmail.internalid>
2022-09-21 18:39 ` [PATCH] revl: Add sched() to thread::Builder Jussi Viiri
2022-09-21 20:23   ` Jussi Viiri
2022-09-26  7:13   ` Philippe Gerum
2022-09-26  7:46   ` Philippe Gerum

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.