CHAPTER 5 SYNCHRONIZATION AND COMMUNICATION FUNCTIONS


This chapter describes the synchronization and communication functions performed by the RI78V4.

5.1 Outline

The synchronization and communication functions of the RI78V4 consist of Semaphores, Eventflags, and Mailboxes that are provided as means for realizing exclusive control, queuing, and communication among tasks.

5.2 Semaphores

In the RI78V4, non-negative number counting semaphores are provided as a means (exclusive control function) for preventing contention for limited resources (hardware devices, library function, etc.) arising from the required conditions of simultaneously running tasks.

The following shows a processing flow when using a semaphore.

Figure 5-1 Processing Flow (Semaphore)



5.2.1 Create semaphore

In the RI78V4, the method of creating a semaphore is limited to "static creation by the Kernel Initialization Module".

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

- Static create
Static semaphore creation is realized by defining Semaphore information in the system configuration file.
The RI78V4 executes semaphore creation processing based on data stored in information files, using the Kernel Initialization Module, and handles the created semaphores as management targets.



5.2.2 Delete semaphore

In the RI78V4, semaphores created statically by the Kernel Initialization Module cannot be deleted dynamically using a method such as issuing a service call from a processing program.

5.2.3 Release semaphore resource

A resource is returned by issuing the following service call from the processing program.

- sig_sem, isig_sem
These service calls return the resource to the semaphore specified by parameter semid (adds 0x1 to the semaphore counter).
If a task is queued in the wait queue of the target semaphore when this service call is issued, the counter manipulation processing is not performed but the resource is passed to the relevant task (first task of wait queue).
As a result, the relevant task is unlinked from the wait queue and is moved from the WAITING state (waiting state for a semaphore resource) to the READY state, or from the WAITING-SUSPENDED state to the SUSPENDED state.
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      semid = ID_semA;    /*Declares and initializes variable*/
 
     /* ............ */
 
     sig_sem ( semid );          /*Release semaphore resource*/
 
     /* ............ */
 }


Note 1 If the first task linked in the wait queue is moved to the READY state after this service call is issued, this service call also re-queues the task at the end of the ready queue corresponding to the priority of the task.

Note 2 The semaphore counter managed by the RI78V4 is configured in 7-bit widths. If the number of resources exceeds the maximum count value 127 as a result of issuing this service call, the counter manipulation processing is therefore not performed but "E_QOVR" is returned.

5.2.4 Acquire semaphore resource

A resource is acquired (waiting forever, polling, or with timeout) by issuing the following service call from the processing program.

- wai_sem
This service call acquires a resource from the semaphore specified by parameter semid (subtracts 0x1 from the semaphore counter).
If a resource could not be acquired from the target semaphore (semaphore counter is set to 0x0) when this service call is issued, the counter manipulation processing is not performed but the invoking task is queued to the target semaphore wait queue in the order of resource acquisition request (FIFO order).
As a result, the invoking task is unlinked from the ready queue and is moved from the RUNNING state to the WAITING state (waiting state for a semaphore resource).
The waiting state for a semaphore state is cancelled in the following cases, and then moved to the READY state.




Waiting State for a Semaphore State Cancel Operation

Return Value

The resource was returned to the target semaphore as a result of issuing sig_sem.

E_OK

The resource was returned to the target semaphore as a result of issuing isig_sem.

E_OK

Forced release from waiting (accept rel_wai while waiting).

E_RLWAI

Forced release from waiting (accept irel_wai while waiting).

E_RLWAI



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 )
 {
     ER      ercd;               /*Declares variable*/
     ID      semid = ID_semA;    /*Declares and initializes variable*/
 
     /* ............ */
 
     ercd = wai_sem ( semid );   /*Acquire semaphore resource (waiting forever)*/
 
     if ( ercd == E_OK ) {
         /* ............ */      /*Normal termination processing*/
     } else if ( ercd == E_RLWAI ) {
         /* ............ */      /*Forced termination processing*/
     }
 
     /* ............ */
 }


- pol_sem
This service call acquires a resource from the semaphore specified by parameter semid (subtracts 0x1 from the semaphore counter).
If a resource could not be acquired from the target semaphore (semaphore counter is set to 0x0) when this service call is issued, the counter manipulation processing is not performed but "E_TMOUT" 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_task ( VP_INT exinf )
 {
     ER      ercd;               /*Declares variable*/
     ID      semid = ID_semA;    /*Declares and initializes variable*/
 
     /* ............ */
 
     ercd = pol_sem ( semid );   /*Acquire semaphore resource (polling)*/
 
     if ( ercd == E_OK ) {
         /* ............ */      /*Polling success processing*/
     } else if ( ercd == E_TMOUT ) {
         /* ............ */      /*Polling failure processing*/
     }
 
     /* ............ */
 }


- twai_sem
This service call acquires a resource from the semaphore specified by parameter semid (subtracts 0x1 from the semaphore counter).
If a resource could not be acquired from the target semaphore (semaphore counter is set to 0x0) when this service call is issued, the counter manipulation processing is not performed but the invoking task is queued to the target semaphore wait queue in the order of resource acquisition request (FIFO order).
As a result, the invoking task is unlinked from the ready queue and is moved from the RUNNING state to the WAITING state (waiting state for a semaphore resource).
The waiting state for a semaphore resource is cancelled in the following cases, and then moved to the READY state.




Waiting State for a Semaphore Resource Cancel Operation

Return Value

The resource was returned to the target semaphore as a result of issuing sig_sem.

E_OK

The resource was returned to the target semaphore as a result of issuing isig_sem.

E_OK

Forced release from waiting (accept rel_wai while waiting).

E_RLWAI

Forced release from waiting (accept irel_wai while waiting).

E_RLWAI

Polling failure or timeout.

E_TMOUT



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 )
 {
     ER      ercd;               /*Declares variable*/
     ID      semid = ID_semA;    /*Declares and initializes variable*/
     TMO     tmout = 3600;       /*Declares and initializes variable*/
 
     /* ............ */
 
                                 /*Acquire semaphore resource (with timeout)*/
     ercd = twai_sem ( semid, tmout );
 
     if ( ercd == E_OK ) {
         /* ............ */      /*Normal termination processing*/
     } else if ( ercd == E_RLWAI ) {
         /* ............ */      /*Forced termination processing*/
     } else if ( ercd == E_TMOUT ) {
         /* ............ */      /*Timeout processing*/
     }
 
     /* ............ */
 }


Note When TMO_FEVR is specified for wait time tmout, processing equivalent to wai_sem will be executed. When TMO_POL is specified, processing equivalent to pol_sem will be executed.

5.2.5 Reference semaphore state

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

- ref_sem
Stores semaphore state packet (such as existence of waiting tasks) of the semaphore specified by parameter semid in the area specified by parameter pk_rsem.
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      semid = ID_semA;    /*Declares and initializes variable*/
     T_RSEM  pk_rsem;            /*Declares data structure*/
     ID      wtskid;             /*Declares variable*/
     UINT    semcnt;             /*Declares variable*/
 
     /* ............ */
 
     ref_sem ( semid, &pk_rsem );/*Reference semaphore state*/
 
     wtskid = pk_rsem.wtskid;    /*Reference ID number of the task at the head of
                                   the wait queue*/
     semcnt = pk_rsem.semcnt;    /*Reference current resource count*/
 
     /* ............ */
 }


Note For details about the semaphore state packet, refer to "12.5.2 Semaphore state packet".