CHAPTER 3 TASK MANAGEMENT FUNCTIONS


This chapter describes the task management functions performed by the RI600V4.

3.1 Outline

The task management functions provided by the RI600V4 include a function to reference task statuses such as priorities and detailed task information, in addition to a function to manipulate task statuses such as generation, activation and termination of tasks.

3.2 Tasks

A task is processing program that is not executed unless it is explicitly manipulated via service calls provided by the RI600V4, unlike other processing programs (interrupt handler, cyclic handler and alarm handler), and is called from the scheduler.

Note The execution environment information required for a task's execution is called "task context". During task execution switching, the task context of the task currently under execution by the RI600V4 is saved and the task context of the next task to be executed is loaded.

3.2.1 Task state

Tasks enter various states according to the acquisition status for the OS resources required for task execution and the occurrence/non-occurrence of various events. In this process, the current state of each task must be checked and managed by the RI600V4.

The RI600V4 classifies task states into the following six types.

Figure 3-1 Task State



1 ) DORMANT state

State of a task that is not active, or the state entered by a task whose processing has ended.
A task in the DORMANT state, while being under management of the RI600V4, is not subject to RI600V4 scheduling.


2 ) READY state

State of a task for which the preparations required for processing execution have been completed, but since another task with a higher priority level or a task with the same priority level is currently being processed, the task is waiting to be given the CPU's use right.

3 ) RUNNING state

State of a task that has acquired the CPU use right and is currently being processed.
Only one task can be in the running state at one time in the entire system.


4 ) WAITING state

State in which processing execution has been suspended because conditions required for execution are not satisfied.
Resumption of processing from the WAITING state starts from the point where the processing execution was suspended. The value of information required for resumption (such as task context) immediately before suspension is therefore restored.
In the RI600V4, the WAITING state is classified into the following 12 types according to their required conditions and managed.



Table 3-1 WAITING State

WAITING State

Service Calls

Sleeping state

Delayed state

WAITING state for a semaphore resource

WAITING state for an eventflag

Sending WAITING state for a data queue

Receiving WAITING state for a data queue

Receiving WAITING state for a mailbox

WAITING state for a mutex

Sending WAITING state for a message buffer

Receiving WAITING state for a message buffer

WAITING state for a fixed-sized memory block

WAITING state for a variable-sized memory block



5 ) SUSPENDED state

State in which processing execution has been suspended forcibly.
Resumption of processing from the SUSPENDED state starts from the point where the processing execution was suspended. The value of information required for resumption (such as task context) immediately before suspension is therefore restored.


6 ) WAITING-SUSPENDED state

State in which the WAITING and SUSPENDED states are combined.
A task enters the SUSPENDED state when the WAITING state is cancelled, or enters the WAITING state when the SUSPENDED state is cancelled.


3.2.2 Task priority

A priority level that determines the order in which that task will be processed in relation to the other tasks is assigned to each task.

As a result, in the RI600V4, the task that has the highest priority level of all the tasks that have entered an executable state (RUNNING state or READY state) is selected and given the CPU use right.

In the RI600V4, the following two types of priorities are used for management purposes.

- Current priority
The RI600V4 performs the following processing according to current priority.


- Task scheduling (Refer to "14.4 Task Scheduling Method")

- Queuing tasks to a wait queue in the order of priority

Note The current priority immediately after it moves from the DORMANT state to the READY state is specified at creating the task.

- Base priority
Unless mutex is used, the base priority is the same as the current priority. When using mutex, refer to "6.2.2 Current priority and base priority".


Note 1 In the RI600V4, a task having a smaller priority number is given a higher priority.

Note 2 The priority range that can be specified in a system can be defined by Maximum task priority (priority) in System Information (system)) when creating a system configuration file.

3.2.3 Basic form of tasks

The following shows the basic form of tasks.

 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     /* ......... */
 
     ext_tsk ();                     /*Terminate invoking task*/
 }


Note 1 The following information is passed to exinf.
How to activate

exinf

Start code (stacd) specified by sta_tsk or ista_tsk



