include/sys/time.h, libs/libc/unistd, sched/timer: Implement alarm(), setitimer() and getitimer() APIs.

This commit is contained in:
liuhaitao 2019-11-13 08:00:45 -06:00 committed by Gregory Nutt
parent 14fcacaf27
commit 9b75ef06ea
13 changed files with 497 additions and 8 deletions

View file

@ -65,6 +65,8 @@ namespace std
using ::timer_settime;
using ::timer_gettime;
using ::timer_getoverrun;
using ::getitimer;
using ::setitimer;
}
#endif // __INCLUDE_CXX_CTIME

View file

@ -541,6 +541,12 @@ struct task_group_s
FAR char *tg_envp; /* Allocated environment strings */
#endif
#ifndef CONFIG_DISABLE_POSIX_TIMERS
/* Interval timer *************************************************************/
timer_t itimer;
#endif
/* PIC data space and address environments ************************************/
/* Logically the PIC data space belongs here (see struct dspace_s). The

View file

@ -255,11 +255,13 @@
#define SYS_clock_getres (__SYS_clock + 1)
#define SYS_clock_gettime (__SYS_clock + 2)
#define SYS_clock_settime (__SYS_clock + 3)
#define SYS_getitime (__SYS_clock + 4)
#define SYS_setitime (__SYS_clock + 5)
#ifdef CONFIG_CLOCK_TIMEKEEPING
# define SYS_adjtime (__SYS_clock + 4)
# define __SYS_timers (__SYS_clock + 5)
# define SYS_adjtime (__SYS_clock + 6)
# define __SYS_timers (__SYS_clock + 7)
#else
# define __SYS_timers (__SYS_clock + 4)
# define __SYS_timers (__SYS_clock + 6)
#endif
/* The following are defined only if POSIX timers are supported */
@ -560,7 +562,8 @@
#endif
/* The following is defined only if entropy pool random number generator
* is enabled. */
* is enabled.
*/
#ifdef CONFIG_CRYPTO_RANDOM_POOL
# define SYS_getrandom (SYS_prctl + 1)

View file

@ -49,6 +49,12 @@
* Pre-processor Definitions
****************************************************************************/
#define ITIMER_REAL 0 /* Timers run in real time. */
#define ITIMER_VIRTUAL 1 /* Timers run only when the process is executing. */
#define ITIMER_PROF 2 /* Timers run when the process is executing and when
* the system is executing on behalf of the process.
*/
/* The following are non-standard interfaces in the sense that they are not
* in POSIX.1-2001 nor are they specified at OpenGroup.org. These interfaces
* are present on most BSD derivatives, however, including Linux.
@ -110,6 +116,24 @@
((tvp)->tv_usec cmp (uvp)->tv_usec) : \
((tvp)->tv_sec cmp (uvp)->tv_sec))
/* Macros for converting between `struct timeval' and `struct timespec'. */
#define TIMEVAL_TO_TIMESPEC(tv, ts) \
do \
{ \
(ts)->tv_sec = (tv)->tv_sec; \
(ts)->tv_nsec = (tv)->tv_usec * 1000; \
} \
while (0)
#define TIMESPEC_TO_TIMEVAL(tv, ts) \
do \
{ \
(tv)->tv_sec = (ts)->tv_sec; \
(tv)->tv_usec = (ts)->tv_nsec / 1000; \
} \
while (0)
/****************************************************************************
* Public Type Definitions
****************************************************************************/
@ -122,6 +146,16 @@ struct timeval
long tv_usec; /* Microseconds */
};
/* Type of the second argument to `getitimer' and
* the second and third arguments `setitimer'.
*/
struct itimerval
{
struct timeval it_interval; /* Interval for periodic timer */
struct timeval it_value; /* Time until next expiration */
};
/* The use of the struct timezone is obsolete; the tz argument should
* normally be specified as NULL (and is ignored in any event).
*/
@ -232,6 +266,89 @@ int settimeofday(FAR const struct timeval *tv, FAR struct timezone *tz);
int adjtime(FAR const struct timeval *delta, FAR struct timeval *olddelta);
#endif
/****************************************************************************
* Name: getitimer
*
* Description:
* The getitimer() function will store the amount of time until the
* specified timer, which, expires and the reload value of the timer
* into the space pointed to by the value argument. The it_value member
* of this structure will contain the amount of time before the timer
* expires, or zero if the timer is disarmed. This value is returned as
* the interval until timer expiration. The it_interval member of value
* will contain the reload value last set by setitime().
*
* Input Parameters:
* which - The predefined timer id
* value - The current timer value
*
* Returned Value:
* If the getitimer() succeeds, a value of 0 (OK) will be returned.
* If an error occurs, the value -1 (ERROR) will be returned, and errno
* set to indicate the error.
*
* EINVAL - The which argument does not correspond to an predefined ID.
*
* Assumptions/Limitations:
* Due to the asynchronous operation of this function, the time reported
* by this function could be significantly more than that actual time
* remaining on the timer at any time.
*
****************************************************************************/
int getitimer(int which, FAR struct itimerval *value);
/****************************************************************************
* Name: setitimer
*
* Description:
* The setitimer() function sets the time until the next expiration of
* the timer specified by which from the it_value member of the value
* argument and arm the timer if the it_value member of value is non-zero.
* If the specified timer was already armed when setitimer() is
* called, this call will reset the time until next expiration to the
* value specified. If the it_value member of value is zero, the timer
* will be disarmed. The effect of disarming or resetting a timer with
* pending expiration notifications is unspecified.
*
* The reload value of the timer will be set to the value specified by the
* it_interval member of value. When a timer is armed with a non-zero
* it_interval, a periodic (or repetitive) timer is specified.
*
* Time values that are between two consecutive non-negative integer
* multiples of the resolution of the specified timer will be rounded up
* to the larger multiple of the resolution. Quantization error will not
* cause the timer to expire earlier than the rounded time value.
*
* If the argument ovalue is not NULL, the setitimer() function will
* store, in the location referenced by ovalue, a value representing the
* previous amount of time before the timer would have expired, or zero if
* the timer was disarmed, together with the previous timer reload value.
* Timers will not expire before their scheduled time.
*
* Input Parameters:
* which - The predefined timer id
* value - Specifies the timer value to set
* ovalue - A location in which to return the time remaining from the
* previous timer setting.
*
* Returned Value:
* If the setitimer() succeeds, a value of 0 (OK) will be returned.
* If an error occurs, the value -1 (ERROR) will be returned, and errno set
* to indicate the error.
*
* EINVAL - The which argument does not correspond to an predefined ID.
* EINVAL - A value structure specified a microsecond value less than zero or
* greater than or equal to 1000 million, and the it_value member of that
* structure did not specify zero seconds and nanoseconds.
*
* Assumptions:
*
****************************************************************************/
int setitimer(int which, FAR const struct itimerval *value,
FAR struct itimerval *ovalue);
#undef EXTERN
#if defined(__cplusplus)
}

