Home |
RTKernel-32 Programming Manual Multitasking, Real-Time, and RTKernel-32 RTKernel-32 Tasks Alternate APIs for RTKernel-32 |
RTKernel-32 TasksA task or thread is a C function with its own stack. A task has a priority between 1 and 256. A high priority means high urgency. Within a program, priorities are only meaningful relative to each other, e.g., the behavior of a program with two tasks is identical if the priorities are 1 and 2 or 10 and 50. Tasks are referenced using task handles1. Task handles are similar to file handles. When a task is created, RTKernel-32 returns a unique handle to the creating task which can subsequently be used to reference the task (e.g., to send data to it). All variables declared local to the task function are allocated on the task's stack (except static variables). The same is true for the local variables of all functions called by the task. Therefore, several tasks can be started using the same task function, and each would be allocated its own stack and thus its own local variables. A function may be called by different tasks; this results in the same code being executed, but no reentrance problems arise, because each task has its own stack - provided the called function accesses only its parameters and local (non-static) variables. The common C/C++ and Pascal visibility rules fully apply to multitasking programs. All tasks can access global data. When RTKernel-32 is initialized, one Idle Task per CPU is created and the main program is converted to the Main Task. The Idle Tasks have a priority of 0. Since user tasks must have priorities between 1 and 256, it is ensured that the Idle Tasks have the lowest priority in the program. They run whenever no other task can run. The Idle Tasks are required by the scheduler, which always needs at least one "Ready" task it can activate. The Main Task's priority is specified when function RTKernelInit is called. The Main Task's stack is provided by the run-time system or linker, i.e., RTKernel-32 does not modify the Main Task's stack. A task is always in one of the following task states: CurrentThe currently active tasks are in the state Current. Under RTKernel-32, there is exactly one Current task on each CPU at any times. If no application task is ready to run, Idle Tasks run. ReadyAll tasks ready to run are in the state Ready. Usually, all Ready tasks have the same or lower priorities as the Current task(s). SuspendedSuspended tasks cannot run because they were stopped explicitly by the RTKernel-32 operation RTKSuspend. They can only be made Ready by calling the RTKernel-32 function RTKResume. BlockedTasks in the state Blocked cannot run because they are waiting for an event (e.g., a semaphore signal or a message coming in at a mailbox). These tasks can only be made Ready by another task or an interrupt handler. DelayingThese tasks have blocked themselves for a certain time span. They will be made Ready automatically by RTKernel-32's timer interrupt handler after their delay has elapsed. TimedTasks waiting for the occurrence of an event with a timeout are in the state Timed. Such a task will become Ready either when the event occurs or when the timeout expires. All tasks not currently running are maintained by RTKernel-32 in several queues. There is, for example, a queue for all Ready tasks. Another queue contains all tasks waiting for a certain point in time. Queues can also build up at semaphores or mailboxes if tasks are blocked on them.
Multitasking, Real-Time, and RTKernel-32
|