Note 2 When the return instruction is issued in a task, the same processing as ext_tsk is performed.

Note 3 For details about the extended information, refer to "3.4 Activate Task".

Note 4 The prototype for tasks are declared in the kernel_id.h which is generated by the cfg600.

3.2.4 Internal processing of task

In the RI600V4, original dispatch processing (task scheduling) is executed during task switching.

Therefore, note the following points when coding tasks.

- Stack
Tasks use user stacks that are defined in Task Information (task[]).


- Service call
Tasks can issue service calls whose "Useful range" is "Task".


- PSW register when processing is started

Table 3-2 PSW Register When Task Processing is Started

Bit

Value

Note

I

1

All interrupts are acceptable.

IPL

0

PM

1

User mode

U

1

User stack

C, Z, S, O

Undefined

Others

0



- FPSW register when processing is started
When setting of Task context register (context) in System Information (system) includes "FPSW", the FPSW when processing is started is shown in Table 3-3. The FPSW when processing is undefined in other cases.


Table 3-3 FPSW Register When Task Processing is Started

Compiler options

Value

-round

-denormalize

nearest (default)

off (default)

0x00000100 (Only DN bit is 1.)

on

0

zero

off (default)

0x00000101 (Only DN bit and RM bit are 1.)

on

1 (Only RM bit is 1.)



3.2.5 Processor mode of task

The processor mode at the time of task execution is always user mode. It is impossible to execute a task in the supervisor mode.

Processing to execute in the supervisor mode should be implemented as an interrupt handler for INT instruction.

For example, the WAIT instruction, that changes the CPU to the power saving mode, is privilege instruction. The WAIT instruction should execute in the supervisor mode.

Note, INT #1 to #8 are reserved by the RI600V4, application cannot use INT #1 to #8.

3.3 Create Task

In the RI600V4, the method of creating a task is limited to "static creation".

Tasks therefore cannot be created dynamically using a method such as issuing a service call from a processing program.

Static task creation means defining of tasks using static API "task[]" in the system configuration file.

For details about the static API "task[]", refer to "19.7 Task Information (task[])".

3.4 Activate Task

The RI600V4 provides two types of interfaces for task activation: queuing an activation request queuing and not queuing an activation request.

3.4.1 Activate task with queuing

A task (queuing an activation request) is activated by issuing the following service call from the processing program.

- act_tsk, iact_tsk
These service calls move the task specified by parameter tskid from the DORMANT state to the READY state.
As a result, the target task is queued at the end on the ready queue corresponding to the initial priority and becomes subject to scheduling by the RI600V4.
If the target task has been moved to a state other than the DORMANT state when this service call is issued, this service call does not move the state but increments the activation request counter (by added 1 to the activation request counter).
The following describes an example for coding these service calls.




 #include    "kernel.h"          /*Standard header file definition*/
 #include    "kernel_id.h"       /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     ID      tskid = 8;          /*Declares and initializes variable*/
 
     /* ......... */
 
     act_tsk (tskid);            /*Activate task (queues an activation request)*/
 
     /* ......... */
 }


Note 1 The activation request counter managed by the RI600V4 is configured in 8-bit widths. If the number of activation requests exceeds the maximum count value 255 as a result of issuing this service call, the counter manipulation processing is therefore not performed but "E_QOVR" is returned.

Note 2 Extended information specified in Task Information (task[]) is passed to the task activated by issuing these service calls.

3.4.2 Activate task without queuing

A task (not queuing an activation request) is activated by issuing the following service call from the processing program.

- sta_tsk, ista_tsk
These service calls move the task specified by parameter tskid from the DORMANT state to the READY state.
As a result, the target task is queued at the end on the ready queue corresponding to the initial priority and becomes subject to scheduling by the RI600V4.
This service call does not perform queuing of activation requests. If the target task is in a state other than the DORMANT state, the status manipulation processing for the target task is therefore not performed but "E_OBJ" is returned.
Specify for parameter stacd the extended information transferred to the target task.
The following describes an example for coding these service calls.





 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     ID      tskid = 8;          /*Declares and initializes variable*/
     VP_INT  stacd = 123;        /*Declares and initializes variable*/
 
     /* ......... */
 
     sta_tsk (tskid, stacd);     /*Activate task (does not queue an activation */
                                 /*request)*/
 
     /* ......... */
 }


