Adding User-Written Code : Synchronizing Tasks : Synchronization

Synchronization

There are three types of synchronization calls:

Each of these calls will suspend the calling task and reschedule another task or the main_task (statechart) on a round-robin basis.

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.

Example:

void sense_start()
{
while (1) {
wait_for_event(SENSE);
/* here you are supposed to check status.*/
printf(“Time generated\n”);
}
} /* end sense_start */

The task_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.

Example:

void poll_input()
{
while (1) {
mouse_input = read_input_from_mouse();
if (mouse_input) {
. . Do Something . . .
}
task_delay(0.1); /* delay 0.1 seconds */
}
}

The scheduler() call is used when you have a calculation which is too long to be executed non-preemptively. 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 scheduler() call will allow other activities to proceed and the calling activity will resume execution in the next available time slot unless a stop or suspend command was 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.

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

Example:

void multiply()
{
for (i = 1; i<=10000; i++) {
for (j = 1; j<=10000; j++) {
/* internal loop is short
enough to complete */
}
scheduler();
}
}