Everything

CHAPTER 11 SCHEDULER


This chapter describes the scheduler of the RI78V4.
11.1 Outline
The scheduling functions provided by the RI78V4 consist of functions manage/decide the order in which tasks are executed by monitoring the transition states of dynamically changing tasks, so that the CPU use right is given to the optimum task.
11.2 Driving Method
The RI78V4 employs the Event-driven system in which the scheduler is activated when an event (trigger) occurs.
- Event-driven system
Under the event-driven system of the RI78V4, the scheduler is activated upon occurrence of the events listed below and dispatch processing (task scheduling processing) is executed.
- Issuance of service call that may cause task state transition
- Issuance of instruction for returning from non-task (cyclic handler, interrupt handler, etc.)
- Occurrence of clock interrupt used when achieving TIME MANAGEMENT FUNCTIONS
11.3 Scheduling System
As task scheduling methods, the RI78V4 employs the Priority level method, which uses the priority level defined for each task, and the FCFS method, which uses the time elapsed from the point when a task becomes subject to the RI78V4 scheduling.
- Priority level method
A task with the highest priority level is selected from among all the tasks that have entered an executable state (RUNNING state or READY state), and given the CPU use right.
Note In the RI78V4, a task having a smaller priority number is given a higher priority.
- FCFS method
The same priority level can be defined for multiple tasks in the RI78V4. Therefore, multiple tasks with the highest priority level, which is used as the criterion for task selection under the Priority level method, may exist simultaneously.
To remedy this, dispatch processing (task scheduling processing) is executed on a first come first served (FCFS) basis, and the task for which the longest interval of time has elapsed since it entered an executable state (READY state) is selected as the task to which the CPU use right is granted.
11.4 Ready Queue
The RI78V4 uses a "ready queue" to implement task scheduling.
The ready queue is a hash table that uses priority as the key, and tasks that have entered an executable state (READY state or RUNNING state) are queued in FIFO order. Therefore, the scheduler realizes the RI78V4's scheduling method (priority level or FCFS) by executing task detection processing from the highest priority level of the ready queue upon activation, and upon detection of queued tasks, giving the CPU use right to the first task of the proper priority level.
The following shows the case where multiple tasks are queued to a ready queue.
Figure 11-1 Implementation of Scheduling Method (Priority Level Method or FCFS Method)
11.4.1 Create ready queue
In the RI78V4, the method of creating a ready queue is limited to "static creation by the Kernel Initialization Module".
Ready queues therefore cannot be created dynamically using a method such as issuing a service call from a processing program.
- Static create
Static ready queue creation is realized by defining Task priority information in the system configuration file.
The RI78V4 executes ready queue creation processing based on data stored in information files, using the Kernel Initialization Module, and handles the created ready queues as management targets.
11.4.2 Delete ready queue
In the RI78V4, ready queues created statically by the Kernel Initialization Module cannot be deleted dynamically using a method such as issuing a service call from a processing program.
11.4.3 Rotate task precedence
The RI78V4 provides a function to change the queuing order of tasks from the processing program, explicitly switching the task execution order.
The following shows the status transition when the task queuing order is changed.
Figure 11-2 Rotate Task Precedence
A ready queue is rotated by issuing the following service call from the processing program.
- rot_rdq, irot_rdq
These service calls re-queue the first task of the ready queue corresponding to the priority specified by parameter tskpri to the end of the queue to change the task execution order explicitly.
The following describes an example for coding this service call.
 #include    <kernel.h>          /*Standard header file definition*/
 #include    <kernel_id.h>       /*System information header file definition*/
 
 void
 func_cychdr ( void )
 {
     PRI     tskpri = 8;         /*Declares and initializes variable*/
 
     /* ............ */
 
     irot_rdq ( tskpri );        /*Rotate task precedence*/
 
     /* ............ */
 
     return;                     /*Terminate cyclic handler*/
 }

Note 1 This service call does not perform queuing of rotation requests. If no task is queued to the ready queue corresponding to the relevant priority, therefore, no processing is performed but it is not handled as an error.
Note 2 Round-robin scheduling can be implemented by issuing this service call via a cyclic handler in a constant cycle.
11.4.4 Change task priority
The RI78V4 provides a function to change the priority level of tasks from the processing program, explicitly switching the task execution order.
The following shows the status transition when this task priority is changed.
Figure 11-3 Change Task Priority
A priority is changed by issuing the following service call from the processing program.
- chg_pri, ichg_pri
This service call changes the priority of the task specified by parameter tskid (current priority) to a value specified by parameter tskpri.
The following describes an example for coding this service call.
 #include    <kernel.h>          /*Standard header file definition*/
 #include    <kernel_id.h>       /*System information header file definition*/
 
 void
 func_task ( VP_INT exinf )
 {
     ID      tskid = ID_tskA;    /*Declares and initializes variable*/
     PRI     tskpri = 9;         /*Declares and initializes variable*/
 
     /* ............ */
 
     chg_pri ( tskid, tskpri );  /*Change task priority*/
 
     /* ............ */
 }