3.5 Cancel Task Activation Requests

An activation request is cancelled by issuing the following service call from the processing program.

- can_act, ican_act
This service call cancels all of the activation requests queued to the task specified by parameter tskid (sets the activation request counter to 0).
When this service call is terminated normally, the number of cancelled activation requests is returned.
The following describes an example for coding these service calls.



 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     ER_UINT ercd;                   /*Declares variable*/
     ID      tskid = 8;              /*Declares and initializes variable*/
 
     /* ......... */
 
     ercd = can_act (tskid);         /*Cancel task activation requests*/
 
     if (ercd >= 0) {
         /* ......... */             /*Normal termination processing*/
     }
 
     /* ......... */
 }


Note This service call does not perform status manipulation processing but performs the setting of activation request counter. Therefore, the task does not move from a state such as the READY state to the DORMANT state.

3.6 Terminate Task

3.6.1 Terminate invoking task

The invoking task is terminated by issuing the following service call from the processing program.

- ext_tsk
This service call moves the invoking task from the RUNNING state to the DORMANT state.
As a result, the invoking task is unlinked from the ready queue and excluded from the RI600V4 scheduling subject.
If an activation request has been queued to the invoking task (the activation request counter > 0) when this service call is issued, this service call moves the task from the RUNNING state to the DORMANT state, decrements the wake-up request counter (by subtracting 1 from the activation request counter), and then moves the task from the DORMANT state to the READY state.
The following describes an example for coding this service call.




 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     /* ......... */
 
     ext_tsk ();                     /*Terminate invoking task*/
 }


Note 1 When the invoking task has locked mutexes, the locked state are released at the same time (processing equivalent to unl_mtx).

Note 2 When the return instruction is issued in a task, the same processing as ext_tsk is performed.

3.6.2 Terminate Another task

- ter_tsk
This service call forcibly moves the task specified by parameter tskid to the DORMANT state.
As a result, the target task is excluded from the RI600V4 scheduling subject.
If an activation request has been queued to the target task (the activation request counter > 0) when this service call is issued, this service call moves the task to the DORMANT state, decrements the wake-up request counter (by subtracting 1 from the activation request counter), and then moves the task from the DORMANT state to the READY state.
The following describes an example for coding this service call.




 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     ID      tskid = 8;              /*Declares and initializes variable*/
 
     /* ......... */
 
     ter_tsk (tskid);                /*Terminate task*/
 
     /* ......... */
 }


Note When the target task has locked mutexes, the locked state are released at the same time (processing equivalent to unl_mtx).

3.7 Change Task Priority

The priority is changed by issuing the following service call from the processing program.

- chg_pri, ichg_pri
This service call changes the base priority of the task specified by parameter tskid to a value specified by parameter tskpri.
The changed base priority is effective until the task terminates or this servie call is issued. When next the task is activated, the base priority is the initial priority which is specified at the task creation.
This service call also changes the current priority of the target task to a value specified by parameter tskpri. However, the current priority is not changed when the target task has locked mutexes.
If the target task has locked mutexes or is waiting for mutex to be locked and if tskpri is higher than the ceiling priority of either of the mutexes, this service call returns "E_ILUSE".
When the current priority is changed, the following state variations are generated.





1 ) When the target task is in the RUNNING or READY state.
This service call re-queues the task at the end of the ready queue corresponding to the priority specified by parameter tskpri.


2 ) When the target task is queued to a wait queue of the object with TA_TPRI or TA_CEILING attribute.
This service call re-queues the task to the wait queue corresponding to the priority specified by parameter tskpri. When two or more tasks of same current priority as this service call re-queues the target task at the end among their tasks.


Example When three tasks (task A: priority level 10, task B: priority level 11, task C: priority level 12) are queued to the semaphore wait queue in the order of priority, and the priority level of task B is changed from 11 to 9, the wait order will be changed as follows.



