Thread Scheduler analysis of the Rt-thread kernel

Source: Internet
Author: User
Tags switches

First, preface

The thread scheduler provided in Rt-thread is a scheduling based on full preemption priority , in which the other parts of the system can be preempted in addition to the interrupt handler function, the code of the lock part of the scheduler, and the code that disables the interrupt . This includes the thread scheduler itself. The system supports a total of 256 priorities (0 to 255, the lower the priority, 0 is the highest priority, and 255 is allocated to idle threads, which is not used by the general user. 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, thread scheduler function interface

Scheduler Initialization: void rt_system_scheduler_init (void), which requires the initialization of the scheduler at system startup to initialize some of the global variables to be implemented by the system scheduler. 
To start the thread scheduler: void Rt_system_scheduler_start (void); switch to the first thread after the system has completed initialization, and you can 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. 
to add a thread to the scheduler: void rt_schedule_insert_thread (struct rt_thread *thread);
to remove 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 the thread. 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. 
to enter the critical section, the Scheduler locks:voidRt_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. to exit the critical section, the Scheduler unlocks: voidRt_exit_critical (voidwhen the system exits the critical section, the system accountant calculates whether there is currently a higher priority thread ready, and if there is a higher priority thread than the current thread is ready, it 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 more than once, but each time a rt_enter_critical is called, a rt_exit_critical exit operation must be called relative to it, and the maximum nesting depth is 65535.  Returns the scheduler lock status:rt_uint16_t rt_critical_level (void){    returnRt_scheduler_lock_nest;} 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 analysis of 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.