Keil Logo

Cooperative Multitasking

If you disable Round-Robin Multitasking you must design and implement your tasks so that they work cooperatively. Specifically, you must call the system wait function like the os_dly_wait() function or the os_tsk_pass() function somewhere in each task. These functions signal the RTX kernel to switch to another task.

The following example shows a simple RTX program that uses Cooperative Multitasking. The RTX kernel starts executing task 1. This function creates task 2. After counter1 is incremented once, the kernel switches to task 2. After counter2 is incremented once, the kernel switches back to task 1. This process repeats indefinitely.

#include <rtl.h>

int counter1;
int counter2;

__task void task1 (void);
__task void task2 (void);

__task void task1 (void) {
  os_tsk_create (task2, 0);  /* Create task 2 and mark it as ready */
  for (;;) {                 /* loop forever */
    counter1++;              /* update the counter */
    os_tsk_pass ();          /* switch to 'task2' */
  }
}

__task void task2 (void) {
  for (;;) {                 /* loop forever */
    counter2++;              /* update the counter */
    os_tsk_pass ();          /* switch to 'task1' */
  }
}

void main (void) {
  os_sys_init(task1);        /* Initialize RTX Kernel and start task 1 */
  for (;;);
}

The difference between the system wait function and os_tsk_pass is that the system wait function allows your task to wait for an event, while os_tsk_pass switches to another ready task immediately.

Note

  • If the next ready task has a lower priority than the currently running task, then calling os_tsk_pass does not cause a task switch.
  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.