2013-12-13 10:12:33 +08:00
|
|
|
/****************************************************************************
|
2021-03-09 05:39:04 +08:00
|
|
|
* sched/signal/sig_nanosleep.c
|
2013-12-13 10:12:33 +08:00
|
|
|
*
|
2024-09-11 19:45:11 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-08 23:33:58 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2013-12-13 10:12:33 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-12-13 10:12:33 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2013-12-13 10:12:33 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/clock.h>
|
2016-02-14 22:17:46 +08:00
|
|
|
#include <nuttx/irq.h>
|
2017-10-06 22:28:20 +08:00
|
|
|
#include <nuttx/signal.h>
|
2016-12-10 23:08:26 +08:00
|
|
|
#include <nuttx/cancelpt.h>
|
2013-12-13 10:12:33 +08:00
|
|
|
|
2014-08-09 04:43:02 +08:00
|
|
|
#include "clock/clock.h"
|
2013-12-13 10:12:33 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-10-06 22:28:20 +08:00
|
|
|
* Name: nxsig_nanosleep
|
2013-12-13 10:12:33 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2017-10-06 22:28:20 +08:00
|
|
|
* The nxsig_nanosleep() function causes the current thread to be
|
|
|
|
* suspended from execution until either the time interval specified by
|
|
|
|
* the rqtp argument has elapsed or a signal is delivered to the calling
|
|
|
|
* thread and its action is to invoke a signal-catching function or to
|
|
|
|
* terminate the process. The suspension time may be longer than requested
|
|
|
|
* because the argument value is rounded up to an integer multiple of the
|
|
|
|
* sleep resolution or because of the scheduling of other activity by the
|
2013-12-13 10:12:33 +08:00
|
|
|
* system. But, except for the case of being interrupted by a signal, the
|
|
|
|
* suspension time will not be less than the time specified by rqtp, as
|
|
|
|
* measured by the system clock, CLOCK_REALTIME.
|
|
|
|
*
|
2017-10-06 22:28:20 +08:00
|
|
|
* The use of the nxsig_nanosleep() function has no effect on the action
|
|
|
|
* or blockage of any signal.
|
2013-12-13 10:12:33 +08:00
|
|
|
*
|
2018-03-13 23:52:27 +08:00
|
|
|
* Input Parameters:
|
2013-12-13 10:12:33 +08:00
|
|
|
* rqtp - The amount of time to be suspended from execution.
|
|
|
|
* rmtp - If the rmtp argument is non-NULL, the timespec structure
|
|
|
|
* referenced by it is updated to contain the amount of time
|
|
|
|
* remaining in the interval (the requested time minus the time
|
|
|
|
* actually slept)
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-10-06 22:28:20 +08:00
|
|
|
* If the nxsig_nanosleep() function returns because the requested time
|
|
|
|
* has elapsed, its return value is zero.
|
2013-12-13 10:12:33 +08:00
|
|
|
*
|
2017-10-06 22:28:20 +08:00
|
|
|
* If the nxsig_nanosleep() function returns because it has been
|
|
|
|
* interrupted by a signal, the function returns a negated errno value
|
|
|
|
* indicate the interruption. If the rmtp argument is non-NULL, the
|
|
|
|
* timespec structure referenced by it is updated to contain the amount
|
|
|
|
* of time remaining in the interval (the requested time minus the time
|
|
|
|
* actually slept). If the rmtp argument is NULL, the remaining time is
|
|
|
|
* not returned.
|
2013-12-13 10:12:33 +08:00
|
|
|
*
|
2017-10-06 22:28:20 +08:00
|
|
|
* If nxsig_nanosleep() fails, it returns a negated errno indicating the
|
|
|
|
* cause of the failure. The nxsig_nanosleep() function will fail if:
|
2013-12-13 10:12:33 +08:00
|
|
|
*
|
2017-10-06 22:28:20 +08:00
|
|
|
* EINTR - The nxsig_nanosleep() function was interrupted by a signal.
|
2013-12-13 10:12:33 +08:00
|
|
|
* EINVAL - The rqtp argument specified a nanosecond value less than
|
2013-12-13 21:57:13 +08:00
|
|
|
* zero or greater than or equal to 1000 million.
|
2017-10-06 22:28:20 +08:00
|
|
|
* ENOSYS - The nxsig_nanosleep() function is not supported by this
|
2013-12-13 21:57:13 +08:00
|
|
|
* implementation.
|
2013-12-13 10:12:33 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-10-06 22:28:20 +08:00
|
|
|
int nxsig_nanosleep(FAR const struct timespec *rqtp,
|
|
|
|
FAR struct timespec *rmtp)
|
2013-12-13 10:12:33 +08:00
|
|
|
{
|
|
|
|
int ret;
|
2016-12-10 06:50:34 +08:00
|
|
|
|
2024-06-11 22:05:35 +08:00
|
|
|
ret = nxsig_clockwait(CLOCK_REALTIME, 0, rqtp, rmtp);
|
2016-12-10 06:50:34 +08:00
|
|
|
|
2024-06-11 22:05:35 +08:00
|
|
|
return ret == -EAGAIN ? 0 : ret;
|
2017-10-06 22:28:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-11-12 07:44:59 +08:00
|
|
|
* Name: clock_nanosleep
|
2017-10-06 22:28:20 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2017-11-12 07:44:59 +08:00
|
|
|
* If the flag TIMER_ABSTIME is not set in the flags argument, the
|
|
|
|
* clock_nanosleep() function will cause the current thread to be suspended
|
|
|
|
* from execution until either the time interval specified by the rqtp
|
|
|
|
* argument has elapsed, or a signal is delivered to the calling thread
|
|
|
|
* and its action is to invoke a signal-catching function, or the process
|
|
|
|
* is terminated. The clock used to measure the time will be the clock
|
|
|
|
* specified by clock_id.
|
|
|
|
*
|
|
|
|
* If the flag TIMER_ABSTIME is set in the flags argument, the
|
|
|
|
* clock_nanosleep() function will cause the current thread to be
|
|
|
|
* suspended from execution until either the time value of the clock
|
|
|
|
* specified by clock_id reaches the absolute time specified by the rqtp
|
|
|
|
* argument, or a signal is delivered to the calling thread and its action
|
2020-03-10 12:50:49 +08:00
|
|
|
* is to invoke a signal-catching function, or the process is terminated.
|
|
|
|
* If, at the time of the call, the time value specified by rqtp is less
|
|
|
|
* than or equal to the time value of the specified clock, then
|
|
|
|
* clock_nanosleep() will return immediately and the calling process will
|
|
|
|
* not be suspended.
|
2017-11-12 07:44:59 +08:00
|
|
|
*
|
|
|
|
* The suspension time caused by this function may be longer than requested
|
|
|
|
* because the argument value is rounded up to an integer multiple of the
|
|
|
|
* sleep resolution, or because of the scheduling of other activity by the
|
2017-10-06 22:28:20 +08:00
|
|
|
* system. But, except for the case of being interrupted by a signal, the
|
2017-11-12 07:44:59 +08:00
|
|
|
* suspension time for the relative clock_nanosleep() function (that is,
|
|
|
|
* with the TIMER_ABSTIME flag not set) will not be less than the time
|
|
|
|
* interval specified by rqtp, as measured by the corresponding clock. The
|
|
|
|
* suspension for the absolute clock_nanosleep() function (that is, with
|
|
|
|
* the TIMER_ABSTIME flag set) will be in effect at least until the value
|
|
|
|
* of the corresponding clock reaches the absolute time specified by rqtp,
|
|
|
|
* except for the case of being interrupted by a signal.
|
|
|
|
*
|
|
|
|
* The use of the clock_nanosleep() function will have no effect on the
|
|
|
|
* action or blockage of any signal.
|
2017-10-06 22:28:20 +08:00
|
|
|
*
|
2017-11-12 07:44:59 +08:00
|
|
|
* The clock_nanosleep() function will fail if the clock_id argument refers
|
|
|
|
* to the CPU-time clock of the calling thread. It is unspecified whether
|
|
|
|
* clock_id values of other CPU-time clocks are allowed.
|
2017-10-06 22:28:20 +08:00
|
|
|
*
|
2018-03-13 23:52:27 +08:00
|
|
|
* Input Parameters:
|
2017-11-12 07:44:59 +08:00
|
|
|
* clockid - The clock to use to interpret the absolute time
|
|
|
|
* flags - Open flags. TIMER_ABSTIME is the only supported flag.
|
|
|
|
* rqtp - The amount of time to be suspended from execution.
|
|
|
|
* rmtp - If the rmtp argument is non-NULL, the timespec structure
|
|
|
|
* referenced by it is updated to contain the amount of time
|
|
|
|
* remaining in the interval (the requested time minus the time
|
|
|
|
* actually slept)
|
2017-10-06 22:28:20 +08:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-11-12 07:44:59 +08:00
|
|
|
* If the clock_nanosleep() function returns because the requested time has
|
2017-10-06 22:28:20 +08:00
|
|
|
* elapsed, its return value is zero.
|
|
|
|
*
|
2020-03-10 12:50:49 +08:00
|
|
|
* If the clock_nanosleep() function returns because it has been
|
|
|
|
* interrupted by a signal, the function returns a value of -1 and sets
|
|
|
|
* errno to indicate the interruption. If the rmtp argument is non-NULL,
|
|
|
|
* the timespec structure referenced by it is updated to contain the amount
|
|
|
|
* of time remaining in the interval (the requested time minus the time
|
|
|
|
* actually slept). If the rmtp argument is NULL, the remaining time is not
|
2017-10-06 22:28:20 +08:00
|
|
|
* returned.
|
|
|
|
*
|
2023-07-11 20:56:40 +08:00
|
|
|
* If clock_nanosleep() fails, it returns a value of errno. The
|
|
|
|
* clock_nanosleep() function will fail if:
|
2017-10-06 22:28:20 +08:00
|
|
|
*
|
2017-11-12 07:44:59 +08:00
|
|
|
* EINTR - The clock_nanosleep() function was interrupted by a signal.
|
2017-10-06 22:28:20 +08:00
|
|
|
* EINVAL - The rqtp argument specified a nanosecond value less than
|
2023-07-11 20:56:40 +08:00
|
|
|
* zero or greater than or equal to 1000 million. Or the clockid that
|
|
|
|
* does not specify a known clock.
|
2017-11-12 07:44:59 +08:00
|
|
|
* ENOSYS - The clock_nanosleep() function is not supported by this
|
2017-10-06 22:28:20 +08:00
|
|
|
* implementation.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-12 07:44:59 +08:00
|
|
|
int clock_nanosleep(clockid_t clockid, int flags,
|
|
|
|
FAR const struct timespec *rqtp,
|
|
|
|
FAR struct timespec *rmtp)
|
2017-10-06 22:28:20 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2017-11-12 07:44:59 +08:00
|
|
|
/* clock_nanosleep() is a cancellation point */
|
2017-10-06 22:28:20 +08:00
|
|
|
|
2020-01-03 00:49:34 +08:00
|
|
|
enter_cancellation_point();
|
2017-10-06 22:28:20 +08:00
|
|
|
|
2023-07-11 20:56:40 +08:00
|
|
|
if (clockid < CLOCK_REALTIME || clockid > CLOCK_BOOTTIME)
|
|
|
|
{
|
|
|
|
leave_cancellation_point();
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2024-06-11 22:05:35 +08:00
|
|
|
/* Just a wrapper around nxsig_clockwait() */
|
2017-11-12 07:44:59 +08:00
|
|
|
|
2024-06-11 22:05:35 +08:00
|
|
|
ret = nxsig_clockwait(clockid, flags, rqtp, rmtp);
|
2017-11-12 08:01:28 +08:00
|
|
|
|
2024-06-11 22:05:35 +08:00
|
|
|
/* Check if nxsig_clockwait() succeeded */
|
2017-11-12 07:44:59 +08:00
|
|
|
|
2024-06-11 22:05:35 +08:00
|
|
|
if (ret == -EAGAIN)
|
2017-11-12 07:44:59 +08:00
|
|
|
{
|
2024-06-11 22:05:35 +08:00
|
|
|
ret = OK;
|
2017-11-12 07:44:59 +08:00
|
|
|
}
|
2024-06-11 22:05:35 +08:00
|
|
|
else if (ret < 0)
|
2017-10-06 22:28:20 +08:00
|
|
|
{
|
2023-07-11 20:56:40 +08:00
|
|
|
/* If not return the errno */
|
2017-11-12 08:01:28 +08:00
|
|
|
|
2023-07-11 20:56:40 +08:00
|
|
|
ret = -ret;
|
2017-10-06 22:28:20 +08:00
|
|
|
}
|
2013-12-13 21:57:13 +08:00
|
|
|
|
2016-12-10 06:50:34 +08:00
|
|
|
leave_cancellation_point();
|
2017-10-06 22:28:20 +08:00
|
|
|
return ret;
|
2013-12-13 10:12:33 +08:00
|
|
|
}
|