Supplementing Generated Ada : Synchronization of Primitive Activities : Synchronization

Synchronization

There are three types of synchronization calls:

synchronize (activity_address);

The activity address is the address of the activity status variable defined in the package where the activity is referenced.
Ada DELAY statement

Each one of these calls suspends the activity and reschedule another activity or the main_task (statechart) on a round-robin policy.

The wait_for_event call suspends the activity until the specified event is generated. It is a way to synchronize the activity with other activities (either user-implemented or statechart controlled). When the event is generated, the code resumes execution after the wait call.

For example:

procedure sense_start IS

while true loop

wait_for_event (start’address);

-- check the status here

put_line(“start generated”) ;

end loop ;

end ;

 

 

The Ada DELAY statement delays the activity for the time specified in the call. It is useful to implement polling processes that periodically perform checks on a time basis.

For example:

procedure poll_input IS

begin

while true loop

mouse_input := get_input_from_mouse ;

if mouse_input /= null . . . . .

DELAY 0.1 ;

end loop ;

end ;

Note: The DELAY statement allows other activities to run, but stop and suspend do not take effect. If you wish to stop or suspend the activity by other activities, add a synchronize call after the DELAY statement.

The activity_synchronize is used when you have a lengthy calculation which is too long to be executed without interruption. For example, if you have to multiply two 10000x10000 matrices, you do not want the rest of the system to be blocked all that time. The activity_synchronize call allows other activities to proceed, and the calling activity resumes execution in the next available time slot unless a stop or suspend command is issued. The call should be placed in a loop in which one cycle can be executed without preemption, but an outer loop may take too long.

procedure multiply IS

begin

for i in 1 . . 10000 loop

for s in 1 . . 10000 loop

-- internal loop

-- the internal loop is short enough to -- complete.

end loop

activity_synchronize(
acy_MULTIPLY’address);

end loop ;

end ;

 

 
Note: No synchronization call should be used by a procedure implemented activity.