Skip to main content

Scheduler in ThreadX

The scheduler in ThreadX is relatively simple.

It is nothing but a tight loop that consumes threads in the Ready Thread Queue and executes a thread by setting up the thread context and pointing it with the Thread Current Pointer.

The picture below illustrates how the scheduler works.

/galleries/scheduler-in-threadx-1.jpg

In the Thread Scheduler, a loop runs checking if there is any threads ready in the Ready Thread Queue. the Thread Execute Pointer always points to the head of the queue. If the Thread Execute Pointer sees a valid thread, it assigns it to the Thread Current Pointer. The thread becomes the Current Thread. This is when the interrupts get disabled. The scheduler then sets up the TCB and other context info for the Current Thread (things like the run counter, time-slice value, CPSR/SPSR, stack will be set for the thread)

The contexts for an interrupted thread and a normal "suspended" thread are different, which are called the "interrupt context" and "solicited context".

After setting up the "interrupt context", the scheduler returns to the point of interrupt immediately, by modifies the PC register. In the case of a "solicited context" the scheduler simply does a "RET" to pop up an address from the stack so that the processor knows where to continue execution. Recall that the stack has been set up in the scheduler before it returns, in this way when the scheduler yields the processor, the Current Thread is given the resource to run on its own.