Skip to content

Commit 4a14b8f

Browse files
committed
[Kernel] Add delay_util implementation.
1 parent 055061a commit 4a14b8f

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

include/rtthread.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ rt_err_t rt_thread_delete(rt_thread_t thread);
138138

139139
rt_err_t rt_thread_yield(void);
140140
rt_err_t rt_thread_delay(rt_tick_t tick);
141+
rt_err_t rt_thread_delay_util(rt_tick_t *tick, rt_tick_t inc_tick);
141142
rt_err_t rt_thread_mdelay(rt_int32_t ms);
142143
rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg);
143144
rt_err_t rt_thread_suspend(rt_thread_t thread);

src/thread.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,61 @@ rt_err_t rt_thread_delay(rt_tick_t tick)
530530
}
531531
RTM_EXPORT(rt_thread_delay);
532532

533+
/**
534+
* This function will let current thread delay util (*tick + inc_tick).
535+
*
536+
* @param tick the tick of last wakeup.
537+
* @param inc_tick the increment tick
538+
*
539+
* @return RT_EOK
540+
*/
541+
rt_err_t rt_thread_delay_util(rt_tick_t *tick, rt_tick_t inc_tick)
542+
{
543+
register rt_base_t level;
544+
struct rt_thread *thread;
545+
546+
/* set to current thread */
547+
thread = rt_thread_self();
548+
RT_ASSERT(thread != RT_NULL);
549+
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
550+
551+
/* disable interrupt */
552+
level = rt_hw_interrupt_disable();
553+
554+
if (rt_tick_get() - *tick < inc_tick)
555+
{
556+
*tick = rt_tick_get() - *tick + inc_tick;
557+
558+
/* suspend thread */
559+
rt_thread_suspend(thread);
560+
561+
/* reset the timeout of thread timer and start it */
562+
rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, tick);
563+
rt_timer_start(&(thread->thread_timer));
564+
565+
/* enable interrupt */
566+
rt_hw_interrupt_enable(level);
567+
568+
rt_schedule();
569+
570+
/* get the wakeup tick */
571+
*tick = rt_tick_get();
572+
573+
/* clear error number of this thread to RT_EOK */
574+
if (thread->error == -RT_ETIMEOUT)
575+
{
576+
thread->error = RT_EOK;
577+
}
578+
}
579+
else
580+
{
581+
rt_hw_interrupt_enable(level);
582+
}
583+
584+
return RT_EOK;
585+
}
586+
RTM_EXPORT(rt_thread_delay_util);
587+
533588
/**
534589
* This function will let current thread delay for some milliseconds.
535590
*

0 commit comments

Comments
 (0)