clock: take clock_timespec_compare/add/subtract() as MACRO

Signed-off-by: ligd <liguiding1@xiaomi.com>
This commit is contained in:
ligd 2024-05-24 23:11:50 +08:00 committed by Xiang Xiao
parent 7223a1d0d4
commit f2a77bbcea
6 changed files with 48 additions and 199 deletions

View file

@ -338,19 +338,6 @@ EXTERN volatile clock_t g_system_ticks;
#define timespec_to_tick(ts) \
((clock_t)(ts)->tv_sec * TICK_PER_SEC + (ts)->tv_nsec / NSEC_PER_TICK)
/****************************************************************************
* Name: clock_timespec_compare
*
* Description:
* Return < 0 if time ts1 is before time ts2
* Return > 0 if time ts2 is before time ts1
* Return 0 if time ts1 is the same as time ts2
*
****************************************************************************/
int clock_timespec_compare(FAR const struct timespec *ts1,
FAR const struct timespec *ts2);
/****************************************************************************
* Name: clock_timespec_add
*
@ -366,9 +353,20 @@ int clock_timespec_compare(FAR const struct timespec *ts1,
*
****************************************************************************/
void clock_timespec_add(FAR const struct timespec *ts1,
FAR const struct timespec *ts2,
FAR struct timespec *ts3);
#define clock_timespec_add(ts1, ts2, ts3) \
do \
{ \
time_t _sec = (ts1)->tv_sec + (ts2)->tv_sec; \
long _nsec = (ts1)->tv_nsec + (ts2)->tv_nsec; \
if (_nsec >= NSEC_PER_SEC) \
{ \
_nsec -= NSEC_PER_SEC; \
_sec++; \
} \
(ts3)->tv_sec = _sec; \
(ts3)->tv_nsec = _nsec; \
}\
while (0)
/****************************************************************************
* Name: clock_timespec_subtract
@ -386,9 +384,40 @@ void clock_timespec_add(FAR const struct timespec *ts1,
*
****************************************************************************/
void clock_timespec_subtract(FAR const struct timespec *ts1,
FAR const struct timespec *ts2,
FAR struct timespec *ts3);
#define clock_timespec_subtract(ts1, ts2, ts3) \
do \
{ \
time_t _sec = (ts1)->tv_sec - (ts2)->tv_sec; \
long _nsec = (ts1)->tv_nsec - (ts2)->tv_nsec; \
if (_nsec < 0) \
{ \
_nsec += NSEC_PER_SEC; \
_sec--; \
} \
if (_sec < 0) \
{ \
_sec = 0; \
_nsec = 0; \
} \
(ts3)->tv_sec = _sec; \
(ts3)->tv_nsec = _nsec; \
}\
while (0)
/****************************************************************************
* Name: clock_timespec_compare
*
* Description:
* Return < 0 if time ts1 is before time ts2
* Return > 0 if time ts2 is before time ts1
* Return 0 if time ts1 is the same as time ts2
*
****************************************************************************/
#define clock_timespec_compare(ts1, ts2) \
(((ts1)->tv_sec < (ts2)->tv_sec) ? -1 : \
((ts1)->tv_sec > (ts2)->tv_sec) ? 1 : \
(ts1)->tv_nsec - (ts2)->tv_nsec)
/****************************************************************************
* Name: clock_compare

View file

@ -23,8 +23,6 @@ set(SRCS
sched_getprioritymin.c
clock_ticks2time.c
clock_time2ticks.c
clock_timespec_add.c
clock_timespec_subtract.c
clock_getcpuclockid.c
clock_getres.c
task_cancelpt.c

View file

@ -22,7 +22,6 @@
CSRCS += sched_getprioritymax.c sched_getprioritymin.c
CSRCS += clock_ticks2time.c clock_time2ticks.c
CSRCS += clock_timespec_add.c clock_timespec_subtract.c
CSRCS += clock_getcpuclockid.c clock_getres.c
CSRCS += task_cancelpt.c task_setcancelstate.c task_setcanceltype.c
CSRCS += task_testcancel.c

View file

@ -1,66 +0,0 @@
/****************************************************************************
* libs/libc/sched/clock_timespec_add.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 <nuttx/clock.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: clock_timespec_add
*
* Description:
* Add timespec ts1 to to2 and return the result in ts3
*
* Input Parameters:
* ts1 and ts2: The two timespecs to be added
* ts3: The location to return the result (may be ts1 or ts2)
*
* Returned Value:
* None
*
****************************************************************************/
void clock_timespec_add(FAR const struct timespec *ts1,
FAR const struct timespec *ts2,
FAR struct timespec *ts3)
{
time_t sec = ts1->tv_sec + ts2->tv_sec;
long nsec = ts1->tv_nsec + ts2->tv_nsec;
if (nsec >= NSEC_PER_SEC)
{
nsec -= NSEC_PER_SEC;
sec++;
}
ts3->tv_sec = sec;
ts3->tv_nsec = nsec;
}

View file

@ -1,85 +0,0 @@
/****************************************************************************
* libs/libc/sched/clock_timespec_subtract.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 <nuttx/clock.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: clock_timespec_subtract
*
* Description:
* Subtract timespec ts2 from to1 and return the result in ts3.
* Zero is returned if the time difference is negative.
*
* Input Parameters:
* ts1 and ts2: The two timespecs to be subtracted (ts1 - ts2)
* ts3: The location to return the result (may be ts1 or ts2)
*
* Returned Value:
* None
*
****************************************************************************/
void clock_timespec_subtract(FAR const struct timespec *ts1,
FAR const struct timespec *ts2,
FAR struct timespec *ts3)
{
time_t sec;
long nsec;
if (ts1->tv_sec < ts2->tv_sec)
{
sec = 0;
nsec = 0;
}
else if (ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec <= ts2->tv_nsec)
{
sec = 0;
nsec = 0;
}
else
{
sec = ts1->tv_sec - ts2->tv_sec;
if (ts1->tv_nsec < ts2->tv_nsec)
{
nsec = (ts1->tv_nsec + NSEC_PER_SEC) - ts2->tv_nsec;
sec--;
}
else
{
nsec = ts1->tv_nsec - ts2->tv_nsec;
}
}
ts3->tv_sec = sec;
ts3->tv_nsec = nsec;
}

View file

@ -35,32 +35,6 @@
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: clock_timespec_compare
*
* Description:
* Return < 0 if time a is before time b
* Return > 0 if time b is before time a
* Return 0 if time a is the same as time b
*
****************************************************************************/
int clock_timespec_compare(FAR const struct timespec *a,
FAR const struct timespec *b)
{
if (a->tv_sec < b->tv_sec)
{
return -1;
}
if (a->tv_sec > b->tv_sec)
{
return 1;
}
return a->tv_nsec - b->tv_nsec;
}
/****************************************************************************
* Name: clock_abstime2ticks
*