clock: Replace all ts and tick conversion functions
Using the ts/tick conversion functions provided in clock.h Do this caused we want speed up the time calculation, so change: clock_time2ticks, clock_ticks2time, clock_timespec_add, clock_timespec_compare, clock_timespec_subtract... to MACRO Signed-off-by: ligd <liguiding1@xiaomi.com>
This commit is contained in:
parent
a10c46d02e
commit
6a2c03732f
24 changed files with 82 additions and 289 deletions
|
@ -989,7 +989,6 @@ int bl_os_timer_start_once(void *timerid, long t_sec, long t_nsec)
|
||||||
{
|
{
|
||||||
struct timer_adpt *timer;
|
struct timer_adpt *timer;
|
||||||
struct timespec reltime;
|
struct timespec reltime;
|
||||||
int32_t tick;
|
|
||||||
|
|
||||||
timer = (struct timer_adpt *)timerid;
|
timer = (struct timer_adpt *)timerid;
|
||||||
|
|
||||||
|
@ -1001,10 +1000,8 @@ int bl_os_timer_start_once(void *timerid, long t_sec, long t_nsec)
|
||||||
reltime.tv_nsec = t_nsec;
|
reltime.tv_nsec = t_nsec;
|
||||||
reltime.tv_sec = t_sec;
|
reltime.tv_sec = t_sec;
|
||||||
|
|
||||||
clock_time2ticks(&reltime, &tick);
|
|
||||||
|
|
||||||
timer->mode = BL_OS_TIEMR_ONCE;
|
timer->mode = BL_OS_TIEMR_ONCE;
|
||||||
timer->delay = tick;
|
timer->delay = clock_time2ticks(&reltime);
|
||||||
|
|
||||||
return wd_start(&timer->wdog,
|
return wd_start(&timer->wdog,
|
||||||
timer->delay,
|
timer->delay,
|
||||||
|
@ -1027,7 +1024,6 @@ int bl_os_timer_start_periodic(void *timerid, long t_sec, long t_nsec)
|
||||||
{
|
{
|
||||||
struct timer_adpt *timer;
|
struct timer_adpt *timer;
|
||||||
struct timespec reltime;
|
struct timespec reltime;
|
||||||
int32_t tick;
|
|
||||||
|
|
||||||
timer = (struct timer_adpt *)timerid;
|
timer = (struct timer_adpt *)timerid;
|
||||||
|
|
||||||
|
@ -1039,10 +1035,8 @@ int bl_os_timer_start_periodic(void *timerid, long t_sec, long t_nsec)
|
||||||
reltime.tv_nsec = t_nsec;
|
reltime.tv_nsec = t_nsec;
|
||||||
reltime.tv_sec = t_sec;
|
reltime.tv_sec = t_sec;
|
||||||
|
|
||||||
clock_time2ticks(&reltime, &tick);
|
|
||||||
|
|
||||||
timer->mode = BL_OS_TIEMR_CYCLE;
|
timer->mode = BL_OS_TIEMR_CYCLE;
|
||||||
timer->delay = tick;
|
timer->delay = clock_time2ticks(&reltime);
|
||||||
|
|
||||||
return wd_start(&timer->wdog,
|
return wd_start(&timer->wdog,
|
||||||
timer->delay,
|
timer->delay,
|
||||||
|
|
|
@ -36,9 +36,6 @@
|
||||||
#define CONFIG_BOARD_LOOPSPER10USEC ((CONFIG_BOARD_LOOPSPERMSEC+50)/100)
|
#define CONFIG_BOARD_LOOPSPER10USEC ((CONFIG_BOARD_LOOPSPERMSEC+50)/100)
|
||||||
#define CONFIG_BOARD_LOOPSPERUSEC ((CONFIG_BOARD_LOOPSPERMSEC+500)/1000)
|
#define CONFIG_BOARD_LOOPSPERUSEC ((CONFIG_BOARD_LOOPSPERMSEC+500)/1000)
|
||||||
|
|
||||||
#define timespec_to_nsec(ts) \
|
|
||||||
((uint64_t)(ts)->tv_sec * NSEC_PER_SEC + (ts)->tv_nsec)
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Data
|
* Private Data
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
@ -53,14 +50,6 @@ static clock_t g_current_tick;
|
||||||
* Private Functions
|
* Private Functions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static inline void timespec_from_nsec(FAR struct timespec *ts,
|
|
||||||
uint64_t nanoseconds)
|
|
||||||
{
|
|
||||||
ts->tv_sec = nanoseconds / NSEC_PER_SEC;
|
|
||||||
nanoseconds -= (uint64_t)ts->tv_sec * NSEC_PER_SEC;
|
|
||||||
ts->tv_nsec = nanoseconds;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void udelay_accurate(useconds_t microseconds)
|
static void udelay_accurate(useconds_t microseconds)
|
||||||
{
|
{
|
||||||
struct timespec now;
|
struct timespec now;
|
||||||
|
@ -68,7 +57,7 @@ static void udelay_accurate(useconds_t microseconds)
|
||||||
struct timespec delta;
|
struct timespec delta;
|
||||||
|
|
||||||
ONESHOT_CURRENT(g_oneshot_lower, &now);
|
ONESHOT_CURRENT(g_oneshot_lower, &now);
|
||||||
timespec_from_nsec(&delta, (uint64_t)microseconds * NSEC_PER_USEC);
|
clock_nsec2time(&delta, (uint64_t)microseconds * NSEC_PER_USEC);
|
||||||
clock_timespec_add(&now, &delta, &end);
|
clock_timespec_add(&now, &delta, &end);
|
||||||
|
|
||||||
while (clock_timespec_compare(&now, &end) < 0)
|
while (clock_timespec_compare(&now, &end) < 0)
|
||||||
|
@ -387,7 +376,7 @@ unsigned long up_perf_gettime(void)
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
|
|
||||||
ONESHOT_CURRENT(g_oneshot_lower, &ts);
|
ONESHOT_CURRENT(g_oneshot_lower, &ts);
|
||||||
ret = timespec_to_nsec(&ts);
|
ret = clock_time2nsec(&ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -400,7 +389,7 @@ unsigned long up_perf_getfreq(void)
|
||||||
|
|
||||||
void up_perf_convert(unsigned long elapsed, FAR struct timespec *ts)
|
void up_perf_convert(unsigned long elapsed, FAR struct timespec *ts)
|
||||||
{
|
{
|
||||||
timespec_from_nsec(ts, elapsed);
|
clock_nsec2time(ts, elapsed);
|
||||||
}
|
}
|
||||||
#endif /* CONFIG_ARCH_PERF_EVENTS */
|
#endif /* CONFIG_ARCH_PERF_EVENTS */
|
||||||
|
|
||||||
|
|
|
@ -61,14 +61,6 @@ static struct arch_timer_s g_timer;
|
||||||
* Private Functions
|
* Private Functions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static inline void timespec_from_usec(FAR struct timespec *ts,
|
|
||||||
uint64_t microseconds)
|
|
||||||
{
|
|
||||||
ts->tv_sec = microseconds / USEC_PER_SEC;
|
|
||||||
microseconds -= (uint64_t)ts->tv_sec * USEC_PER_SEC;
|
|
||||||
ts->tv_nsec = microseconds * NSEC_PER_USEC;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CONFIG_SCHED_TICKLESS
|
#ifdef CONFIG_SCHED_TICKLESS
|
||||||
|
|
||||||
static uint32_t update_timeout(uint32_t timeout)
|
static uint32_t update_timeout(uint32_t timeout)
|
||||||
|
@ -435,7 +427,7 @@ unsigned long up_perf_getfreq(void)
|
||||||
void up_perf_convert(unsigned long elapsed,
|
void up_perf_convert(unsigned long elapsed,
|
||||||
FAR struct timespec *ts)
|
FAR struct timespec *ts)
|
||||||
{
|
{
|
||||||
timespec_from_usec(ts, elapsed);
|
clock_usec2time(ts, elapsed);
|
||||||
}
|
}
|
||||||
#endif /* CONFIG_ARCH_PERF_EVENTS */
|
#endif /* CONFIG_ARCH_PERF_EVENTS */
|
||||||
|
|
||||||
|
|
|
@ -535,8 +535,8 @@ int timerfd_settime(int fd, int flags,
|
||||||
|
|
||||||
/* Convert that to a struct timespec and return it */
|
/* Convert that to a struct timespec and return it */
|
||||||
|
|
||||||
clock_ticks2time(delay, &old_value->it_value);
|
clock_ticks2time(&old_value->it_value, delay);
|
||||||
clock_ticks2time(dev->delay, &old_value->it_interval);
|
clock_ticks2time(&old_value->it_interval, dev->delay);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Disarm the timer (in case the timer was already armed when
|
/* Disarm the timer (in case the timer was already armed when
|
||||||
|
@ -561,7 +561,7 @@ int timerfd_settime(int fd, int flags,
|
||||||
|
|
||||||
/* Setup up any repetitive timer */
|
/* Setup up any repetitive timer */
|
||||||
|
|
||||||
clock_time2ticks(&new_value->it_interval, &delay);
|
delay = clock_time2ticks(&new_value->it_interval);
|
||||||
dev->delay = delay;
|
dev->delay = delay;
|
||||||
|
|
||||||
/* We need to disable timer interrupts through the following section so
|
/* We need to disable timer interrupts through the following section so
|
||||||
|
@ -583,7 +583,7 @@ int timerfd_settime(int fd, int flags,
|
||||||
* returns success.
|
* returns success.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
clock_time2ticks(&new_value->it_value, &delay);
|
delay = clock_time2ticks(&new_value->it_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If the time is in the past or now, then set up the next interval
|
/* If the time is in the past or now, then set up the next interval
|
||||||
|
@ -651,8 +651,8 @@ int timerfd_gettime(int fd, FAR struct itimerspec *curr_value)
|
||||||
|
|
||||||
/* Convert that to a struct timespec and return it */
|
/* Convert that to a struct timespec and return it */
|
||||||
|
|
||||||
clock_ticks2time(ticks, &curr_value->it_value);
|
clock_ticks2time(&curr_value->it_value, ticks);
|
||||||
clock_ticks2time(dev->delay, &curr_value->it_interval);
|
clock_ticks2time(&curr_value->it_interval, dev->delay);
|
||||||
return OK;
|
return OK;
|
||||||
|
|
||||||
errout:
|
errout:
|
||||||
|
|
|
@ -325,7 +325,7 @@ EXTERN volatile clock_t g_system_ticks;
|
||||||
* Public Function Prototypes
|
* Public Function Prototypes
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#define timespec_from_tick(ts, tick) \
|
#define clock_ticks2time(ts, tick) \
|
||||||
do \
|
do \
|
||||||
{ \
|
{ \
|
||||||
clock_t _tick = (tick); \
|
clock_t _tick = (tick); \
|
||||||
|
@ -335,9 +335,35 @@ EXTERN volatile clock_t g_system_ticks;
|
||||||
} \
|
} \
|
||||||
while (0)
|
while (0)
|
||||||
|
|
||||||
#define timespec_to_tick(ts) \
|
#define clock_time2ticks(ts) \
|
||||||
((clock_t)(ts)->tv_sec * TICK_PER_SEC + (ts)->tv_nsec / NSEC_PER_TICK)
|
((clock_t)(ts)->tv_sec * TICK_PER_SEC + (ts)->tv_nsec / NSEC_PER_TICK)
|
||||||
|
|
||||||
|
#define clock_usec2time(ts, usec) \
|
||||||
|
do \
|
||||||
|
{ \
|
||||||
|
uint64_t _usec = (usec); \
|
||||||
|
(ts)->tv_sec = _usec / USEC_PER_SEC; \
|
||||||
|
_usec -= (uint64_t)(ts)->tv_sec * USEC_PER_SEC; \
|
||||||
|
(ts)->tv_nsec = _usec * NSEC_PER_USEC; \
|
||||||
|
} \
|
||||||
|
while (0)
|
||||||
|
|
||||||
|
#define clock_time2usec(ts) \
|
||||||
|
((uint64_t)(ts)->tv_sec * USEC_PER_SEC + (ts)->tv_nsec / NSEC_PER_USEC)
|
||||||
|
|
||||||
|
#define clock_nsec2time(ts, nsec) \
|
||||||
|
do \
|
||||||
|
{ \
|
||||||
|
uint64_t _nsec = (nsec); \
|
||||||
|
(ts)->tv_sec = _nsec / NSEC_PER_SEC; \
|
||||||
|
_nsec -= (uint64_t)(ts)->tv_sec * NSEC_PER_SEC; \
|
||||||
|
(ts)->tv_nsec = _nsec; \
|
||||||
|
} \
|
||||||
|
while (0)
|
||||||
|
|
||||||
|
#define clock_time2nsec(ts) \
|
||||||
|
((uint64_t)(ts)->tv_sec * NSEC_PER_SEC + (ts)->tv_nsec)
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: clock_timespec_add
|
* Name: clock_timespec_add
|
||||||
*
|
*
|
||||||
|
@ -602,51 +628,6 @@ void clock_resynchronize(FAR struct timespec *rtc_diff);
|
||||||
clock_t clock_systime_ticks(void);
|
clock_t clock_systime_ticks(void);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Name: clock_time2ticks
|
|
||||||
*
|
|
||||||
* Description:
|
|
||||||
* Return the given struct timespec as systime ticks.
|
|
||||||
*
|
|
||||||
* NOTE: This is an internal OS interface and should not be called from
|
|
||||||
* application code.
|
|
||||||
*
|
|
||||||
* Input Parameters:
|
|
||||||
* reltime - Pointer to the time presented as struct timespec
|
|
||||||
*
|
|
||||||
* Output Parameters:
|
|
||||||
* ticks - Pointer to receive the time value presented as systime ticks
|
|
||||||
*
|
|
||||||
* Returned Value:
|
|
||||||
* Always returns OK (0)
|
|
||||||
*
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
int clock_time2ticks(FAR const struct timespec *reltime,
|
|
||||||
FAR sclock_t *ticks);
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Name: clock_ticks2time
|
|
||||||
*
|
|
||||||
* Description:
|
|
||||||
* Return the given systime ticks as a struct timespec.
|
|
||||||
*
|
|
||||||
* NOTE: This is an internal OS interface and should not be called from
|
|
||||||
* application code.
|
|
||||||
*
|
|
||||||
* Input Parameters:
|
|
||||||
* ticks - Time presented as systime ticks
|
|
||||||
*
|
|
||||||
* Output Parameters:
|
|
||||||
* reltime - Pointer to receive the time value presented as struct timespec
|
|
||||||
*
|
|
||||||
* Returned Value:
|
|
||||||
* Always returns OK (0)
|
|
||||||
*
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
int clock_ticks2time(sclock_t ticks, FAR struct timespec *reltime);
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: clock_systime_timespec
|
* Name: clock_systime_timespec
|
||||||
*
|
*
|
||||||
|
|
|
@ -270,7 +270,7 @@ int oneshot_max_delay(FAR struct oneshot_lowerhalf_s *lower,
|
||||||
DEBUGASSERT(lower->ops->tick_max_delay);
|
DEBUGASSERT(lower->ops->tick_max_delay);
|
||||||
|
|
||||||
ret = lower->ops->tick_max_delay(lower, &tick);
|
ret = lower->ops->tick_max_delay(lower, &tick);
|
||||||
timespec_from_tick(ts, tick);
|
clock_ticks2time(ts, tick);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -283,7 +283,7 @@ int oneshot_start(FAR struct oneshot_lowerhalf_s *lower,
|
||||||
|
|
||||||
DEBUGASSERT(lower->ops->tick_start);
|
DEBUGASSERT(lower->ops->tick_start);
|
||||||
|
|
||||||
tick = timespec_to_tick(ts);
|
tick = clock_time2ticks(ts);
|
||||||
return lower->ops->tick_start(lower, callback, arg, tick);
|
return lower->ops->tick_start(lower, callback, arg, tick);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -297,7 +297,7 @@ int oneshot_cancel(FAR struct oneshot_lowerhalf_s *lower,
|
||||||
DEBUGASSERT(lower->ops->tick_cancel);
|
DEBUGASSERT(lower->ops->tick_cancel);
|
||||||
|
|
||||||
ret = lower->ops->tick_cancel(lower, &tick);
|
ret = lower->ops->tick_cancel(lower, &tick);
|
||||||
timespec_from_tick(ts, tick);
|
clock_ticks2time(ts, tick);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -312,7 +312,7 @@ int oneshot_current(FAR struct oneshot_lowerhalf_s *lower,
|
||||||
DEBUGASSERT(lower->ops->tick_current);
|
DEBUGASSERT(lower->ops->tick_current);
|
||||||
|
|
||||||
ret = lower->ops->tick_current(lower, &tick);
|
ret = lower->ops->tick_current(lower, &tick);
|
||||||
timespec_from_tick(ts, tick);
|
clock_ticks2time(ts, tick);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -327,7 +327,7 @@ int oneshot_tick_max_delay(FAR struct oneshot_lowerhalf_s *lower,
|
||||||
DEBUGASSERT(lower->ops->max_delay);
|
DEBUGASSERT(lower->ops->max_delay);
|
||||||
|
|
||||||
ret = lower->ops->max_delay(lower, &ts);
|
ret = lower->ops->max_delay(lower, &ts);
|
||||||
*ticks = timespec_to_tick(&ts);
|
*ticks = clock_time2ticks(&ts);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -340,7 +340,7 @@ int oneshot_tick_start(FAR struct oneshot_lowerhalf_s *lower,
|
||||||
|
|
||||||
DEBUGASSERT(lower->ops->start);
|
DEBUGASSERT(lower->ops->start);
|
||||||
|
|
||||||
timespec_from_tick(&ts, ticks);
|
clock_ticks2time(&ts, ticks);
|
||||||
return lower->ops->start(lower, callback, arg, &ts);
|
return lower->ops->start(lower, callback, arg, &ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -354,7 +354,7 @@ int oneshot_tick_cancel(FAR struct oneshot_lowerhalf_s *lower,
|
||||||
DEBUGASSERT(lower->ops->cancel);
|
DEBUGASSERT(lower->ops->cancel);
|
||||||
|
|
||||||
ret = lower->ops->cancel(lower, &ts);
|
ret = lower->ops->cancel(lower, &ts);
|
||||||
*ticks = timespec_to_tick(&ts);
|
*ticks = clock_time2ticks(&ts);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -369,7 +369,7 @@ int oneshot_tick_current(FAR struct oneshot_lowerhalf_s *lower,
|
||||||
DEBUGASSERT(lower->ops->current);
|
DEBUGASSERT(lower->ops->current);
|
||||||
|
|
||||||
ret = lower->ops->current(lower, &ts);
|
ret = lower->ops->current(lower, &ts);
|
||||||
*ticks = timespec_to_tick(&ts);
|
*ticks = clock_time2ticks(&ts);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
@ -353,7 +353,7 @@ int nxmutex_timedlock(FAR mutex_t *mutex, unsigned int timeout)
|
||||||
struct timespec rqtp;
|
struct timespec rqtp;
|
||||||
|
|
||||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||||
clock_ticks2time(MSEC2TICK(timeout), &delay);
|
clock_ticks2time(&delay, MSEC2TICK(timeout));
|
||||||
clock_timespec_add(&now, &delay, &rqtp);
|
clock_timespec_add(&now, &delay, &rqtp);
|
||||||
|
|
||||||
/* Wait until we get the lock or until the timeout expires */
|
/* Wait until we get the lock or until the timeout expires */
|
||||||
|
|
|
@ -21,8 +21,6 @@
|
||||||
set(SRCS
|
set(SRCS
|
||||||
sched_getprioritymax.c
|
sched_getprioritymax.c
|
||||||
sched_getprioritymin.c
|
sched_getprioritymin.c
|
||||||
clock_ticks2time.c
|
|
||||||
clock_time2ticks.c
|
|
||||||
clock_getcpuclockid.c
|
clock_getcpuclockid.c
|
||||||
clock_getres.c
|
clock_getres.c
|
||||||
task_cancelpt.c
|
task_cancelpt.c
|
||||||
|
|
|
@ -21,7 +21,6 @@
|
||||||
# Add the sched C files to the build
|
# Add the sched C files to the build
|
||||||
|
|
||||||
CSRCS += sched_getprioritymax.c sched_getprioritymin.c
|
CSRCS += sched_getprioritymax.c sched_getprioritymin.c
|
||||||
CSRCS += clock_ticks2time.c clock_time2ticks.c
|
|
||||||
CSRCS += clock_getcpuclockid.c clock_getres.c
|
CSRCS += clock_getcpuclockid.c clock_getres.c
|
||||||
CSRCS += task_cancelpt.c task_setcancelstate.c task_setcanceltype.c
|
CSRCS += task_cancelpt.c task_setcancelstate.c task_setcanceltype.c
|
||||||
CSRCS += task_testcancel.c
|
CSRCS += task_testcancel.c
|
||||||
|
|
|
@ -1,59 +0,0 @@
|
||||||
/****************************************************************************
|
|
||||||
* libs/libc/sched/clock_ticks2time.c
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Included Files
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#include <nuttx/config.h>
|
|
||||||
|
|
||||||
#include <time.h>
|
|
||||||
#include <nuttx/clock.h>
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Public Functions
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Name: clock_ticks2time
|
|
||||||
*
|
|
||||||
* Description:
|
|
||||||
* Convert the system time tick value to a relative time.
|
|
||||||
*
|
|
||||||
* Input Parameters:
|
|
||||||
* ticks - The number of system time ticks to convert.
|
|
||||||
* reltime - Return the converted system time here.
|
|
||||||
*
|
|
||||||
* Returned Value:
|
|
||||||
* Always returns OK
|
|
||||||
*
|
|
||||||
* Assumptions:
|
|
||||||
*
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
int clock_ticks2time(sclock_t ticks, FAR struct timespec *reltime)
|
|
||||||
{
|
|
||||||
sclock_t remainder;
|
|
||||||
|
|
||||||
reltime->tv_sec = ticks / TICK_PER_SEC;
|
|
||||||
remainder = ticks - TICK_PER_SEC * reltime->tv_sec;
|
|
||||||
reltime->tv_nsec = remainder * NSEC_PER_TICK;
|
|
||||||
return OK;
|
|
||||||
}
|
|
|
@ -1,103 +0,0 @@
|
||||||
/****************************************************************************
|
|
||||||
* libs/libc/sched/clock_time2ticks.c
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Included Files
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#include <nuttx/config.h>
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
#include <nuttx/clock.h>
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Public Functions
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Name: clock_time2ticks
|
|
||||||
*
|
|
||||||
* Description:
|
|
||||||
* Convert a timespec delay to system timer ticks. This function is
|
|
||||||
* suitable for calculating relative time delays and does not depend on
|
|
||||||
* the other clock_* logic.
|
|
||||||
*
|
|
||||||
* Input Parameters:
|
|
||||||
* reltime - Convert this relative time to system clock ticks.
|
|
||||||
* ticks - Return the converted number of ticks here.
|
|
||||||
*
|
|
||||||
* Returned Value:
|
|
||||||
* Always returns OK
|
|
||||||
*
|
|
||||||
* Assumptions:
|
|
||||||
*
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
int clock_time2ticks(FAR const struct timespec *reltime,
|
|
||||||
FAR sclock_t *ticks)
|
|
||||||
{
|
|
||||||
#ifdef CONFIG_HAVE_LONG_LONG
|
|
||||||
int64_t relnsec;
|
|
||||||
|
|
||||||
/* Convert the relative time into nanoseconds. The range of the int64_t
|
|
||||||
* is sufficiently large that there is no real need for range checking.
|
|
||||||
*/
|
|
||||||
|
|
||||||
relnsec = (int64_t)reltime->tv_sec * NSEC_PER_SEC +
|
|
||||||
(int64_t)reltime->tv_nsec;
|
|
||||||
|
|
||||||
/* Convert nanoseconds to clock ticks, rounding up to the smallest integer
|
|
||||||
* that is greater than or equal to the exact number of tick.
|
|
||||||
*/
|
|
||||||
|
|
||||||
*ticks = (sclock_t)((relnsec + NSEC_PER_TICK - 1) / NSEC_PER_TICK);
|
|
||||||
return OK;
|
|
||||||
#else
|
|
||||||
int32_t relusec;
|
|
||||||
|
|
||||||
/* This function uses an int32_t to only the relative time in microseconds.
|
|
||||||
* that means that the maximum supported relative time is 2,147,487.647
|
|
||||||
* seconds
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if 0 // overkill
|
|
||||||
DEBUGASSERT(reltime->tv_sec < 2147487 ||
|
|
||||||
reltime->tv_sec == 2147487 &&
|
|
||||||
reltime->tv_nsec <= 647 * NSEC_PER_MSEC);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Convert the relative time into microseconds, rounding up to the smallest
|
|
||||||
* value that is greater than or equal to the exact number of microseconds.
|
|
||||||
*/
|
|
||||||
|
|
||||||
relusec = reltime->tv_sec * USEC_PER_SEC +
|
|
||||||
(reltime->tv_nsec + NSEC_PER_USEC - 1) / NSEC_PER_USEC;
|
|
||||||
|
|
||||||
/* Convert microseconds to clock ticks, rounding up to the smallest integer
|
|
||||||
* that is greater than or equal to the exact number of tick.
|
|
||||||
*/
|
|
||||||
|
|
||||||
*ticks = (sclock_t)((relusec + USEC_PER_TICK - 1) / USEC_PER_TICK);
|
|
||||||
return OK;
|
|
||||||
#endif
|
|
||||||
}
|
|
|
@ -205,7 +205,7 @@ static void work_process(FAR struct usr_wqueue_s *wqueue)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
clock_gettime(CLOCK_REALTIME, &now);
|
clock_gettime(CLOCK_REALTIME, &now);
|
||||||
clock_ticks2time(next, &delay);
|
clock_ticks2time(&delay, next);
|
||||||
clock_timespec_add(&now, &delay, &rqtp);
|
clock_timespec_add(&now, &delay, &rqtp);
|
||||||
|
|
||||||
nxsem_timedwait(&wqueue->wake, &rqtp);
|
nxsem_timedwait(&wqueue->wake, &rqtp);
|
||||||
|
|
|
@ -100,5 +100,7 @@ int clock_abstime2ticks(clockid_t clockid,
|
||||||
|
|
||||||
/* Convert this relative time into clock ticks. */
|
/* Convert this relative time into clock ticks. */
|
||||||
|
|
||||||
return clock_time2ticks(&reltime, ticks);
|
*ticks = clock_time2ticks(&reltime);
|
||||||
|
|
||||||
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
|
@ -190,7 +190,7 @@ clock_t perf_gettime(void)
|
||||||
|
|
||||||
void perf_convert(clock_t elapsed, FAR struct timespec *ts)
|
void perf_convert(clock_t elapsed, FAR struct timespec *ts)
|
||||||
{
|
{
|
||||||
clock_ticks2time(elapsed, ts);
|
clock_ticks2time(ts, elapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
|
|
|
@ -92,7 +92,7 @@ clock_t clock_systime_ticks(void)
|
||||||
* then in clock tick units.
|
* then in clock tick units.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
return timespec_to_tick(&ts);
|
return clock_time2ticks(&ts);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -120,7 +120,7 @@ clock_t clock_systime_ticks(void)
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
if (up_timer_gettime(&ts) == OK)
|
if (up_timer_gettime(&ts) == OK)
|
||||||
{
|
{
|
||||||
return timespec_to_tick(&ts);
|
return clock_time2ticks(&ts);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -120,7 +120,7 @@ int clock_systime_timespec(FAR struct timespec *ts)
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ret = up_timer_gettick(&ticks);
|
ret = up_timer_gettick(&ticks);
|
||||||
timespec_from_tick(ts, ticks);
|
clock_ticks2time(ts, ticks);
|
||||||
return ret;
|
return ret;
|
||||||
#elif defined(CONFIG_SCHED_TICKLESS)
|
#elif defined(CONFIG_SCHED_TICKLESS)
|
||||||
return up_timer_gettime(ts);
|
return up_timer_gettime(ts);
|
||||||
|
@ -129,7 +129,7 @@ int clock_systime_timespec(FAR struct timespec *ts)
|
||||||
* when the clock period is in units of microseconds.
|
* when the clock period is in units of microseconds.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
timespec_from_tick(ts, clock_systime_ticks());
|
clock_ticks2time(ts, clock_systime_ticks());
|
||||||
return OK;
|
return OK;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
|
@ -339,8 +339,8 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
|
||||||
|
|
||||||
/* Convert timespec values to system clock ticks */
|
/* Convert timespec values to system clock ticks */
|
||||||
|
|
||||||
clock_time2ticks(¶m.sched_ss_repl_period, &repl_ticks);
|
repl_ticks = clock_time2ticks(¶m.sched_ss_repl_period);
|
||||||
clock_time2ticks(¶m.sched_ss_init_budget, &budget_ticks);
|
budget_ticks = clock_time2ticks(¶m.sched_ss_init_budget);
|
||||||
|
|
||||||
/* The replenishment period must be greater than or equal to the
|
/* The replenishment period must be greater than or equal to the
|
||||||
* budget period.
|
* budget period.
|
||||||
|
|
|
@ -123,10 +123,10 @@ int nxsched_get_param(pid_t pid, FAR struct sched_param *param)
|
||||||
param->sched_ss_low_priority = (int)sporadic->low_priority;
|
param->sched_ss_low_priority = (int)sporadic->low_priority;
|
||||||
param->sched_ss_max_repl = (int)sporadic->max_repl;
|
param->sched_ss_max_repl = (int)sporadic->max_repl;
|
||||||
|
|
||||||
clock_ticks2time((sclock_t)sporadic->repl_period,
|
clock_ticks2time(¶m->sched_ss_repl_period,
|
||||||
¶m->sched_ss_repl_period);
|
sporadic->repl_period);
|
||||||
clock_ticks2time((sclock_t)sporadic->budget,
|
clock_ticks2time(¶m->sched_ss_init_budget,
|
||||||
¶m->sched_ss_init_budget);
|
sporadic->budget);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -136,8 +136,8 @@ int nxsched_set_param(pid_t pid, FAR const struct sched_param *param)
|
||||||
|
|
||||||
/* Convert timespec values to system clock ticks */
|
/* Convert timespec values to system clock ticks */
|
||||||
|
|
||||||
clock_time2ticks(¶m->sched_ss_repl_period, &repl_ticks);
|
repl_ticks = clock_time2ticks(¶m->sched_ss_repl_period);
|
||||||
clock_time2ticks(¶m->sched_ss_init_budget, &budget_ticks);
|
budget_ticks = clock_time2ticks(¶m->sched_ss_init_budget);
|
||||||
|
|
||||||
/* Avoid zero/negative times */
|
/* Avoid zero/negative times */
|
||||||
|
|
||||||
|
|
|
@ -192,8 +192,8 @@ int nxsched_set_scheduler(pid_t pid, int policy,
|
||||||
|
|
||||||
/* Convert timespec values to system clock ticks */
|
/* Convert timespec values to system clock ticks */
|
||||||
|
|
||||||
clock_time2ticks(¶m->sched_ss_repl_period, &repl_ticks);
|
repl_ticks = clock_time2ticks(¶m->sched_ss_repl_period);
|
||||||
clock_time2ticks(¶m->sched_ss_init_budget, &budget_ticks);
|
budget_ticks = clock_time2ticks(¶m->sched_ss_init_budget);
|
||||||
|
|
||||||
/* Avoid zero/negative times */
|
/* Avoid zero/negative times */
|
||||||
|
|
||||||
|
|
|
@ -112,7 +112,7 @@ int up_timer_gettick(FAR clock_t *ticks)
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
int ret;
|
int ret;
|
||||||
ret = up_timer_gettime(&ts);
|
ret = up_timer_gettime(&ts);
|
||||||
*ticks = timespec_to_tick(&ts);
|
*ticks = clock_time2ticks(&ts);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -122,7 +122,7 @@ int up_timer_gettick(FAR clock_t *ticks)
|
||||||
int up_alarm_tick_start(clock_t ticks)
|
int up_alarm_tick_start(clock_t ticks)
|
||||||
{
|
{
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
timespec_from_tick(&ts, ticks);
|
clock_ticks2time(&ts, ticks);
|
||||||
return up_alarm_start(&ts);
|
return up_alarm_start(&ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,14 +131,14 @@ int up_alarm_tick_cancel(FAR clock_t *ticks)
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
int ret;
|
int ret;
|
||||||
ret = up_alarm_cancel(&ts);
|
ret = up_alarm_cancel(&ts);
|
||||||
*ticks = timespec_to_tick(&ts);
|
*ticks = clock_time2ticks(&ts);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
# else
|
# else
|
||||||
int up_timer_tick_start(clock_t ticks)
|
int up_timer_tick_start(clock_t ticks)
|
||||||
{
|
{
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
timespec_from_tick(&ts, ticks);
|
clock_ticks2time(&ts, ticks);
|
||||||
return up_timer_start(&ts);
|
return up_timer_start(&ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,7 +147,7 @@ int up_timer_tick_cancel(FAR clock_t *ticks)
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
int ret;
|
int ret;
|
||||||
ret = up_timer_cancel(&ts);
|
ret = up_timer_cancel(&ts);
|
||||||
*ticks = timespec_to_tick(&ts);
|
*ticks = clock_time2ticks(&ts);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
# endif
|
# endif
|
||||||
|
@ -474,7 +474,7 @@ void nxsched_alarm_expiration(FAR const struct timespec *ts)
|
||||||
|
|
||||||
DEBUGASSERT(ts);
|
DEBUGASSERT(ts);
|
||||||
|
|
||||||
ticks = timespec_to_tick(ts);
|
ticks = clock_time2ticks(ts);
|
||||||
nxsched_alarm_tick_expiration(ticks);
|
nxsched_alarm_tick_expiration(ticks);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -174,7 +174,7 @@ int nxsig_nanosleep(FAR const struct timespec *rqtp,
|
||||||
* wait.
|
* wait.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
clock_time2ticks(rqtp, &ticks);
|
ticks = clock_time2ticks(rqtp);
|
||||||
|
|
||||||
/* Get the number of ticks that we actually waited */
|
/* Get the number of ticks that we actually waited */
|
||||||
|
|
||||||
|
@ -194,7 +194,7 @@ int nxsig_nanosleep(FAR const struct timespec *rqtp,
|
||||||
remaining = (clock_t)ticks - elapsed;
|
remaining = (clock_t)ticks - elapsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
clock_ticks2time((sclock_t)remaining, rmtp);
|
clock_ticks2time(rmtp, remaining);
|
||||||
}
|
}
|
||||||
|
|
||||||
leave_critical_section(flags);
|
leave_critical_section(flags);
|
||||||
|
|
|
@ -87,8 +87,8 @@ int timer_gettime(timer_t timerid, FAR struct itimerspec *value)
|
||||||
|
|
||||||
/* Convert that to a struct timespec and return it */
|
/* Convert that to a struct timespec and return it */
|
||||||
|
|
||||||
clock_ticks2time(ticks, &value->it_value);
|
clock_ticks2time(&value->it_value, ticks);
|
||||||
clock_ticks2time(timer->pt_delay, &value->it_interval);
|
clock_ticks2time(&value->it_interval, timer->pt_delay);
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -244,8 +244,8 @@ int timer_settime(timer_t timerid, int flags,
|
||||||
|
|
||||||
/* Convert that to a struct timespec and return it */
|
/* Convert that to a struct timespec and return it */
|
||||||
|
|
||||||
clock_ticks2time(delay, &ovalue->it_value);
|
clock_ticks2time(&ovalue->it_value, delay);
|
||||||
clock_ticks2time(timer->pt_delay, &ovalue->it_interval);
|
clock_ticks2time(&ovalue->it_interval, timer->pt_delay);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Disarm the timer (in case the timer was already armed when
|
/* Disarm the timer (in case the timer was already armed when
|
||||||
|
@ -271,7 +271,7 @@ int timer_settime(timer_t timerid, int flags,
|
||||||
|
|
||||||
if (value->it_interval.tv_sec > 0 || value->it_interval.tv_nsec > 0)
|
if (value->it_interval.tv_sec > 0 || value->it_interval.tv_nsec > 0)
|
||||||
{
|
{
|
||||||
clock_time2ticks(&value->it_interval, &delay);
|
delay = clock_time2ticks(&value->it_interval);
|
||||||
|
|
||||||
/* REVISIT: Should pt_delay be sclock_t? */
|
/* REVISIT: Should pt_delay be sclock_t? */
|
||||||
|
|
||||||
|
@ -303,7 +303,7 @@ int timer_settime(timer_t timerid, int flags,
|
||||||
* returns success.
|
* returns success.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
ret = clock_time2ticks(&value->it_value, &delay);
|
delay = clock_time2ticks(&value->it_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
|
|
Loading…
Reference in a new issue