Skip to content

Commit 0995e17

Browse files
no1wudixiaoxiang781216
authored andcommitted
sched: Check for zero sleep time and yield CPU if
necessary Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
1 parent d410a6e commit 0995e17

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

sched/signal/sig_nanosleep.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,24 @@ int nxsig_nanosleep(FAR const struct timespec *rqtp,
103103
return -EINVAL;
104104
}
105105

106+
/* If rqtp is zero, yield CPU and return
107+
* Notice: The behavior of sleep(0) is not defined in POSIX, so there are
108+
* different implementations:
109+
* 1. In Linux, nanosleep(0) will call schedule() to yield CPU:
110+
* https://elixir.bootlin.com/linux/latest/source/kernel/time/
111+
* hrtimer.c#L2038
112+
* 2. In BSD, nanosleep(0) will return immediately:
113+
* https://github.com/freebsd/freebsd-src/blob/
114+
* 475fa89800086718bd9249fd4dc3f862549f1f78/crypto/openssh/
115+
* openbsd-compat/bsd-misc.c#L243
116+
*/
117+
118+
if (rqtp->tv_sec == 0 && rqtp->tv_nsec == 0)
119+
{
120+
sched_yield();
121+
return OK;
122+
}
123+
106124
/* Get the start time of the wait. Interrupts are disabled to prevent
107125
* timer interrupts while we do tick-related calculations before and
108126
* after the wait.

0 commit comments

Comments
 (0)