Skip to content

Commit

Permalink
解决ktime部分函数计算时unsigned long溢出 (RT-Thread#9008)
Browse files Browse the repository at this point in the history
* 解决ktime部分函数计算时unsigned long溢出

* 恢复部分不需要ktime中部分rt_uint64_t类型的为unsigned long
  • Loading branch information
ComerLater authored May 30, 2024
1 parent 2fcf151 commit 2cbe8bd
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ AlignConsecutiveBitFields:
AlignCompound: true
PadOperators: true
AlignConsecutiveDeclarations:
Enabled: false
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
Expand Down
8 changes: 4 additions & 4 deletions components/drivers/ktime/inc/ktime.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

#include "rtthread.h"

#define RT_KTIME_RESMUL (1000000UL)
#define RT_KTIME_RESMUL (1000000ULL)

struct rt_ktime_hrtimer
{
struct rt_object parent; /**< inherit from rt_object */
rt_list_t row;
void *parameter;
void *parameter;
unsigned long init_cnt;
unsigned long timeout_cnt;
rt_err_t error;
Expand Down Expand Up @@ -62,7 +62,7 @@ rt_err_t rt_ktime_boottime_get_ns(struct timespec *ts);
*
* @return (resolution * RT_KTIME_RESMUL)
*/
unsigned long rt_ktime_cputimer_getres(void);
rt_uint64_t rt_ktime_cputimer_getres(void);

/**
* @brief Get cputimer frequency
Expand Down Expand Up @@ -96,7 +96,7 @@ void rt_ktime_cputimer_init(void);
*
* @return (resolution * RT_KTIME_RESMUL)
*/
unsigned long rt_ktime_hrtimer_getres(void);
rt_uint64_t rt_ktime_hrtimer_getres(void);

/**
* @brief Get hrtimer frequency
Expand Down
4 changes: 2 additions & 2 deletions components/drivers/ktime/src/aarch64/cputimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

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)
Expand Down
18 changes: 9 additions & 9 deletions components/drivers/ktime/src/boottime.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@

#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_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_usec = (ns % (1000UL * 1000 * 1000)) / 1000;
tv->tv_sec = ns / (1000ULL * 1000 * 1000);
tv->tv_usec = (ns % (1000ULL * 1000 * 1000)) / 1000;

return RT_EOK;
}
Expand All @@ -28,9 +28,9 @@ rt_weak rt_err_t rt_ktime_boottime_get_s(time_t *t)
{
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;
}
Expand All @@ -39,10 +39,10 @@ rt_weak rt_err_t rt_ktime_boottime_get_ns(struct timespec *ts)
{
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_nsec = ns % (1000UL * 1000 * 1000);
ts->tv_sec = ns / (1000ULL * 1000 * 1000);
ts->tv_nsec = ns % (1000ULL * 1000 * 1000);

return RT_EOK;
}
4 changes: 2 additions & 2 deletions components/drivers/ktime/src/cputimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

#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)
Expand Down
116 changes: 58 additions & 58 deletions components/drivers/ktime/src/hrtimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_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)
Expand Down Expand Up @@ -181,11 +181,11 @@ void rt_ktime_hrtimer_init(rt_ktime_hrtimer_t timer,
timer->parent.flag = flag;

/* set deactivated */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
timer->timeout_func = timeout;
timer->parameter = parameter;
timer->timeout_cnt = cnt + rt_ktime_cputimer_getcnt();
timer->init_cnt = cnt;
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
timer->timeout_func = timeout;
timer->parameter = parameter;
timer->timeout_cnt = cnt + rt_ktime_cputimer_getcnt();
timer->init_cnt = cnt;

rt_list_init(&(timer->row));
rt_completion_init(&timer->completion);
Expand All @@ -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 */
/* change status of timer */
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)
{
rt_ktime_hrtimer_t t;
Expand Down Expand Up @@ -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);
switch (cmd)
{
case RT_TIMER_CTRL_GET_TIME:
*(unsigned long *)arg = timer->init_cnt;
break;

case RT_TIMER_CTRL_SET_TIME:
RT_ASSERT((*(unsigned long *)arg) < (_HRTIMER_MAX_CNT / 2));
timer->init_cnt = *(unsigned long *)arg;
timer->timeout_cnt = *(unsigned long *)arg + rt_ktime_cputimer_getcnt();
break;

case RT_TIMER_CTRL_SET_ONESHOT:
timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
break;

case RT_TIMER_CTRL_SET_PERIODIC:
timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
break;

case RT_TIMER_CTRL_GET_STATE:
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
{
/*timer is start and run*/
*(rt_uint32_t *)arg = RT_TIMER_FLAG_ACTIVATED;
}
else
{
/*timer is stop*/
*(rt_uint32_t *)arg = RT_TIMER_FLAG_DEACTIVATED;
}
break;
case RT_TIMER_CTRL_GET_TIME:
*(unsigned long *)arg = timer->init_cnt;
break;

case RT_TIMER_CTRL_SET_TIME:
RT_ASSERT((*(unsigned long *)arg) < (_HRTIMER_MAX_CNT / 2));
timer->init_cnt = *(unsigned long *)arg;
timer->timeout_cnt = *(unsigned long *)arg + rt_ktime_cputimer_getcnt();
break;

case RT_TIMER_CTRL_SET_ONESHOT:
timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
break;

case RT_TIMER_CTRL_SET_PERIODIC:
timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
break;

case RT_TIMER_CTRL_GET_STATE:
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
{
/*timer is start and run*/
*(rt_uint32_t *)arg = RT_TIMER_FLAG_ACTIVATED;
}
else
{
/*timer is stop*/
*(rt_uint32_t *)arg = RT_TIMER_FLAG_DEACTIVATED;
}
break;

case RT_TIMER_CTRL_GET_REMAIN_TIME:
*(unsigned long *)arg = timer->timeout_cnt;
break;
case RT_TIMER_CTRL_GET_FUNC:
arg = (void *)timer->timeout_func;
break;
case RT_TIMER_CTRL_GET_REMAIN_TIME:
*(unsigned long *)arg = timer->timeout_cnt;
break;
case RT_TIMER_CTRL_GET_FUNC:
arg = (void *)timer->timeout_func;
break;

case RT_TIMER_CTRL_SET_FUNC:
timer->timeout_func = (void (*)(void *))arg;
break;
case RT_TIMER_CTRL_SET_FUNC:
timer->timeout_func = (void (*)(void *))arg;
break;

case RT_TIMER_CTRL_GET_PARM:
*(void **)arg = timer->parameter;
break;
case RT_TIMER_CTRL_GET_PARM:
*(void **)arg = timer->parameter;
break;

case RT_TIMER_CTRL_SET_PARM:
timer->parameter = arg;
break;
case RT_TIMER_CTRL_SET_PARM:
timer->parameter = arg;
break;

default:
break;
default:
break;
}
rt_spin_unlock_irqrestore(&_spinlock, level);

Expand Down Expand Up @@ -366,8 +366,8 @@ rt_err_t rt_ktime_hrtimer_sleep(struct rt_ktime_hrtimer *timer, unsigned long cn
if (cnt == 0)
return -RT_EINVAL;

timer->timeout_cnt = cnt + rt_ktime_cputimer_getcnt();
timer->init_cnt = cnt;
timer->timeout_cnt = cnt + rt_ktime_cputimer_getcnt();
timer->init_cnt = cnt;

rt_ktime_hrtimer_start(timer); /* reset the timeout of thread timer and start it */
err = rt_completion_wait_flags(&(timer->completion), RT_WAITING_FOREVER,
Expand All @@ -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)
{
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);
}

Expand Down
4 changes: 2 additions & 2 deletions components/drivers/ktime/src/risc-v/virt64/cputimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

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)
Expand Down

0 comments on commit 2cbe8bd

Please sign in to comment.