2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2014-08-08 22:55:16 +02:00
|
|
|
* sched/timer/timer_settime.c
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
2024-09-11 13:45:11 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-03-11 21:09:41 +01: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
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
2020-03-11 21:09:41 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
2020-03-11 21:09:41 +01: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.
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 01:56:49 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2007-03-21 01:56:49 +01:00
|
|
|
* Included Files
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 01:56:49 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2007-03-21 01:56:49 +01:00
|
|
|
#include <time.h>
|
2008-06-01 19:46:26 +02:00
|
|
|
#include <string.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2007-03-21 01:56:49 +01:00
|
|
|
#include <errno.h>
|
2008-06-01 19:46:26 +02:00
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
#include <nuttx/irq.h>
|
2015-12-30 20:20:31 +01:00
|
|
|
|
2014-08-08 22:43:02 +02:00
|
|
|
#include "clock/clock.h"
|
2014-08-08 22:55:16 +02:00
|
|
|
#include "timer/timer.h"
|
2007-03-21 01:56:49 +01:00
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_POSIX_TIMERS
|
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2007-03-21 18:21:26 +01:00
|
|
|
* Private Function Prototypes
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 18:21:26 +01:00
|
|
|
|
2015-12-30 20:20:31 +01:00
|
|
|
static inline void timer_signotify(FAR struct posix_timer_s *timer);
|
2016-08-07 16:25:30 +02:00
|
|
|
static inline void timer_restart(FAR struct posix_timer_s *timer,
|
|
|
|
wdparm_t itimer);
|
2020-08-09 20:29:35 +02:00
|
|
|
static void timer_timeout(wdparm_t itimer);
|
2007-03-21 18:21:26 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2007-03-21 01:56:49 +01:00
|
|
|
* Private Functions
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 01:56:49 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2015-12-30 20:20:31 +01:00
|
|
|
* Name: timer_signotify
|
2007-03-21 18:21:26 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2018-08-19 19:19:43 +02:00
|
|
|
* This function basically re-implements nxsig_queue() so that the si_code
|
2017-10-07 18:57:09 +02:00
|
|
|
* can be correctly set to SI_TIMER
|
2007-03-21 18:21:26 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-03-21 18:21:26 +01:00
|
|
|
* timer - A reference to the POSIX timer that just timed out
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2007-03-21 18:21:26 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function executes in the context of the watchod timer interrupt.
|
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 18:21:26 +01:00
|
|
|
|
2015-12-30 20:20:31 +01:00
|
|
|
static inline void timer_signotify(FAR struct posix_timer_s *timer)
|
2007-03-21 18:21:26 +01:00
|
|
|
{
|
2019-01-27 16:28:59 +01:00
|
|
|
DEBUGVERIFY(nxsig_notification(timer->pt_owner, &timer->pt_event,
|
|
|
|
SI_TIMER, &timer->pt_work));
|
2007-03-21 18:21:26 +01:00
|
|
|
}
|
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: timer_restart
|
2007-03-21 18:21:26 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* If a periodic timer has been selected, then restart the watchdog.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-03-21 18:21:26 +01:00
|
|
|
* timer - A reference to the POSIX timer that just timed out
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2007-03-21 18:21:26 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2015-05-18 16:53:42 +02:00
|
|
|
* This function executes in the context of the watchdog timer interrupt.
|
2007-03-21 18:21:26 +01:00
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 18:21:26 +01:00
|
|
|
|
2015-05-18 16:53:42 +02:00
|
|
|
static inline void timer_restart(FAR struct posix_timer_s *timer,
|
|
|
|
wdparm_t itimer)
|
2007-03-21 18:21:26 +01:00
|
|
|
{
|
2024-06-18 14:25:50 +02:00
|
|
|
clock_t ticks;
|
|
|
|
sclock_t delay;
|
2024-09-13 11:57:25 +02:00
|
|
|
sclock_t frame;
|
2024-06-18 14:25:50 +02:00
|
|
|
|
2007-03-21 18:21:26 +01:00
|
|
|
/* If this is a repetitive timer, then restart the watchdog */
|
|
|
|
|
|
|
|
if (timer->pt_delay)
|
|
|
|
{
|
2024-06-18 14:25:50 +02:00
|
|
|
/* Check whether next expected time is reached */
|
|
|
|
|
|
|
|
ticks = clock_systime_ticks();
|
2024-09-13 11:57:25 +02:00
|
|
|
delay = ticks - timer->pt_expected;
|
|
|
|
|
|
|
|
/* Calculate the number of timer overruns and the next expected tick.
|
|
|
|
* The next expired tick frame can be computed as align up:
|
|
|
|
* frame <- (elapsed_ticks + pt_delay) / pt_delay
|
|
|
|
* For instance:
|
|
|
|
* | pt_delay | pt_delay | pt_delay | ... |
|
|
|
|
* ^ pt_expected ^ ticks ^ next pt_expected
|
|
|
|
* In this case, frame equals 3.
|
|
|
|
* Then, pt_overrun <- frame - 1 and
|
|
|
|
* the next pt_expected <- pt_expected + frame * pt_delay.
|
|
|
|
* Assumption of correctness:
|
|
|
|
* (delay + timer->pt_delay) should not overflow.
|
|
|
|
*/
|
|
|
|
|
|
|
|
frame = (delay + timer->pt_delay) / timer->pt_delay;
|
|
|
|
timer->pt_overrun = frame - 1;
|
|
|
|
timer->pt_expected += frame * timer->pt_delay;
|
2024-06-18 14:25:50 +02:00
|
|
|
|
2024-08-08 06:16:15 +02:00
|
|
|
wd_start_abstick(&timer->pt_wdog, timer->pt_expected,
|
|
|
|
timer_timeout, itimer);
|
2007-03-21 18:21:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: timer_timeout
|
2007-03-21 18:21:26 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 21:30:31 +02:00
|
|
|
* This function is called if the timeout elapses before the condition is
|
|
|
|
* signaled.
|
2007-03-21 18:21:26 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-03-21 18:21:26 +01:00
|
|
|
* itimer - A reference to the POSIX timer that just timed out
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2007-03-21 18:21:26 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function executes in the context of the watchod timer interrupt.
|
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 18:21:26 +01:00
|
|
|
|
2020-08-09 20:29:35 +02:00
|
|
|
static void timer_timeout(wdparm_t itimer)
|
2007-03-21 18:21:26 +01:00
|
|
|
{
|
2022-10-31 19:11:18 +01:00
|
|
|
FAR struct posix_timer_s *timer = timer_gethandle((timer_t)itimer);
|
|
|
|
|
|
|
|
if (timer == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2007-03-21 18:21:26 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/* Send the specified signal to the specified task. Increment the
|
|
|
|
* reference count on the timer first so that will not be deleted until
|
|
|
|
* after the signal handler returns.
|
2008-06-03 03:21:12 +02:00
|
|
|
*/
|
2007-03-21 18:21:26 +01:00
|
|
|
|
2008-06-03 03:21:12 +02:00
|
|
|
timer->pt_crefs++;
|
2015-12-30 20:20:31 +01:00
|
|
|
timer_signotify(timer);
|
2007-03-21 18:21:26 +01:00
|
|
|
|
2008-06-03 03:21:12 +02:00
|
|
|
/* Release the reference. timer_release will return nonzero if the timer
|
|
|
|
* was not deleted.
|
|
|
|
*/
|
2008-06-01 19:46:26 +02:00
|
|
|
|
2008-06-03 03:21:12 +02:00
|
|
|
if (timer_release(timer))
|
2008-06-01 19:46:26 +02:00
|
|
|
{
|
|
|
|
/* If this is a repetitive timer, the restart the watchdog */
|
|
|
|
|
|
|
|
timer_restart(timer, itimer);
|
|
|
|
}
|
2007-03-21 18:21:26 +01:00
|
|
|
}
|
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2007-03-21 01:56:49 +01:00
|
|
|
* Public Functions
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 01:56:49 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: timer_settime
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2016-08-07 16:25:30 +02:00
|
|
|
* The timer_settime() function sets the time until the next expiration of
|
|
|
|
* the timer specified by timerid 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 timer_settime() 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.
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
* If the flag TIMER_ABSTIME is not set in the argument flags,
|
|
|
|
* timer_settime() will behave as if the time until next expiration is set
|
|
|
|
* to be equal to the interval specified by the it_value member of value.
|
|
|
|
* That is, the timer will expire in it_value nanoseconds from when the
|
|
|
|
* call is made. If the flag TIMER_ABSTIME is set in the argument flags,
|
|
|
|
* timer_settime() will behave as if the time until next expiration is set
|
|
|
|
* to be equal to the difference between the absolute time specified by
|
|
|
|
* the it_value member of value and the current value of the clock
|
|
|
|
* associated with timerid. That is, the timer will expire when the clock
|
|
|
|
* reaches the value specified by the it_value member of value. If the
|
|
|
|
* specified time has already passed, the function will succeed and the
|
2007-03-21 01:56:49 +01:00
|
|
|
* expiration notification will be made.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
* 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.
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
* If the argument ovalue is not NULL, the timer_settime() 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.
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-03-21 01:56:49 +01:00
|
|
|
* timerid - The pre-thread timer, previously created by the call to
|
|
|
|
* timer_create(), to be be set.
|
2018-08-19 19:19:43 +02:00
|
|
|
* flags - Specifies characteristics of the timer (see above)
|
2007-03-21 01:56:49 +01:00
|
|
|
* value - Specifies the timer value to set
|
2016-08-07 16:25:30 +02:00
|
|
|
* ovalue - A location in which to return the time remaining from the
|
2019-11-13 15:00:45 +01:00
|
|
|
* previous timer setting.
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2007-03-21 01:56:49 +01:00
|
|
|
* If the timer_settime() succeeds, a value of 0 (OK) will be returned.
|
2016-08-07 16:25:30 +02:00
|
|
|
* If an error occurs, the value -1 (ERROR) will be returned, and errno set
|
|
|
|
* to indicate the error.
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
|
|
|
* EINVAL - The timerid argument does not correspond to an ID returned by
|
|
|
|
* timer_create() but not yet deleted by timer_delete().
|
2020-03-11 21:09:41 +01:00
|
|
|
* EINVAL - A value structure specified a nanosecond 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.
|
2007-03-21 01:56:49 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-03-21 01:56:49 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
int timer_settime(timer_t timerid, int flags,
|
|
|
|
FAR const struct itimerspec *value,
|
2007-03-21 01:56:49 +01:00
|
|
|
FAR struct itimerspec *ovalue)
|
|
|
|
{
|
2022-10-31 19:11:18 +01:00
|
|
|
FAR struct posix_timer_s *timer = timer_gethandle(timerid);
|
2018-06-16 20:16:13 +02:00
|
|
|
sclock_t delay;
|
2007-03-21 18:21:26 +01:00
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Some sanity checks */
|
|
|
|
|
|
|
|
if (!timer || !value)
|
|
|
|
{
|
2014-08-29 01:00:24 +02:00
|
|
|
set_errno(EINVAL);
|
2007-03-21 18:21:26 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2019-11-13 15:00:45 +01:00
|
|
|
if (ovalue)
|
|
|
|
{
|
|
|
|
/* Get the number of ticks before the underlying watchdog expires */
|
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
delay = wd_gettime(&timer->pt_wdog);
|
2019-11-13 15:00:45 +01:00
|
|
|
|
|
|
|
/* Convert that to a struct timespec and return it */
|
|
|
|
|
2024-05-24 15:55:42 +02:00
|
|
|
clock_ticks2time(&ovalue->it_value, delay);
|
|
|
|
clock_ticks2time(&ovalue->it_interval, timer->pt_delay);
|
2019-11-13 15:00:45 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 21:09:41 +01:00
|
|
|
/* Disarm the timer (in case the timer was already armed when
|
|
|
|
* timer_settime() is called).
|
2007-03-21 18:21:26 +01:00
|
|
|
*/
|
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_cancel(&timer->pt_wdog);
|
2007-03-21 18:21:26 +01:00
|
|
|
|
2019-01-27 16:28:59 +01:00
|
|
|
/* Cancel any pending notification */
|
|
|
|
|
|
|
|
nxsig_cancel_notification(&timer->pt_work);
|
|
|
|
|
2020-04-07 07:42:58 +02:00
|
|
|
/* If the it_value member of value is zero, the timer will not be
|
|
|
|
* re-armed
|
|
|
|
*/
|
2007-03-21 18:21:26 +01:00
|
|
|
|
|
|
|
if (value->it_value.tv_sec <= 0 && value->it_value.tv_nsec <= 0)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2018-08-19 19:19:43 +02:00
|
|
|
/* Setup up any repetitive timer */
|
2007-03-21 18:21:26 +01:00
|
|
|
|
|
|
|
if (value->it_interval.tv_sec > 0 || value->it_interval.tv_nsec > 0)
|
|
|
|
{
|
2024-05-24 15:55:42 +02:00
|
|
|
delay = clock_time2ticks(&value->it_interval);
|
2024-06-18 14:25:50 +02:00
|
|
|
timer->pt_delay = delay;
|
2007-03-21 18:21:26 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-15 01:19:27 +02:00
|
|
|
timer->pt_delay = 0;
|
2007-03-21 18:21:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if abstime is selected */
|
|
|
|
|
|
|
|
if ((flags & TIMER_ABSTIME) != 0)
|
|
|
|
{
|
2020-10-29 04:10:26 +01:00
|
|
|
/* Calculate a delay corresponding to the absolute time in 'value' */
|
2007-03-21 18:21:26 +01:00
|
|
|
|
2024-08-19 09:37:42 +02:00
|
|
|
clock_abstime2ticks(timer->pt_clock, &value->it_value,
|
|
|
|
&timer->pt_expected);
|
|
|
|
timer->pt_expected += clock_systime_ticks();
|
2007-03-21 18:21:26 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-14 21:30:31 +02:00
|
|
|
/* Calculate a delay assuming that 'value' holds the relative time
|
|
|
|
* to wait. We have internal knowledge that clock_time2ticks always
|
|
|
|
* returns success.
|
|
|
|
*/
|
2007-03-21 18:21:26 +01:00
|
|
|
|
2024-05-24 15:55:42 +02:00
|
|
|
delay = clock_time2ticks(&value->it_value);
|
2024-06-18 14:25:50 +02:00
|
|
|
timer->pt_expected = clock_systime_ticks() + delay;
|
2007-03-21 18:21:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then start the watchdog */
|
|
|
|
|
2024-08-08 06:16:15 +02:00
|
|
|
ret = wd_start_abstick(&timer->pt_wdog, timer->pt_expected,
|
|
|
|
timer_timeout, (wdparm_t)timer);
|
2022-10-31 19:11:18 +01:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
2007-03-21 18:21:26 +01:00
|
|
|
return ret;
|
2007-03-21 01:56:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_DISABLE_POSIX_TIMERS */
|