diff options
| author | xqyjlj <[email protected]> | 2023-04-13 16:00:19 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-04-13 16:00:19 +0800 |
| commit | 970c7c6f7a8b6e26e3254de5c9c0877b18fa0627 (patch) | |
| tree | 13ccd1fd1ca4b87d117966303fb3833ba31fb583 /components/libc/compilers | |
| parent | 590e603997de5c3a96bfd5311fc5c04fac7bc90d (diff) | |
🐞 fix(components/drivers): fix cpu timer in multithreading (#7222)
* 🐞 fix(components/drivers): fix cpu timer in multithreading
* 🎈 perf(components): change double to uint64_t
* 🎈 perf(components): add UL suffix
Diffstat (limited to 'components/libc/compilers')
| -rw-r--r-- | components/libc/compilers/common/ctime.c | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/components/libc/compilers/common/ctime.c b/components/libc/compilers/common/ctime.c index c1573bf661..88f4560d3c 100644 --- a/components/libc/compilers/common/ctime.c +++ b/components/libc/compilers/common/ctime.c @@ -536,9 +536,9 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) return -1; } #ifdef RT_USING_CPUTIME - double unit = clock_cpu_getres(); + rt_uint64_t unit = clock_cpu_getres(); rt_uint64_t ns = rqtp->tv_sec * NANOSECOND_PER_SECOND + rqtp->tv_nsec; - rt_uint64_t tick = ns / unit; + rt_uint64_t tick = (ns * (1000UL * 1000)) / unit; rt_cputime_sleep(tick); if (rt_get_errno() == -RT_EINTR) @@ -546,8 +546,8 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) if (rmtp) { uint64_t rmtp_cpu_tick = tick - clock_cpu_gettime(); - rmtp->tv_sec = ((time_t)(rmtp_cpu_tick * unit)) / NANOSECOND_PER_SECOND; - rmtp->tv_nsec = ((long)(rmtp_cpu_tick * unit)) % NANOSECOND_PER_SECOND; + rmtp->tv_sec = ((time_t)((rmtp_cpu_tick * unit) / (1000UL * 1000))) / NANOSECOND_PER_SECOND; + rmtp->tv_nsec = ((long)((rmtp_cpu_tick * unit) / (1000UL * 1000))) % NANOSECOND_PER_SECOND; } rt_set_errno(EINTR); return -1; @@ -634,7 +634,7 @@ int clock_getres(clockid_t clockid, struct timespec *res) #ifdef RT_USING_CPUTIME case CLOCK_CPUTIME_ID: res->tv_sec = 0; - res->tv_nsec = clock_cpu_getres(); + res->tv_nsec = (clock_cpu_getres() / (1000UL * 1000)); break; #endif @@ -684,14 +684,14 @@ int clock_gettime(clockid_t clockid, struct timespec *tp) case CLOCK_MONOTONIC: case CLOCK_CPUTIME_ID: { - double unit = 0; + uint64_t unit = 0; uint64_t cpu_tick; unit = clock_cpu_getres(); cpu_tick = clock_cpu_gettime(); - tp->tv_sec = ((uint64_t)(cpu_tick * unit)) / NANOSECOND_PER_SECOND; - tp->tv_nsec = ((uint64_t)(cpu_tick * unit)) % NANOSECOND_PER_SECOND; + tp->tv_sec = ((uint64_t)((cpu_tick * unit) / (1000UL * 1000))) / NANOSECOND_PER_SECOND; + tp->tv_nsec = ((uint64_t)((cpu_tick * unit) / (1000UL * 1000))) % NANOSECOND_PER_SECOND; } break; #endif @@ -757,9 +757,9 @@ int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, s case CLOCK_CPUTIME_ID: { rt_uint64_t cpu_tick_old = clock_cpu_gettime(); - double unit = clock_cpu_getres(); + uint64_t unit = clock_cpu_getres(); rt_uint64_t ns = rqtp->tv_sec * NANOSECOND_PER_SECOND + rqtp->tv_nsec; - rt_uint64_t tick = ns / unit; + rt_uint64_t tick = (ns * (1000UL * 1000)) / unit; if ((flags & TIMER_ABSTIME) == TIMER_ABSTIME) tick -= cpu_tick_old; rt_cputime_sleep(tick); @@ -769,8 +769,8 @@ int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, s if (rmtp) { uint64_t rmtp_cpu_tick = tick - clock_cpu_gettime(); - rmtp->tv_sec = ((time_t)(rmtp_cpu_tick * unit)) / NANOSECOND_PER_SECOND; - rmtp->tv_nsec = ((long)(rmtp_cpu_tick * unit)) % NANOSECOND_PER_SECOND; + rmtp->tv_sec = ((time_t)((rmtp_cpu_tick * unit) / (1000UL * 1000))) / NANOSECOND_PER_SECOND; + rmtp->tv_nsec = ((long)((rmtp_cpu_tick * unit) / (1000UL * 1000))) % NANOSECOND_PER_SECOND; } rt_set_errno(EINTR); return -1; @@ -906,7 +906,7 @@ static void rtthread_timer_wrapper(void *timerobj) #ifdef RT_USING_CPUTIME if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout()) { - timer->reload = (timer->interval.tv_sec * NANOSECOND_PER_SECOND + timer->interval.tv_nsec) / clock_cpu_getres(); + timer->reload = ((timer->interval.tv_sec * NANOSECOND_PER_SECOND + timer->interval.tv_nsec) * (1000UL * 1000)) / clock_cpu_getres(); if (timer->reload) rt_cputimer_control(&timer->cputimer, RT_TIMER_CTRL_SET_TIME, &(timer->reload)); } @@ -1174,7 +1174,7 @@ int timer_gettime(timer_t timerid, struct itimerspec *its) rt_uint64_t remain_tick; rt_uint64_t remaining; rt_cputimer_control(&timer->cputimer, RT_TIMER_CTRL_GET_REMAIN_TIME, &remain_tick); - remaining = (remain_tick - clock_cpu_gettime()) / clock_cpu_getres(); + remaining = ((remain_tick - clock_cpu_gettime()) * (1000UL * 1000)) / clock_cpu_getres(); seconds = remaining / NANOSECOND_PER_SECOND; nanoseconds = remaining % NANOSECOND_PER_SECOND; } @@ -1278,9 +1278,9 @@ int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, if (timer->clockid == CLOCK_CPUTIME_ID && clock_cpu_issettimeout()) { rt_uint64_t tick; - double unit = clock_cpu_getres(); + uint64_t unit = clock_cpu_getres(); - tick = (value->it_value.tv_sec * NANOSECOND_PER_SECOND + value->it_value.tv_nsec) / unit; + tick = ((value->it_value.tv_sec * NANOSECOND_PER_SECOND + value->it_value.tv_nsec) * (1000UL * 1000)) / unit; if ((flags & TIMER_ABSTIME) == TIMER_ABSTIME) { tick -= clock_cpu_gettime(); |
