2013-12-13 03:12:33 +01:00
|
|
|
/****************************************************************************
|
2014-08-09 00:57:47 +02:00
|
|
|
* sched/signal/sig/nanosleep.c
|
2013-12-13 03:12:33 +01:00
|
|
|
*
|
2017-10-06 16:28:20 +02:00
|
|
|
* Copyright (C) 2013, 2016-2017 Gregory Nutt. All rights reserved.
|
2013-12-13 03:12:33 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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 15:17:46 +01:00
|
|
|
#include <nuttx/irq.h>
|
2017-10-06 16:28:20 +02:00
|
|
|
#include <nuttx/signal.h>
|
2016-12-10 16:08:26 +01:00
|
|
|
#include <nuttx/cancelpt.h>
|
2013-12-13 03:12:33 +01:00
|
|
|
|
2014-08-08 22:43:02 +02:00
|
|
|
#include "clock/clock.h"
|
2013-12-13 03:12:33 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-10-06 16:28:20 +02:00
|
|
|
* Name: nxsig_nanosleep
|
2013-12-13 03:12:33 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2017-10-06 16:28:20 +02: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 03:12:33 +01: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 16:28:20 +02:00
|
|
|
* The use of the nxsig_nanosleep() function has no effect on the action
|
|
|
|
* or blockage of any signal.
|
2013-12-13 03:12:33 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2013-12-13 03:12:33 +01: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 16:28:20 +02:00
|
|
|
* If the nxsig_nanosleep() function returns because the requested time
|
|
|
|
* has elapsed, its return value is zero.
|
2013-12-13 03:12:33 +01:00
|
|
|
*
|
2017-10-06 16:28:20 +02: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 03:12:33 +01:00
|
|
|
*
|
2017-10-06 16:28:20 +02: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 03:12:33 +01:00
|
|
|
*
|
2017-10-06 16:28:20 +02:00
|
|
|
* EINTR - The nxsig_nanosleep() function was interrupted by a signal.
|
2013-12-13 03:12:33 +01:00
|
|
|
* EINVAL - The rqtp argument specified a nanosecond value less than
|
2013-12-13 14:57:13 +01:00
|
|
|
* zero or greater than or equal to 1000 million.
|
2017-10-06 16:28:20 +02:00
|
|
|
* ENOSYS - The nxsig_nanosleep() function is not supported by this
|
2013-12-13 14:57:13 +01:00
|
|
|
* implementation.
|
2013-12-13 03:12:33 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-10-06 16:28:20 +02:00
|
|
|
int nxsig_nanosleep(FAR const struct timespec *rqtp,
|
|
|
|
FAR struct timespec *rmtp)
|
2013-12-13 03:12:33 +01:00
|
|
|
{
|
|
|
|
irqstate_t flags;
|
2018-06-16 20:16:13 +02:00
|
|
|
clock_t starttick;
|
2013-12-13 03:12:33 +01:00
|
|
|
sigset_t set;
|
|
|
|
int ret;
|
2016-12-09 23:50:34 +01:00
|
|
|
|
2017-10-06 16:28:20 +02:00
|
|
|
/* Sanity check */
|
2016-12-09 23:50:34 +01:00
|
|
|
|
2018-09-17 14:21:25 +02:00
|
|
|
if (rqtp == NULL || rqtp->tv_nsec < 0 || rqtp->tv_nsec >= 1000000000)
|
2013-12-13 03:12:33 +01:00
|
|
|
{
|
2017-10-06 16:28:20 +02:00
|
|
|
return -EINVAL;
|
2013-12-13 03:12:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
2013-12-13 14:57:13 +01:00
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2013-12-13 03:12:33 +01:00
|
|
|
starttick = clock_systimer();
|
|
|
|
|
|
|
|
/* Set up for the sleep. Using the empty set means that we are not
|
|
|
|
* waiting for any particular signal. However, any unmasked signal can
|
2017-10-06 16:28:20 +02:00
|
|
|
* still awaken nxsig_timedwait().
|
2013-12-13 03:12:33 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
(void)sigemptyset(&set);
|
|
|
|
|
2017-10-06 16:28:20 +02:00
|
|
|
/* nxsig_nanosleep is a simple application of nxsig_timedwait. */
|
2013-12-13 03:12:33 +01:00
|
|
|
|
2017-10-06 16:28:20 +02:00
|
|
|
ret = nxsig_timedwait(&set, NULL, rqtp);
|
2013-12-13 03:12:33 +01:00
|
|
|
|
2017-10-06 16:28:20 +02:00
|
|
|
/* nxsig_timedwait() cannot succeed. It should always return error with
|
2013-12-13 03:12:33 +01:00
|
|
|
* either (1) EAGAIN meaning that the timeout occurred, or (2) EINTR
|
|
|
|
* meaning that some other unblocked signal was caught.
|
|
|
|
*/
|
|
|
|
|
2017-10-06 16:28:20 +02:00
|
|
|
DEBUGASSERT(ret < 0 && (ret == -EAGAIN || ret == -EINTR));
|
2013-12-13 03:12:33 +01:00
|
|
|
|
2017-10-06 16:28:20 +02:00
|
|
|
if (ret == -EAGAIN)
|
2013-12-13 03:12:33 +01:00
|
|
|
{
|
|
|
|
/* The timeout "error" is the normal, successful result */
|
2014-03-21 18:22:50 +01:00
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2013-12-13 03:12:33 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2014-03-21 18:22:50 +01:00
|
|
|
/* If we get there, the wait has failed because we were awakened by a
|
2013-12-13 03:12:33 +01:00
|
|
|
* signal. Return the amount of "unwaited" time if rmtp is non-NULL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (rmtp)
|
|
|
|
{
|
2018-06-16 20:16:13 +02:00
|
|
|
clock_t elapsed;
|
|
|
|
clock_t remaining;
|
|
|
|
sclock_t ticks;
|
2013-12-13 03:12:33 +01:00
|
|
|
|
2016-08-10 15:37:25 +02:00
|
|
|
/* REVISIT: The conversion from time to ticks and back could
|
|
|
|
* be avoided. clock_timespec_subtract() would be used instead
|
|
|
|
* to get the time difference.
|
|
|
|
*/
|
|
|
|
|
2013-12-13 03:12:33 +01:00
|
|
|
/* First get the number of clock ticks that we were requested to
|
|
|
|
* wait.
|
|
|
|
*/
|
|
|
|
|
|
|
|
(void)clock_time2ticks(rqtp, &ticks);
|
|
|
|
|
|
|
|
/* Get the number of ticks that we actually waited */
|
|
|
|
|
|
|
|
elapsed = clock_systimer() - starttick;
|
|
|
|
|
|
|
|
/* The difference between the number of ticks that we were requested
|
2018-09-14 17:02:03 +02:00
|
|
|
* to wait and the number of ticks that we actually waited is that
|
2013-12-13 03:12:33 +01:00
|
|
|
* amount of time that we failed to wait.
|
|
|
|
*/
|
|
|
|
|
2018-06-16 20:16:13 +02:00
|
|
|
if (elapsed >= (clock_t)ticks)
|
2013-12-13 03:12:33 +01:00
|
|
|
{
|
|
|
|
remaining = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-16 20:16:13 +02:00
|
|
|
remaining = (clock_t)ticks - elapsed;
|
2013-12-13 03:12:33 +01:00
|
|
|
}
|
|
|
|
|
2018-06-16 20:16:13 +02:00
|
|
|
(void)clock_ticks2time((sclock_t)remaining, rmtp);
|
2013-12-13 03:12:33 +01:00
|
|
|
}
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2017-10-06 16:28:20 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-11-12 00:44:59 +01:00
|
|
|
* Name: clock_nanosleep
|
2017-10-06 16:28:20 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2017-11-12 00:44:59 +01: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
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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 16:28:20 +02:00
|
|
|
* system. But, except for the case of being interrupted by a signal, the
|
2017-11-12 00:44:59 +01: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 16:28:20 +02:00
|
|
|
*
|
2017-11-12 00:44:59 +01: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 16:28:20 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-11-12 00:44:59 +01: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 16:28:20 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-11-12 00:44:59 +01:00
|
|
|
* If the clock_nanosleep() function returns because the requested time has
|
2017-10-06 16:28:20 +02:00
|
|
|
* elapsed, its return value is zero.
|
|
|
|
*
|
2017-11-12 00:44:59 +01:00
|
|
|
* If the clock_nanosleep() function returns because it has been interrupted by
|
2017-10-06 16:28:20 +02:00
|
|
|
* 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
|
|
|
|
* returned.
|
|
|
|
*
|
2017-11-12 00:44:59 +01:00
|
|
|
* If clock_nanosleep() fails, it returns a value of -1 and sets errno to
|
|
|
|
* indicate the error. The clock_nanosleep() function will fail if:
|
2017-10-06 16:28:20 +02:00
|
|
|
*
|
2017-11-12 00:44:59 +01:00
|
|
|
* EINTR - The clock_nanosleep() function was interrupted by a signal.
|
2017-10-06 16:28:20 +02:00
|
|
|
* EINVAL - The rqtp argument specified a nanosecond value less than
|
|
|
|
* zero or greater than or equal to 1000 million.
|
2017-11-12 00:44:59 +01:00
|
|
|
* ENOSYS - The clock_nanosleep() function is not supported by this
|
2017-10-06 16:28:20 +02:00
|
|
|
* implementation.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-12 00:44:59 +01:00
|
|
|
int clock_nanosleep(clockid_t clockid, int flags,
|
|
|
|
FAR const struct timespec *rqtp,
|
|
|
|
FAR struct timespec *rmtp)
|
2017-10-06 16:28:20 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2017-11-12 00:44:59 +01:00
|
|
|
/* clock_nanosleep() is a cancellation point */
|
2017-10-06 16:28:20 +02:00
|
|
|
|
|
|
|
(void)enter_cancellation_point();
|
|
|
|
|
2017-11-12 00:44:59 +01:00
|
|
|
/* Check if absolute time is selected */
|
|
|
|
|
|
|
|
if ((flags & TIMER_ABSTIME) != 0)
|
|
|
|
{
|
|
|
|
struct timespec reltime;
|
|
|
|
struct timespec now;
|
|
|
|
irqstate_t irqstate;
|
|
|
|
|
|
|
|
/* Calculate the relative time delay. We need to enter a critical
|
|
|
|
* section early to assure the relative time is valid from this
|
|
|
|
* point in time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
irqstate = enter_critical_section();
|
|
|
|
ret = clock_gettime(clockid, &now);
|
2017-11-12 01:01:28 +01:00
|
|
|
if (ret < 0)
|
2017-11-12 00:44:59 +01:00
|
|
|
{
|
2017-11-12 01:01:28 +01:00
|
|
|
/* clock_gettime() sets the errno variable */
|
|
|
|
|
|
|
|
leave_critical_section(irqstate);
|
|
|
|
leave_cancellation_point();
|
|
|
|
return ERROR;
|
2017-11-12 00:44:59 +01:00
|
|
|
}
|
|
|
|
|
2017-11-12 01:01:28 +01:00
|
|
|
clock_timespec_subtract(rqtp, &now, &reltime);
|
|
|
|
|
|
|
|
/* Now that we have the relative time, the remaining operations
|
|
|
|
* are equivalent to nxsig_nanosleep().
|
2017-11-12 00:44:59 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
ret = nxsig_nanosleep(&reltime, rmtp);
|
|
|
|
leave_critical_section(irqstate);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* In the relative time case, clock_nanosleep() is equivalent to
|
|
|
|
* nanosleep. In this case, it is a paper thin wrapper around
|
|
|
|
* nxsig_nanosleep().
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = nxsig_nanosleep(rqtp, rmtp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if nxsig_nanosleep() succeeded */
|
2017-10-06 16:28:20 +02:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-11-12 01:01:28 +01:00
|
|
|
/* If not set the errno variable and return -1 */
|
|
|
|
|
2017-10-06 16:28:20 +02:00
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
2013-12-13 14:57:13 +01:00
|
|
|
|
2016-12-09 23:50:34 +01:00
|
|
|
leave_cancellation_point();
|
2017-10-06 16:28:20 +02:00
|
|
|
return ret;
|
2013-12-13 03:12:33 +01:00
|
|
|
}
|