解决ktime部分函数计算时unsigned long溢出 (#9008)

* 解决ktime部分函数计算时unsigned long溢出

* 恢复部分不需要ktime中部分rt_uint64_t类型的为unsigned long
This commit is contained in:
LaterComer 2024-05-30 12:04:33 +08:00 committed by GitHub
parent 2fcf151a8e
commit 2cbe8bdae0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 74 additions and 74 deletions

View File

@ -23,7 +23,7 @@ AlignConsecutiveBitFields:
AlignCompound: true AlignCompound: true
PadOperators: true PadOperators: true
AlignConsecutiveDeclarations: AlignConsecutiveDeclarations:
Enabled: false Enabled: true
AcrossEmptyLines: false AcrossEmptyLines: false
AcrossComments: false AcrossComments: false
AlignCompound: false AlignCompound: false

View File

@ -18,13 +18,13 @@
#include "rtthread.h" #include "rtthread.h"
#define RT_KTIME_RESMUL (1000000UL) #define RT_KTIME_RESMUL (1000000ULL)
struct rt_ktime_hrtimer struct rt_ktime_hrtimer
{ {
struct rt_object parent; /**< inherit from rt_object */ struct rt_object parent; /**< inherit from rt_object */
rt_list_t row; rt_list_t row;
void *parameter; void *parameter;
unsigned long init_cnt; unsigned long init_cnt;
unsigned long timeout_cnt; unsigned long timeout_cnt;
rt_err_t error; rt_err_t error;
@ -62,7 +62,7 @@ rt_err_t rt_ktime_boottime_get_ns(struct timespec *ts);
* *
* @return (resolution * RT_KTIME_RESMUL) * @return (resolution * RT_KTIME_RESMUL)
*/ */
unsigned long rt_ktime_cputimer_getres(void); rt_uint64_t rt_ktime_cputimer_getres(void);
/** /**
* @brief Get cputimer frequency * @brief Get cputimer frequency
@ -96,7 +96,7 @@ void rt_ktime_cputimer_init(void);
* *
* @return (resolution * RT_KTIME_RESMUL) * @return (resolution * RT_KTIME_RESMUL)
*/ */
unsigned long rt_ktime_hrtimer_getres(void); rt_uint64_t rt_ktime_hrtimer_getres(void);
/** /**
* @brief Get hrtimer frequency * @brief Get hrtimer frequency

View File

@ -13,9 +13,9 @@
static volatile unsigned long _init_cnt = 0; static volatile unsigned long _init_cnt = 0;
unsigned long rt_ktime_cputimer_getres(void) rt_uint64_t rt_ktime_cputimer_getres(void)
{ {
return ((1000UL * 1000 * 1000) * RT_KTIME_RESMUL) / rt_hw_get_gtimer_frq(); return ((1000ULL * 1000 * 1000) * RT_KTIME_RESMUL) / rt_hw_get_gtimer_frq();
} }
unsigned long rt_ktime_cputimer_getfrq(void) unsigned long rt_ktime_cputimer_getfrq(void)

View File

@ -10,16 +10,16 @@
#include "ktime.h" #include "ktime.h"
#define __KTIME_MUL ((1000UL * 1000 * 1000) / RT_TICK_PER_SECOND) #define __KTIME_MUL ((1000ULL * 1000 * 1000) / RT_TICK_PER_SECOND)
rt_weak rt_err_t rt_ktime_boottime_get_us(struct timeval *tv) rt_weak rt_err_t rt_ktime_boottime_get_us(struct timeval *tv)
{ {
RT_ASSERT(tv != RT_NULL); RT_ASSERT(tv != RT_NULL);
unsigned long ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL; rt_uint64_t ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL;
tv->tv_sec = ns / (1000UL * 1000 * 1000); tv->tv_sec = ns / (1000ULL * 1000 * 1000);
tv->tv_usec = (ns % (1000UL * 1000 * 1000)) / 1000; tv->tv_usec = (ns % (1000ULL * 1000 * 1000)) / 1000;
return RT_EOK; return RT_EOK;
} }
@ -28,9 +28,9 @@ rt_weak rt_err_t rt_ktime_boottime_get_s(time_t *t)
{ {
RT_ASSERT(t != RT_NULL); RT_ASSERT(t != RT_NULL);
unsigned long ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL; rt_uint64_t ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL;
*t = ns / (1000UL * 1000 * 1000); *t = ns / (1000ULL * 1000 * 1000);
return RT_EOK; return RT_EOK;
} }
@ -39,10 +39,10 @@ rt_weak rt_err_t rt_ktime_boottime_get_ns(struct timespec *ts)
{ {
RT_ASSERT(ts != RT_NULL); RT_ASSERT(ts != RT_NULL);
unsigned long ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL; rt_uint64_t ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL;
ts->tv_sec = ns / (1000UL * 1000 * 1000); ts->tv_sec = ns / (1000ULL * 1000 * 1000);
ts->tv_nsec = ns % (1000UL * 1000 * 1000); ts->tv_nsec = ns % (1000ULL * 1000 * 1000);
return RT_EOK; return RT_EOK;
} }

View File

@ -10,9 +10,9 @@
#include "ktime.h" #include "ktime.h"
rt_weak unsigned long rt_ktime_cputimer_getres(void) rt_weak rt_uint64_t rt_ktime_cputimer_getres(void)
{ {
return ((1000UL * 1000 * 1000) * RT_KTIME_RESMUL) / RT_TICK_PER_SECOND; return ((1000ULL * 1000 * 1000) * RT_KTIME_RESMUL) / RT_TICK_PER_SECOND;
} }
rt_weak unsigned long rt_ktime_cputimer_getfrq(void) rt_weak unsigned long rt_ktime_cputimer_getfrq(void)

View File

@ -25,9 +25,9 @@ static rt_list_t _timer_list = RT_LIST_OBJECT_INIT(_timer_list);
static rt_ktime_hrtimer_t _nowtimer = RT_NULL; static rt_ktime_hrtimer_t _nowtimer = RT_NULL;
static RT_DEFINE_SPINLOCK(_spinlock); static RT_DEFINE_SPINLOCK(_spinlock);
rt_weak unsigned long rt_ktime_hrtimer_getres(void) rt_weak rt_uint64_t rt_ktime_hrtimer_getres(void)
{ {
return ((1000UL * 1000 * 1000) * RT_KTIME_RESMUL) / RT_TICK_PER_SECOND; return ((1000ULL * 1000 * 1000) * RT_KTIME_RESMUL) / RT_TICK_PER_SECOND;
} }
rt_weak unsigned long rt_ktime_hrtimer_getfrq(void) rt_weak unsigned long rt_ktime_hrtimer_getfrq(void)
@ -181,11 +181,11 @@ void rt_ktime_hrtimer_init(rt_ktime_hrtimer_t timer,
timer->parent.flag = flag; timer->parent.flag = flag;
/* set deactivated */ /* set deactivated */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED; timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
timer->timeout_func = timeout; timer->timeout_func = timeout;
timer->parameter = parameter; timer->parameter = parameter;
timer->timeout_cnt = cnt + rt_ktime_cputimer_getcnt(); timer->timeout_cnt = cnt + rt_ktime_cputimer_getcnt();
timer->init_cnt = cnt; timer->init_cnt = cnt;
rt_list_init(&(timer->row)); rt_list_init(&(timer->row));
rt_completion_init(&timer->completion); rt_completion_init(&timer->completion);
@ -203,7 +203,7 @@ rt_err_t rt_ktime_hrtimer_start(rt_ktime_hrtimer_t timer)
rt_list_remove(&timer->row); /* remove timer from list */ rt_list_remove(&timer->row); /* remove timer from list */
/* change status of timer */ /* change status of timer */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED; timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
timer_list = &_timer_list; timer_list = &_timer_list;
for (; timer_list != _timer_list.prev; timer_list = timer_list->next) for (; timer_list != _timer_list.prev; timer_list = timer_list->next)
{ {
rt_ktime_hrtimer_t t; rt_ktime_hrtimer_t t;
@ -259,58 +259,58 @@ rt_err_t rt_ktime_hrtimer_control(rt_ktime_hrtimer_t timer, int cmd, void *arg)
level = rt_spin_lock_irqsave(&_spinlock); level = rt_spin_lock_irqsave(&_spinlock);
switch (cmd) switch (cmd)
{ {
case RT_TIMER_CTRL_GET_TIME: case RT_TIMER_CTRL_GET_TIME:
*(unsigned long *)arg = timer->init_cnt; *(unsigned long *)arg = timer->init_cnt;
break; break;
case RT_TIMER_CTRL_SET_TIME: case RT_TIMER_CTRL_SET_TIME:
RT_ASSERT((*(unsigned long *)arg) < (_HRTIMER_MAX_CNT / 2)); RT_ASSERT((*(unsigned long *)arg) < (_HRTIMER_MAX_CNT / 2));
timer->init_cnt = *(unsigned long *)arg; timer->init_cnt = *(unsigned long *)arg;
timer->timeout_cnt = *(unsigned long *)arg + rt_ktime_cputimer_getcnt(); timer->timeout_cnt = *(unsigned long *)arg + rt_ktime_cputimer_getcnt();
break; break;
case RT_TIMER_CTRL_SET_ONESHOT: case RT_TIMER_CTRL_SET_ONESHOT:
timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC; timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
break; break;
case RT_TIMER_CTRL_SET_PERIODIC: case RT_TIMER_CTRL_SET_PERIODIC:
timer->parent.flag |= RT_TIMER_FLAG_PERIODIC; timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
break; break;
case RT_TIMER_CTRL_GET_STATE: case RT_TIMER_CTRL_GET_STATE:
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
{ {
/*timer is start and run*/ /*timer is start and run*/
*(rt_uint32_t *)arg = RT_TIMER_FLAG_ACTIVATED; *(rt_uint32_t *)arg = RT_TIMER_FLAG_ACTIVATED;
} }
else else
{ {
/*timer is stop*/ /*timer is stop*/
*(rt_uint32_t *)arg = RT_TIMER_FLAG_DEACTIVATED; *(rt_uint32_t *)arg = RT_TIMER_FLAG_DEACTIVATED;
} }
break; break;
case RT_TIMER_CTRL_GET_REMAIN_TIME: case RT_TIMER_CTRL_GET_REMAIN_TIME:
*(unsigned long *)arg = timer->timeout_cnt; *(unsigned long *)arg = timer->timeout_cnt;
break; break;
case RT_TIMER_CTRL_GET_FUNC: case RT_TIMER_CTRL_GET_FUNC:
arg = (void *)timer->timeout_func; arg = (void *)timer->timeout_func;
break; break;
case RT_TIMER_CTRL_SET_FUNC: case RT_TIMER_CTRL_SET_FUNC:
timer->timeout_func = (void (*)(void *))arg; timer->timeout_func = (void (*)(void *))arg;
break; break;
case RT_TIMER_CTRL_GET_PARM: case RT_TIMER_CTRL_GET_PARM:
*(void **)arg = timer->parameter; *(void **)arg = timer->parameter;
break; break;
case RT_TIMER_CTRL_SET_PARM: case RT_TIMER_CTRL_SET_PARM:
timer->parameter = arg; timer->parameter = arg;
break; break;
default: default:
break; break;
} }
rt_spin_unlock_irqrestore(&_spinlock, level); rt_spin_unlock_irqrestore(&_spinlock, level);
@ -366,8 +366,8 @@ rt_err_t rt_ktime_hrtimer_sleep(struct rt_ktime_hrtimer *timer, unsigned long cn
if (cnt == 0) if (cnt == 0)
return -RT_EINVAL; return -RT_EINVAL;
timer->timeout_cnt = cnt + rt_ktime_cputimer_getcnt(); timer->timeout_cnt = cnt + rt_ktime_cputimer_getcnt();
timer->init_cnt = cnt; timer->init_cnt = cnt;
rt_ktime_hrtimer_start(timer); /* reset the timeout of thread timer and start it */ rt_ktime_hrtimer_start(timer); /* reset the timeout of thread timer and start it */
err = rt_completion_wait_flags(&(timer->completion), RT_WAITING_FOREVER, err = rt_completion_wait_flags(&(timer->completion), RT_WAITING_FOREVER,
@ -379,7 +379,7 @@ rt_err_t rt_ktime_hrtimer_sleep(struct rt_ktime_hrtimer *timer, unsigned long cn
rt_err_t rt_ktime_hrtimer_ndelay(struct rt_ktime_hrtimer *timer, unsigned long ns) rt_err_t rt_ktime_hrtimer_ndelay(struct rt_ktime_hrtimer *timer, unsigned long ns)
{ {
unsigned long res = rt_ktime_cputimer_getres(); rt_uint64_t res = rt_ktime_cputimer_getres();
return rt_ktime_hrtimer_sleep(timer, (ns * RT_KTIME_RESMUL) / res); return rt_ktime_hrtimer_sleep(timer, (ns * RT_KTIME_RESMUL) / res);
} }

View File

@ -12,9 +12,9 @@
static volatile unsigned long _init_cnt = 0; static volatile unsigned long _init_cnt = 0;
unsigned long rt_ktime_cputimer_getres(void) rt_uint64_t rt_ktime_cputimer_getres(void)
{ {
return ((1000UL * 1000 * 1000) * RT_KTIME_RESMUL) / CPUTIME_TIMER_FREQ; return ((1000ULL * 1000 * 1000) * RT_KTIME_RESMUL) / CPUTIME_TIMER_FREQ;
} }
unsigned long rt_ktime_cputimer_getfrq(void) unsigned long rt_ktime_cputimer_getfrq(void)