Thread Scheduler for the Rt-thread kernel

Source: Internet
Author: User
Tags switches

Http://www.cnblogs.com/King-Gentleman/p/4278012.html

First, preface

The thread scheduler provided in Rt-thread is based on all preemptive priority scheduling, in addition to the interrupt handler function, the code of the lock part of the scheduler and the code that prohibits the interrupt, the other parts of the system can be preempted, including the thread scheduler itself. Total 256 priority levels supported by the system (0 ~ 255, the smaller the priority, the higher the 0 is the highest priority, and the 255 is allocated to the idle thread, the average user does not use.) In some resource-intensive systems, you can choose to support only 8 or 32 priority system configurations, depending on the situation. In the system, when there are threads that are higher than the current thread priority, the current thread is swapped out immediately, and the high-priority thread preempted the processor for execution.

Second, the thread priority management system

In the implementation of the Rt-thread scheduler, an array of 256 priority queues is included (if the system supports a maximum of 32 priority levels, this will be an array containing 32 priority queues), and the header of the same priority list is placed in each array element. These same priority lists form a two-way ring list, and the lowest priority line threads typically contains only one idle thread.

In the SCHEDULER.C:

rt_list_t Rt_thread_priority_table[rt_thread_priority_max];

Third, the Thread Scheduler function interface (the following function interface is implemented in SRC/SCHEDULER.C)

Scheduler initialization:void Rt_system_scheduler_init (void), the initialization of the scheduler is required at system startup to initialize some of the global variables to be implemented by the system scheduler.
Start thread Scheduler:void Rt_system_scheduler_start (void); switch to the first thread after the system completes initialization, and call the following function interface. When this function is called, it looks for the highest-priority ready-to-state thread in the system and switches past execution. In addition, before calling this function, the initialization of the idle thread must be done to ensure that the system can at least find a ready state for the thread to execute. This function is never returned.
add a thread to the scheduler:void Rt_schedule_insert_thread (struct rt_thread *thread);
removing a thread from the scheduler:void Rt_schedule_remove_thread (struct rt_thread *thread);
thread scheduling:void rt_schedule (void); Let the scheduler perform one-time scheduling of threads. When this function is called, the system calculates the ready state of the thread in the system, and if there is a higher priority thread than the current thread, the system switches to the high-priority thread. The upper-level application generally does not need to call this function.
Enter the critical section, the Scheduler is locked:void rt_enter_critical (void); After calling this function, the scheduler will be locked. During the time the system locks the scheduler, the system still responds with interrupts, and if the interrupt wakes up a higher priority thread, the scheduler does not immediately execute it until the Unlock scheduler function is called to attempt the next dispatch. Locking the scheduler as an interrupt lock also allows the currently running task to be swapped out until the scheduler is unlocked. But a bit different from the interrupt lock is that the scheduler is locked, the system can still respond to external interrupts, and the interrupt service routine can still respond accordingly. Therefore, when using the scheduler lock-in way of task synchronization, it is necessary to consider whether the critical resource of the task access will be modified by the interrupt service routine, if it can be modified, it will not be appropriate to synchronize in this way. exits the critical section, the Scheduler unlocks:void rt_exit_critical (void), when the system exits the critical section, the system calculates whether there are currently higher priority threads ready, and if there is a higher priority thread than the current thread is ready, Will switch to this high-priority thread, and if no higher-priority thread is ready, the current task will continue to execute. Note: rt_enter_critical/rt_exit_critical can be nested multiple times, but each time a rt_enter_critical is called, the rt_exit_critical exit operation must be called in a relative manner. The maximum nesting depth is 65535. returns the scheduler lock nesting count:rt_uint16_t rt_critical_level (void) {    return rt_scheduler_lock_nest;//0 means no scheduler lock} The scheduler lock can be conveniently used for synchronization between some threads and threads, and it will not burden the system interrupt response due to the light weight;
But its flaw is also obvious, that it cannot be used to interrupt the synchronization or notification between threads, and if the time to execute the scheduler lock is too long, it will affect the real-time performance of the system (because after using the scheduler lock, the system will no longer have a priority relationship until it is out of the state of the scheduler lock).

Thread Scheduler for the Rt-thread kernel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.