Skip to content

Commit 38f921f

Browse files
committed
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>
1 parent 0890c46 commit 38f921f

24 files changed

+82
-289
lines changed

arch/risc-v/src/bl602/bl602_os_hal.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,6 @@ int bl_os_timer_start_once(void *timerid, long t_sec, long t_nsec)
989989
{
990990
struct timer_adpt *timer;
991991
struct timespec reltime;
992-
int32_t tick;
993992

994993
timer = (struct timer_adpt *)timerid;
995994

@@ -1001,10 +1000,8 @@ int bl_os_timer_start_once(void *timerid, long t_sec, long t_nsec)
10011000
reltime.tv_nsec = t_nsec;
10021001
reltime.tv_sec = t_sec;
10031002

1004-
clock_time2ticks(&reltime, &tick);
1005-
10061003
timer->mode = BL_OS_TIEMR_ONCE;
1007-
timer->delay = tick;
1004+
timer->delay = clock_time2ticks(&reltime);
10081005

10091006
return wd_start(&timer->wdog,
10101007
timer->delay,
@@ -1027,7 +1024,6 @@ int bl_os_timer_start_periodic(void *timerid, long t_sec, long t_nsec)
10271024
{
10281025
struct timer_adpt *timer;
10291026
struct timespec reltime;
1030-
int32_t tick;
10311027

10321028
timer = (struct timer_adpt *)timerid;
10331029

@@ -1039,10 +1035,8 @@ int bl_os_timer_start_periodic(void *timerid, long t_sec, long t_nsec)
10391035
reltime.tv_nsec = t_nsec;
10401036
reltime.tv_sec = t_sec;
10411037

1042-
clock_time2ticks(&reltime, &tick);
1043-
10441038
timer->mode = BL_OS_TIEMR_CYCLE;
1045-
timer->delay = tick;
1039+
timer->delay = clock_time2ticks(&reltime);
10461040

10471041
return wd_start(&timer->wdog,
10481042
timer->delay,

drivers/timers/arch_alarm.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
#define CONFIG_BOARD_LOOPSPER10USEC ((CONFIG_BOARD_LOOPSPERMSEC+50)/100)
3737
#define CONFIG_BOARD_LOOPSPERUSEC ((CONFIG_BOARD_LOOPSPERMSEC+500)/1000)
3838

39-
#define timespec_to_nsec(ts) \
40-
((uint64_t)(ts)->tv_sec * NSEC_PER_SEC + (ts)->tv_nsec)
41-
4239
/****************************************************************************
4340
* Private Data
4441
****************************************************************************/
@@ -53,22 +50,14 @@ static clock_t g_current_tick;
5350
* Private Functions
5451
****************************************************************************/
5552

56-
static inline void timespec_from_nsec(FAR struct timespec *ts,
57-
uint64_t nanoseconds)
58-
{
59-
ts->tv_sec = nanoseconds / NSEC_PER_SEC;
60-
nanoseconds -= (uint64_t)ts->tv_sec * NSEC_PER_SEC;
61-
ts->tv_nsec = nanoseconds;
62-
}
63-
6453
static void udelay_accurate(useconds_t microseconds)
6554
{
6655
struct timespec now;
6756
struct timespec end;
6857
struct timespec delta;
6958

7059
ONESHOT_CURRENT(g_oneshot_lower, &now);
71-
timespec_from_nsec(&delta, (uint64_t)microseconds * NSEC_PER_USEC);
60+
clock_nsec2time(&delta, (uint64_t)microseconds * NSEC_PER_USEC);
7261
clock_timespec_add(&now, &delta, &end);
7362

7463
while (clock_timespec_compare(&now, &end) < 0)
@@ -375,7 +364,7 @@ unsigned long up_perf_gettime(void)
375364
struct timespec ts;
376365

377366
ONESHOT_CURRENT(g_oneshot_lower, &ts);
378-
ret = timespec_to_nsec(&ts);
367+
ret = clock_time2nsec(&ts);
379368
}
380369

381370
return ret;
@@ -388,7 +377,7 @@ unsigned long up_perf_getfreq(void)
388377

389378
void up_perf_convert(unsigned long elapsed, FAR struct timespec *ts)
390379
{
391-
timespec_from_nsec(ts, elapsed);
380+
clock_nsec2time(ts, elapsed);
392381
}
393382
#endif /* CONFIG_ARCH_PERF_EVENTS */
394383

drivers/timers/arch_timer.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,6 @@ static struct arch_timer_s g_timer;
6161
* Private Functions
6262
****************************************************************************/
6363

64-
static inline void timespec_from_usec(FAR struct timespec *ts,
65-
uint64_t microseconds)
66-
{
67-
ts->tv_sec = microseconds / USEC_PER_SEC;
68-
microseconds -= (uint64_t)ts->tv_sec * USEC_PER_SEC;
69-
ts->tv_nsec = microseconds * NSEC_PER_USEC;
70-
}
71-
7264
#ifdef CONFIG_SCHED_TICKLESS
7365

7466
static uint32_t update_timeout(uint32_t timeout)
@@ -421,7 +413,7 @@ unsigned long up_perf_getfreq(void)
421413
void up_perf_convert(unsigned long elapsed,
422414
FAR struct timespec *ts)
423415
{
424-
timespec_from_usec(ts, elapsed);
416+
clock_usec2time(ts, elapsed);
425417
}
426418
#endif /* CONFIG_ARCH_PERF_EVENTS */
427419

fs/vfs/fs_timerfd.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,8 @@ int timerfd_settime(int fd, int flags,
535535

536536
/* Convert that to a struct timespec and return it */
537537

538-
clock_ticks2time(delay, &old_value->it_value);
539-
clock_ticks2time(dev->delay, &old_value->it_interval);
538+
clock_ticks2time(&old_value->it_value, delay);
539+
clock_ticks2time(&old_value->it_interval, dev->delay);
540540
}
541541

542542
/* Disarm the timer (in case the timer was already armed when
@@ -561,7 +561,7 @@ int timerfd_settime(int fd, int flags,
561561

562562
/* Setup up any repetitive timer */
563563

564-
clock_time2ticks(&new_value->it_interval, &delay);
564+
delay = clock_time2ticks(&new_value->it_interval);
565565
dev->delay = delay;
566566

567567
/* We need to disable timer interrupts through the following section so
@@ -583,7 +583,7 @@ int timerfd_settime(int fd, int flags,
583583
* returns success.
584584
*/
585585

586-
clock_time2ticks(&new_value->it_value, &delay);
586+
delay = clock_time2ticks(&new_value->it_value);
587587
}
588588

589589
/* 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)
651651

652652
/* Convert that to a struct timespec and return it */
653653

654-
clock_ticks2time(ticks, &curr_value->it_value);
655-
clock_ticks2time(dev->delay, &curr_value->it_interval);
654+
clock_ticks2time(&curr_value->it_value, ticks);
655+
clock_ticks2time(&curr_value->it_interval, dev->delay);
656656
return OK;
657657

658658
errout:

include/nuttx/clock.h

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ EXTERN volatile clock_t g_system_ticks;
325325
* Public Function Prototypes
326326
****************************************************************************/
327327

328-
#define timespec_from_tick(ts, tick) \
328+
#define clock_ticks2time(ts, tick) \
329329
do \
330330
{ \
331331
clock_t _tick = (tick); \
@@ -335,9 +335,35 @@ EXTERN volatile clock_t g_system_ticks;
335335
} \
336336
while (0)
337337

338-
#define timespec_to_tick(ts) \
338+
#define clock_time2ticks(ts) \
339339
((clock_t)(ts)->tv_sec * TICK_PER_SEC + (ts)->tv_nsec / NSEC_PER_TICK)
340340

341+
#define clock_usec2time(ts, usec) \
342+
do \
343+
{ \
344+
uint64_t _usec = (usec); \
345+
(ts)->tv_sec = _usec / USEC_PER_SEC; \
346+
_usec -= (uint64_t)(ts)->tv_sec * USEC_PER_SEC; \
347+
(ts)->tv_nsec = _usec * NSEC_PER_USEC; \
348+
} \
349+
while (0)
350+
351+
#define clock_time2usec(ts) \
352+
((uint64_t)(ts)->tv_sec * USEC_PER_SEC + (ts)->tv_nsec / NSEC_PER_USEC)
353+
354+
#define clock_nsec2time(ts, nsec) \
355+
do \
356+
{ \
357+
uint64_t _nsec = (nsec); \
358+
(ts)->tv_sec = _nsec / NSEC_PER_SEC; \
359+
_nsec -= (uint64_t)(ts)->tv_sec * NSEC_PER_SEC; \
360+
(ts)->tv_nsec = _nsec; \
361+
} \
362+
while (0)
363+
364+
#define clock_time2nsec(ts) \
365+
((uint64_t)(ts)->tv_sec * NSEC_PER_SEC + (ts)->tv_nsec)
366+
341367
/****************************************************************************
342368
* Name: clock_timespec_add
343369
*
@@ -561,51 +587,6 @@ void clock_resynchronize(FAR struct timespec *rtc_diff);
561587
clock_t clock_systime_ticks(void);
562588
#endif
563589

564-
/****************************************************************************
565-
* Name: clock_time2ticks
566-
*
567-
* Description:
568-
* Return the given struct timespec as systime ticks.
569-
*
570-
* NOTE: This is an internal OS interface and should not be called from
571-
* application code.
572-
*
573-
* Input Parameters:
574-
* reltime - Pointer to the time presented as struct timespec
575-
*
576-
* Output Parameters:
577-
* ticks - Pointer to receive the time value presented as systime ticks
578-
*
579-
* Returned Value:
580-
* Always returns OK (0)
581-
*
582-
****************************************************************************/
583-
584-
int clock_time2ticks(FAR const struct timespec *reltime,
585-
FAR sclock_t *ticks);
586-
587-
/****************************************************************************
588-
* Name: clock_ticks2time
589-
*
590-
* Description:
591-
* Return the given systime ticks as a struct timespec.
592-
*
593-
* NOTE: This is an internal OS interface and should not be called from
594-
* application code.
595-
*
596-
* Input Parameters:
597-
* ticks - Time presented as systime ticks
598-
*
599-
* Output Parameters:
600-
* reltime - Pointer to receive the time value presented as struct timespec
601-
*
602-
* Returned Value:
603-
* Always returns OK (0)
604-
*
605-
****************************************************************************/
606-
607-
int clock_ticks2time(sclock_t ticks, FAR struct timespec *reltime);
608-
609590
/****************************************************************************
610591
* Name: clock_systime_timespec
611592
*

include/nuttx/timers/oneshot.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ int oneshot_max_delay(FAR struct oneshot_lowerhalf_s *lower,
270270
DEBUGASSERT(lower->ops->tick_max_delay);
271271

272272
ret = lower->ops->tick_max_delay(lower, &tick);
273-
timespec_from_tick(ts, tick);
273+
clock_ticks2time(ts, tick);
274274
return ret;
275275
}
276276

@@ -283,7 +283,7 @@ int oneshot_start(FAR struct oneshot_lowerhalf_s *lower,
283283

284284
DEBUGASSERT(lower->ops->tick_start);
285285

286-
tick = timespec_to_tick(ts);
286+
tick = clock_time2ticks(ts);
287287
return lower->ops->tick_start(lower, callback, arg, tick);
288288
}
289289

@@ -297,7 +297,7 @@ int oneshot_cancel(FAR struct oneshot_lowerhalf_s *lower,
297297
DEBUGASSERT(lower->ops->tick_cancel);
298298

299299
ret = lower->ops->tick_cancel(lower, &tick);
300-
timespec_from_tick(ts, tick);
300+
clock_ticks2time(ts, tick);
301301

302302
return ret;
303303
}
@@ -312,7 +312,7 @@ int oneshot_current(FAR struct oneshot_lowerhalf_s *lower,
312312
DEBUGASSERT(lower->ops->tick_current);
313313

314314
ret = lower->ops->tick_current(lower, &tick);
315-
timespec_from_tick(ts, tick);
315+
clock_ticks2time(ts, tick);
316316

317317
return ret;
318318
}
@@ -327,7 +327,7 @@ int oneshot_tick_max_delay(FAR struct oneshot_lowerhalf_s *lower,
327327
DEBUGASSERT(lower->ops->max_delay);
328328

329329
ret = lower->ops->max_delay(lower, &ts);
330-
*ticks = timespec_to_tick(&ts);
330+
*ticks = clock_time2ticks(&ts);
331331
return ret;
332332
}
333333

@@ -340,7 +340,7 @@ int oneshot_tick_start(FAR struct oneshot_lowerhalf_s *lower,
340340

341341
DEBUGASSERT(lower->ops->start);
342342

343-
timespec_from_tick(&ts, ticks);
343+
clock_ticks2time(&ts, ticks);
344344
return lower->ops->start(lower, callback, arg, &ts);
345345
}
346346

@@ -354,7 +354,7 @@ int oneshot_tick_cancel(FAR struct oneshot_lowerhalf_s *lower,
354354
DEBUGASSERT(lower->ops->cancel);
355355

356356
ret = lower->ops->cancel(lower, &ts);
357-
*ticks = timespec_to_tick(&ts);
357+
*ticks = clock_time2ticks(&ts);
358358

359359
return ret;
360360
}
@@ -369,7 +369,7 @@ int oneshot_tick_current(FAR struct oneshot_lowerhalf_s *lower,
369369
DEBUGASSERT(lower->ops->current);
370370

371371
ret = lower->ops->current(lower, &ts);
372-
*ticks = timespec_to_tick(&ts);
372+
*ticks = clock_time2ticks(&ts);
373373

374374
return ret;
375375
}

libs/libc/misc/lib_mutex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ int nxmutex_timedlock(FAR mutex_t *mutex, unsigned int timeout)
353353
struct timespec rqtp;
354354

355355
clock_gettime(CLOCK_MONOTONIC, &now);
356-
clock_ticks2time(MSEC2TICK(timeout), &delay);
356+
clock_ticks2time(&delay, MSEC2TICK(timeout));
357357
clock_timespec_add(&now, &delay, &rqtp);
358358

359359
/* Wait until we get the lock or until the timeout expires */

libs/libc/sched/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
set(SRCS
2222
sched_getprioritymax.c
2323
sched_getprioritymin.c
24-
clock_ticks2time.c
25-
clock_time2ticks.c
2624
clock_getcpuclockid.c
2725
clock_getres.c
2826
task_cancelpt.c

libs/libc/sched/Make.defs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
# Add the sched C files to the build
2222

2323
CSRCS += sched_getprioritymax.c sched_getprioritymin.c
24-
CSRCS += clock_ticks2time.c clock_time2ticks.c
2524
CSRCS += clock_getcpuclockid.c clock_getres.c
2625
CSRCS += task_cancelpt.c task_setcancelstate.c task_setcanceltype.c
2726
CSRCS += task_testcancel.c

0 commit comments

Comments
 (0)