* @brief This function will let current thread sleep for some ticks. Change current thread state to suspend, * when the thread timer reaches the tick value, scheduler will awaken this thread. * * @param tick is the sleep ticks. * * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed. * If the return value is any oth
| 640 | * If the return value is any other values, it means this operation failed. |
| 641 | */ |
| 642 | static rt_err_t _thread_sleep(rt_tick_t tick) |
| 643 | { |
| 644 | struct rt_thread *thread; |
| 645 | rt_base_t critical_level; |
| 646 | int err; |
| 647 | |
| 648 | if (tick == 0) |
| 649 | { |
| 650 | return -RT_EINVAL; |
| 651 | } |
| 652 | |
| 653 | /* set to current thread */ |
| 654 | thread = rt_thread_self(); |
| 655 | RT_ASSERT(thread != RT_NULL); |
| 656 | RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread); |
| 657 | |
| 658 | /* current context checking */ |
| 659 | RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE); |
| 660 | |
| 661 | /* reset thread error */ |
| 662 | thread->error = RT_EOK; |
| 663 | |
| 664 | /* lock scheduler since current thread may be suspended */ |
| 665 | critical_level = rt_enter_critical(); |
| 666 | |
| 667 | /* suspend thread */ |
| 668 | err = rt_thread_suspend_with_flag(thread, RT_INTERRUPTIBLE); |
| 669 | |
| 670 | /* reset the timeout of thread timer and start it */ |
| 671 | if (err == RT_EOK) |
| 672 | { |
| 673 | rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick); |
| 674 | rt_timer_start(&(thread->thread_timer)); |
| 675 | |
| 676 | thread->error = -RT_EINTR; |
| 677 | |
| 678 | /* notify a pending rescheduling */ |
| 679 | rt_schedule(); |
| 680 | |
| 681 | /* exit critical and do a rescheduling */ |
| 682 | rt_exit_critical_safe(critical_level); |
| 683 | |
| 684 | /* clear error number of this thread to RT_EOK */ |
| 685 | if (thread->error == -RT_ETIMEOUT) |
| 686 | thread->error = RT_EOK; |
| 687 | } |
| 688 | else |
| 689 | { |
| 690 | rt_exit_critical_safe(critical_level); |
| 691 | } |
| 692 | |
| 693 | return err; |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * @brief This function will let current thread delay for some ticks. |
no test coverage detected