Note If the target task is in the RUNNING or READY state after this service call is issued, this service call re-queues the task at the end of the ready queue corresponding to the priority specified by parameter tskpri, following priority change processing.
11.5 Scheduling Disabling
The RI78V4 provides a function to disable scheduler activation by referencing the system state from the processing program and explicitly prohibiting dispatch processing (task scheduling processing).
The following shows a processing flow when using the scheduling suppressing function.
Figure 11-4 Scheduling Suppression Function
11.5.1 Disable dispatching
A task is moved to the dispatching disabled state by issuing the following service call from the processing program.
- dis_dsp
This service call changes the system status to the dispatching disabled state.
As a result, dispatch processing (task scheduling) is disabled from when this service call is issued until ena_dsp is issued.
If a service call (chg_pri, sig_sem, etc.) accompanying dispatch processing is issued during the interval from when this service call is issued until ena_dsp is issued, the RI78V4 executes only processing such as queue manipulation, counter manipulation, etc., and the actual dispatch processing is delayed until ena_dsp is issued, upon which the actual dispatch processing is performed in batch.
The following describes an example for coding this service call.
 #include    <kernel.h>          /*Standard header file definition*/
 #include    <kernel_id.h>       /*System information header file definition*/
 
 void
 func_task ( VP_INT exinf )
 {
     /* ............ */
 
     dis_dsp ( );                /*Disable dispatching*/
 
     /* ............ */          /*Dispatching disabled state*/
 
     ena_dsp ( );                /*Enable dispatching*/
 
     /* ............ */
 }

Note 1 This service call does not perform queuing of disable requests. If the system is in the dispatching disabled state, therefore, no processing is performed but it is not handled as an error.
Note 2 The dispatching disabled state changed by issuing this service call must be cancelled before the task that issued this service call moves to the DORMANT state.
11.5.2 Enable dispatching
The dispatching disabled state is cancelled by issuing the following service call from the processing program.
- ena_dsp
This service call changes the system status to the dispatching enabled state.
As a result, dispatch processing (task scheduling) that has been disabled by issuing dis_dsp is enabled.
If a service call (chg_pri, sig_sem, etc.) accompanying dispatch processing is issued during the interval from when dis_dsp is issued until this service call is issued, the RI78V4 executes only processing such as queue manipulation, counter manipulation, etc., and the actual dispatch processing is delayed until this service call is issued, upon which the actual dispatch processing is performed in batch.
The following describes an example for coding this service call.
 #include    <kernel.h>          /*Standard header file definition*/
 #include    <kernel_id.h>       /*System information header file definition*/
 
 void
 func_task ( VP_INT exinf )
 {
     /* ............ */
 
     dis_dsp ( );                /*Disable dispatching*/
 
     /* ............ */          /*Dispatching disabled state*/
 
     ena_dsp ( );                /*Enable dispatching*/
 
     /* ............ */
 }

Note This service call does not queue enable requests. If the system is in the dispatching enabled state, therefore, no processing is performed but it is not handled as an error.
11.6 Delay of Scheduling
If a service call (ichg_pri, isig_sem, etc.) accompanying dispatch processing (task scheduling processing) is issued in order to quickly complete the processing in a non-task (cyclic handler, interrupt handler, etc.) during the interval until the processing in the non-task ends, the RI78V4 executes only processing such as queue manipulation, counter manipulation, etc., and the actual dispatch processing is delayed until a return instruction is issued by the non-task, upon which the actual dispatch processing is performed in batch.
The following shows a processing flow when a service call that involves dispatch processing in a non-task is issued.
Figure 11-5 Delay of Scheduling
11.7 Idle Routine
The idle routine is a routine dedicated to idle processing that is extracted as a user-own coding module to utilize the standby function provided by the CPU (to achieve the low-power consumption system), and is called from the scheduler when there no longer remains a task subject to scheduling by the RI78V4 (task in the RUNNING or READY state) in the system.
11.7.1 Define idle routine
In the RI78V4, the method of registering an idle routine is limited to "static registration by the Kernel Initialization Module".
Idle routines therefore cannot be created dynamically using a method such as issuing a service call from a processing program.
- Static define
Static idle routine registration is realized by coding idle routines by using the prescribed function name idle_handler.
The RI78V4 executes idle routine registration processing based on relevant symbol information, using the Kernel Initialization Module, and handles the registered idle routines as management targets.
11.7.2 Undefine idle routine
In the RI78V4, idle routines registered statically by the Kernel Initialization Module cannot be unregistered dynamically using a method such as issuing a service call from a processing program.
11.7.3 Basic form of idle routine
Write idle routines using void type functions that do not have arguments (function: idle_handler).
The following shows the basic form of idle routine.
[ C Language ]
 #include    <kernel.h>          /*Standard header file definition*/
 #include    <kernel_id.h>       /*System information header file definition*/
 
 void
 idle_handler ( void )
 {
     /* ............ */          /*Main processing*/
 
     return;                     /*Terminate idle routine*/
 }

[ Assembly Language ]
 $INCLUDE     (kernel.inc)       ;Standard header file definition
 $INCLUDE     (kernel_id.inc)    ;System information header file definition
 
    .PUBLIC  _idle_handler
 
    .SECTION .textf, TEXTF
 _idle_handler:
     ; ............              ;Main processing
 
     RET                         ;Terminate idle routine

11.7.4 Internal processing of idle routine
The RI78V4 handles the idle routine as a "non-task (module independent from tasks)".
Moreover, the RI78V4 executes "original pre-processing" when passing control to the idle routine, as well as "original post-processing" when regaining control from the idle routine.
Therefore, note the following points when coding idle routines.
- Coding method
Code idle routines using C or assembly language in the format shown in "11.7.3 Basic form of idle routine".
- Stack switching
The RI78V4 executes processing to switch to the system stack when passing control to the idle routine, and processing to switch to the stack for the switch destination processing program (system stack or task stack) when regaining control from the idle routine.
The user is therefore not required to code processing related to stack switching in idle routines.
- Interrupt status
Maskable interrupt acknowledgement is prohibited in the RI78V4 when control is passed to the idle routine.
The user is therefore not required to write the code related to maskable interrupt acknowledgment in idle routines.
- Service call issuance
The RI78V4 prohibits issuance of service calls in idle routines.
The following lists processing that should be executed in idle routines.
- Effective use of standby function provided by the CPU