This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 36f32318dc39d199bde639550fc3b8d762127ca3
Author: ganjing <[email protected]>
AuthorDate: Mon Nov 17 20:49:05 2025 +0800

    !sched: Make the SCHED_* values match the TCB policy bits.
    
    The SCHED_* values were one more than the policy bits that the TCB
    stores, so every policy had to be translated between the two.
    
    Renumber SCHED_FIFO, SCHED_RR, SCHED_SPORADIC and SCHED_OTHER to the
    values 0 to 3, the same as the policy bits of the TCB, and define
    TCB_FLAG_SCHED_OTHER for the value 3:
    * nxsched_get_scheduler() returns the policy bits directly instead of
      adding one.
    * nxsched_set_scheduler() handles SCHED_OTHER, SCHED_FIFO and SCHED_RR
      in one path, and maps SCHED_OTHER to SCHED_FIFO also when
      CONFIG_RR_INTERVAL is 0.
    * sched_get_priority_min() returns EINVAL for an unknown policy instead
      of failing an assertion, as sched_get_priority_max() already does.
    
    BREAKING CHANGE: The values of SCHED_FIFO, SCHED_RR, SCHED_SPORADIC,
    SCHED_OTHER and SCHED_NORMAL changed.  Code that uses the symbolic
    names only needs to be rebuilt.  Code that stores or exchanges the
    numeric values must be updated.
    
    Assisted-by: Kiro:auto
    Signed-off-by: ganjing <[email protected]>
---
 include/nuttx/sched.h                  |  1 +
 include/sched.h                        | 10 ++++----
 libs/libc/sched/sched_getprioritymax.c |  2 +-
 libs/libc/sched/sched_getprioritymin.c |  7 +++++-
 sched/sched/sched_getscheduler.c       | 10 +++-----
 sched/sched/sched_setscheduler.c       | 45 ++++++++++++++++------------------
 6 files changed, 37 insertions(+), 38 deletions(-)

diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h
index 5bdea2c168b..fa5883b350f 100644
--- a/include/nuttx/sched.h
+++ b/include/nuttx/sched.h
@@ -100,6 +100,7 @@
 #  define TCB_FLAG_SCHED_FIFO      (0 << TCB_FLAG_POLICY_SHIFT)  /* FIFO 
scheding policy */
 #  define TCB_FLAG_SCHED_RR        (1 << TCB_FLAG_POLICY_SHIFT)  /* Round 
robin scheding policy */
 #  define TCB_FLAG_SCHED_SPORADIC  (2 << TCB_FLAG_POLICY_SHIFT)  /* Sporadic 
scheding policy */
+#  define TCB_FLAG_SCHED_OTHER     (3 << TCB_FLAG_POLICY_SHIFT)  /* Other 
scheduling policy */
 #define TCB_FLAG_CPU_LOCKED        (1 << 5)                      /* Bit 5: 
Locked to this CPU */
 #define TCB_FLAG_SIGNAL_ACTION     (1 << 6)                      /* Bit 6: In 
a signal handler */
 #define TCB_FLAG_SYSCALL           (1 << 7)                      /* Bit 7: In 
a system call */
diff --git a/include/sched.h b/include/sched.h
index e064b5189bf..831ef43751c 100644
--- a/include/sched.h
+++ b/include/sched.h
@@ -42,11 +42,11 @@
 
 /* POSIX-like scheduling policies */
 
-#define SCHED_NORMAL              0  /* Alias to SCHED_OTHER */
-#define SCHED_OTHER               0  /* Map to SCHED_FIFO or SCHED_RR */
-#define SCHED_FIFO                1  /* FIFO priority scheduling policy */
-#define SCHED_RR                  2  /* Round robin scheduling policy */
-#define SCHED_SPORADIC            3  /* Sporadic scheduling policy */
+#define SCHED_FIFO                0  /* FIFO priority scheduling policy */
+#define SCHED_RR                  1  /* Round robin scheduling policy */
+#define SCHED_SPORADIC            2  /* Sporadic scheduling policy */
+#define SCHED_OTHER               3  /* Map to SCHED_FIFO or SCHED_RR */
+#define SCHED_NORMAL              3  /* Alias to SCHED_OTHER */
 #define SCHED_BATCH               4  /* Batch scheduling policy */
 #define SCHED_IDLE                5  /* Idle scheduling policy */
 
