Skip to content

Commit 610fb55

Browse files
committed
clock: take clock_timespec_compare/add/subtract() as MACRO
Signed-off-by: ligd <[email protected]>
1 parent 4fe1458 commit 610fb55

File tree

6 files changed

+48
-199
lines changed

6 files changed

+48
-199
lines changed

include/nuttx/clock.h

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -338,19 +338,6 @@ EXTERN volatile clock_t g_system_ticks;
338338
#define timespec_to_tick(ts) \
339339
((clock_t)(ts)->tv_sec * TICK_PER_SEC + (ts)->tv_nsec / NSEC_PER_TICK)
340340

341-
/****************************************************************************
342-
* Name: clock_timespec_compare
343-
*
344-
* Description:
345-
* Return < 0 if time ts1 is before time ts2
346-
* Return > 0 if time ts2 is before time ts1
347-
* Return 0 if time ts1 is the same as time ts2
348-
*
349-
****************************************************************************/
350-
351-
int clock_timespec_compare(FAR const struct timespec *ts1,
352-
FAR const struct timespec *ts2);
353-
354341
/****************************************************************************
355342
* Name: clock_timespec_add
356343
*
@@ -366,9 +353,20 @@ int clock_timespec_compare(FAR const struct timespec *ts1,
366353
*
367354
****************************************************************************/
368355

369-
void clock_timespec_add(FAR const struct timespec *ts1,
370-
FAR const struct timespec *ts2,
371-
FAR struct timespec *ts3);
356+
#define clock_timespec_add(ts1, ts2, ts3) \
357+
do \
358+
{ \
359+
time_t _sec = (ts1)->tv_sec + (ts2)->tv_sec; \
360+
long _nsec = (ts1)->tv_nsec + (ts2)->tv_nsec; \
361+
if (_nsec >= NSEC_PER_SEC) \
362+
{ \
363+
_nsec -= NSEC_PER_SEC; \
364+
_sec++; \
365+
} \
366+
(ts3)->tv_sec = _sec; \
367+
(ts3)->tv_nsec = _nsec; \
368+
}\
369+
while (0)
372370

373371
/****************************************************************************
374372
* Name: clock_timespec_subtract
@@ -386,9 +384,40 @@ void clock_timespec_add(FAR const struct timespec *ts1,
386384
*
387385
****************************************************************************/
388386

389-
void clock_timespec_subtract(FAR const struct timespec *ts1,
390-
FAR const struct timespec *ts2,
391-
FAR struct timespec *ts3);
387+
#define clock_timespec_subtract(ts1, ts2, ts3) \
388+
do \
389+
{ \
390+
time_t _sec = (ts1)->tv_sec - (ts2)->tv_sec; \
391+
long _nsec = (ts1)->tv_nsec - (ts2)->tv_nsec; \
392+
if (_nsec < 0) \
393+
{ \
394+
_nsec += NSEC_PER_SEC; \
395+
_sec--; \
396+
} \
397+
if (_sec < 0) \
398+
{ \
399+
_sec = 0; \
400+
_nsec = 0; \
401+
} \
402+
(ts3)->tv_sec = _sec; \
403+
(ts3)->tv_nsec = _nsec; \
404+
}\
405+
while (0)
406+
407+
/****************************************************************************
408+
* Name: clock_timespec_compare
409+
*
410+
* Description:
411+
* Return < 0 if time ts1 is before time ts2
412+
* Return > 0 if time ts2 is before time ts1
413+
* Return 0 if time ts1 is the same as time ts2
414+
*
415+
****************************************************************************/
416+
417+
#define clock_timespec_compare(ts1, ts2) \
418+
(((ts1)->tv_sec < (ts2)->tv_sec) ? -1 : \
419+
((ts1)->tv_sec > (ts2)->tv_sec) ? 1 : \
420+
(ts1)->tv_nsec - (ts2)->tv_nsec)
392421

393422
/****************************************************************************
394423
* Name: clock_compare

libs/libc/sched/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ set(SRCS
2323
sched_getprioritymin.c
2424
clock_ticks2time.c
2525
clock_time2ticks.c
26-
clock_timespec_add.c
27-
clock_timespec_subtract.c
2826
clock_getcpuclockid.c
2927
clock_getres.c
3028
task_cancelpt.c

libs/libc/sched/Make.defs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
CSRCS += sched_getprioritymax.c sched_getprioritymin.c
2424
CSRCS += clock_ticks2time.c clock_time2ticks.c
25-
CSRCS += clock_timespec_add.c clock_timespec_subtract.c
2625
CSRCS += clock_getcpuclockid.c clock_getres.c
2726
CSRCS += task_cancelpt.c task_setcancelstate.c task_setcanceltype.c
2827
CSRCS += task_testcancel.c

libs/libc/sched/clock_timespec_add.c

Lines changed: 0 additions & 66 deletions
This file was deleted.

libs/libc/sched/clock_timespec_subtract.c

Lines changed: 0 additions & 85 deletions
This file was deleted.

sched/clock/clock_abstime2ticks.c

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,6 @@
3333
* Public Functions
3434
****************************************************************************/
3535

36-
/****************************************************************************
37-
* Name: clock_timespec_compare
38-
*
39-
* Description:
40-
* Return < 0 if time a is before time b
41-
* Return > 0 if time b is before time a
42-
* Return 0 if time a is the same as time b
43-
*
44-
****************************************************************************/
45-
46-
int clock_timespec_compare(FAR const struct timespec *a,
47-
FAR const struct timespec *b)
48-
{
49-
if (a->tv_sec < b->tv_sec)
50-
{
51-
return -1;
52-
}
53-
54-
if (a->tv_sec > b->tv_sec)
55-
{
56-
return 1;
57-
}
58-
59-
return a->tv_nsec - b->tv_nsec;
60-
}
61-
6236
/****************************************************************************
6337
* Name: clock_abstime2ticks
6438
*

0 commit comments

Comments
 (0)