Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions src/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
* 2022-10-16 Bernard add prioceiling feature in mutex
* 2023-04-16 Xin-zheqi redesigen queue recv and send function return real message size
* 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
* 2025-06-01 htl5241 fix timer overflow
*
*/

#include <rtthread.h>
Expand Down Expand Up @@ -2567,7 +2569,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 */
Expand All @@ -2578,7 +2580,7 @@ static rt_err_t _rt_mb_send_wait(rt_mailbox_t mb,
RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);

/* initialize delta tick */
tick_delta = 0;
tick_stamp = 0;
/* get current thread */
thread = rt_thread_self();

Expand Down Expand Up @@ -2622,7 +2624,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);
Expand Down Expand Up @@ -2650,8 +2652,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;
}
Expand Down Expand Up @@ -2846,7 +2847,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 */
Expand All @@ -2857,7 +2858,7 @@ static rt_err_t _rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeo
RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);

/* initialize delta tick */
tick_delta = 0;
tick_stamp = 0;
/* get current thread */
thread = rt_thread_self();

Expand Down Expand Up @@ -2902,7 +2903,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);
Expand Down Expand Up @@ -2930,8 +2931,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;
}
Expand Down Expand Up @@ -3382,7 +3382,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;

Expand All @@ -3402,7 +3402,7 @@ static rt_err_t _rt_mq_send_wait(rt_mq_t mq,
return -RT_ERROR;

/* initialize delta tick */
tick_delta = 0;
tick_stamp = 0;
/* get current thread */
thread = rt_thread_self();

Expand Down Expand Up @@ -3447,7 +3447,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);
Expand Down Expand Up @@ -3475,8 +3475,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;
}
Expand Down Expand Up @@ -3765,7 +3764,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;

Expand All @@ -3781,7 +3780,7 @@ static rt_ssize_t _rt_mq_recv(rt_mq_t mq,
RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);

/* initialize delta tick */
tick_delta = 0;
tick_stamp = 0;
/* get current thread */
thread = rt_thread_self();
RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mq->parent.parent)));
Expand Down Expand Up @@ -3826,7 +3825,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);
Expand Down Expand Up @@ -3855,8 +3854,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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/mempool.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to mempool.c
* 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
* 2023-12-10 xqyjlj fix spinlock assert
* 2025-06-01 htl5241 fix timer overflow
*/

#include <rthw.h>
Expand Down Expand Up @@ -335,7 +336,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;
}
Expand Down
15 changes: 9 additions & 6 deletions src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
* 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
* 2023-12-10 xqyjlj fix thread_exit/detach/delete
* fix rt_thread_delay
* 2025-06-01 htl5241 fix timer overflow
*
*/

#include <rthw.h>
Expand Down Expand Up @@ -692,7 +694,6 @@ RTM_EXPORT(rt_thread_delay);
rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick)
{
struct rt_thread *thread;
rt_tick_t cur_tick;
rt_base_t critical_level;

RT_ASSERT(tick != RT_NULL);
Expand All @@ -708,13 +709,15 @@ rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick)
/* disable interrupt */
critical_level = rt_enter_critical();

cur_tick = rt_tick_get();
if (cur_tick - *tick < inc_tick)
if (rt_tick_get_delta(*tick) < inc_tick)
{
rt_tick_t left_tick;
rt_tick_t target_tick;
target_tick = *tick + inc_tick;
left_tick = target_tick - rt_tick_get();

*tick += inc_tick;
left_tick = *tick - cur_tick;
if (left_tick > target_tick)
left_tick = RT_TICK_MAX - left_tick + 1;

/* suspend thread */
rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
Expand All @@ -735,7 +738,7 @@ rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick)
}
else
{
*tick = cur_tick;
*tick = rt_tick_get();
rt_exit_critical_safe(critical_level);
}

Expand Down
6 changes: 3 additions & 3 deletions src/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* 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
* fix timer overflow
*/

#include <rtthread.h>
Expand Down Expand Up @@ -494,8 +496,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]))
Expand Down Expand Up @@ -762,7 +762,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);
}
Expand Down
Loading