diff --git a/src/ipc.c b/src/ipc.c index 35c0146dd33..fea31f488b8 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -2567,7 +2567,7 @@ static rt_err_t _rt_mb_send_wait(rt_mailbox_t mb, { struct rt_thread *thread; rt_base_t level; - rt_uint32_t tick_delta; + rt_uint32_t tick_stamp; rt_err_t ret; /* parameter check */ @@ -2577,8 +2577,8 @@ static rt_err_t _rt_mb_send_wait(rt_mailbox_t mb, /* current context checking */ RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0); - /* initialize delta tick */ - tick_delta = 0; + /* initialize tick_stamp tick */ + tick_stamp = 0; /* get current thread */ thread = rt_thread_self(); @@ -2622,7 +2622,7 @@ static rt_err_t _rt_mb_send_wait(rt_mailbox_t mb, if (timeout > 0) { /* get the start tick of timer */ - tick_delta = rt_tick_get(); + tick_stamp = rt_tick_get(); LOG_D("mb_send_wait: start timer of thread:%s", thread->parent.name); @@ -2650,8 +2650,7 @@ static rt_err_t _rt_mb_send_wait(rt_mailbox_t mb, /* if it's not waiting forever and then re-calculate timeout tick */ if (timeout > 0) { - tick_delta = rt_tick_get() - tick_delta; - timeout -= tick_delta; + timeout -= rt_tick_get_delta(tick_stamp); if (timeout < 0) timeout = 0; } @@ -2846,7 +2845,7 @@ static rt_err_t _rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeo { struct rt_thread *thread; rt_base_t level; - rt_uint32_t tick_delta; + rt_uint32_t tick_stamp; rt_err_t ret; /* parameter check */ @@ -2856,8 +2855,8 @@ static rt_err_t _rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeo /* current context checking */ RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0); - /* initialize delta tick */ - tick_delta = 0; + /* initialize tick_stamp tick */ + tick_stamp = 0; /* get current thread */ thread = rt_thread_self(); @@ -2902,7 +2901,7 @@ static rt_err_t _rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeo if (timeout > 0) { /* get the start tick of timer */ - tick_delta = rt_tick_get(); + tick_stamp = rt_tick_get(); LOG_D("mb_recv: start timer of thread:%s", thread->parent.name); @@ -2930,8 +2929,7 @@ static rt_err_t _rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeo /* if it's not waiting forever and then re-calculate timeout tick */ if (timeout > 0) { - tick_delta = rt_tick_get() - tick_delta; - timeout -= tick_delta; + timeout -= rt_tick_get_delta(tick_stamp); if (timeout < 0) timeout = 0; } @@ -3382,7 +3380,7 @@ static rt_err_t _rt_mq_send_wait(rt_mq_t mq, { rt_base_t level; struct rt_mq_message *msg; - rt_uint32_t tick_delta; + rt_uint32_t tick_stamp; struct rt_thread *thread; rt_err_t ret; @@ -3401,8 +3399,8 @@ static rt_err_t _rt_mq_send_wait(rt_mq_t mq, if (size > mq->msg_size) return -RT_ERROR; - /* initialize delta tick */ - tick_delta = 0; + /* initialize tick_stamp tick */ + tick_stamp = 0; /* get current thread */ thread = rt_thread_self(); @@ -3447,7 +3445,7 @@ static rt_err_t _rt_mq_send_wait(rt_mq_t mq, if (timeout > 0) { /* get the start tick of timer */ - tick_delta = rt_tick_get(); + tick_stamp = rt_tick_get(); LOG_D("mq_send_wait: start timer of thread:%s", thread->parent.name); @@ -3475,8 +3473,7 @@ static rt_err_t _rt_mq_send_wait(rt_mq_t mq, /* if it's not waiting forever and then re-calculate timeout tick */ if (timeout > 0) { - tick_delta = rt_tick_get() - tick_delta; - timeout -= tick_delta; + timeout -= rt_tick_get_delta(tick_stamp); if (timeout < 0) timeout = 0; } @@ -3765,7 +3762,7 @@ static rt_ssize_t _rt_mq_recv(rt_mq_t mq, struct rt_thread *thread; rt_base_t level; struct rt_mq_message *msg; - rt_uint32_t tick_delta; + rt_uint32_t tick_stamp; rt_err_t ret; rt_size_t len; @@ -3780,8 +3777,8 @@ static rt_ssize_t _rt_mq_recv(rt_mq_t mq, /* current context checking */ RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0); - /* initialize delta tick */ - tick_delta = 0; + /* initialize tick_stamp tick */ + tick_stamp = 0; /* get current thread */ thread = rt_thread_self(); RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mq->parent.parent))); @@ -3826,7 +3823,7 @@ static rt_ssize_t _rt_mq_recv(rt_mq_t mq, if (timeout > 0) { /* get the start tick of timer */ - tick_delta = rt_tick_get(); + tick_stamp = rt_tick_get(); LOG_D("set thread:%s to timer list", thread->parent.name); @@ -3855,8 +3852,7 @@ static rt_ssize_t _rt_mq_recv(rt_mq_t mq, /* if it's not waiting forever and then re-calculate timeout tick */ if (timeout > 0) { - tick_delta = rt_tick_get() - tick_delta; - timeout -= tick_delta; + timeout -= rt_tick_get_delta(tick_stamp); if (timeout < 0) timeout = 0; } diff --git a/src/mempool.c b/src/mempool.c index 28bdb6cf896..0b80eea9a2c 100644 --- a/src/mempool.c +++ b/src/mempool.c @@ -335,7 +335,7 @@ void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time) if (time > 0) { - time -= rt_tick_get() - before_sleep; + time -= rt_tick_get_delta(before_sleep); if (time < 0) time = 0; } diff --git a/src/timer.c b/src/timer.c index ae6da29f760..9671997ec9b 100644 --- a/src/timer.c +++ b/src/timer.c @@ -22,6 +22,7 @@ * 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable * 2024-01-25 Shell add RT_TIMER_FLAG_THREAD_TIMER for timer to sync with sched * 2024-05-01 wdfk-prog The rt_timer_check and _soft_timer_check functions are merged + * 2025-06-01 htl5241 remove redundancy and fix timer overflow */ #include @@ -494,8 +495,6 @@ static void _timer_check(rt_list_t *timer_list, struct rt_spinlock *lock) level = rt_spin_lock_irqsave(lock); - current_tick = rt_tick_get(); - rt_list_init(&list); while (!rt_list_isempty(&timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1])) @@ -762,7 +761,7 @@ void rt_timer_check(void) rt_tick_t next_timeout; ret = _timer_list_next_timeout(_soft_timer_list, &next_timeout); - if ((ret == RT_EOK) && (next_timeout <= rt_tick_get())) + if ((ret == RT_EOK) && ((rt_tick_get() - next_timeout) < RT_TICK_MAX / 2)) { rt_sem_release(&_soft_timer_sem); }