diff --git a/libs/libc/sched/sched_getprioritymax.c 
b/libs/libc/sched/sched_getprioritymax.c
index c8db4327ce1..569fba524dc 100644
--- a/libs/libc/sched/sched_getprioritymax.c
+++ b/libs/libc/sched/sched_getprioritymax.c
@@ -54,7 +54,7 @@
 
 int sched_get_priority_max(int policy)
 {
-  if (policy < SCHED_OTHER || policy > SCHED_SPORADIC)
+  if (policy < SCHED_FIFO || policy > SCHED_OTHER)
     {
       set_errno(EINVAL);
       return ERROR;
diff --git a/libs/libc/sched/sched_getprioritymin.c 
b/libs/libc/sched/sched_getprioritymin.c
index e1c3fb58cb0..4f574df353e 100644
--- a/libs/libc/sched/sched_getprioritymin.c
+++ b/libs/libc/sched/sched_getprioritymin.c
@@ -54,6 +54,11 @@
 
 int sched_get_priority_min(int policy)
 {
-  DEBUGASSERT(policy >= SCHED_OTHER && policy <= SCHED_SPORADIC);
+  if (policy < SCHED_FIFO || policy > SCHED_OTHER)
+    {
+      set_errno(EINVAL);
+      return ERROR;
+    }
+
   return SCHED_PRIORITY_MIN;
 }
diff --git a/sched/sched/sched_getscheduler.c b/sched/sched/sched_getscheduler.c
index 379aaa4eaaf..86d5cb00b3a 100644
--- a/sched/sched/sched_getscheduler.c
+++ b/sched/sched/sched_getscheduler.c
@@ -72,7 +72,6 @@ int nxsched_get_scheduler(pid_t pid)
 {
   FAR struct tcb_s *tcb;
   int ret = -ESRCH;
-  int policy;
 
   /* Verify that the PID corresponds to a real task */
 
@@ -87,13 +86,9 @@ int nxsched_get_scheduler(pid_t pid)
 
   if (tcb != NULL)
     {
-      /* Return the scheduling policy from the TCB.  NOTE that the user-
-       * interpretable values are 1 based; the TCB values are zero-based.
-       */
+      /* Return the scheduling policy from the TCB */
 
-      policy = (tcb->flags & TCB_FLAG_POLICY_MASK) >> TCB_FLAG_POLICY_SHIFT;
-
-      ret = policy + 1;
+      ret = (tcb->flags & TCB_FLAG_POLICY_MASK) >> TCB_FLAG_POLICY_SHIFT;
     }
 
   return ret;
@@ -126,6 +121,7 @@ int nxsched_get_scheduler(pid_t pid)
 int sched_getscheduler(pid_t pid)
 {
   int ret = nxsched_get_scheduler(pid);
+
   if (ret < 0)
     {
       set_errno(-ret);
diff --git a/sched/sched/sched_setscheduler.c b/sched/sched/sched_setscheduler.c
index e17c28141ad..2aee700e573 100644
--- a/sched/sched/sched_setscheduler.c
+++ b/sched/sched/sched_setscheduler.c
@@ -189,7 +189,16 @@ int nxsched_set_scheduler(pid_t pid, int policy,
 
           switch (policy)
             {
+              case SCHED_OTHER:
+#if CONFIG_RR_INTERVAL > 0
+                policy = SCHED_RR;
+#else
+                policy = SCHED_FIFO;
+#endif
               case SCHED_FIFO:
+#if CONFIG_RR_INTERVAL > 0
+              case SCHED_RR:
+#endif
 #ifdef CONFIG_SCHED_SPORADIC
                 /* Cancel any on-going sporadic scheduling */
 
@@ -200,35 +209,23 @@ int nxsched_set_scheduler(pid_t pid, int policy,
                   }
 #endif
 
-                /* Save the FIFO scheduling parameters */
+                /* Set the new policy flag */
 
-                tcb->flags     &= ~TCB_FLAG_POLICY_MASK;
-                tcb->flags     |= TCB_FLAG_SCHED_FIFO;
-#if CONFIG_RR_INTERVAL > 0 || defined(CONFIG_SCHED_SPORADIC)
-                tcb->timeslice  = 0;
-#endif
-                break;
-
-#if CONFIG_RR_INTERVAL > 0
-              case SCHED_OTHER:
-              case SCHED_RR:
-#  ifdef CONFIG_SCHED_SPORADIC
-                /* Cancel any on-going sporadic scheduling */
+                tcb->flags &= ~TCB_FLAG_POLICY_MASK;
+                tcb->flags |= policy << TCB_FLAG_POLICY_SHIFT;
 
-                if ((tcb->flags & TCB_FLAG_POLICY_MASK) ==
-                    TCB_FLAG_SCHED_SPORADIC)
+                /* Configure timeslice based on policy */
+#if CONFIG_RR_INTERVAL > 0 || defined(CONFIG_SCHED_SPORADIC)
+                if (policy == SCHED_FIFO)
                   {
-                    DEBUGVERIFY(nxsched_stop_sporadic(tcb));
+                    tcb->timeslice = 0;
+                  }
+                else
+                  {
+                    tcb->timeslice = MSEC2TICK(CONFIG_RR_INTERVAL);
                   }
-#  endif
-
-                /* Save the round robin scheduling parameters */
-
-                tcb->flags     &= ~TCB_FLAG_POLICY_MASK;
-                tcb->flags     |= TCB_FLAG_SCHED_RR;
-                tcb->timeslice  = MSEC2TICK(CONFIG_RR_INTERVAL);
-                break;
 #endif
+                break;
 
 #ifdef CONFIG_SCHED_SPORADIC
               case SCHED_SPORADIC:

Reply via email to