Concepts of Programming Languages

(Sean Pound) #1

598 Chapter 13 Concurrency


customers at a drive-up window. The following skeletal teller task illustrates a
select construct:

task body Teller is
begin
loop
select
accept Drive_Up(formal parameters) do

...
end Drive_Up;
...
or
accept Walk_Up(formal parameters) do
...
end Walk_Up;
...
end select;
end loop;
end Teller;


In this task, there are two accept clauses, Walk_Up and Drive_Up, each of
which has an associated queue. The action of the select, when it is executed,
is to examine the queues associated with the two accept clauses. If one of
the queues is empty, but the other contains at least one waiting message (cus-
tomer), the accept clause associated with the waiting message or messages
has a rendezvous with the task that sent the first message that was received.
If both accept clauses have empty queues, the select waits until one of
the entries is called. If both accept clauses have nonempty queues, one
of the accept clauses is nondeterministically chosen to have a rendezvous
with one of its callers. The loop forces the select statement to be executed
repeatedly, forever.
The end of the accept clause marks the end of the code that assigns or
references the formal parameters of the accept clause. The code, if there
is any, between an accept clause and the next or (or the end select, if
the accept clause is the last one in the select) is called the extended
accept clause. The extended accept clause is executed only after the asso-
ciated (immediately preceding) accept clause is executed. This execution of
the extended accept clause is not part of the rendezvous and can take place
concurrently with the execution of the calling task. The sender is suspended
during the rendezvous, but it is put back in the ready queue when the end of
the accept clause is reached. If an accept clause has no formal parameters,
the do-end is not required, and the accept clause can consist entirely of an
extended accept clause. Such an accept clause would be used exclusively for
synchronization. Extended accept clauses are illustrated in the Buf_Task
task in Section 13.6.3.
Free download pdf