diff --git a/include/nuttx/clock.h b/include/nuttx/clock.h index 71258c5309c..52e8bdb89c3 100644 --- a/include/nuttx/clock.h +++ b/include/nuttx/clock.h @@ -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 diff --git a/libs/libc/sched/CMakeLists.txt b/libs/libc/sched/CMakeLists.txt index 7436860ce68..2741130ebc9 100644 --- a/libs/libc/sched/CMakeLists.txt +++ b/libs/libc/sched/CMakeLists.txt @@ -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 diff --git a/libs/libc/sched/Make.defs b/libs/libc/sched/Make.defs index 123151b16f9..ed0399b05ba 100644 --- a/libs/libc/sched/Make.defs +++ b/libs/libc/sched/Make.defs @@ -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 diff --git a/libs/libc/sched/clock_timespec_add.c b/libs/libc/sched/clock_timespec_add.c deleted file mode 100644 index 04d8234828b..00000000000 --- a/libs/libc/sched/clock_timespec_add.c +++ /dev/null @@ -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 - -#include -#include - -#include - -/**************************************************************************** - * 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; -} diff --git a/libs/libc/sched/clock_timespec_subtract.c b/libs/libc/sched/clock_timespec_subtract.c deleted file mode 100644 index 1cfd599903c..00000000000 --- a/libs/libc/sched/clock_timespec_subtract.c +++ /dev/null @@ -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 - -#include -#include - -#include - -/**************************************************************************** - * 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; -} diff --git a/sched/clock/clock_abstime2ticks.c b/sched/clock/clock_abstime2ticks.c index 83eafd20814..776ac6aee00 100644 --- a/sched/clock/clock_abstime2ticks.c +++ b/sched/clock/clock_abstime2ticks.c @@ -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 *