View file

@ -320,6 +320,10 @@ FAR void *sbrk(intptr_t incr);
int pipe(int fd[2]);
/* Schedule an alarm */
unsigned int alarm(unsigned int seconds);
/* Working directory operations */
int chdir(FAR const char *path);

View file

@ -4,6 +4,7 @@
"aio_error","aio.h","defined(CONFIG_FS_AIO)","int","FAR struct aiocb *"
"aio_return","aio.h","defined(CONFIG_FS_AIO)","ssize_t","FAR struct aiocb *"
"aio_suspend","aio.h","defined(CONFIG_FS_AIO)","int","FAR struct aiocb *const []|FAR struct aiocb *const *","int","FAR const struct timespec *"
"alarm","unistd.h","!defined(CONFIG_DISABLE_POSIX_TIMERS)","unsigned int","unsigned int"
"asprintf","stdio.h","","int","FAR char **","FAR const char *","..."
"vasprintf","stdio.h","","int","FAR char **","FAR const char *","va_list"
"b16atan2","fixedmath.h","!defined(CONFIG_HAVE_LONG_LONG)","b16_t","b16_t","b16_t"

Can't render this file because it has a wrong number of fields in line 2.

View file

@ -37,7 +37,7 @@
CSRCS += lib_access.c lib_daemon.c lib_swab.c lib_sysconf.c
CSRCS += lib_getopt.c lib_getoptargp.c lib_getoptindp.c lib_getoptoptp.c
CSRCS += lib_sleep.c lib_usleep.c
CSRCS += lib_alarm.c lib_sleep.c lib_usleep.c
CSRCS += lib_seteuid.c lib_setegid.c lib_geteuid.c lib_getegid.c
CSRCS += lib_setreuid.c lib_setregid.c

View file

