freertos-Why do I switch processes after the interrupt is off?

Source: Internet
Author: User
Tags goto semaphore switches

HTTPS://MP.WEIXIN.QQ.COM/S/S5HBH3RTO0B2IRR8SGWDDW I. Basic ISSUESThe FreeRTOS will perform a process switchover in the zone where the critical area is taskenter_critical () and the taskexit_critical () package. That is, the process switches when the interrupt is closed. we already know that even if the interrupt is off, PowerPC's SC interrupt can be responded to. But the clock is interrupted? This is an external interrupt and cannot be responded to. So when is the interruption open? Open the interrupt immediately after the new process has cut in? Switch interrupt pairing at multiple task levels? 1. Key areas and key resources or call the competition zone, use the PV primitive to protect the operating system basic concepts. In FreeRTOS, the range used for taskenter_critical () and taskexit_critical () wraps. resources that are protected in the key area using the PV primitives are key resources. 2. Basic synchronization mechanism: Switch Interrupta single-core CPU has two parallel processes: task and interrupt ISR. Interrupts can interrupt the execution of a task. So the critical interval in the task needs to be protected with a switch interrupt, and the interrupt ISR does not need to be shut down again. This is the most basic synchronization mechanism. If there is a priority for interrupts, high-priority interrupts can interrupt low-priority interrupts, there are three parallel processes: TASK, low-priority interrupt ISR, and high-priority interrupt ISR. It is no longer possible to protect critical areas by simply shutting down interrupts. The current interrupt response level of the CPU needs to be raised to the highest level required by the critical interval. What is needed here is the priority of the interrupt that will be used to the highest priority of the protected resource. Even if it is not the highest, but its upper priority interrupt ISR does not use protected resources, it does not cause deadlocks. In addition to considering the synchronization of this core, multi-core CPUs also need to consider synchronizing with other cores, requiring the use of system-level flags. Because multiple cores share the same memory space, this synchronization flag can be a variable that exists in memory. 3. The critical area is as short as possible shutting down interrupts will make the CPU unable to respond to external interrupts in a timely manner. So in order to improve the real-time response, we need to make the critical interval as short as possible. That is, the time to close the interrupt is as short as possible. Operations that do not involve critical resources, and are carried out as far as possible beyond the critical interval. 4.PV operation in pairs appears, reducing complexityin order to reduce the complexity of the program. Make synchronous operations easier to read and understand, while reducing the probability of errors. PV operations are generally paired and appear in the same function. To ensure a paired appearance, you can even use a goto statement. Even if you use the if to determine an exception that requires a direct exit, instead of opening the interrupt directly in the if and then exiting, instead use Goto to jump to the only open interrupt operation with a pair. 5. The switching process in the critical interval increases complexitytherefore, in the critical interval, the process is switched in the state of the shutdown interrupt, which increases the complexity of the code. How to ensure that the operation of the switch interrupt is turned into a complex problem. below we follow the xsemaphoregive/xsemaphoretake process to see when an interrupt is opened. two. Semaphore the use queue implementation of the semaphore. Xsemaphore's give and tasks are stored and taken from the queue, respectively. 1.xSemaphoreGiveafter entering the critical area, if the queue is not empty, the process is immediately switched. As you can see from the annotations, there are two implementations: a. switch processes immediately; b. This records only the status and performs a switchover when exiting the critical area. This has to do with the implementation of port, which is not related to FreeRTOS. Also, there is no need to switch processes, which are related to the priority of the process. If the process priority of the wait semaphore is lower than the current process priority, no switchover is made. Here we assume that we will switch. 2.xSemaphoreTakeif the queue is empty, sleep waits at the arrows. At the same time, after the queue is deposited into the element, it is also awakened here. After wake up, execute the following code to check if the queue is empty:As you can see, the taskenter_critical () is re-executed once, and after the queue element is taken, a taskexit_critical () is executed once on exit, which is a paired operation. So there is no situation where the cut-off process closes before the process switches off. the problem is not resolved: When was the interrupt opened? three. Interrupt nested implementationsin the case of FreeRTOS, there are two implementations. 1. Customizing nested level variables 2. Using nested level variables in the TCB That is, directly using FreeRTOS's vtaskentercritical/vtaskexitcritical, these two functions:the difference between the two is that one uses global variables, and one uses each task variable to store an interrupt nesting level. Why does each task interrupt the nesting level? Four. Who interrupts the nesting levelas a general rule of understanding, interrupts belong to the CPU, not to a task, and a CPU can have multiple tasks executing. While the CPU is global, the interrupt state should also be global, so the interrupt nesting level should also be stored using global variables. How do you understand each task with an interrupt nesting level? we understand that each task records its own values for each register. In order to revert to the previous state when you cut in. Does each task also maintain a break state during execution so that it resumes execution when it is cut in? Does each task also record the switching state of the interrupt and the interrupt switch state before the cut-in is resumed? take a look at the registers that are saved and recovered when the task is switched, taking PowerPC as an example. Five. Register state for saving and recovering when an interrupt occursThe SRR1 stores the value of the MSR register before the interrupt occurs, and also includes the EE bit. Then, when recovering, the EE bit will be restored naturally. Six. SummaryFirst answer the question of when to open the interrupt: When the process is cut in, that is, the interrupt switch state before the recovery process is cut out. Another conclusion: the interrupt state is physically global, but in software it is maintained for each task. PS. The complexity is still elevated, and the OS layer needs to be careful, but the app layer doesn't need to be too much of a consideration .

freertos-Why do I switch processes after the interrupt is off?

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.