CHAPTER 8 SYSTEM STATE MANAGEMENT FUNCTIONS


This chapter describes the system state management functions performed by the RI78V4.

8.1 Outline

The system state control functions of the RI78V4 include, in addition to functions to manipulate the state of the system, such as transition to the CPU locked state and transition to the dispatching disabled state, functions for referencing the state of the system, such as context type referencing and CPU locked state referencing.

8.2 Rotate Task Precedence

A ready queue is rotated by issuing the following service call from the processing program.

- rot_rdq, irot_rdq
This service call re-queues 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 shows the status transition when this service call is used.


Figure 8-1 Rotate Task Precedence



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.

Note 3 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 System 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.


8.3 Reference Task ID in the RUNNING State

A RUNNING-state task is referenced by issuing the following service call from the processing program.

- get_tid, iget_tid
These service calls store the ID of a task in the RUNNING state in the area specified by parameter p_tskid.
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 )
 {
     ID      p_tskid;            /*Declares variable*/
 
     /* ............ */
 
     iget_tid ( &p_tskid );      /*Reference task ID in the RUNNING state*/
 
     /* ............ */
 
     return;                     /*Terminate cyclic handler*/
 }


Note This service call stores TSK_NONE in the area specified by parameter p_tskid if no tasks that have entered the RUNNING state exist (all tasks in the IDLE state).

8.4 Lock the CPU

A task is moved to the CPU locked state by issuing the following service call from the processing program.

- loc_cpu, iloc_cpu
These service calls change the system status type to the CPU locked state.
As a result, maskable interrupt acknowledgment processing is prohibited during the interval from this service call is issued until unl_cpu or iunl_cpu is issued, and service call issuance is also restricted.
If a maskable interrupt is created during this period, the RI78V4 delays transition to the relevant interrupt processing (interrupt handler) until either unl_cpu or iunl_cpu is issued.
The service calls that can be issued in the CPU locked state are limited to the one listed below.




Service Call

Function

Lock the CPU.

Unlock the CPU.

Reference contexts.

Reference CPU state.

Reference dispatching state.

Reference dispatch pending state.



The following shows a processing flow when using this service call.

Figure 8-2 Lock the CPU



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 )
 {
     /* ............ */
 
     loc_cpu ( );                /*Lock the CPU*/
 
     /* ............ */          /*CPU locked state*/
 
     unl_cpu ( );                /*Unlock the CPU*/
 
     /* ............ */
 }


Note 1 The CPU locked state changed by issuing this service call must be cancelled before the processing program that issued this service call ends.

Note 2 This service call does not perform queuing of lock requests. If the system is in the CPU locked state, therefore, no processing is performed but it is not handled as an error.

Note 3 The RI78V4 implements disabling of maskable interrupt acknowledgment bu manipulating the interrupt mask flag register (MKxx) and the in-service priority flag (ISPx) of the program status word (PSW). Therefore, manipulating of these registers from the processing program is prohibited from when this service call is issued until unl_cpu or iunl_cpu is issued.

8.5 Unlock the CPU

The CPU locked state is cancelled by issuing the following service call from the processing program.

- unl_cpu, iunl_cpu
These service calls change the system status to the CPU unlocked state.
As a result, acknowledge processing of maskable interrupts prohibited through issuance of either loc_cpu or iloc_cpu is enabled, and the restriction on service call issuance is released.
If a maskable interrupt is created during the interval from when either loc_cpu or iloc_cpu is issued until this service call is issued, the RI78V4 delays transition to the relevant interrupt processing (interrupt handler) until this service call is issued.
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 )
 {
     /* ............ */
 
     loc_cpu ( );                /*Lock the CPU*/
 
     /* ............ */          /*CPU locked state*/
 
     unl_cpu ( );                /*Unlock the CPU*/
 
     /* ............ */
 }


Note 1 This service call does not perform queuing of cancellation requests. If the system is in the CPU unlocked state, therefore, no processing is performed but it is not handled as an error.