@ -0,0 +1,83 @@
/****************************************************************************
* lib/lib_alarm.c
*
* Copyright (C) 2019 Xiaomi Inc. All rights reserved.
*
* 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 <unistd.h>
#include <sys/time.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: alarm
*
* Description:
* The alarm() function will arranges for a SIGALRM signal to be delivered
* to the calling process in seconds seconds.
*
* Interactions between alarm() and any of setitimer() are unspecified.
*
* Input Parameters:
* seconds - The number of seconds to alarm
*
* Returned Value:
* alarm() returns the number of seconds remaining until any previously
* scheduled alarm was due to be delivered, or zero if there was no
* previously scheduled alarm.
*
* Assumptions:
*
****************************************************************************/
unsigned int alarm(unsigned int seconds)
{
struct itimerval value =
{
};
struct itimerval ovalue =
{
};
value.it_value.tv_sec = seconds;
setitimer(ITIMER_REAL, &value, &ovalue);
return ovalue.it_value.tv_sec;
}

View file

@ -36,8 +36,8 @@
ifneq ($(CONFIG_DISABLE_POSIX_TIMERS),y)
CSRCS += timer_initialize.c timer_create.c timer_delete.c
CSRCS += timer_getoverrun.c timer_gettime.c timer_settime.c
CSRCS += timer_release.c
CSRCS += timer_getoverrun.c timer_getitimer.c timer_gettime.c
CSRCS += timer_setitimer.c timer_settime.c timer_release.c
# Include timer build support

View file

@ -0,0 +1,113 @@
/****************************************************************************
* sched/timer/timer_getitimer.c
*
* Copyright (C) 2019 Xiaomi Inc. All rights reserved.
* Author: Xiang Xiao <xiaoxiang@xiaomi.com>
*
* 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 <errno.h>
#include <time.h>
#include <sys/time.h>
#include "sched/sched.h"
#ifndef CONFIG_DISABLE_POSIX_TIMERS
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: getitimer
*
* Description:
* The getitimer() function will store the amount of time until the
* specified timer, which, expires and the reload value of the timer
* into the space pointed to by the value argument. The it_value member
* of this structure will contain the amount of time before the timer
* expires, or zero if the timer is disarmed. This value is returned as
* the interval until timer expiration. The it_interval member of value
* will contain the reload value last set by setitime().
*
* Input Parameters:
* which - The predefined timer id
* value - The current timer value
*
* Returned Value:
* If the getitimer() succeeds, a value of 0 (OK) will be returned.
* If an error occurs, the value -1 (ERROR) will be returned, and errno
* set to indicate the error.
*
* EINVAL - The which argument does not correspond to an predefined ID.
*
* Assumptions/Limitations:
* Due to the asynchronous operation of this function, the time reported
* by this function could be significantly more than that actual time
* remaining on the timer at any time.
*
****************************************************************************/
int getitimer(int which, FAR struct itimerval *value)
{
FAR struct tcb_s *rtcb = this_task();
struct itimerspec spec =
{
};
int ret = OK;
if (which != ITIMER_REAL || !value)
{
set_errno(EINVAL);
return ERROR;
}
if (rtcb->group->itimer)
{
ret = timer_gettime(rtcb->group->itimer, &spec);
}
if (ret == OK)
{
TIMESPEC_TO_TIMEVAL(&value->it_value, &spec.it_value);
TIMESPEC_TO_TIMEVAL(&value->it_interval, &spec.it_interval);
}
return ret;
}
#endif /* CONFIG_DISABLE_POSIX_TIMERS */

View file