The following describes an example for coding these service calls.

 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     ID      tskid = 8;              /*Declares and initializes variable*/
     PRI     tskpri = 9;             /*Declares and initializes variable*/
 
     /* ......... */
 
     chg_pri (tskid, tskpri);        /*Change task priority*/
 
     /* ......... */
 }


Note For current priority and base priority, refer to "6.2.2 Current priority and base priority".

3.8 Reference Task Priority

A task priority is referenced by issuing the following service call from the processing program.

- get_pri, iget_pri
Stores current priority of the task specified by parameter tskid in the area specified by parameter p_tskpri.
The following describes an example for coding these service calls.


 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     ID      tskid = 8;              /*Declares and initializes variable*/
     PRI     p_tskpri;               /*Declares variable*/
 
     /* ......... */
 
     get_pri (tskid, &p_tskpri);     /*Reference task priority*/
 
     /* ......... */
 }


Note For current priority and base priority, refer to "6.2.2 Current priority and base priority".

3.9 Reference Task State

3.9.1 Reference task state

A task status is referenced by issuing the following service call from the processing program.

- ref_tsk, iref_tsk
Stores task state packet (current state, current priority, etc.) of the task specified by parameter tskid in the area specified by parameter pk_rtsk.
The following describes an example for coding these service calls.


 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     ID      tskid = 8;              /*Declares and initializes variable*/
     T_RTSK  pk_rtsk;                /*Declares data structure*/
     STAT    tskstat;                /*Declares variable*/
     PRI     tskpri;                 /*Declares variable*/
     PRI     tskbpri;                /*Declares variable*/
     STAT    tskwait;                /*Declares variable*/
     ID      wobjid;                 /*Declares variable*/
     TMO     lefttmo;                /*Declares variable*/
     UINT    actcnt;                 /*Declares variable*/
     UINT    wupcnt;                 /*Declares variable*/
     UINT    suscnt;                 /*Declares variable*/
 
     /* ......... */
 
     ref_tsk (tskid, &pk_rtsk);      /*Reference task state*/
 
     tskstat = pk_rtsk.tskstat;      /*Reference current state*/
     tskpri = pk_rtsk.tskpri;        /*Reference current priority*/
     tskbpri = pk_rtsk.tskbpri;      /*Reference base priority*/
     tskwait = pk_rtsk.tskwait;      /*Reference reason for waiting*/
     wobjid = pk_rtsk.wobjid;        /*Reference object ID number for which the */
                                     /*task is waiting*/
     lefttmo = pk_rtsk.lefttmo;      /*Reference remaining time until time-out*/
     actcnt = pk_rtsk.actcnt;        /*Reference activation request count*/
     wupcnt = pk_rtsk.wupcnt;        /*Reference wake-up request count*/
     suscnt = pk_rtsk.suscnt;        /*Reference suspension count*/
 
     /* ......... */
 }


Note For details about the task state packet, refer to "[Task state packet: T_RTSK]".

3.9.2 Reference task state (simplified version)

A task status (simplified version) is referenced by issuing the following service call from the processing program.

- ref_tst, iref_tst
Stores task state packet (current state, reason for waiting) of the task specified by parameter tskid in the area specified by parameter pk_rtst.
Used for referencing only the current state and reason for wait among task information.
Response becomes faster than using ref_tsk or iref_tsk because only a few information items are acquired.
The following describes an example for coding these service calls.




 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     ID      tskid = 8;              /*Declares and initializes variable*/
     T_RTST  pk_rtst;                /*Declares data structure*/
     STAT    tskstat;                /*Declares variable*/
     STAT    tskwait;                /*Declares variable*/
 
     /* ......... */
 
     ref_tst (tskid, &pk_rtst);      /*Reference task state (simplified version)*/
 
     tskstat = pk_rtst.tskstat;      /*Reference current state*/
     tskwait = pk_rtst.tskwait;      /*Reference reason for waiting*/
 
     /* ......... */
 }


Note For details about the task state packet (simplified version), refer to " [Task state packet (simplified version): T_RTST]".