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

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


The following commit(s) were added to refs/heads/master by this push:
     new 0995e17927 sched: Check for zero sleep time and yield CPU if necessary
0995e17927 is described below

commit 0995e17927c70f32b9ce51d2bc774f7c6ad3c29f
Author: Huang Qi <[email protected]>
AuthorDate: Mon Nov 6 12:22:27 2023 +0800

    sched: Check for zero sleep time and yield CPU if
    necessary
    
    Signed-off-by: Huang Qi <[email protected]>
---
 sched/signal/sig_nanosleep.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/sched/signal/sig_nanosleep.c b/sched/signal/sig_nanosleep.c
index 8fca6f9f7b..30e3220fe6 100644
--- a/sched/signal/sig_nanosleep.c
+++ b/sched/signal/sig_nanosleep.c
@@ -103,6 +103,24 @@ int nxsig_nanosleep(FAR const struct timespec *rqtp,
       return -EINVAL;
     }
 
+  /* If rqtp is zero, yield CPU and return
+   * Notice: The behavior of sleep(0) is not defined in POSIX, so there are
+   * different implementations:
+   * 1. In Linux, nanosleep(0) will call schedule() to yield CPU:
+   *    https://elixir.bootlin.com/linux/latest/source/kernel/time/
+   *    hrtimer.c#L2038
+   * 2. In BSD, nanosleep(0) will return immediately:
+   *    https://github.com/freebsd/freebsd-src/blob/
+   *    475fa89800086718bd9249fd4dc3f862549f1f78/crypto/openssh/
+   *    openbsd-compat/bsd-misc.c#L243
+   */
+
+  if (rqtp->tv_sec == 0 && rqtp->tv_nsec == 0)
+    {
+      sched_yield();
+      return OK;
+    }
+
   /* Get the start time of the wait.  Interrupts are disabled to prevent
    * timer interrupts while we do tick-related calculations before and
    * after the wait.

Reply via email to