Merge pull request #4800 from mysterywolf/sleep

[sleep] 加入调度器尚未运行时的延时情况
This commit is contained in:
guo 2021-06-16 12:37:17 +08:00 committed by GitHub
commit c3afd9cc23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 8 deletions

View File

@ -43,13 +43,20 @@ RTM_EXPORT(ttyname);
unsigned int sleep(unsigned int seconds) unsigned int sleep(unsigned int seconds)
{ {
rt_tick_t delta_tick; if (rt_thread_self() != RT_NULL)
{
delta_tick = rt_tick_get();
rt_thread_delay(seconds * RT_TICK_PER_SECOND); rt_thread_delay(seconds * RT_TICK_PER_SECOND);
delta_tick = rt_tick_get() - delta_tick; }
else /* scheduler has not run yet */
{
while(seconds > 0)
{
rt_hw_us_delay(1000000u);
seconds --;
}
}
return seconds - delta_tick/RT_TICK_PER_SECOND; return 0;
} }
RTM_EXPORT(sleep); RTM_EXPORT(sleep);
@ -59,11 +66,12 @@ int usleep(useconds_t usec)
{ {
rt_thread_mdelay(usec / 1000u); rt_thread_mdelay(usec / 1000u);
} }
else else /* scheduler has not run yet */
{ {
rt_hw_us_delay(usec / 1000u); rt_hw_us_delay(usec / 1000u);
} }
rt_hw_us_delay(usec % 1000u); rt_hw_us_delay(usec % 1000u);
return 0; return 0;
} }
RTM_EXPORT(usleep); RTM_EXPORT(usleep);