Skip to content

Commit e748d42

Browse files
committed
clock: use clock_timespec_add/subtract to optimize code
Signed-off-by: ligd <[email protected]>
1 parent 70a35eb commit e748d42

File tree

2 files changed

+3
-54
lines changed

2 files changed

+3
-54
lines changed

sched/clock/clock_gettime.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp)
6060
{
6161
#ifndef CONFIG_CLOCK_TIMEKEEPING
6262
struct timespec ts;
63-
uint32_t carry;
6463
#endif
6564
int ret = OK;
6665

@@ -120,25 +119,8 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp)
120119
*/
121120

122121
flags = spin_lock_irqsave(NULL);
123-
124-
ts.tv_sec += g_basetime.tv_sec;
125-
ts.tv_nsec += g_basetime.tv_nsec;
126-
122+
clock_timespec_add(&g_basetime, &ts, tp);
127123
spin_unlock_irqrestore(NULL, flags);
128-
129-
/* Handle carry to seconds. */
130-
131-
if (ts.tv_nsec >= NSEC_PER_SEC)
132-
{
133-
carry = ts.tv_nsec / NSEC_PER_SEC;
134-
ts.tv_sec += carry;
135-
ts.tv_nsec -= (carry * NSEC_PER_SEC);
136-
}
137-
138-
/* And return the result to the caller. */
139-
140-
tp->tv_sec = ts.tv_sec;
141-
tp->tv_nsec = ts.tv_nsec;
142124
}
143125
#endif /* CONFIG_CLOCK_TIMEKEEPING */
144126
}

sched/clock/clock_initialize.c

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ void clock_resynchronize(FAR struct timespec *rtc_diff)
309309
struct timespec curr_ts;
310310
struct timespec rtc_diff_tmp;
311311
irqstate_t flags;
312-
int32_t carry;
313312
int ret;
314313

315314
if (rtc_diff == NULL)
@@ -346,17 +345,7 @@ void clock_resynchronize(FAR struct timespec *rtc_diff)
346345
* was last set, this gives us the current time.
347346
*/
348347

349-
curr_ts.tv_sec = bias.tv_sec + g_basetime.tv_sec;
350-
curr_ts.tv_nsec = bias.tv_nsec + g_basetime.tv_nsec;
351-
352-
/* Handle carry to seconds. */
353-
354-
if (curr_ts.tv_nsec >= NSEC_PER_SEC)
355-
{
356-
carry = curr_ts.tv_nsec / NSEC_PER_SEC;
357-
curr_ts.tv_sec += carry;
358-
curr_ts.tv_nsec -= (carry * NSEC_PER_SEC);
359-
}
348+
clock_timespec_add(&bias, &g_basetime, &curr_ts);
360349

361350
/* Check if RTC has advanced past system time. */
362351

@@ -377,29 +366,7 @@ void clock_resynchronize(FAR struct timespec *rtc_diff)
377366
{
378367
/* Output difference between time at entry and new current time. */
379368

380-
rtc_diff->tv_sec = rtc_time.tv_sec - curr_ts.tv_sec;
381-
rtc_diff->tv_nsec = rtc_time.tv_nsec - curr_ts.tv_nsec;
382-
383-
/* Handle carry to seconds. */
384-
385-
if (rtc_diff->tv_nsec < 0)
386-
{
387-
carry = -((-(rtc_diff->tv_nsec + 1)) / NSEC_PER_SEC + 1);
388-
}
389-
else if (rtc_diff->tv_nsec >= NSEC_PER_SEC)
390-
{
391-
carry = rtc_diff->tv_nsec / NSEC_PER_SEC;
392-
}
393-
else
394-
{
395-
carry = 0;
396-
}
397-
398-
if (carry != 0)
399-
{
400-
rtc_diff->tv_sec += carry;
401-
rtc_diff->tv_nsec -= (carry * NSEC_PER_SEC);
402-
}
369+
clock_timespec_subtract(&rtc_time, &curr_ts, rtc_diff);
403370

404371
/* Add the sleep time to correct system timer */
405372

0 commit comments

Comments
 (0)