Keil Logo

Pre-emptive Scheduling

RTX is a pre-emptive multitasking operating system. If a task with a higher priority than the currently running task becomes ready to run, RTX suspends the currently running task.

A preemptive task switch occurs when:

  • the task scheduler is executed from the system tick timer interrupt. Task scheduler processes the delays of tasks. If the delay for a task with a higher priority has expired, then the higher priority task starts to execute instead of currently running task.
  • an event is set for a higher priority task by the currently running task or by an interrupt service routine. The currently running task is suspended, and the higher priority task starts to run.
  • a token is returned to a semaphore, and a higher priority task is waiting for the semaphore token. The currently running task is suspended, and the higher priority task starts to run. The token can be returned by the currently running task or by an interrupt service routine.
  • a mutex is released and a higher priority task is waiting for the mutex. The currently running task is suspended, and the higher priority task starts to run.
  • a message is posted to a mailbox, and a higher priority task is waiting for the mailbox message. The currently running task is suspended, and the higher priority task starts to run. The message can be posted by the currently running task or by an interrupt service routine.
  • a mailbox is full, and a higher priority task is waiting to post a message to a mailbox. As soon as the currently running task or an interrupt service routine takes a message out from the mailbox, the higher priority task starts to run.
  • the priority of the currently running task is reduced. If another task is ready to run and has a higher priority than the new priority of the currently running task, then the current task is suspended immediately, and the higher priority task resumes its execution.

The following example demonstrates one of the task switching mechanisms. Task job1 has a higher priority than task job2. When job1 starts, it creates task job2 and then enters the os_evt_wait_or function. The RTX kernel suspends job1 at this point, and job2 starts executing. As soon as job2 sets an event flag for job1, the RTX kernel suspends job2 and then resumes job1. Task job1 then increments counter cnt1 and calls the os_evt_wait_or function, which suspends it again. The kernel resumes job2, which increments counter cnt2 and sets an event flag for job1. This process of task switching continues indefinitely.

#include <rtl.h>

OS_TID tsk1,tsk2;
int    cnt1,cnt2;

__task void job1 (void);
__task void job2 (void);

__task void job1 (void) {
  os_tsk_prio (2);
  tsk1 = os_tsk_self ();
  os_tsk_create (job2, 1);
  while (1) {
    os_evt_wait_or (0x0001, 0xffff);
    cnt1++;
  }
}

__task void job2 (void) {
  while (1) {
    os_evt_set (0x0001, tsk1);
    cnt2++;
  }
}

void main (void) {
  os_sys_init (job1);
  while (1);
}
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.