Skip to content

Commit

Permalink
sched: Check for zero sleep time and yield CPU if
Browse files Browse the repository at this point in the history
necessary

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
  • Loading branch information
no1wudi authored and xiaoxiang781216 committed Nov 7, 2023
1 parent d410a6e commit 0995e17
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions sched/signal/sig_nanosleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 0995e17

Please sign in to comment.