@ -0,0 +1,146 @@
/****************************************************************************
* sched/timer/timer_setitimer.c
*
* Copyright (C) 2019 Xiaomi Inc. All rights reserved.
* Author: Xiang Xiao <xiaoxiang@xiaomi.com>
*
* 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 <errno.h>
#include <time.h>
#include <sys/time.h>
#include "sched/sched.h"
#ifndef CONFIG_DISABLE_POSIX_TIMERS
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: setitimer
*
* Description:
* The setitimer() function sets the time until the next expiration of
* the timer specified by which from the it_value member of the value
* argument and arm the timer if the it_value member of value is non-zero.
* If the specified timer was already armed when setitimer() is
* called, this call will reset the time until next expiration to the
* value specified. If the it_value member of value is zero, the timer
* will be disarmed. The effect of disarming or resetting a timer with
* pending expiration notifications is unspecified.
*
* The reload value of the timer will be set to the value specified by the
* it_interval member of value. When a timer is armed with a non-zero
* it_interval, a periodic (or repetitive) timer is specified.
*
* Time values that are between two consecutive non-negative integer
* multiples of the resolution of the specified timer will be rounded up
* to the larger multiple of the resolution. Quantization error will not
* cause the timer to expire earlier than the rounded time value.
*
* If the argument ovalue is not NULL, the setitimer() function will
* store, in the location referenced by ovalue, a value representing the
* previous amount of time before the timer would have expired, or zero if
* the timer was disarmed, together with the previous timer reload value.
* Timers will not expire before their scheduled time.
*
* Input Parameters:
* which - The predefined timer id
* value - Specifies the timer value to set
* ovalue - A location in which to return the time remaining from the
* previous timer setting.
*
* Returned Value:
* If the setitimer() succeeds, a value of 0 (OK) will be returned.
* If an error occurs, the value -1 (ERROR) will be returned, and errno set
* to indicate the error.
*
* EINVAL - The which argument does not correspond to an predefined ID.
* EINVAL - A value structure specified a microsecond value less than zero or
* greater than or equal to 1000 million, and the it_value member of that
* structure did not specify zero seconds and nanoseconds.
*
* Assumptions:
*
****************************************************************************/
int setitimer(int which, FAR const struct itimerval *value,
FAR struct itimerval *ovalue)
{
FAR struct tcb_s *rtcb = this_task();
struct itimerspec spec;
struct itimerspec ospec;
irqstate_t flags;
int ret = OK;
if (which != ITIMER_REAL || !value)
{
set_errno(EINVAL);
return ERROR;
}
if (!rtcb->group->itimer)
{
flags = enter_critical_section();
if (!rtcb->group->itimer)
{
ret = timer_create(CLOCK_REALTIME, NULL, &rtcb->group->itimer);
}
leave_critical_section(flags);
if (ret != OK)
{
return ret;
}
}
TIMEVAL_TO_TIMESPEC(&value->it_value, &spec.it_value);
TIMEVAL_TO_TIMESPEC(&value->it_interval, &spec.it_interval);
ret = timer_settime(rtcb->group->itimer, 0, &spec, ovalue ? &ospec : NULL);
if (ret == OK && ovalue)
{
TIMESPEC_TO_TIMEVAL(&ovalue->it_value, &ospec.it_value);
TIMESPEC_TO_TIMEVAL(&ovalue->it_interval, &ospec.it_interval);
}
return ret;
}
#endif /* CONFIG_DISABLE_POSIX_TIMERS */

View file

@ -248,7 +248,7 @@ static void timer_timeout(int argc, wdparm_t itimer)
* flags - Specifies characteristics of the timer (see above)
* value - Specifies the timer value to set
* ovalue - A location in which to return the time remaining from the
* previous timer setting. (ignored)
* previous timer setting.
*
* Returned Value:
* If the timer_settime() succeeds, a value of 0 (OK) will be returned.
@ -282,6 +282,18 @@ int timer_settime(timer_t timerid, int flags,
return ERROR;
}
if (ovalue)
{
/* Get the number of ticks before the underlying watchdog expires */
delay = wd_gettime(timer->pt_wdog);
/* Convert that to a struct timespec and return it */
clock_ticks2time(delay, &ovalue->it_value);
clock_ticks2time(timer->pt_last, &ovalue->it_interval);
}
/* Disarm the timer (in case the timer was already armed when timer_settime()
* is called).
*/

View file

@ -33,6 +33,7 @@
"get_errno_ptr","errno.h","defined(__DIRECT_ERRNO_ACCESS)","FAR int*"
"getenv","stdlib.h","!defined(CONFIG_DISABLE_ENVIRON)","FAR char*","FAR const char*"
"getgid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","gid_t"
"getitimer","sys/time.h","!defined(CONFIG_DISABLE_POSIX_TIMERS)","int","int","FAR struct itimerval*"
"getpeername","sys/socket.h","defined(CONFIG_NET)","int","int","FAR struct sockaddr *","FAR socklen_t *"
"getpid","unistd.h","","pid_t"
"getrandom","sys/random.h","defined(CONFIG_CRYPTO_RANDOM_POOL)","void","FAR void*","size_t"
@ -146,6 +147,7 @@
"setenv","stdlib.h","!defined(CONFIG_DISABLE_ENVIRON)","int","FAR const char*","FAR const char*","int"
"setgid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","int","gid_t"
"sethostname","unistd.h","defined(CONFIG_LIBC_NETDB)","int","FAR const char*","size_t"
"setitimer","sys/time.h","!defined(CONFIG_DISABLE_POSIX_TIMERS)","int","int","FAR const struct itimerval*","FAR struct itimerval*"
"setsockopt","sys/socket.h","defined(CONFIG_NET)","int","int","int","int","FAR const void*","socklen_t"
"setuid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","int","uid_t"
"shmat", "sys/shm.h", "defined(CONFIG_MM_SHM)", "FAR void *", "int", "FAR const void *", "int"

Can't render this file because it has a wrong number of fields in line 2.