Note 2 The RI78V4 implements enabling of maskable interrupt acknowledgment bu manipulating the interrupt mask flag register (MKxx) and the in-service priority flag (ISPx) of the program status word (PSW). Therefore, manipulating of these registers from the processing program is prohibited from when loc_cpu or iloc_cpu is issued until this service call is issued.

8.6 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 shows a processing flow when using this service call.




Figure 8-3 Disable Dispatching



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.

8.7 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 perform queuing of enable requests. If the system is in the dispatching enabled state, therefore, no processing is performed but it is not handled as an error.

8.8 Reference Contexts

The context type is referenced by issuing the following service call from the processing program.

- sns_ctx
This service call acquires the context type of the processing program that issued this service call (non-task context or task context).
When this service call is terminated normally, the acquired context type (TRUE: non-task context, FALSE: task context) is returned.


Non-task contexts: cyclic handler, interrupt handler

task contexts: task

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_sub ( void )
 {
     BOOL    ercd;               /*Declares variable*/
 
     /* ............ */
 
     ercd = sns_ctx ( );         /*Reference contexts*/
 
     if ( ercd == TRUE ) {
         /* ............ */      /*Non-task contexts*/
     } else if ( ercd == FALSE ) {
         /* ............ */      /*Task contexts*/
     }
 
     /* ............ */
 }


8.9 Reference CPU State

The CPU locked state is referenced by issuing the following service call from the processing program.

- sns_loc
This service call acquires the system status type when this service call is issued (CPU locked state or CPU unlocked state).
When this service call is terminated normally, the acquired system state type (TRUE: CPU locked state, FALSE: CPU unlocked state) is returned.
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_sub ( void )
 {
     BOOL    ercd;               /*Declares variable*/
 
     /* ............ */
 
     ercd = sns_loc ( );         /*Reference CPU state*/
 
     if ( ercd == TRUE ) {
         /* ............ */      /*CPU locked state*/
     } else if ( ercd == FALSE ) {
         /* ............ */      /*CPU unlocked state*/
     }
 
     /* ............ */
 }


Note The system enters the CPU locked state when loc_cpu or iloc_cpu is issued, and enters the CPU unlocked state when unl_cpu or iunl_cpu is issued.

8.10 Reference Dispatching State

The dispatching state is referenced by issuing the following service call from the processing program.

- sns_dsp
This service call acquires the system status type when this service call is issued (dispatching disabled state or dispatching enabled state).
When this service call is terminated normally, the acquired system state type (TRUE: dispatching disabled state, FALSE: dispatching enabled state) is returned.
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_sub ( void )
 {
     BOOL    ercd;               /*Declares variable*/
 
     /* ............ */
 
     ercd = sns_dsp ( );         /*Reference dispatching state*/
 
     if ( ercd == TRUE ) {
         /* ............ */      /*Dispatching disabled state*/
     } else if ( ercd == FALSE ) {
         /* ............ */      /*Dispatching enabled state*/
     }
 
     /* ............ */
 }


Note The system enters the dispatching disabled state when dis_dsp is issued, and enters the dispatching enabled state when ena_dsp is issued.

8.11 Reference Dispatch Pending State

The dispatch pending state is referenced by issuing the following service call from the processing program.

- sns_dpn
This service call acquires the system status type when this service call is issued (whether in dispatch pending state or not).
When this service call is terminated normally, the acquired system state type (TRUE: dispatch pending state, FALSE: dispatch not-pending state) is returned.
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_sub ( void )
 {
     BOOL    ercd;               /*Declares variable*/
 
     /* ............ */
 
     ercd = sns_dpn ( );         /*Reference dispatch pending state*/
 
     if ( ercd == TRUE ) {
         /* ............ */      /*Dispatch pending state*/
     } else if ( ercd == FALSE ) {
         /* ............ */      /*Other state*/
     }
 
     /* ............ */
 }


Note The dispatch pending state designates the state in which explicit execution of dispatch processing (task scheduling processing) is prohibited by issuing either the dis_dsp, loc_cpu, or iloc_cpu service call, as well as the state during which processing of a non-task is being executed.