Skip to content

Commit 0890c46

Browse files
committed
clock: use clock_timespec_add/subtract to optimize code
Signed-off-by: ligd <[email protected]>
1 parent 9921d1c commit 0890c46

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
@@ -58,7 +58,6 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp)
5858
{
5959
#ifndef CONFIG_CLOCK_TIMEKEEPING
6060
struct timespec ts;
61-
uint32_t carry;
6261
#endif
6362
int ret = OK;
6463

@@ -118,25 +117,8 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp)
118117
*/
119118

120119
flags = spin_lock_irqsave(NULL);
121-
122-
ts.tv_sec += g_basetime.tv_sec;
123-
ts.tv_nsec += g_basetime.tv_nsec;
124-
120+
clock_timespec_add(&g_basetime, &ts, tp);
125121
spin_unlock_irqrestore(NULL, flags);
126-
127-
/* Handle carry to seconds. */
128-
129-
if (ts.tv_nsec >= NSEC_PER_SEC)
130-
{
131-
carry = ts.tv_nsec / NSEC_PER_SEC;
132-
ts.tv_sec += carry;
133-
ts.tv_nsec -= (carry * NSEC_PER_SEC);
134-
}
135-
136-
/* And return the result to the caller. */
137-
138-
tp->tv_sec = ts.tv_sec;
139-
tp->tv_nsec = ts.tv_nsec;
140122
}
141123
#endif /* CONFIG_CLOCK_TIMEKEEPING */
142124
}

sched/clock/clock_initialize.c

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,6 @@ void clock_resynchronize(FAR struct timespec *rtc_diff)
307307
struct timespec curr_ts;
308308
struct timespec rtc_diff_tmp;
309309
irqstate_t flags;
310-
int32_t carry;
311310
int ret;
312311

313312
if (rtc_diff == NULL)
@@ -344,17 +343,7 @@ void clock_resynchronize(FAR struct timespec *rtc_diff)
344343
* was last set, this gives us the current time.
345344
*/
346345

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

359348
/* Check if RTC has advanced past system time. */
360349

@@ -375,29 +364,7 @@ void clock_resynchronize(FAR struct timespec *rtc_diff)
375364
{
376365
/* Output difference between time at entry and new current time. */
377366

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

402369
/* Add the sleep time to correct system timer */
403370

0 commit comments

Comments
 (0)