sched/signal/sig_nanosleep: fix the clock_nanosleep posix case

1. make the clock_nanosleep can pass
ltp/open_posix_testsuite/clock_nanosleep 13-1, 10-1, 9-1 cases
2. the modification are referred to https://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_nanosleep.html

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
This commit is contained in:
guoshichao 2023-07-11 20:56:40 +08:00 committed by Xiang Xiao
parent 8abf803a1d
commit 0092b3e30f
2 changed files with 27 additions and 8 deletions

View File

@ -24,6 +24,7 @@
#include <nuttx/config.h>
#include <errno.h>
#include <time.h>
/****************************************************************************
@ -80,10 +81,22 @@
int nanosleep(FAR const struct timespec *rqtp, FAR struct timespec *rmtp)
{
int ret;
/* Calling clock_nanosleep() with the value TIMER_ABSTIME not set in the
* flags argument and with a clock_id of CLOCK_REALTIME is equivalent t
* calling nanosleep() with the same rqtp and rmtp arguments.
* As clock_nanosleep() method return errno on fail, which is not
* compatible with nanosleep(), the nanosleep() need to return -1 on fail,
* so we need to convert the return value.
*/
return clock_nanosleep(CLOCK_REALTIME, 0, rqtp, rmtp);
ret = clock_nanosleep(CLOCK_REALTIME, 0, rqtp, rmtp);
if (ret != 0)
{
set_errno(ret);
ret = ERROR;
}
return ret;
}

View File

@ -244,12 +244,13 @@ int nxsig_nanosleep(FAR const struct timespec *rqtp,
* actually slept). If the rmtp argument is NULL, the remaining time is not
* returned.
*
* If clock_nanosleep() fails, it returns a value of -1 and sets errno to
* indicate the error. The clock_nanosleep() function will fail if:
* If clock_nanosleep() fails, it returns a value of errno. The
* clock_nanosleep() function will fail if:
*
* EINTR - The clock_nanosleep() function was interrupted by a signal.
* EINVAL - The rqtp argument specified a nanosecond value less than
* zero or greater than or equal to 1000 million.
* zero or greater than or equal to 1000 million. Or the clockid that
* does not specify a known clock.
* ENOSYS - The clock_nanosleep() function is not supported by this
* implementation.
*
@ -265,6 +266,12 @@ int clock_nanosleep(clockid_t clockid, int flags,
enter_cancellation_point();
if (clockid < CLOCK_REALTIME || clockid > CLOCK_BOOTTIME)
{
leave_cancellation_point();
return EINVAL;
}
/* Check if absolute time is selected */
if ((flags & TIMER_ABSTIME) != 0)
@ -286,7 +293,7 @@ int clock_nanosleep(clockid_t clockid, int flags,
leave_critical_section(irqstate);
leave_cancellation_point();
return ERROR;
return -ret;
}
clock_timespec_subtract(rqtp, &now, &reltime);
@ -312,10 +319,9 @@ int clock_nanosleep(clockid_t clockid, int flags,
if (ret < 0)
{
/* If not set the errno variable and return -1 */
/* If not return the errno */
set_errno(-ret);
ret = ERROR;
ret = -ret;
}
leave_cancellation_point();