Task objects

The Sirius RTOS is a multitasking operating system, where multiple tasks are executed simultaneously. These tasks are very similar to threads running under single process. Each task has the individual stack, where the local variables are stored. The global data is available for all tasks.

Each task can be in one of the three states: running, ready or blocked. Only one task can be executed by the CPU in a given time. The currently executed task is in a running state. Their simultaneous execution is ensured by fast switching from execution of one task to another. The scheduler assigns a short CPU time slice to each task that is in a ready state. Tasks that are in blocked state cannot be executed, as they are waiting for the event that makes them ready. The task states are dynamic and depend on operations that are performed on system objects.

The tasks are objects, so they also can be in the signaled or non-signaled state. Each task remains in a non-signaled state, until it is terminated. All tasks waiting for a task object will be resumed, when the task object is terminated.

Tasks creation, exit and termination

Tasks, as the objects, can be created in any time i.e. before system start, from an interrupt or by other task. The created task is identified by its handle, returned during task creation by osCreateTask function. The startup routine is also defined during a task creation. It is a standard C language function of TTaskProc type.

During task creation, there are defined its parameters, such as address of the task startup routine, priority, stack size and so on. Stack size can be omitted (by specifying a 0). At this time a stack with a default size is assigned. The default stack size is defined in the OS_DEFAULT_TASK_STACK_SIZE constant and by default it is 512 bytes.

Task execution is finished when the task will exit from the startup routine by using the return instruction of the C language. Task can be terminated also in any time by calling osExitTask. Additionally if the value of OS_TERMINATE_TASK_FUNC is set for 1, the task can be terminated by another task (also from an interrupt handler) by using osTerminateTask function.

When a task is finished, it set an error code that has been given during its execution. It can be performed by C language return instruction by the startup procedure or as an osExitTaskfunction argument. If the task is terminated by osTerminateTask, the error code is set to ERR_TASK_TERMINATED_BY_OTHER. If the value of OS_TASK_EXIT_CODE_FUNCis set for 1, the osGetTaskExitCode function is available that allows to read the error code which finishes the task.

If the task performs the operations on itself (e.g. priority change or suspending), it should specify its own handle. The task can be created by another task, so it may not have access to the handle by variable. To obtain its own handle, use an osGetTaskHandle function. It is available only, when OS_GET_TASK_HANDLE_FUNC constant is set to 1.

During task termination, all of the owned critical sections are released and marked as abandoned. When the OS_ALLOW_OBJECT_DELETION is set to 1, all of the opened objects are closed.

Tasks priority and time quantum

Only tasks that are in ready state with the highest priority are performed in a system. The priority is given during task creation and its value is in range from 0 (the highest priority) up to the value determined by OS_LOWEST_USED_PRIORITY. The value of this constant should be set for the lowest used priority to reduce memory usage. It should be in range between 0 and 254. Task priority can be changed in any time by using osSetTaskPriorityfunction. Its value can be read by osGetTaskPriority. Both functions are available if the value of OS_TASK_PRIORITY_FUNC is set for 1.

If two or more tasks that are in ready state have the same priority, their simultaneous execution is ensured by fast switching from execution of one task to another. The schedulerassigns a short CPU time slice (called a time quantum) to each task that runs. When a task performs a long operation, you may inform the scheduler to assign more CPU time slices to this task. The scheduler usually assigns only one quantum. To change the number of assigned time quanta, use an osSetTaskQuantum function. Only the values between 1 and 255 are valid. To obtain this information osGetTaskQuantum should be used. Both functions are available when OS_TASK_QUANTUM_FUNC constant is set to 1.

Tasks suspending and resuming

When OS_SUSP_RES_TASK_FUNC constant is set to 1, the osResumeTask and osSuspendTask functions are available. They allow suspending or resuming a task execution. These functions can be called from another task or even from an interrupt handler. The task can also suspend itself. After suspending a task, its execution is stopped immediately and the scheduler will choose another task to run.

Tasks statistics

When the OS_GET_TASK_STAT_FUNC constant is set to 1, the osGetTaskStat function is available. It allows obtaining a calculated CPU load for specified task. The OS_STAT_SAMPLE_RATE constant defines sampling rate (specified in time units) for calculating a statistics. By default it is set to 100 time units.

SpaceShadow documentation