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
44 changes: 20 additions & 24 deletions src/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 */
Expand All @@ -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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

Expand All @@ -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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

Expand All @@ -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)));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mempool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 2 additions & 3 deletions src/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <rtthread.h>
Expand Down Expand Up @@ -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]))
Expand Down Expand Up @@ -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);
}
Expand Down