clock: use clock_timespec_add/subtract to optimize code

Signed-off-by: ligd <liguiding1@xiaomi.com>
This commit is contained in:
ligd 2024-05-24 23:21:35 +08:00 committed by Xiang Xiao
parent 45d6b5f8f2
commit a10c46d02e
2 changed files with 3 additions and 54 deletions

View File

@ -60,7 +60,6 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp)
{
#ifndef CONFIG_CLOCK_TIMEKEEPING
struct timespec ts;
uint32_t carry;
#endif
int ret = OK;
@ -120,25 +119,8 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp)
*/
flags = spin_lock_irqsave(NULL);
ts.tv_sec += g_basetime.tv_sec;
ts.tv_nsec += g_basetime.tv_nsec;
clock_timespec_add(&g_basetime, &ts, tp);
spin_unlock_irqrestore(NULL, flags);
/* Handle carry to seconds. */
if (ts.tv_nsec >= NSEC_PER_SEC)
{
carry = ts.tv_nsec / NSEC_PER_SEC;
ts.tv_sec += carry;
ts.tv_nsec -= (carry * NSEC_PER_SEC);
}
/* And return the result to the caller. */
tp->tv_sec = ts.tv_sec;
tp->tv_nsec = ts.tv_nsec;
}
#endif /* CONFIG_CLOCK_TIMEKEEPING */
}

View File

@ -309,7 +309,6 @@ void clock_resynchronize(FAR struct timespec *rtc_diff)
struct timespec curr_ts;
struct timespec rtc_diff_tmp;
irqstate_t flags;
int32_t carry;
int ret;
if (rtc_diff == NULL)
@ -346,17 +345,7 @@ void clock_resynchronize(FAR struct timespec *rtc_diff)
* was last set, this gives us the current time.
*/
curr_ts.tv_sec = bias.tv_sec + g_basetime.tv_sec;
curr_ts.tv_nsec = bias.tv_nsec + g_basetime.tv_nsec;
/* Handle carry to seconds. */
if (curr_ts.tv_nsec >= NSEC_PER_SEC)
{
carry = curr_ts.tv_nsec / NSEC_PER_SEC;
curr_ts.tv_sec += carry;
curr_ts.tv_nsec -= (carry * NSEC_PER_SEC);
}
clock_timespec_add(&bias, &g_basetime, &curr_ts);
/* Check if RTC has advanced past system time. */
@ -377,29 +366,7 @@ void clock_resynchronize(FAR struct timespec *rtc_diff)
{
/* Output difference between time at entry and new current time. */
rtc_diff->tv_sec = rtc_time.tv_sec - curr_ts.tv_sec;
rtc_diff->tv_nsec = rtc_time.tv_nsec - curr_ts.tv_nsec;
/* Handle carry to seconds. */
if (rtc_diff->tv_nsec < 0)
{
carry = -((-(rtc_diff->tv_nsec + 1)) / NSEC_PER_SEC + 1);
}
else if (rtc_diff->tv_nsec >= NSEC_PER_SEC)
{
carry = rtc_diff->tv_nsec / NSEC_PER_SEC;
}
else
{
carry = 0;
}
if (carry != 0)
{
rtc_diff->tv_sec += carry;
rtc_diff->tv_nsec -= (carry * NSEC_PER_SEC);
}
clock_timespec_subtract(&rtc_time, &curr_ts, rtc_diff);
/* Add the sleep time to correct system timer */