Apologies, if it is not the right place for this question.
I am working on an RTOS (RTX), my question is if a High Priority Thread is waiting for a Thread Flag (says the flag is X_High) (currently is in a blocked state) and a Low Priority Thread is running, and this low priority thread sets "X_High" thread flag.
Would context switching from low priority to High Thread take place immediately or low priority thread will keep on running till a blocking statement (Delay or waiting for thread flag)?
CodePudding user response:
The guarantee of a pre-emptive priority bases real-time scheduler is that the highest priority thread that is ready to run, will run (outside if interrupt processing).
The scheduling decision is made whenever a scheduling call is made in a normal thread and in exit from the interrupt context. osEventFlagsSet
is a scheduling call. If any event set by it causes a higher priority thread to become ready, the calling thread is pre-empted immediately (before the function returns).
CodePudding user response:
Scheduler typically uses priorities to determine which threads should be given access to the CPU.
If a high-priority thread is waiting for a thread flag and a low-priority thread sets that flag, the scheduler will typically perform a context switch to allow the high-priority thread to run. This is because the high-priority thread is blocked and is not able to make progress until the flag it is waiting for is set. The scheduler will usually prioritize the high-priority thread over the low-priority thread to ensure that it is able to make progress as soon as possible.
However, the specific behavior of the scheduler may depend on the implementation of the RTOS and the specific configuration options that have been set. It is important to consult the documentation for your particular RTOS to understand how it handles thread priorities and context switches.
Here are a few examples of how different RTOSes might handle the scenario you described:
In an RTOS with a fixed-priority scheduler, the high-priority thread would be given priority over the low-priority thread. If the high-priority thread was waiting for a thread flag, it would be placed in a blocked state until the flag was set. When the low-priority thread sets the flag, the scheduler would perform a context switch to allow the high-priority thread to run. The low-priority thread would then be preempted and would not be able to run again until the high-priority thread had finished.
In an RTOS with a dynamic-priority scheduler, the priority of the high-priority thread might be temporarily boosted when it is waiting for a thread flag. This would allow the high-priority thread to be given priority over the low-priority thread, even if it has a lower static priority. When the low-priority thread sets the flag, the scheduler would perform a context switch to allow the high-priority thread to run. The priority of the high-priority thread would then be restored to its original value.
In an RTOS with a round-robin scheduler, both the high-priority and low-priority threads would be given equal access to the CPU, but the high-priority thread would be given a longer time slice. This would allow the high-priority thread to make progress even if the low-priority thread is running. When the low-priority thread sets the flag, the scheduler would perform a context switch to allow the high-priority thread to run. The high-priority thread would then be given a longer time slice to allow it to make progress.
It is important to consult the documentation for your particular RTOS to understand how it handles thread priorities and context switches. This will help you to understand how the scheduler will behave in different scenarios and how you can configure your application to meet your real-time requirements.
CodePudding user response:
It depends on the specific RTOS you're using, and how it is configured. For example, FreeRTOS can be configured to use either cooperative scheduling or preemptive scheduling. When using preemptive scheduling, task switching takes place as soon as the flag is set (semaphore is given). But when using cooperative scheduling, task switching takes place when the currently running task is blocked, or yields voluntarily.